Skip to content
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,27 @@ $ gotest -v github.com/rakyll/hey
```
![go test output](https://i.imgur.com/udjWuZx.gif)

gotest comes with many colors! Configure the color of the output by setting the following env variable:
gotest comes with many colors! Configure the color of the output by setting the following environment variables:

- `GOTEST_FAIL`
- `GOTEST_PASS`
- `GOTEST_SKIP`

Alternatively you can use a single environment supporting a list of colors, in the order: fail, pass, and skip.

```
$ GOTEST_PALETTE="magenta,white"
```

The output will have magenta for failed cases, white for success.
Available colors: black, hiblack, red, hired, green, higreen, yellow, hiyellow, blue, hiblue, magenta, himagenta, cyan, hicyan, white, hiwhite.

Do note that the individually set environment variables take precedence over the palette variable

For the setting:

```
$ GOTEST_PASS="hiblue" GOTEST_PALETTE="magenta,white"
```

The output will have magenta for failed cases, hiblue for success.
63 changes: 48 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@ import (
"github.com/fatih/color"
)

const (
paletteEnv = "GOTEST_PALETTE"
failEnv = "GOTEST_FAIL_COLOR"
passEnv = "GOTEST_PASS_COLOR"
skipEnv = "GOTEST_SKIP_COLOR"
)

var (
pass = color.FgGreen
skip = color.FgYellow
fail = color.FgHiRed
)

const paletteEnv = "GOTEST_PALETTE"

func main() {
setPalette()
parseEnvAndSetPalette()
enableOnCI()
os.Exit(gotest(os.Args[1:]))
}
Expand Down Expand Up @@ -144,20 +149,48 @@ func enableOnCI() {
}
}

func setPalette() {
func parseEnvAndSetPalette() {
parsePaletteEnv()
parseColorEnvs()
}

func parsePaletteEnv() {
v := os.Getenv(paletteEnv)
if v == "" {
return
}
vals := strings.Split(v, ",")
if len(vals) != 2 {
return
}
if c, ok := colors[vals[0]]; ok {
fail = c

if v != "" {
vals := strings.Split(v, ",")
states := []color.Attribute{fail, pass, skip}
for i := range vals {
if c, ok := colors[vals[i]]; ok {
states[i] = color.Attribute(c)
}
}

fail = states[0]
pass = states[1]
skip = states[2]
}
if c, ok := colors[vals[1]]; ok {
pass = c
}

func parseColorEnvs() {

envArray := [3]string{skipEnv, failEnv, passEnv}

for _, e := range envArray {
v := os.Getenv(e)
if v == "" {
continue
}
if c, ok := colors[v]; ok {
switch e {
case failEnv:
fail = c
case passEnv:
pass = c
case skipEnv:
skip = c
}
}
}
}

Expand Down