├── LICENSE ├── README.md ├── go.mod ├── go.sum ├── main.go └── testdata └── main_test.go /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Masayuki Izumi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cgt 2 | Painting `go test` outputs (inspired by [rakyll/gotest](https://github.com/rakyll/gotest)) 3 | 4 | ## Usage 5 | 6 | ``` 7 | $ go test -v ./... | cgt 8 | ``` 9 | 10 | [![asciicast](https://asciinema.org/a/VJO3hJng5gTll8klmqeO3oKZW.svg)](https://asciinema.org/a/VJO3hJng5gTll8klmqeO3oKZW) 11 | 12 | ## Installation 13 | 14 | ``` 15 | $ go get -u github.com/izumin5210/cgt 16 | ``` 17 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/izumin5210/cgt 2 | 3 | require ( 4 | github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e 5 | golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 6 | golang.org/x/sys v0.0.0-20181031143558-9b800f95dbbc // indirect 7 | ) 8 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e h1:9MlwzLdW7QSDrhDjFlsEYmxpFyIoXmYRon3dt0io31k= 2 | github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= 3 | golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA= 4 | golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 5 | golang.org/x/sys v0.0.0-20181031143558-9b800f95dbbc h1:SdCq5U4J+PpbSDIl9bM0V1e1Ug1jsnBkAFvTs1htn7U= 6 | golang.org/x/sys v0.0.0-20181031143558-9b800f95dbbc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 7 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "errors" 6 | "fmt" 7 | "os" 8 | "strings" 9 | 10 | "github.com/logrusorgru/aurora" 11 | "golang.org/x/crypto/ssh/terminal" 12 | ) 13 | 14 | func main() { 15 | var exitCode int 16 | 17 | if err := run(); err != nil { 18 | fmt.Fprintln(os.Stderr, err) 19 | exitCode = 1 20 | } 21 | 22 | os.Exit(exitCode) 23 | } 24 | 25 | func run() error { 26 | if terminal.IsTerminal(0) { 27 | return errors.New("require stdin from pipe") 28 | } 29 | 30 | s := bufio.NewScanner(os.Stdin) 31 | 32 | for s.Scan() { 33 | parse(s.Text()) 34 | } 35 | 36 | return nil 37 | } 38 | 39 | var ( 40 | success = aurora.Green 41 | fail = aurora.Red 42 | skipped = aurora.Brown 43 | info = aurora.Gray 44 | 45 | c func(interface{}) aurora.Value 46 | ) 47 | 48 | // ref: https://github.com/rakyll/gotest/blob/86f0749cd8ccdc08f2edb69f170ad6f06393455d/main.go#L77-L108 49 | func parse(line string) { 50 | trimmed := strings.TrimSpace(line) 51 | 52 | switch { 53 | // start 54 | case strings.HasPrefix(trimmed, "=== RUN"): 55 | c = aurora.Bold 56 | case strings.HasPrefix(trimmed, "?"): 57 | c = nil 58 | 59 | // info 60 | case strings.HasPrefix(trimmed, "=== CONT"): 61 | fallthrough 62 | case strings.HasPrefix(trimmed, "=== PAUSE"): 63 | c = info 64 | 65 | // success 66 | case strings.HasPrefix(trimmed, "--- PASS"): 67 | fallthrough 68 | case strings.HasPrefix(trimmed, "ok"): 69 | fallthrough 70 | case strings.HasPrefix(trimmed, "PASS"): 71 | c = success 72 | 73 | // failure 74 | case strings.HasPrefix(trimmed, "--- FAIL"): 75 | fallthrough 76 | case strings.HasPrefix(trimmed, "FAIL"): 77 | c = fail 78 | 79 | // skipped 80 | case strings.HasPrefix(trimmed, "--- SKIP"): 81 | c = skipped 82 | } 83 | 84 | if c == nil { 85 | fmt.Println(line) 86 | return 87 | } 88 | fmt.Println(c(line)) 89 | } 90 | -------------------------------------------------------------------------------- /testdata/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "testing" 4 | 5 | func TestA(t *testing.T) { 6 | // no-op 7 | } 8 | 9 | func TestB(t *testing.T) { 10 | t.Error("error!") 11 | t.Fatal("failed") 12 | } 13 | 14 | func TestC(t *testing.T) { 15 | t.Skip("skipped") 16 | t.Log("hello") 17 | } 18 | 19 | func TestD(t *testing.T) { 20 | t.Parallel() 21 | t.Log("hello") 22 | } 23 | 24 | func TestE(t *testing.T) { 25 | t.Parallel() 26 | t.Run("1", func(t *testing.T) { 27 | // no-op 28 | }) 29 | t.Run("2", func(t *testing.T) { 30 | t.Error("error!") 31 | }) 32 | t.Run("3", func(t *testing.T) { 33 | t.Skip("skipped") 34 | }) 35 | t.Run("4", func(t *testing.T) { 36 | t.Error("error!") 37 | }) 38 | } 39 | 40 | func TestF(t *testing.T) { 41 | t.Parallel() 42 | t.Error("error!") 43 | } 44 | --------------------------------------------------------------------------------