forked from stretchr/testify
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: added optional support for colorized output #33
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Package colors is an indirection to handle colorized output. | ||
| // | ||
| // This package allows the builder to override the indirection with an alternative implementation | ||
| // of colorized printers. | ||
| package colors | ||
|
|
||
| import ( | ||
| colorstub "github.com/go-openapi/testify/v2/internal/assertions/enable/colors" | ||
| ) | ||
|
|
||
| // Enable registers colorized options for pretty-printing the output of assertions. | ||
| // | ||
| // The provided enabler defers the initialization, so we may retrieve flags after command line parsing | ||
| // or other initialization tasks. | ||
| // | ||
| // This is not intended for concurrent use. | ||
| func Enable(enabler func() []Option) { | ||
| colorstub.Enable(enabler) | ||
| } | ||
|
|
||
| // re-exposed internal types. | ||
| type ( | ||
| // Option is a colorization option. | ||
| Option = colorstub.Option | ||
|
|
||
| // Theme is a colorization theme for testify output. | ||
| Theme = colorstub.Theme | ||
| ) | ||
|
|
||
| // WithEnable enables colorization. | ||
| func WithEnable(enabled bool) Option { | ||
| return colorstub.WithEnable(enabled) | ||
| } | ||
|
|
||
| // WithSanitizedTheme sets a colorization theme from a string. | ||
| func WithSanitizedTheme(theme string) Option { | ||
| return colorstub.WithSanitizedTheme(theme) | ||
| } | ||
|
|
||
| // WithTheme sets a colorization theme. | ||
| func WithTheme(theme Theme) Option { | ||
| return colorstub.WithTheme(theme) | ||
| } | ||
|
|
||
| // WithDark sets the [ThemeDark] color theme. | ||
| func WithDark() Option { | ||
| return colorstub.WithDark() | ||
| } | ||
|
|
||
| // WithLight sets the [ThemeLight] color theme. | ||
| func WithLight() Option { | ||
| return colorstub.WithLight() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package colors | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| target "github.com/go-openapi/testify/v2/assert" | ||
| colorstub "github.com/go-openapi/testify/v2/assert/enable/colors" | ||
| ) | ||
|
|
||
| func TestMain(m *testing.M) { | ||
| // we can't easily simulate arg flags in CI (uses gotestsum etc). | ||
| // Similarly, env vars are evaluated too early. | ||
| colorstub.Enable( | ||
| func() []colorstub.Option { | ||
| return []colorstub.Option{ | ||
| colorstub.WithEnable(true), | ||
| colorstub.WithSanitizedTheme(flags.theme), | ||
| } | ||
| }) | ||
|
|
||
| os.Exit(m.Run()) | ||
| } | ||
|
|
||
| func TestAssertJSONEq(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| mockT := new(mockT) | ||
| res := target.JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"hello": "worldwide", "foo": "bar"}`) | ||
|
|
||
| target.False(t, res) | ||
|
|
||
| output := mockT.errorString() | ||
| t.Log(output) // best to visualize the output | ||
| target.Contains(t, neuterize(output), neuterize(expectedColorizedDiff)) | ||
| } | ||
|
|
||
| func TestAssertJSONEq_Array(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| mockT := new(mockT) | ||
| res := target.JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `["bar", {"nested": "hash", "hello": "world"}]`) | ||
|
|
||
| target.False(t, res) | ||
| output := mockT.errorString() | ||
| t.Log(output) // best to visualize the output | ||
| target.Contains(t, neuterize(output), neuterize(expectedColorizedArrayDiff)) | ||
| } | ||
|
|
||
| func neuterize(str string) string { | ||
| // remove blanks and replace escape sequences for readability | ||
| blankRemover := strings.NewReplacer("\t", "", " ", "", "\x1b", "^[") | ||
| return blankRemover.Replace(str) | ||
| } | ||
|
|
||
| type mockT struct { | ||
| errorFmt string | ||
| args []any | ||
| } | ||
|
|
||
| // Helper is like [testing.T.Helper] but does nothing. | ||
| func (mockT) Helper() {} | ||
|
|
||
| func (m *mockT) Errorf(format string, args ...any) { | ||
| m.errorFmt = format | ||
| m.args = args | ||
| } | ||
|
|
||
| func (m *mockT) Failed() bool { | ||
| return m.errorFmt != "" | ||
| } | ||
|
|
||
| func (m *mockT) errorString() string { | ||
| return fmt.Sprintf(m.errorFmt, m.args...) | ||
| } | ||
|
|
||
| // captured output (indentation is not checked) | ||
| // | ||
| //nolint:staticcheck // indeed we want to check the escape sequences in this test | ||
| const ( | ||
| expectedColorizedDiff = ` Not equal: | ||
| expected: [0;92mmap[string]interface {}{"foo":"bar", "hello":"world"}[0m | ||
| actual : [0;91mmap[string]interface {}{"foo":"bar", "hello":"worldwide"}[0m | ||
|
|
||
| Diff: | ||
| --- Expected | ||
| +++ Actual | ||
| @@ -2,3 +2,3 @@ | ||
| [0;92m (string) (len=3) "foo": (string) (len=3) "bar", | ||
| [0m[0;91m- (string) (len=5) "hello": (string) (len=5) "world" | ||
| [0m[0;93m+ (string) (len=5) "hello": (string) (len=9) "worldwide" | ||
| [0m[0;92m } | ||
| [0m | ||
| ` | ||
|
|
||
| expectedColorizedArrayDiff = `Not equal: | ||
| expected: [0;92m[]interface {}{"foo", map[string]interface {}{"hello":"world", "nested":"hash"}}[0m | ||
| actual : [0;91m[]interface {}{"bar", map[string]interface {}{"hello":"world", "nested":"hash"}}[0m | ||
|
|
||
| Diff: | ||
| --- Expected | ||
| +++ Actual | ||
| @@ -1,3 +1,3 @@ | ||
| [0;92m ([]interface {}) (len=2) { | ||
| [0m[0;91m- (string) (len=3) "foo", | ||
| [0m[0;93m+ (string) (len=3) "bar", | ||
| [0m[0;92m (map[string]interface {}) (len=2) { | ||
| [0m | ||
| ` | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Package colors enables colorized tests with basic and portable ANSI terminal codes. | ||
| // | ||
| // Colorization is disabled by default when the standard output is not a terminal. | ||
| // | ||
| // Colors are somewhat limited, but the package works on unix and windows without any extra dependencies. | ||
| // | ||
| // # Command line arguments | ||
| // | ||
| // - testify.colorized={true|false} | ||
| // - testify.theme={dark|light} | ||
| // - testify.colorized.notty={true|false} (enable colorization even when the output is not a terminal) | ||
| // | ||
| // The default theme used is dark. | ||
| // | ||
| // To run tests on a terminal with colorized output: | ||
| // | ||
| // - run: go test -v -testify.colorized ./... | ||
| // | ||
| // # Environment variables | ||
| // | ||
| // Colorization may be enabled from environment: | ||
| // | ||
| // - TESTIFY_COLORIZED=true | ||
| // - TESTIFY_THEME=dark | ||
| // - TESTIFY_COLORIZED_NOTTY=true | ||
| // | ||
| // Command line arguments take precedence over environment. | ||
| package colors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package colors | ||
|
|
||
| import ( | ||
| "flag" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "golang.org/x/term" | ||
|
|
||
| colorstub "github.com/go-openapi/testify/v2/assert/enable/colors" | ||
| ) | ||
|
|
||
| const ( | ||
| envVarColorize = "TESTIFY_COLORIZED" | ||
| envVarTheme = "TESTIFY_THEME" | ||
| envVarNoTTY = "TESTIFY_COLORIZED_NOTTY" | ||
| ) | ||
|
|
||
| var flags cliFlags //nolint:gochecknoglobals // it's okay to store the state CLI flags in a package global | ||
|
|
||
| type cliFlags struct { | ||
| colorized bool | ||
| theme string | ||
| notty bool | ||
| } | ||
|
|
||
| func init() { //nolint:gochecknoinits // it's okay: we want to declare CLI flags when a blank import references this package | ||
| isTerminal := term.IsTerminal(1) | ||
|
|
||
| flag.BoolVar(&flags.colorized, "testify.colorized", colorizeFromEnv(), "testify: colorized output") | ||
| flag.StringVar(&flags.theme, "testify.theme", themeFromEnv(), "testify: color theme (light,dark)") | ||
| flag.BoolVar(&flags.notty, "testify.colorized.notty", nottyFromEnv(), "testify: force colorization, even if not a tty") | ||
|
|
||
| colorstub.Enable( | ||
| func() []colorstub.Option { | ||
| return []colorstub.Option{ | ||
| colorstub.WithEnable(flags.colorized && (isTerminal || flags.notty)), | ||
| colorstub.WithSanitizedTheme(flags.theme), | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func colorizeFromEnv() bool { | ||
| envColorize := os.Getenv(envVarColorize) | ||
| isEnvConfigured, _ := strconv.ParseBool(envColorize) | ||
|
|
||
| return isEnvConfigured | ||
| } | ||
|
|
||
| func themeFromEnv() string { | ||
| envTheme := os.Getenv(envVarTheme) | ||
|
|
||
| return strings.ToLower(envTheme) | ||
| } | ||
|
|
||
| func nottyFromEnv() bool { | ||
| envNoTTY := os.Getenv(envVarNoTTY) | ||
| isEnvNoTTY, _ := strconv.ParseBool(envNoTTY) | ||
|
|
||
| return isEnvNoTTY | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| module github.com/go-openapi/testify/enable/colors/v2 | ||
|
|
||
| require ( | ||
| github.com/go-openapi/testify/v2 v2.1.8 | ||
| golang.org/x/term v0.39.0 | ||
| ) | ||
|
|
||
| require golang.org/x/sys v0.40.0 // indirect | ||
|
|
||
| replace github.com/go-openapi/testify/v2 => ../.. | ||
|
|
||
| go 1.24.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ= | ||
| golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= | ||
| golang.org/x/term v0.39.0 h1:RclSuaJf32jOqZz74CkPA9qFuVTX7vhLlpfj/IGWlqY= | ||
| golang.org/x/term v0.39.0/go.mod h1:yxzUCTP/U+FzoxfdKmLaA0RV1WgE0VY7hXBwKtY/4ww= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.