Skip to content

Commit 7ef9f72

Browse files
committed
cmd/go: fix missing conversions in -json output
1. Apply JSON conversion when -bench is in use. 2. Apply JSON conversion to "no test files" result. 3. Apply JSON conversion to test case-ending SKIP status. Fixes #22769. Fixes #22790. Change-Id: I67ad656fc58bacae8c51d23b1e6d543cad190f08 Reviewed-on: https://go-review.googlesource.com/81535 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 232b2e3 commit 7ef9f72

File tree

5 files changed

+50
-15
lines changed

5 files changed

+50
-15
lines changed

src/cmd/go/go_test.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -5237,13 +5237,20 @@ func TestGoTestJSON(t *testing.T) {
52375237
// It would be nice to test that the output is interlaced
52385238
// but it seems to be impossible to do that in a short test
52395239
// that isn't also flaky. Just check that we get JSON output.
5240-
tg.run("test", "-json", "-short", "-v", "errors")
5241-
for _, line := range strings.Split(tg.getStdout(), "\n") {
5242-
if strings.Contains(line, `"Package":"errors"`) {
5243-
return
5244-
}
5245-
}
5246-
t.Fatalf("did not see JSON output")
5240+
tg.run("test", "-json", "-short", "-v", "errors", "empty/pkg", "skipper")
5241+
tg.grepStdout(`"Package":"errors"`, "did not see JSON output")
5242+
tg.grepStdout(`"Action":"run"`, "did not see JSON output")
5243+
5244+
tg.grepStdout(`"Action":"output","Package":"empty/pkg","Output":".*no test files`, "did not see no test files print")
5245+
tg.grepStdout(`"Action":"skip","Package":"empty/pkg"`, "did not see skip")
5246+
5247+
tg.grepStdout(`"Action":"output","Package":"skipper","Test":"Test","Output":"--- SKIP:`, "did not see SKIP output")
5248+
tg.grepStdout(`"Action":"skip","Package":"skipper","Test":"Test"`, "did not see skip result for Test")
5249+
5250+
tg.run("test", "-json", "-bench=NONE", "-short", "-v", "errors")
5251+
tg.grepStdout(`"Package":"errors"`, "did not see JSON output")
5252+
tg.grepStdout(`"Action":"run"`, "did not see JSON output")
5253+
52475254
}
52485255

52495256
func TestFailFast(t *testing.T) {

src/cmd/go/internal/test/test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ func (c *runCache) builderRunTest(b *work.Builder, a *work.Action) error {
12831283
// Stream test output (no buffering) when no package has
12841284
// been given on the command line (implicit current directory)
12851285
// or when benchmarking.
1286-
cmd.Stdout = os.Stdout
1286+
cmd.Stdout = stdout
12871287
} else {
12881288
// If we're only running a single package under test or if parallelism is
12891289
// set to 1, and if we're displaying all output (testShowPass), we can
@@ -1547,7 +1547,13 @@ func builderPrintTest(b *work.Builder, a *work.Action) error {
15471547

15481548
// builderNoTest is the action for testing a package with no test files.
15491549
func builderNoTest(b *work.Builder, a *work.Action) error {
1550-
fmt.Printf("? \t%s\t[no test files]\n", a.Package.ImportPath)
1550+
var stdout io.Writer = os.Stdout
1551+
if testJSON {
1552+
json := test2json.NewConverter(lockedStdout{}, a.Package.ImportPath, test2json.Timestamp)
1553+
defer json.Close()
1554+
stdout = json
1555+
}
1556+
fmt.Fprintf(stdout, "? \t%s\t[no test files]\n", a.Package.ImportPath)
15511557
return nil
15521558
}
15531559

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package skipper
2+
3+
import "testing"
4+
5+
func Test(t *testing.T) {
6+
t.Skip("skipping")
7+
}

src/cmd/internal/test2json/test2json.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type converter struct {
5454
start time.Time // time converter started
5555
testName string // name of current test, for output attribution
5656
report []*event // pending test result reports (nested for subtests)
57-
passed bool // whether we've seen the final whole-package PASS line
57+
result string // overall test result if seen
5858
input lineBuffer // input buffer
5959
output lineBuffer // output buffer
6060
}
@@ -139,9 +139,13 @@ var (
139139
reports = [][]byte{
140140
[]byte("--- PASS: "),
141141
[]byte("--- FAIL: "),
142+
[]byte("--- SKIP: "),
142143
}
143144

144145
fourSpace = []byte(" ")
146+
147+
skipLinePrefix = []byte("? \t")
148+
skipLineSuffix = []byte("\t[no test files]\n")
145149
)
146150

147151
// handleInputLine handles a single whole test output line.
@@ -152,10 +156,20 @@ func (c *converter) handleInputLine(line []byte) {
152156
if bytes.Equal(line, bigPass) || bytes.Equal(line, bigFail) {
153157
c.flushReport(0)
154158
c.output.write(line)
155-
c.passed = bytes.Equal(line, bigPass)
159+
if bytes.Equal(line, bigPass) {
160+
c.result = "pass"
161+
} else {
162+
c.result = "fail"
163+
}
156164
return
157165
}
158166

167+
// Special case for entirely skipped test binary: "? \tpkgname\t[no test files]\n" is only line.
168+
// Report it as plain output but remember to say skip in the final summary.
169+
if bytes.HasPrefix(line, skipLinePrefix) && bytes.HasSuffix(line, skipLineSuffix) && len(c.report) == 0 {
170+
c.result = "skip"
171+
}
172+
159173
// "=== RUN "
160174
// "=== PAUSE "
161175
// "=== CONT "
@@ -171,6 +185,7 @@ func (c *converter) handleInputLine(line []byte) {
171185
if !ok {
172186
// "--- PASS: "
173187
// "--- FAIL: "
188+
// "--- SKIP: "
174189
// but possibly indented.
175190
for bytes.HasPrefix(line, fourSpace) {
176191
line = line[4:]
@@ -257,8 +272,8 @@ func (c *converter) Close() error {
257272
c.input.flush()
258273
c.output.flush()
259274
e := &event{Action: "fail"}
260-
if c.passed {
261-
e.Action = "pass"
275+
if c.result != "" {
276+
e.Action = c.result
262277
}
263278
if c.mode&Timestamp != 0 {
264279
dt := time.Since(c.start).Round(1 * time.Millisecond).Seconds()

src/cmd/test2json/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//
2626
// type TestEvent struct {
2727
// Time time.Time // encodes as an RFC3339-format string
28-
// Event string
28+
// Action string
2929
// Package string
3030
// Test string
3131
// Elapsed float64 // seconds
@@ -35,7 +35,7 @@
3535
// The Time field holds the time the event happened.
3636
// It is conventionally omitted for cached test results.
3737
//
38-
// The Event field is one of a fixed set of event descriptions:
38+
// The Action field is one of a fixed set of action descriptions:
3939
//
4040
// run - the test has started running
4141
// pause - the test has been paused

0 commit comments

Comments
 (0)