From 83fb25c5dfb630dc8012290bb39072fe64210c53 Mon Sep 17 00:00:00 2001 From: Ricky Stewart Date: Wed, 31 Aug 2022 15:01:09 -0500 Subject: [PATCH] also write test stderr to json (xml) output file Without this, people are forced to look in the test.log file for output to stderr, rather than test.xml. --- go/tools/bzltestutil/wrap.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/go/tools/bzltestutil/wrap.go b/go/tools/bzltestutil/wrap.go index 382d636a66..7c90b51a24 100644 --- a/go/tools/bzltestutil/wrap.go +++ b/go/tools/bzltestutil/wrap.go @@ -25,6 +25,7 @@ import ( "path/filepath" "strconv" "strings" + "sync" ) // TestWrapperAbnormalExit is used by Wrap to indicate the child @@ -61,6 +62,7 @@ func shouldAddTestV() bool { func Wrap(pkg string) error { var jsonBuffer bytes.Buffer jsonConverter := NewConverter(&jsonBuffer, pkg, Timestamp) + pipeRead, pipeWrite := io.Pipe() args := os.Args[1:] if shouldAddTestV() { @@ -72,9 +74,20 @@ func Wrap(pkg string) error { } cmd := exec.Command(exePath, args...) cmd.Env = append(os.Environ(), "GO_TEST_WRAP=0") - cmd.Stderr = os.Stderr - cmd.Stdout = io.MultiWriter(os.Stdout, jsonConverter) + cmd.Stderr = io.MultiWriter(os.Stderr, pipeWrite) + cmd.Stdout = io.MultiWriter(os.Stdout, pipeWrite) + var wg sync.WaitGroup + wg.Add(1) + go func() { + _, err := io.Copy(jsonConverter, pipeRead) + if err != nil { + panic(err) + } + wg.Done() + }() err := cmd.Run() + pipeWrite.Close() + wg.Wait() jsonConverter.Close() if out, ok := os.LookupEnv("XML_OUTPUT_FILE"); ok { werr := writeReport(jsonBuffer, pkg, out)