Skip to content

Commit 5b123ae

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
cmd/go: avoid writing non-JSON "build failed" errors from 'go test -json'
In 'go test -json' we expect stdout to contain only JSON events, not unstructured text. Unstructured text should either go to stderr or be wrapped in a JSON event. (If we add structured build output in #62067, we can emit this output as a build event instead of a test event.) Fixes #35169. For #54378. Change-Id: Ibedd28e79b5adf8d6ae56165b9f0393b14ece9aa Reviewed-on: https://go-review.googlesource.com/c/go/+/529120 Reviewed-by: Austin Clements <[email protected]> Auto-Submit: Bryan Mills <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent f4e7675 commit 5b123ae

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,18 +1233,6 @@ func (r *runTestActor) Act(b *work.Builder, ctx context.Context, a *work.Action)
12331233
return nil
12341234
}
12351235

1236-
if a.Failed {
1237-
// We were unable to build the binary.
1238-
a.Failed = false
1239-
a.TestOutput = new(bytes.Buffer)
1240-
fmt.Fprintf(a.TestOutput, "FAIL\t%s [build failed]\n", a.Package.ImportPath)
1241-
base.SetExitStatus(1)
1242-
1243-
// release next test to start
1244-
close(r.next)
1245-
return nil
1246-
}
1247-
12481236
var stdout io.Writer = os.Stdout
12491237
var err error
12501238
if testJSON {
@@ -1259,6 +1247,16 @@ func (r *runTestActor) Act(b *work.Builder, ctx context.Context, a *work.Action)
12591247
// Release next test to start (test2json.NewConverter writes the start event).
12601248
close(r.next)
12611249

1250+
if a.Failed {
1251+
// We were unable to build the binary.
1252+
a.Failed = false
1253+
fmt.Fprintf(stdout, "FAIL\t%s [build failed]\n", a.Package.ImportPath)
1254+
// Tell the JSON converter that this was a failure, not a passing run.
1255+
err = errors.New("build failed")
1256+
base.SetExitStatus(1)
1257+
return nil
1258+
}
1259+
12621260
if p := a.Package; len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
12631261
fmt.Fprintf(stdout, "? \t%s\t[no test files]\n", p.ImportPath)
12641262
return nil
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
! go test -json .
2+
3+
# We should see only JSON output on stdout, no non-JSON.
4+
# To simplify the check, we just look for non-curly-braces, since
5+
# every JSON entry has them and they're unlikely to occur
6+
# in other error messages.
7+
! stdout '^[^{]'
8+
! stdout '[^}]\n$'
9+
10+
# Since the only test we requested failed to build, we should
11+
# not see any "pass" actions in the JSON stream.
12+
! stdout '\{.*"Action":"pass".*\}'
13+
14+
# TODO(#62067): emit this as a build event instead of a test event.
15+
stdout '\{.*"Action":"output","Package":"example","Output":"FAIL\\texample \[build failed\]\\n"\}'
16+
stdout '\{.*"Action":"fail","Package":"example",.*\}'
17+
18+
-- go.mod --
19+
module example
20+
go 1.19
21+
-- example.go --
22+
package example
23+
24+
This is not valid Go source.
25+
-- example_test.go --
26+
package example
27+
28+
func Test(*testing.T) {}

0 commit comments

Comments
 (0)