Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add RunCommandAndGetStdOutErr #1482

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions modules/shell/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ func RunCommandAndGetStdOutE(t testing.TestingT, command Command) (string, error
return output.Stdout(), nil
}

// RunCommandAndGetStdOutErr runs a shell command and returns solely its stdout and stderr as a string. The stdout and
// stderr of that command will also be logged with Command.Log to make debugging easier. If there are any errors, fail
// the test.
func RunCommandAndGetStdOutErr(t testing.TestingT, command Command) (stdout string, stderr string) {
stdout, stderr, err := RunCommandAndGetStdOutErrE(t, command)
require.NoError(t, err)
return stdout, stderr
}

// RunCommandAndGetStdOutErrE runs a shell command and returns solely its stdout and stderr as a string. The stdout
// and stderr of that command will also be printed to the stdout and stderr of this Go program to make debugging easier.
// Any returned error will be of type ErrWithCmdOutput, containing the output streams and the underlying error.
func RunCommandAndGetStdOutErrE(t testing.TestingT, command Command) (stdout string, stderr string, err error) {
output, err := runCommand(t, command)
if err != nil {
return output.Stdout(), output.Stderr(), &ErrWithCmdOutput{err, output}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'output' might have 'nil' value?

In some cases can be returned error and nil...

https://github.com/gruntwork-io/terratest/blob/main/modules/shell/command.go#L109

it may require to change in multiple places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Will address this in a follow-up PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

return output.Stdout(), output.Stderr(), nil
}

type ErrWithCmdOutput struct {
Underlying error
Output *output
Expand Down
27 changes: 27 additions & 0 deletions modules/shell/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/random"
Expand Down Expand Up @@ -183,3 +184,29 @@ func TestCommandOutputType(t *testing.T) {
assert.Len(t, o.Output.Combined(), len(stdout)+len(stderr)+1) // +1 for newline
}
}

func TestCommandWithStdoutAndStdErr(t *testing.T) {
t.Parallel()

stdout := "hello world"
stderr := "this command has failed"
command := Command{
Command: "sh",
Args: []string{"-c", `echo "` + stdout + `" && echo "` + stderr + `" >&2`},
Logger: logger.Discard,
}

t.Run("MustNotError", func(t *testing.T) {
ostdout, ostderr := RunCommandAndGetStdOutErr(t, command)
assert.Equal(t, stdout, ostdout)
assert.Equal(t, stderr, ostderr)
})

t.Run("ReturnError", func(t *testing.T) {
ostdout, ostderr, err := RunCommandAndGetStdOutErrE(t, command)
require.NoError(t, err)
assert.Equal(t, stdout, ostdout)
assert.Equal(t, stderr, ostderr)
})

}