diff --git a/testjson/format.go b/testjson/format.go index 81cdcf8b..4739fbd6 100644 --- a/testjson/format.go +++ b/testjson/format.go @@ -1,6 +1,7 @@ package testjson import ( + "bufio" "fmt" "io" "strings" @@ -41,6 +42,17 @@ func standardQuietFormat(event TestEvent, _ *Execution) string { return event.Output } +// go test -json +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 { result := colorEvent(event)(strings.ToUpper(string(event.Action))) formatTest := func() string { @@ -230,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 @@ -240,6 +258,8 @@ func NewEventFormatter(out io.Writer, format string, formatOpts FormatOptions) E switch format { case "debug": return &formatAdapter{out, debugFormat} + case "standard-json": + return standardJSONFormat(out) case "standard-verbose": return &formatAdapter{out, standardVerboseFormat} case "standard-quiet": diff --git a/testjson/format_test.go b/testjson/format_test.go index 87e8e376..e11cdd9a 100644 --- a/testjson/format_test.go +++ b/testjson/format_test.go @@ -2,6 +2,7 @@ package testjson import ( "bytes" + "io" "testing" "gotest.tools/v3/assert" @@ -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), } } @@ -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 { @@ -101,39 +108,44 @@ 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: "input/go-test-json.out", + }, } for _, tc := range testCases {