fix: capture stderr in executil RunWithExecRunOpts (#25139)

Signed-off-by: Eugene Doudine <eugene.doudine@octopus.com>
This commit is contained in:
dudinea
2025-11-02 17:01:38 +02:00
committed by GitHub
parent 4ea276860c
commit fe4ab01cba
2 changed files with 9 additions and 1 deletions

View File

@@ -63,7 +63,7 @@ func RunWithRedactor(cmd *exec.Cmd, redactor func(text string) string) (string,
}
func RunWithExecRunOpts(cmd *exec.Cmd, opts ExecRunOpts) (string, error) {
cmdOpts := CmdOpts{Timeout: timeout, FatalTimeout: fatalTimeout, Redactor: opts.Redactor, TimeoutBehavior: opts.TimeoutBehavior, SkipErrorLogging: opts.SkipErrorLogging}
cmdOpts := CmdOpts{Timeout: timeout, FatalTimeout: fatalTimeout, Redactor: opts.Redactor, TimeoutBehavior: opts.TimeoutBehavior, SkipErrorLogging: opts.SkipErrorLogging, CaptureStderr: opts.CaptureStderr}
span := tracing.NewLoggingTracer(log.NewLogrusLogger(log.NewWithCurrentConfig())).StartSpan(fmt.Sprintf("exec %v", cmd.Args[0]))
span.SetBaggageItem("dir", cmd.Dir)
if cmdOpts.Redactor != nil {

View File

@@ -218,3 +218,11 @@ func TestRunCaptureStderr(t *testing.T) {
assert.Equal(t, "hello world\nmy-error", output)
assert.NoError(t, err)
}
func TestRunWithExecRunOptsCaptureStderr(t *testing.T) {
ctx := t.Context()
cmd := exec.CommandContext(ctx, "sh", "-c", "echo hello world && echo my-error >&2 && exit 0")
output, err := RunWithExecRunOpts(cmd, ExecRunOpts{CaptureStderr: true})
assert.Equal(t, "hello world\nmy-error", output)
assert.NoError(t, err)
}