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 (
"bytes"
"context"
"errors"
"fmt"
"os"
"reflect"
@ -1990,35 +1991,75 @@ func TestFParseErrWhitelistSiblingCommand(t *testing.T) {
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) {
c := &Command{
Use: "cmd",
}
if c.Name() != "cmd" {
t.Error("Unexpected name with simple Command")
}
// 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")
err := commandIsNotColored(c)
if err != nil {
t.Error(err)
}
c = &Command{
Use: "cmd",
Color: ColorRed,
}
if c.Name() != "cmd" {
t.Errorf("Unexpected name with Colored Command: %s\n", c.Name())
}
// 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")
}
if c.ColoredName() != "\033[31m"+c.Name()+"\033[0m" {
t.Error("ColoredName should only add color to the name")
}
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)
}
}
func TestColoredNameWithNoColorSetup(t *testing.T) {
c := &Command{
Use: "cmd",
Color: ColorRed,
}
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)
}
}