Skip to content

Commit

Permalink
feat: Make the test output easier to read (#4931)
Browse files Browse the repository at this point in the history
* feat: Add coloring to the test output

Makes it a bit easier to read the output at a glance. If the terminal does not support coloring it gets disabled automatically https://github.com/fatih/color#disableenable-color

* feat: Separate test failures a bit more

I find these tend to bleed together, so forcing a newline between each makes it clear where the output of one test ends and the next begins.

* chore: make staticcheck
  • Loading branch information
Markus Westerlind authored Jul 5, 2022
1 parent 9f8842a commit ad965c1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
56 changes: 20 additions & 36 deletions cmd/flux/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"strings"
"sync"

"github.com/fatih/color"
"github.com/influxdata/flux"
"github.com/influxdata/flux/ast"
"github.com/influxdata/flux/ast/astutil"
Expand Down Expand Up @@ -54,6 +55,7 @@ func (e failedTests) Silent() {}

func TestCommand(setup TestSetupFunc) *cobra.Command {
var flags TestFlags

testCommand := &cobra.Command{
Use: "test",
Short: "Run flux tests",
Expand All @@ -80,6 +82,7 @@ error running the tests or at least one test failed.
return nil
},
}

testCommand.Flags().StringSliceVarP(&flags.paths, "path", "p", nil, "The root level directory for all packages.")
testCommand.Flags().StringSliceVar(&flags.testNames, "test", []string{}, "List of test names to run. These tests will run regardless of tags or skips.")
testCommand.Flags().StringSliceVar(&flags.testTags, "tags", []string{}, "List of tags. Tests only run if all of their tags are provided.")
Expand All @@ -88,6 +91,9 @@ error running the tests or at least one test failed.
testCommand.Flags().BoolVarP(&flags.parallel, "parallel", "", false, "Enables parallel test execution.")
testCommand.Flags().CountVarP(&flags.verbosity, "verbose", "v", "verbose (-v, -vv, or -vvv)")
testCommand.Flags().BoolVarP(&flags.noinit, "noinit", "", false, "Disables Flux initialization, used for testing this command.")

testCommand.SetOutput(color.Output)

return testCommand
}

Expand Down Expand Up @@ -904,49 +910,27 @@ func NewTestReporter(out io.Writer, verbosity int) TestReporter {
func (t *TestReporter) ReportTestRun(test *Test) {
if t.verbosity == 0 {
if test.skip {
fmt.Fprint(t.out, "s")
fmt.Fprint(t.out, color.YellowString("s"))
} else if test.Error() != nil {
fmt.Fprint(t.out, "x")
} else {
fmt.Fprint(t.out, ".")
}
} else if t.verbosity == 1 {
if test.skip {
// Do not print skipped tests until the next verbosity level
} else if err := test.Error(); err != nil {
fmt.Fprintf(t.out, "%s...fail: %s\n", test.FullName(), err)
} else {
fmt.Fprintf(t.out, "%s...success\n", test.FullName())
}
} else if t.verbosity == 2 {
if test.skip {
fmt.Fprintf(t.out, "%s...skip\n", test.FullName())
} else if err := test.Error(); err != nil {
fmt.Fprintf(t.out, "%s...fail: %s\n", test.FullName(), err)
fmt.Fprint(t.out, color.RedString("x"))
} else {
fmt.Fprintf(t.out, "%s...success\n", test.FullName())
fmt.Fprint(t.out, color.GreenString("."))
}
} else {
if test.skip {
// Do not print full output of skipped tests
// Using verbosity >=3 is about debugging a running test,
// we do not need information about skipped tests
return
}
fmt.Fprintf(t.out, "Testcase: %s\n", test.FullName())
fmt.Fprintf(t.out, "Tags: %v\n", test.tags)
source, err := test.SourceCode()
if err != nil {
fmt.Fprintln(t.out, "Source: FAILED")
} else {
fmt.Fprintf(t.out, "Source: \n%s", source)
if t.verbosity != 1 {
source, err := test.SourceCode()
if err != nil {
fmt.Printf("failed to get source for test %s: %s\n", test.FullName(), err)
} else {
fmt.Printf("Full source for test case %q\n%s", test.FullName(), source)
}
}
if test.skip {
fmt.Fprintf(t.out, "Result: %s...skip\n", test.Name())
fmt.Fprintf(t.out, "%s ... %s\n", test.FullName(), color.YellowString("skip"))
} else if err := test.Error(); err != nil {
fmt.Fprintf(t.out, "Result: %s...fail: %s\n", test.Name(), err)
fmt.Fprintf(t.out, "%s ... %s: %s\n", test.FullName(), color.RedString("fail"), err)
} else {
fmt.Fprintf(t.out, "Result: %s...success\n", test.Name())
fmt.Fprintf(t.out, "%s ... %s\n", test.FullName(), color.GreenString("success"))
}
}
}
Expand All @@ -966,7 +950,7 @@ func (t *TestReporter) Summarize(tests []*Test) bool {
fmt.Fprintf(t.out, "\nfailures:\n\n")
for _, test := range tests {
if err := test.Error(); err != nil {
fmt.Fprintf(t.out, "\t%s...fail: %s\n", test.FullName(), err)
fmt.Fprintf(t.out, "\t%s ... %s: %s\n\n", test.FullName(), color.RedString("fail"), err)
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require github.com/fatih/color v1.13.0

require (
cloud.google.com/go/bigquery v1.8.0 // indirect
github.com/Azure/azure-pipeline-go v0.2.3 // indirect
Expand Down Expand Up @@ -100,9 +102,9 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-ieproxy v0.0.1 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/miekg/dns v1.1.22 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pierrec/lz4/v4 v4.1.11 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.m
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
Expand Down Expand Up @@ -444,16 +446,18 @@ github.com/matryer/moq v0.0.0-20190312154309-6cfb0558e1bd/go.mod h1:9ELz6aaclSIG
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-ieproxy v0.0.1 h1:qiyop7gCflfhwCzGyeT0gro3sF9AIg9HU98JORTkqfI=
github.com/mattn/go-ieproxy v0.0.1/go.mod h1:pYabZ6IHcRpFh7vIaLfK7rdcWgFEb3SFJ6/gNWuh88E=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
Expand Down

0 comments on commit ad965c1

Please sign in to comment.