Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override formatter colors from envvars #1095

Merged
merged 2 commits into from Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 21 additions & 12 deletions formatter/formatter.go
Expand Up @@ -50,25 +50,34 @@ 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{
"/": "\x1b[0m",
"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{}
Expand Down
26 changes: 26 additions & 0 deletions formatter/formatter_test.go
@@ -1,6 +1,7 @@
package formatter_test

import (
"os"
"strings"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down