Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add standard-json format #313

Merged
merged 2 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to use the input file as the expected value, to show that we output the exact value as the input.

},
}

for _, tc := range testCases {
Expand Down