From cffa99d93a90d58dabed0ba6eae64af1a1b1d0e9 Mon Sep 17 00:00:00 2001 From: Connor Date: Fri, 5 Jun 2020 17:22:17 -0400 Subject: [PATCH] Fix color leakage (#10) Only colorizing output that indicates test result status. This includes "ok", "PASS", etc. The fix prevents that coloration from remaining set and colorizing error and logging output that doesn't directly indicate whether a test passed or failed. For example, a line like "some/package/file.go:20:10: something went wrong" won't be colorized. --- Gopkg.lock | 41 +++++++++++++++++++++++++++++++++++++++++ main.go | 30 ++++++++++-------------------- 2 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 Gopkg.lock diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..7208b10 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,41 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + digest = "1:c9bebdae4ac52d0c3bbe5876de3d72f3bb05b4986865cdb3f15e305e1dc4fbca" + name = "github.com/fatih/color" + packages = ["."] + pruneopts = "" + revision = "570b54cabe6b8eb0bc2dfce68d964677d63b5260" + version = "v1.5.0" + +[[projects]] + digest = "1:9ea83adf8e96d6304f394d40436f2eb44c1dc3250d223b74088cc253a6cd0a1c" + name = "github.com/mattn/go-colorable" + packages = ["."] + pruneopts = "" + revision = "167de6bfdfba052fa6b2d3664c8f5272e23c9072" + version = "v0.0.9" + +[[projects]] + digest = "1:420bb8eabfa6dff0cb5c8e786489a420e7e9d93a8b1d83882cce70c5a5b9bc3e" + name = "github.com/mattn/go-isatty" + packages = ["."] + pruneopts = "" + revision = "fc9e8d8ef48496124e79ae0df75490096eccf6fe" + version = "v0.0.2" + +[[projects]] + branch = "master" + digest = "1:c61dac47382f9834b58d28a3ef9072a569960d364fd76afc19487fb63550be81" + name = "golang.org/x/sys" + packages = ["unix"] + pruneopts = "" + revision = "b6e1ae21643682ce023deb8d152024597b0e9bb4" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + input-imports = ["github.com/fatih/color"] + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/main.go b/main.go index e99d1e6..4bf89f1 100644 --- a/main.go +++ b/main.go @@ -21,9 +21,9 @@ import ( ) var ( - success = color.New(color.FgGreen) - skipped = color.New(color.FgYellow) - fail = color.New(color.FgHiRed) + success = color.FgGreen + skipped = color.FgYellow + fail = color.FgHiRed ) const paletteEnv = "GOTEST_PALETTE" @@ -75,41 +75,31 @@ func consume(wg *sync.WaitGroup, r io.Reader) { } } -var c *color.Color - func parse(line string) { trimmed := strings.TrimSpace(line) + defer color.Unset() switch { - case strings.HasPrefix(trimmed, "=== RUN"): - fallthrough - case strings.HasPrefix(trimmed, "?"): - c = nil - // success case strings.HasPrefix(trimmed, "--- PASS"): fallthrough case strings.HasPrefix(trimmed, "ok"): fallthrough case strings.HasPrefix(trimmed, "PASS"): - c = success + color.Set(success) // skipped case strings.HasPrefix(trimmed, "--- SKIP"): - c = skipped + color.Set(skipped) // failure case strings.HasPrefix(trimmed, "--- FAIL"): fallthrough case strings.HasPrefix(trimmed, "FAIL"): - c = fail + color.Set(fail) } - if c == nil { - fmt.Printf("%s\n", line) - return - } - c.Printf("%s\n", line) + fmt.Printf("%s\n", line) } func enableOnCI() { @@ -138,10 +128,10 @@ func setPalette() { return } if c, ok := colors[vals[0]]; ok { - fail = color.New(c) + fail = c } if c, ok := colors[vals[1]]; ok { - success = color.New(c) + success = c } }