diff --git a/formatter/formatter.go b/formatter/formatter.go index 43b16211d..da224eab6 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -50,6 +50,15 @@ func NewWithNoColorBool(noColor bool) Formatter { } func New(colorMode ColorMode) Formatter { + getColor := func(color, defaultEscapeCode string) string { + color = strings.ToUpper(strings.ReplaceAll(color, "-", "_")) + envVar := fmt.Sprintf("GINKGO_CLI_COLOR_%s", color) + if escapeCode := os.Getenv(envVar); escapeCode != "" { + return escapeCode + } + return defaultEscapeCode + } + f := Formatter{ ColorMode: colorMode, colors: map[string]string{ @@ -57,18 +66,18 @@ func New(colorMode ColorMode) Formatter { "bold": "\x1b[1m", "underline": "\x1b[4m", - "red": "\x1b[38;5;9m", - "orange": "\x1b[38;5;214m", - "coral": "\x1b[38;5;204m", - "magenta": "\x1b[38;5;13m", - "green": "\x1b[38;5;10m", - "dark-green": "\x1b[38;5;28m", - "yellow": "\x1b[38;5;11m", - "light-yellow": "\x1b[38;5;228m", - "cyan": "\x1b[38;5;14m", - "gray": "\x1b[38;5;243m", - "light-gray": "\x1b[38;5;246m", - "blue": "\x1b[38;5;12m", + "red": getColor("red", "\x1b[38;5;9m"), + "orange": getColor("orange", "\x1b[38;5;214m"), + "coral": getColor("coral", "\x1b[38;5;204m"), + "magenta": getColor("magenta", "\x1b[38;5;13m"), + "green": getColor("green", "\x1b[38;5;10m"), + "dark-green": getColor("dark-green", "\x1b[38;5;28m"), + "yellow": getColor("yellow", "\x1b[38;5;11m"), + "light-yellow": getColor("light-yellow", "\x1b[38;5;228m"), + "cyan": getColor("cyan", "\x1b[38;5;14m"), + "gray": getColor("gray", "\x1b[38;5;243m"), + "light-gray": getColor("light-gray", "\x1b[38;5;246m"), + "blue": getColor("blue", "\x1b[38;5;12m"), }, } colors := []string{} diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index fab3f6234..b8db73164 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -1,6 +1,7 @@ package formatter_test import ( + "os" "strings" . "github.com/onsi/ginkgo/v2" @@ -14,6 +15,17 @@ var _ = Describe("Formatter", func() { BeforeEach(func() { colorMode = formatter.ColorModeTerminal + os.Unsetenv("GINKGO_CLI_COLOR_RED") + os.Unsetenv("GINKGO_CLI_COLOR_ORANGE") + os.Unsetenv("GINKGO_CLI_COLOR_CORAL") + os.Unsetenv("GINKGO_CLI_COLOR_MAGENTA") + os.Unsetenv("GINKGO_CLI_COLOR_GREEN") + os.Unsetenv("GINKGO_CLI_COLOR_DARK_GREEN") + os.Unsetenv("GINKGO_CLI_COLOR_YELLOW") + os.Unsetenv("GINKGO_CLI_COLOR_LIGHT_YELLOW") + os.Unsetenv("GINKGO_CLI_COLOR_CYAN") + os.Unsetenv("GINKGO_CLI_COLOR_LIGHT_GRAY") + os.Unsetenv("GINKGO_CLI_COLOR_BLUE") }) JustBeforeEach(func() { @@ -50,6 +62,20 @@ var _ = Describe("Formatter", func() { }) }) + Context("with environment overrides", func() { + BeforeEach(func() { + os.Setenv("GINKGO_CLI_COLOR_RED", "\x1b[31m") + }) + + AfterEach(func() { + os.Unsetenv("GINKGO_CLI_COLOR_RED") + }) + + It("uses the escape codes from the environment variables", func() { + Ω(f.F("{{red}}hi there{{/}}")).Should(Equal("\x1b[31mhi there\x1b[0m")) + }) + }) + Describe("NewWithNoColorBool", func() { Context("when the noColor bool is true", func() { It("strips out color information", func() {