Skip to content

Commit

Permalink
Merge pull request #32 from dnephin/add-no-output-value-for-summary
Browse files Browse the repository at this point in the history
Add output value as an option for summary flag
  • Loading branch information
dnephin committed Dec 1, 2018
2 parents 3bf723a + 2867c70 commit 23f01ef
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 17 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ output.
- [JUnit XML](#junit-xml)
- [JSON file](#json-file-output)
- [Custom command](#custom-go-test-command)

### Format

Set a format with the `--format` flag or the `GOTESTSUM_FORMAT` environment
Expand Down Expand Up @@ -63,6 +64,12 @@ Example: hide failed and skipped
gotestsum --no-summary=skipped,failed
```

Example: hide output in the summary, only print names of failed and skipped tests
and errors
```
gotestsum --no-summary=output
```

### JUnit XML

In addition to the normal test output you can write a JUnit XML file for
Expand Down
2 changes: 1 addition & 1 deletion dobifiles/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ENV PS1="# "


FROM golang as tools
ARG FILEWATCHER=v0.3.0
ARG FILEWATCHER=v0.3.2
RUN go get -d github.com/dnephin/filewatcher && \
cd /go/src/github.com/dnephin/filewatcher && \
git checkout -q "$FILEWATCHER" && \
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Formats:
"write a JUnit XML file")
flags.BoolVar(&opts.noColor, "no-color", false, "disable color output")
flags.StringSliceVar(&opts.noSummary, "no-summary", nil,
"do not print summary of: failed, skipped, errors")
"do not print summary of: failed, skipped, errors, output")
return flags, opts
}

Expand Down Expand Up @@ -215,6 +215,8 @@ func summarizer(opts *options) func(io.Writer, *testjson.Execution) error {
summary -= testjson.SummarizeSkipped
case "errors":
summary -= testjson.SummarizeErrors
case "output":
summary -= testjson.SummarizeOutput
}
}
return func(out io.Writer, exec *testjson.Execution) error {
Expand Down
37 changes: 30 additions & 7 deletions testjson/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ const (
SummarizeSkipped
SummarizeFailed
SummarizeErrors
SummarizeAll = SummarizeSkipped | SummarizeFailed | SummarizeErrors
SummarizeOutput
SummarizeAll = SummarizeSkipped | SummarizeFailed | SummarizeErrors | SummarizeOutput
)

// PrintSummary of a test Execution. Prints a section for each summary type
// followed by a DONE line.
func PrintSummary(out io.Writer, execution *Execution, opts Summary) error {
execSummary := newExecSummary(execution, opts)
if opts&SummarizeSkipped != 0 {
writeTestCaseSummary(out, execution, formatSkipped())
writeTestCaseSummary(out, execSummary, formatSkipped())
}
if opts&SummarizeFailed != 0 {
writeTestCaseSummary(out, execution, formatFailed())
writeTestCaseSummary(out, execSummary, formatFailed())
}

errors := execution.Errors()
Expand Down Expand Up @@ -88,7 +90,28 @@ func countErrors(errors []string) int {
return count
}

func writeTestCaseSummary(out io.Writer, execution *Execution, conf testCaseFormatConfig) {
type executionSummary interface {
Failed() []TestCase
Skipped() []TestCase
OutputLines(pkg, test string) []string
}

type noOutputSummary struct {
Execution
}

func (s *noOutputSummary) OutputLines(_, _ string) []string {
return nil
}

func newExecSummary(execution *Execution, opts Summary) executionSummary {
if opts&SummarizeOutput != 0 {
return execution
}
return &noOutputSummary{Execution: *execution}
}

func writeTestCaseSummary(out io.Writer, execution executionSummary, conf testCaseFormatConfig) {
testCases := conf.getter(execution)
if len(testCases) == 0 {
return
Expand All @@ -114,7 +137,7 @@ type testCaseFormatConfig struct {
header string
prefix string
filter func(string) bool
getter func(*Execution) []TestCase
getter func(executionSummary) []TestCase
}

func formatFailed() testCaseFormatConfig {
Expand All @@ -125,7 +148,7 @@ func formatFailed() testCaseFormatConfig {
filter: func(line string) bool {
return strings.HasPrefix(line, "--- FAIL: Test")
},
getter: func(execution *Execution) []TestCase {
getter: func(execution executionSummary) []TestCase {
return execution.Failed()
},
}
Expand All @@ -139,7 +162,7 @@ func formatSkipped() testCaseFormatConfig {
filter: func(line string) bool {
return strings.HasPrefix(line, "--- SKIP: Test")
},
getter: func(execution *Execution) []TestCase {
getter: func(execution executionSummary) []TestCase {
return execution.Skipped()
},
}
Expand Down
61 changes: 53 additions & 8 deletions testjson/summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"gotest.tools/assert"
)

func TestPrintSummaryNoFailures(t *testing.T) {
func TestPrintSummary_NoFailures(t *testing.T) {
fake, reset := patchClock()
defer reset()

Expand All @@ -23,19 +23,18 @@ func TestPrintSummaryNoFailures(t *testing.T) {
},
}
fake.Advance(34123111 * time.Microsecond)
err := PrintSummary(out, exec, SummarizeNone)
err := PrintSummary(out, exec, SummarizeAll)
assert.NilError(t, err)

expected := "\nDONE 13 tests in 34.123s\n"
assert.Equal(t, out.String(), expected)
}

func TestPrintSummaryWithFailures(t *testing.T) {
func TestPrintSummary_WithFailures(t *testing.T) {
defer patchPkgPathPrefix("example.com")()
fake, reset := patchClock()
defer reset()

out := new(bytes.Buffer)
exec := &Execution{
started: fake.Now(),
packages: map[string]*Package{
Expand Down Expand Up @@ -105,10 +104,13 @@ Some stdout/stderr here
},
}
fake.Advance(34123111 * time.Microsecond)
err := PrintSummary(out, exec, SummarizeAll)
assert.NilError(t, err)

expected := `
t.Run("summarize all", func(t *testing.T) {
out := new(bytes.Buffer)
err := PrintSummary(out, exec, SummarizeAll)
assert.NilError(t, err)

expected := `
=== Skipped
=== SKIP: project/pkg/more TestOnlySometimes (0.00s)
good_test.go:27: the skip message
Expand All @@ -133,7 +135,50 @@ pkg/file.go:99:12: missing ',' before newline
DONE 13 tests, 1 skipped, 4 failures, 1 error in 34.123s
`
assert.Equal(t, out.String(), expected)
assert.Equal(t, out.String(), expected)
})

t.Run("summarize no output", func(t *testing.T) {
out := new(bytes.Buffer)
err := PrintSummary(out, exec, SummarizeAll-SummarizeOutput)
assert.NilError(t, err)

expected := `
=== Skipped
=== SKIP: project/pkg/more TestOnlySometimes (0.00s)
=== Failed
=== FAIL: project/badmain (0.00s)
=== FAIL: project/fs TestFileDo (1.41s)
=== FAIL: project/fs TestFileDoError (0.01s)
=== FAIL: project/pkg/more TestAlbatross (0.04s)
=== Errors
pkg/file.go:99:12: missing ',' before newline
DONE 13 tests, 1 skipped, 4 failures, 1 error in 34.123s
`
assert.Equal(t, out.String(), expected)
})

t.Run("summarize only errors", func(t *testing.T) {
out := new(bytes.Buffer)
err := PrintSummary(out, exec, SummarizeErrors)
assert.NilError(t, err)

expected := `
=== Errors
pkg/file.go:99:12: missing ',' before newline
DONE 13 tests, 1 skipped, 4 failures, 1 error in 34.123s
`
assert.Equal(t, out.String(), expected)
})
}

func patchClock() (clockwork.FakeClock, func()) {
Expand Down

0 comments on commit 23f01ef

Please sign in to comment.