Skip to content

Commit 2ffffd5

Browse files
committed
stream output to stdout too
1 parent f4e3a0c commit 2ffffd5

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

tools/flakeguard/runner/executor/executor.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"io"
78
"os"
89
"os/exec"
910
"path/filepath"
@@ -102,20 +103,20 @@ func (e *commandExecutor) RunTestPackage(cfg Config, packageName string, runInde
102103
var stderrBuf bytes.Buffer
103104
cmd.Stderr = &stderrBuf
104105

105-
stdoutBytes, err := cmd.Output()
106-
107-
writeErr := os.WriteFile(tempFilePath, stdoutBytes, 0644)
108-
if writeErr != nil {
109-
log.Error().Err(writeErr).Str("file", tempFilePath).Msg("Failed to write captured stdout to temp file")
110-
_ = os.Remove(tempFilePath)
111-
return "", false, fmt.Errorf("failed to write command output to %s: %w", tempFilePath, writeErr)
106+
// Open the temp file for writing
107+
tmpFileWriter, err := os.OpenFile(tempFilePath, os.O_WRONLY|os.O_TRUNC, 0644)
108+
if err != nil {
109+
return "", false, fmt.Errorf("failed to open temp file for writing: %w", err)
112110
}
111+
defer tmpFileWriter.Close()
113112

114-
if err != nil {
113+
// Write to both STDOUT and the file
114+
cmd.Stdout = io.MultiWriter(os.Stdout, tmpFileWriter)
115+
116+
if err := cmd.Run(); err != nil {
115117
if stderrStr := stderrBuf.String(); stderrStr != "" {
116118
log.Error().Str("package", packageName).Str("stderr", stderrStr).Msg("Command failed with error and stderr output")
117119
}
118-
119120
var exitErr *exec.ExitError
120121
if errors.As(err, &exitErr) {
121122
return tempFilePath, false, nil
@@ -161,16 +162,17 @@ func (e *commandExecutor) RunCmd(cfg Config, testCmd []string, runIndex int) (te
161162
var stderrBuf bytes.Buffer
162163
cmd.Stderr = &stderrBuf
163164

164-
stdoutBytes, err := cmd.Output()
165-
166-
writeErr := os.WriteFile(tempFilePath, stdoutBytes, 0644)
167-
if writeErr != nil {
168-
log.Error().Err(writeErr).Str("file", tempFilePath).Msg("Failed to write captured stdout to temp file (cmd run)")
169-
_ = os.Remove(tempFilePath)
170-
return "", false, fmt.Errorf("failed to write command output to %s: %w", tempFilePath, writeErr)
165+
// Open the temp file for writing
166+
tmpFileWriter, err := os.OpenFile(tempFilePath, os.O_WRONLY|os.O_TRUNC, 0644)
167+
if err != nil {
168+
return "", false, fmt.Errorf("failed to open temp file for writing: %w", err)
171169
}
170+
defer tmpFileWriter.Close()
172171

173-
if err != nil {
172+
// Write to both STDOUT and the file
173+
cmd.Stdout = io.MultiWriter(os.Stdout, tmpFileWriter)
174+
175+
if err := cmd.Run(); err != nil {
174176
if stderrStr := stderrBuf.String(); stderrStr != "" {
175177
log.Error().Str("command", strings.Join(testCmd, " ")).Str("stderr", stderrStr).Msg("Custom command failed with error and stderr output")
176178
}

0 commit comments

Comments
 (0)