diff options
| author | Austin Clements <austin@google.com> | 2023-10-17 17:27:00 -0400 |
|---|---|---|
| committer | Austin Clements <austin@google.com> | 2024-11-17 14:32:05 +0000 |
| commit | f9a95b1bdcff42730c836a792b27740b9c0df645 (patch) | |
| tree | d37d7c9d345363861518e7e2935ddaca85839bf2 /src/cmd/internal/test2json | |
| parent | 9060fa5afd52fcdb60c09e26c73f5980520ca9f9 (diff) | |
| download | go-f9a95b1bdcff42730c836a792b27740b9c0df645.tar.xz | |
cmd/go: print build errors during go test -json in JSON
Currently, if a test or imported package fails to build during "go
test -json", the build error text will be interleaved with the JSON
output of tests. Furthermore, there’s currently no way to reliably
associate a build error with the test package or packages it affected.
This creates unnecessary friction and complexity in tools that consume
the "go test -json" output.
This CL makes "go test -json" enable JSON reporting of build errors.
It also adds a "FailedBuild" field to the "fail" TestEvent, which
gives the package ID of the package that failed to build and caused
the test to fail.
Using this, CI systems should be able to consume the entire output
stream from "go test -json" in a structured way and easily associate
build failures with test failures during reporting.
Fixes #62067.
Updates #35169.
Updates #37486.
Change-Id: I49091dcc7aa52db01fc9fa6042771633e97b8407
Reviewed-on: https://go-review.googlesource.com/c/go/+/536399
Reviewed-by: Russ Cox <rsc@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/cmd/internal/test2json')
| -rw-r--r-- | src/cmd/internal/test2json/test2json.go | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/src/cmd/internal/test2json/test2json.go b/src/cmd/internal/test2json/test2json.go index f7dfbe69d7..ed78764d26 100644 --- a/src/cmd/internal/test2json/test2json.go +++ b/src/cmd/internal/test2json/test2json.go @@ -29,12 +29,13 @@ const ( // event is the JSON struct we emit. type event struct { - Time *time.Time `json:",omitempty"` - Action string - Package string `json:",omitempty"` - Test string `json:",omitempty"` - Elapsed *float64 `json:",omitempty"` - Output *textBytes `json:",omitempty"` + Time *time.Time `json:",omitempty"` + Action string + Package string `json:",omitempty"` + Test string `json:",omitempty"` + Elapsed *float64 `json:",omitempty"` + Output *textBytes `json:",omitempty"` + FailedBuild string `json:",omitempty"` } // textBytes is a hack to get JSON to emit a []byte as a string @@ -59,6 +60,10 @@ type Converter struct { input lineBuffer // input buffer output lineBuffer // output buffer needMarker bool // require ^V marker to introduce test framing line + + // failedBuild is set to the package ID of the cause of a build failure, + // if that's what caused this test to fail. + failedBuild string } // inBuffer and outBuffer are the input and output buffer sizes. @@ -140,6 +145,13 @@ func (c *Converter) Exited(err error) { } } +// SetFailedBuild sets the package ID that is the root cause of a build failure +// for this test. This will be reported in the final "fail" event's FailedBuild +// field. +func (c *Converter) SetFailedBuild(pkgID string) { + c.failedBuild = pkgID +} + const marker = byte(0x16) // ^V var ( @@ -369,6 +381,9 @@ func (c *Converter) Close() error { dt := time.Since(c.start).Round(1 * time.Millisecond).Seconds() e.Elapsed = &dt } + if c.result == "fail" { + e.FailedBuild = c.failedBuild + } c.writeEvent(e) } return nil |
