main: make sure all testing output goes to the same place

Этот коммит содержится в:
Damian Gryski 2023-03-30 23:08:05 -07:00 коммит произвёл Ron Evans
родитель 84a3273131
коммит 4a81cac53b

17
main.go
Просмотреть файл

@ -249,10 +249,12 @@ func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options
flags = append(flags, "-test.count="+strconv.Itoa(*testConfig.Count)) flags = append(flags, "-test.count="+strconv.Itoa(*testConfig.Count))
} }
logToStdout := testConfig.Verbose || testConfig.BenchRegexp != ""
var buf bytes.Buffer var buf bytes.Buffer
var output io.Writer = &buf var output io.Writer = &buf
// Send the test output to stdout if -v or -bench // Send the test output to stdout if -v or -bench
if testConfig.Verbose || testConfig.BenchRegexp != "" { if logToStdout {
output = os.Stdout output = os.Stdout
} }
@ -318,7 +320,7 @@ func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options
// if verbose or benchmarks, then output is already going to stdout // if verbose or benchmarks, then output is already going to stdout
// However, if we failed and weren't printing to stdout, print the output we accumulated. // However, if we failed and weren't printing to stdout, print the output we accumulated.
if !passed && output != os.Stdout { if !passed && !logToStdout {
buf.WriteTo(stdout) buf.WriteTo(stdout)
} }
@ -331,14 +333,19 @@ func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options
return err return err
}) })
importPath := strings.TrimSuffix(result.ImportPath, ".test") importPath := strings.TrimSuffix(result.ImportPath, ".test")
var w io.Writer = stdout
if logToStdout {
w = os.Stdout
}
if err, ok := err.(loader.NoTestFilesError); ok { if err, ok := err.(loader.NoTestFilesError); ok {
fmt.Fprintf(stdout, "? \t%s\t[no test files]\n", err.ImportPath) fmt.Fprintf(w, "? \t%s\t[no test files]\n", err.ImportPath)
// Pretend the test passed - it at least didn't fail. // Pretend the test passed - it at least didn't fail.
return true, nil return true, nil
} else if passed { } else if passed {
fmt.Fprintf(stdout, "ok \t%s\t%.3fs\n", importPath, duration.Seconds()) fmt.Fprintf(w, "ok \t%s\t%.3fs\n", importPath, duration.Seconds())
} else { } else {
fmt.Fprintf(stdout, "FAIL\t%s\t%.3fs\n", importPath, duration.Seconds()) fmt.Fprintf(w, "FAIL\t%s\t%.3fs\n", importPath, duration.Seconds())
} }
return passed, err return passed, err
} }