diff --git a/printer/html_test.go b/printer/html_test.go index d961595..f442f98 100644 --- a/printer/html_test.go +++ b/printer/html_test.go @@ -67,6 +67,7 @@ func TestPrettyHTMLVoidTagsAndComments(t *testing.T) { } func TestPrettyHTMLColorization(t *testing.T) { + t.Setenv("NO_COLOR", "") input := `
Content
` var buf bytes.Buffer _, err := PrettyHTML(&buf, strings.NewReader(input), true) // enabled = true diff --git a/printer/json_test.go b/printer/json_test.go index bdc999e..ca1e7a2 100644 --- a/printer/json_test.go +++ b/printer/json_test.go @@ -31,6 +31,7 @@ func TestPrettyJSON(t *testing.T) { } func TestPrettyJSONColored(t *testing.T) { + t.Setenv("NO_COLOR", "") input := `{"key": "value", "num": 42}` var buf bytes.Buffer _, err := PrettyJSON(&buf, strings.NewReader(input), true) diff --git a/printer/no_color_test.go b/printer/no_color_test.go new file mode 100644 index 0000000..d856b86 --- /dev/null +++ b/printer/no_color_test.go @@ -0,0 +1,32 @@ +package printer + +import ( + "bytes" + "io" + "strings" + "testing" +) + +func TestPrettyRespectsNoColorEnvironment(t *testing.T) { + t.Setenv("NO_COLOR", "1") + for _, tc := range []struct { + name, input string + format func(io.Writer, io.Reader, bool) (int64, error) + }{ + {"json", `{"value":42}`, PrettyJSON}, + {"html", `
Hello
`, PrettyHTML}, + } { + t.Run(tc.name, func(t *testing.T) { + var out bytes.Buffer + if _, err := tc.format(&out, strings.NewReader(tc.input), true); err != nil { + t.Fatal(err) + } + if out.Len() == 0 { + t.Fatal("empty output") + } + if strings.Contains(out.String(), "\x1b[") { + t.Fatalf("NO_COLOR output contains ANSI codes: %q", out.String()) + } + }) + } +}