|
4 | 4 | "bytes" |
5 | 5 | "errors" |
6 | 6 | "fmt" |
| 7 | + "io" |
7 | 8 | "os" |
8 | 9 | "os/exec" |
9 | 10 | "path/filepath" |
@@ -102,20 +103,20 @@ func (e *commandExecutor) RunTestPackage(cfg Config, packageName string, runInde |
102 | 103 | var stderrBuf bytes.Buffer |
103 | 104 | cmd.Stderr = &stderrBuf |
104 | 105 |
|
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) |
112 | 110 | } |
| 111 | + defer tmpFileWriter.Close() |
113 | 112 |
|
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 { |
115 | 117 | if stderrStr := stderrBuf.String(); stderrStr != "" { |
116 | 118 | log.Error().Str("package", packageName).Str("stderr", stderrStr).Msg("Command failed with error and stderr output") |
117 | 119 | } |
118 | | - |
119 | 120 | var exitErr *exec.ExitError |
120 | 121 | if errors.As(err, &exitErr) { |
121 | 122 | return tempFilePath, false, nil |
@@ -161,16 +162,17 @@ func (e *commandExecutor) RunCmd(cfg Config, testCmd []string, runIndex int) (te |
161 | 162 | var stderrBuf bytes.Buffer |
162 | 163 | cmd.Stderr = &stderrBuf |
163 | 164 |
|
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) |
171 | 169 | } |
| 170 | + defer tmpFileWriter.Close() |
172 | 171 |
|
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 { |
174 | 176 | if stderrStr := stderrBuf.String(); stderrStr != "" { |
175 | 177 | log.Error().Str("command", strings.Join(testCmd, " ")).Str("stderr", stderrStr).Msg("Custom command failed with error and stderr output") |
176 | 178 | } |
|
0 commit comments