Skip to content

Support for configuration of color for skipped tests via environment variable #17

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
59 changes: 52 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Like `go test` but with colors.

## Installation

```
```bash
$ go get -u github.com/rakyll/gotest
```

Expand All @@ -14,16 +14,61 @@ Accepts all the arguments and flags `go test` works with.

Example:

```
```bash
$ 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:
The default output colors for `gotest` are:

- `hired` for failed test cases
- `green` for passing test cases
- `yellow` for skipped test cases

`gotest` comes with many colors. All available colors are:

- `black`
- `hiblack`
- `red`
- `hired`
- `green`
- `higreen`
- `yellow`
- `hiyellow`
- `blue`
- `hiblue`
- `magenta`
- `himagenta`
- `cyan`
- `hicyan`
- `white`
- `hiwhite`

For a graphical presentation of the colors please see the documentation for the Go package [fatih/color](https://pkg.go.dev/mod/github.com/fatih/color).

You can configure the color of the output by setting the environment variable `GOTEST_PALETTE`:

```bash
$ GOTEST_PALETTE="magenta,white,hiyellow" gotest -v github.com/rakyll/hey
```
$ 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.
The output will have `magenta` for failed test cases and `white` for passing test cases and `hiyellow` for skipped test cases.

The order of the colors for are:

1 failed test cases
1 passing test cases
1 skipped test cases

If you only want to overwrite the colors for indicating passing or skipped test cases, you can leave the spot empty.

This example demonstrates:

- `hired` for failed test cases, using the default
- `white` for passing test cases, overwriting the default
- `hiyellow` for skipped test cases, overwriting the default

```bash
$ GOTEST_PALETTE=",white,hiyellow" gotest -v github.com/rakyll/hey
```
18 changes: 10 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,18 @@ func setPalette() {
if v == "" {
return
}

vals := strings.Split(v, ",")
if len(vals) != 2 {
return
}
if c, ok := colors[vals[0]]; ok {
fail = color.New(c)
}
if c, ok := colors[vals[1]]; ok {
success = color.New(c)
states := []*color.Color{fail, success, skipped}
for i := range vals {
if c, ok := colors[vals[i]]; ok {
states[i] = color.New(c)
}
}

fail = states[0]
success = states[1]
skipped = states[2]
}

var colors = map[string]color.Attribute{
Expand Down