add tests for the NO_COLOR env variable

This commit is contained in:
JulesDT 2021-01-22 18:33:11 -05:00
parent 4ac302ae86
commit b2f7bca857

View file

@ -3,6 +3,7 @@ package cobra
import ( import (
"bytes" "bytes"
"context" "context"
"errors"
"fmt" "fmt"
"os" "os"
"reflect" "reflect"
@ -1990,35 +1991,75 @@ func TestFParseErrWhitelistSiblingCommand(t *testing.T) {
checkStringContains(t, output, "unknown flag: --unknown") checkStringContains(t, output, "unknown flag: --unknown")
} }
func commandIsColoredRed(c *Command) error {
if c.Name() != "cmd" {
return fmt.Errorf("Unexpected name with Colored Command: %s", c.Name())
}
// If a color is specified, the ColoredName and the Name should be different
if c.Name() == c.ColoredName() {
return errors.New("Name and ColoredName should not give the same result")
}
if c.ColoredName() != "\033[31m"+c.Name()+"\033[0m" {
return errors.New("ColoredName should only add color to the name")
}
if c.additionalNamePadding() == 0 {
return errors.New("With a color, the additionalNamePadding should be more than 0")
}
return nil
}
func commandIsNotColored(c *Command) error {
if c.Name() != "cmd" {
return errors.New("Unexpected name with simple Command")
}
// If no color is specified, the ColoredName should equal the Name
if c.Name() != c.ColoredName() {
return errors.New("Name and ColoredName should give the same result")
}
if c.additionalNamePadding() != 0 {
return errors.New("With no color, the additionalNamePadding should be 0")
}
return nil
}
func TestColoredName(t *testing.T) { func TestColoredName(t *testing.T) {
c := &Command{ c := &Command{
Use: "cmd", Use: "cmd",
} }
if c.Name() != "cmd" { err := commandIsNotColored(c)
t.Error("Unexpected name with simple Command") if err != nil {
} t.Error(err)
// If no color is specified, the ColoredName should equal the Name
if c.Name() != c.ColoredName() {
t.Error("Name and ColoredName should give the same result")
}
if c.additionalNamePadding() != 0 {
t.Error("With no color, the additionalNamePadding should be 0")
} }
c = &Command{ c = &Command{
Use: "cmd", Use: "cmd",
Color: ColorRed, Color: ColorRed,
} }
if c.Name() != "cmd" { err = commandIsColoredRed(c)
t.Errorf("Unexpected name with Colored Command: %s\n", c.Name()) if err != nil {
} t.Error(err)
// If a color is specified, the ColoredName and the Name should be different }
if c.Name() == c.ColoredName() { }
t.Error("Name and ColoredName should not give the same result")
} func TestColoredNameWithNoColorSetup(t *testing.T) {
if c.ColoredName() != "\033[31m"+c.Name()+"\033[0m" { c := &Command{
t.Error("ColoredName should only add color to the name") Use: "cmd",
} Color: ColorRed,
if c.additionalNamePadding() == 0 { }
t.Error("With a color, the additionalNamePadding should be more than 0") err := commandIsColoredRed(c)
if err != nil {
t.Error(err)
}
os.Setenv("NO_COLOR", "true")
err = commandIsNotColored(c)
if err != nil {
t.Error(err)
}
os.Unsetenv("NO_COLOR")
c.DisableColors = true
err = commandIsNotColored(c)
if err != nil {
t.Error(err)
} }
} }