Skip to content

Commit

Permalink
Fix color leakage (#10)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
subtlepseudonym committed Jun 5, 2020
1 parent f3a7c3e commit cffa99d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
41 changes: 41 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 10 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit cffa99d

Please sign in to comment.