From 1fd8679e5f8a39bf1e1cc0f0abd949d0c4d7809f Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Fri, 23 Jul 2021 16:25:20 +0300 Subject: [PATCH] Support NO_COLOR (https://no-color.org). Also, handle `IS_NO_COLOR=1` and `IS_NO_COLOR=FALSE`. --- README.md | 5 +++-- is.go | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e2d1763..1ea022f 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,13 @@ func Test(t *testing.T) { ## Color -To turn off the colors, run `go test` with the `-nocolor` flag, or with the env var `IS_NO_COLOR=true`. +To turn off the colors, run `go test` with the `-nocolor` flag, +or with the env var [`NO_COLOR` (with any value)](https://no-color.org). ``` go test -nocolor ``` ``` -IS_NO_COLOR=true go test +NO_COLOR=1 go test ``` diff --git a/is.go b/is.go index a95a409..305c153 100644 --- a/is.go +++ b/is.go @@ -22,7 +22,6 @@ // the helper methods: // // func Test(t *testing.T) { -// // // always start tests with this // is := is.New(t) // @@ -32,7 +31,6 @@ // // body := readBody(r) // is.True(strings.Contains(body, "Hi there")) -// // } package is @@ -45,6 +43,7 @@ import ( "os" "path/filepath" "reflect" + "strconv" "strings" ) @@ -74,7 +73,19 @@ type I struct { var noColorFlag bool func init() { - envNoColor := os.Getenv("IS_NO_COLOR") == "true" + var envNoColor bool + + // prefer https://no-color.org (with any value) + if _, ok := os.LookupEnv("NO_COLOR"); ok { + envNoColor = true + } + + if v, ok := os.LookupEnv("IS_NO_COLOR"); ok { + if b, err := strconv.ParseBool(v); err == nil { + envNoColor = b + } + } + flag.BoolVar(&noColorFlag, "nocolor", envNoColor, "turns off colors") }