Print race detector logs after running a test with -race

This commit is contained in:
Stefan Haller
2023-09-09 13:41:26 +02:00
parent f108fd2236
commit 59cc6843e6
3 changed files with 22 additions and 13 deletions

View File

@@ -88,12 +88,15 @@ outer:
return testsToRun return testsToRun
} }
func runCmdInTerminal(cmd *exec.Cmd) error { func runCmdInTerminal(cmd *exec.Cmd) (int, error) {
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
return cmd.Run() if err := cmd.Start(); err != nil {
return -1, err
}
return cmd.Process.Pid, cmd.Wait()
} }
func tryConvert(numStr string, defaultVal int) int { func tryConvert(numStr string, defaultVal int) int {

View File

@@ -63,7 +63,7 @@ func TestIntegration(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
} }
func runCmdHeadless(cmd *exec.Cmd) error { func runCmdHeadless(cmd *exec.Cmd) (int, error) {
cmd.Env = append( cmd.Env = append(
cmd.Env, cmd.Env,
"HEADLESS=true", "HEADLESS=true",
@@ -81,7 +81,7 @@ func runCmdHeadless(cmd *exec.Cmd) error {
// running other commands in a pty. // running other commands in a pty.
f, err := pty.StartWithSize(cmd, &pty.Winsize{Rows: 300, Cols: 300}) f, err := pty.StartWithSize(cmd, &pty.Winsize{Rows: 300, Cols: 300})
if err != nil { if err != nil {
return err return -1, err
} }
_, _ = io.Copy(io.Discard, f) _, _ = io.Copy(io.Discard, f)
@@ -89,8 +89,8 @@ func runCmdHeadless(cmd *exec.Cmd) error {
if cmd.Wait() != nil { if cmd.Wait() != nil {
_ = f.Close() _ = f.Close()
// return an error with the stderr output // return an error with the stderr output
return errors.New(stderr.String()) return cmd.Process.Pid, errors.New(stderr.String())
} }
return f.Close() return cmd.Process.Pid, f.Close()
} }

View File

@@ -26,7 +26,7 @@ const (
func RunTests( func RunTests(
tests []*IntegrationTest, tests []*IntegrationTest,
logf func(format string, formatArgs ...interface{}), logf func(format string, formatArgs ...interface{}),
runCmd func(cmd *exec.Cmd) error, runCmd func(cmd *exec.Cmd) (int, error),
testWrapper func(test *IntegrationTest, f func() error), testWrapper func(test *IntegrationTest, f func() error),
sandbox bool, sandbox bool,
waitForDebugger bool, waitForDebugger bool,
@@ -60,7 +60,7 @@ func RunTests(
) )
for i := 0; i < maxAttempts; i++ { for i := 0; i < maxAttempts; i++ {
err := runTest(test, paths, projectRootDir, logf, runCmd, sandbox, waitForDebugger, inputDelay, gitVersion) err := runTest(test, paths, projectRootDir, logf, runCmd, sandbox, waitForDebugger, raceDetector, inputDelay, gitVersion)
if err != nil { if err != nil {
if i == maxAttempts-1 { if i == maxAttempts-1 {
return err return err
@@ -83,9 +83,10 @@ func runTest(
paths Paths, paths Paths,
projectRootDir string, projectRootDir string,
logf func(format string, formatArgs ...interface{}), logf func(format string, formatArgs ...interface{}),
runCmd func(cmd *exec.Cmd) error, runCmd func(cmd *exec.Cmd) (int, error),
sandbox bool, sandbox bool,
waitForDebugger bool, waitForDebugger bool,
raceDetector bool,
inputDelay int, inputDelay int,
gitVersion *git_commands.GitVersion, gitVersion *git_commands.GitVersion,
) error { ) error {
@@ -108,12 +109,17 @@ func runTest(
return err return err
} }
err = runCmd(cmd) pid, err := runCmd(cmd)
if err != nil {
return err // Print race detector log regardless of the command's exit status
if raceDetector {
logPath := fmt.Sprintf("%s.%d", raceDetectorLogsPath(), pid)
if bytes, err := os.ReadFile(logPath); err == nil {
logf("Race detector log:\n" + string(bytes))
}
} }
return nil return err
} }
func prepareTestDir( func prepareTestDir(