Skip to content

Commit

Permalink
Convert two more formatters to new interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed Mar 31, 2023
1 parent 04b65f9 commit fb4d0c4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
27 changes: 17 additions & 10 deletions testjson/dotformat.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testjson

import (
"bufio"
"fmt"
"io"
"os"
Expand All @@ -13,15 +14,21 @@ import (
"gotest.tools/gotestsum/internal/log"
)

func dotsFormatV1(event TestEvent, exec *Execution) string {
pkg := exec.Package(event.Package)
switch {
case event.PackageEvent():
return ""
case event.Action == ActionRun && pkg.Total == 1:
return "[" + RelativePackagePath(event.Package) + "]"
}
return fmtDot(event)
func dotsFormatV1(out io.Writer) EventFormatter {
buf := bufio.NewWriter(out)
// nolint:errcheck
return eventFormatterFunc(func(event TestEvent, exec *Execution) error {
pkg := exec.Package(event.Package)
switch {
case event.PackageEvent():
return nil
case event.Action == ActionRun && pkg.Total == 1:
buf.WriteString("[" + RelativePackagePath(event.Package) + "]")
return buf.Flush()
}
buf.WriteString(fmtDot(event))
return buf.Flush()
})
}

func fmtDot(event TestEvent) string {
Expand Down Expand Up @@ -72,7 +79,7 @@ func newDotFormatter(out io.Writer, opts FormatOptions) EventFormatter {
w, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil || w == 0 {
log.Warnf("Failed to detect terminal width for dots format, error: %v", err)
return &formatAdapter{format: dotsFormatV1, out: out}
return dotsFormatV1(out)
}
return &dotFormatter{
pkgs: make(map[string]*dotLine),
Expand Down
30 changes: 17 additions & 13 deletions testjson/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@ func standardVerboseFormat(out io.Writer) EventFormatter {
}

// go test
func standardQuietFormat(event TestEvent, _ *Execution) string {
if !event.PackageEvent() {
return ""
}
if event.Output == "PASS\n" || isCoverageOutput(event.Output) {
return ""
}
if isWarningNoTestsToRunOutput(event.Output) {
return ""
}
func standardQuietFormat(out io.Writer) EventFormatter {
buf := bufio.NewWriter(out)
return eventFormatterFunc(func(event TestEvent, _ *Execution) error {
if !event.PackageEvent() {
return nil
}
if event.Output == "PASS\n" || isCoverageOutput(event.Output) {
return nil
}
if isWarningNoTestsToRunOutput(event.Output) {
return nil
}

return event.Output
_, _ = buf.WriteString(event.Output)
return buf.Flush()
})
}

// go test -json
Expand Down Expand Up @@ -272,9 +276,9 @@ func NewEventFormatter(out io.Writer, format string, formatOpts FormatOptions) E
case "standard-verbose":
return standardVerboseFormat(out)
case "standard-quiet":
return &formatAdapter{out, standardQuietFormat}
return standardQuietFormat(out)
case "dots", "dots-v1":
return &formatAdapter{out, dotsFormatV1}
return dotsFormatV1(out)
case "dots-v2":
return newDotFormatter(out, formatOpts)
case "testname", "short-verbose":
Expand Down
8 changes: 4 additions & 4 deletions testjson/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestFormats_DefaultGoTestJson(t *testing.T) {
},
{
name: "dots-v1",
format: withAdapter(dotsFormatV1),
format: dotsFormatV1,
expectedOut: "format/dots-v1.out",
},
{
Expand All @@ -124,7 +124,7 @@ func TestFormats_DefaultGoTestJson(t *testing.T) {
},
{
name: "standard-quiet",
format: withAdapter(standardQuietFormat),
format: standardQuietFormat,
expectedOut: "format/standard-quiet.out",
},
{
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestFormats_Coverage(t *testing.T) {
},
{
name: "standard-quiet",
format: withAdapter(standardQuietFormat),
format: standardQuietFormat,
expectedOut: "format/standard-quiet-coverage.out",
},
}
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestFormats_Shuffle(t *testing.T) {
},
{
name: "standard-quiet",
format: withAdapter(standardQuietFormat),
format: standardQuietFormat,
expectedOut: "format/standard-quiet-shuffle.out",
},
}
Expand Down

0 comments on commit fb4d0c4

Please sign in to comment.