Skip to content

Commit

Permalink
NO_COLOR can now be set to anything
Browse files Browse the repository at this point in the history
Signed-off-by: Lasse Gaardsholt <[email protected]>
  • Loading branch information
Gaardsholt committed Sep 4, 2023
1 parent 6a31fc3 commit 04c188f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 21 deletions.
2 changes: 1 addition & 1 deletion console.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func needsQuote(s string) bool {
// colorize returns the string s wrapped in ANSI code c, unless disabled is true.
func colorize(s interface{}, c int, disabled bool) string {
e := os.Getenv("NO_COLOR")
if e == "true" {
if e != "" {
disabled = true
}

Expand Down
21 changes: 1 addition & 20 deletions console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestConsoleWriter(t *testing.T) {
})

t.Run("NO_COLOR = true", func(t *testing.T) {
os.Setenv("NO_COLOR", "true")
os.Setenv("NO_COLOR", "anything")

buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf}
Expand All @@ -123,25 +123,6 @@ func TestConsoleWriter(t *testing.T) {
os.Unsetenv("NO_COLOR")
})

t.Run("NO_COLOR = false", func(t *testing.T) {
os.Setenv("NO_COLOR", "false")

buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf}

_, err := w.Write([]byte(`{"level": "warn", "message": "Foobar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}

expectedOutput := "\x1b[90m<nil>\x1b[0m \x1b[31mWRN\x1b[0m Foobar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
os.Unsetenv("NO_COLOR")
})

t.Run("Write fields", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}
Expand Down

0 comments on commit 04c188f

Please sign in to comment.