Skip to content

Commit

Permalink
Add standard-json format
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawka committed Mar 29, 2023
1 parent f39e7fa commit 612250f
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ 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: 1 addition & 0 deletions cmd/testdata/gotestsum-help-text
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ 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
12 changes: 12 additions & 0 deletions testjson/format.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testjson

import (
"encoding/json"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -41,6 +42,15 @@ func standardQuietFormat(event TestEvent, _ *Execution) string {
return event.Output
}

// go test -json
func standardJSONFormat(event TestEvent, _ *Execution) string {
b, err := json.Marshal(event)
if err != nil {
return ""
}
return string(b) + "\n"
}

func testNameFormat(event TestEvent, exec *Execution) string {
result := colorEvent(event)(strings.ToUpper(string(event.Action)))
formatTest := func() string {
Expand Down Expand Up @@ -240,6 +250,8 @@ func NewEventFormatter(out io.Writer, format string, formatOpts FormatOptions) E
switch format {
case "debug":
return &formatAdapter{out, debugFormat}
case "standard-json":
return &formatAdapter{out, standardJSONFormat}
case "standard-verbose":
return &formatAdapter{out, standardVerboseFormat}
case "standard-quiet":
Expand Down
5 changes: 5 additions & 0 deletions testjson/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ func TestFormats_DefaultGoTestJson(t *testing.T) {
format: standardQuietFormat,
expectedOut: "format/standard-quiet.out",
},
{
name: "standard-json",
format: standardJSONFormat,
expectedOut: "format/standard-json.out",
},
}

for _, tc := range testCases {
Expand Down
Loading

0 comments on commit 612250f

Please sign in to comment.