Skip to content

Commit

Permalink
Hide standard-json format for now
Browse files Browse the repository at this point in the history
Similar to debug, keep this format out of help text, since in most cases
it would be better to product the desired format directly in gotestsum.

Also change to the new closure style of formatter that allows writing
bytes directly without converting to string.
  • Loading branch information
dnephin committed Mar 29, 2023
1 parent 612250f commit 7d73b4e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 351 deletions.
1 change: 0 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ Formats:
pkgname print a line for each package
pkgname-and-test-fails print a line for each package and failed test output
testname print a line for each test and package
standard-json standard go test -json format
standard-quiet standard go test format
standard-verbose standard go test -v format
Expand Down
1 change: 0 additions & 1 deletion cmd/testdata/gotestsum-help-text
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Formats:
pkgname print a line for each package
pkgname-and-test-fails print a line for each package and failed test output
testname print a line for each test and package
standard-json standard go test -json format
standard-quiet standard go test format
standard-verbose standard go test -v format

Expand Down
24 changes: 16 additions & 8 deletions testjson/format.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package testjson

import (
"encoding/json"
"bufio"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -43,12 +43,14 @@ func standardQuietFormat(event TestEvent, _ *Execution) string {
}

// go test -json
func standardJSONFormat(event TestEvent, _ *Execution) string {
b, err := json.Marshal(event)
if err != nil {
return ""
}
return string(b) + "\n"
func standardJSONFormat(out io.Writer) EventFormatter {
buf := bufio.NewWriter(out)
// nolint:errcheck // errors are returned by Flush
return eventFormatterFunc(func(event TestEvent, _ *Execution) error {
buf.Write(event.raw)
buf.WriteRune('\n')
return buf.Flush()
})
}

func testNameFormat(event TestEvent, exec *Execution) string {
Expand Down Expand Up @@ -240,6 +242,12 @@ type EventFormatter interface {
Format(event TestEvent, output *Execution) error
}

type eventFormatterFunc func(event TestEvent, output *Execution) error

func (e eventFormatterFunc) Format(event TestEvent, output *Execution) error {
return e(event, output)
}

type FormatOptions struct {
HideEmptyPackages bool
UseHiVisibilityIcons bool
Expand All @@ -251,7 +259,7 @@ func NewEventFormatter(out io.Writer, format string, formatOpts FormatOptions) E
case "debug":
return &formatAdapter{out, debugFormat}
case "standard-json":
return &formatAdapter{out, standardJSONFormat}
return standardJSONFormat(out)
case "standard-verbose":
return &formatAdapter{out, standardVerboseFormat}
case "standard-quiet":
Expand Down
31 changes: 19 additions & 12 deletions testjson/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testjson

import (
"bytes"
"io"
"testing"

"gotest.tools/v3/assert"
Expand Down Expand Up @@ -55,7 +56,6 @@ func newFakeHandler(formatter EventFormatter, inputName string) *fakeHandler {
return &fakeHandler{
inputName: inputName,
formatter: formatter,
out: new(bytes.Buffer),
err: new(bytes.Buffer),
}
}
Expand All @@ -77,20 +77,27 @@ func patchPkgPathPrefix(t *testing.T, val string) {
})
}

func withAdapter(format func(TestEvent, *Execution) string) func(io.Writer) EventFormatter {
return func(out io.Writer) EventFormatter {
return &formatAdapter{out: out, format: format}
}
}

func TestFormats_DefaultGoTestJson(t *testing.T) {
type testCase struct {
name string
format func(event TestEvent, exec *Execution) string
format func(io.Writer) EventFormatter
expectedOut string
expected func(t *testing.T, exec *Execution)
}

run := func(t *testing.T, tc testCase) {
shim := newFakeHandlerWithAdapter(tc.format, "input/go-test-json")
out := new(bytes.Buffer)
shim := newFakeHandler(tc.format(out), "input/go-test-json")
exec, err := ScanTestOutput(shim.Config(t))
assert.NilError(t, err)

golden.Assert(t, shim.out.String(), tc.expectedOut)
golden.Assert(t, out.String(), tc.expectedOut)
golden.Assert(t, shim.err.String(), "input/go-test-json.err")

if tc.expected != nil {
Expand All @@ -101,43 +108,43 @@ func TestFormats_DefaultGoTestJson(t *testing.T) {
testCases := []testCase{
{
name: "testname",
format: testNameFormat,
format: withAdapter(testNameFormat),
expectedOut: "format/testname.out",
},
{
name: "dots-v1",
format: dotsFormatV1,
format: withAdapter(dotsFormatV1),
expectedOut: "format/dots-v1.out",
},
{
name: "pkgname",
format: pkgNameFormat(FormatOptions{}),
format: withAdapter(pkgNameFormat(FormatOptions{})),
expectedOut: "format/pkgname.out",
},
{
name: "pkgname-hivis",
format: pkgNameFormat(FormatOptions{UseHiVisibilityIcons: true}),
format: withAdapter(pkgNameFormat(FormatOptions{UseHiVisibilityIcons: true})),
expectedOut: "format/pkgname-hivis.out",
},
{
name: "pkgname",
format: pkgNameFormat(FormatOptions{HideEmptyPackages: true}),
format: withAdapter(pkgNameFormat(FormatOptions{HideEmptyPackages: true})),
expectedOut: "format/pkgname-hide-empty.out",
},
{
name: "standard-verbose",
format: standardVerboseFormat,
format: withAdapter(standardVerboseFormat),
expectedOut: "format/standard-verbose.out",
},
{
name: "standard-quiet",
format: standardQuietFormat,
format: withAdapter(standardQuietFormat),
expectedOut: "format/standard-quiet.out",
},
{
name: "standard-json",
format: standardJSONFormat,
expectedOut: "format/standard-json.out",
expectedOut: "input/go-test-json.out",
},
}

Expand Down
Loading

0 comments on commit 7d73b4e

Please sign in to comment.