Skip to content

Commit

Permalink
Merge pull request #313 from Pawka/issue_312
Browse files Browse the repository at this point in the history
Add standard-json format
  • Loading branch information
dnephin authored Mar 29, 2023
2 parents f39e7fa + 7d73b4e commit 790e071
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
20 changes: 20 additions & 0 deletions testjson/format.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testjson

import (
"bufio"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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":
Expand Down
34 changes: 23 additions & 11 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,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 {
Expand Down

0 comments on commit 790e071

Please sign in to comment.