diff --git a/plugin/exec/assertions.go b/plugin/exec/assertions.go index defebb0..84fb1f9 100644 --- a/plugin/exec/assertions.go +++ b/plugin/exec/assertions.go @@ -166,9 +166,13 @@ func newAssertions( outPipe *bytes.Buffer, errPipe *bytes.Buffer, ) api.Assertions { + expExitCode := 0 + if e != nil { + expExitCode = e.ExitCode + } a := &assertions{ failures: []error{}, - expExitCode: exitCode, + expExitCode: expExitCode, exitCode: exitCode, } if e != nil { diff --git a/plugin/exec/eval_test.go b/plugin/exec/eval_test.go index 60f40e8..67f339d 100644 --- a/plugin/exec/eval_test.go +++ b/plugin/exec/eval_test.go @@ -9,6 +9,7 @@ import ( "bytes" "context" "flag" + "fmt" "os" "os/exec" "path/filepath" @@ -71,6 +72,52 @@ func TestExitCode(t *testing.T) { require.Nil(err) } +func TestFailExecExitCodeNotSpecified(t *testing.T) { + if !*failFlag { + t.Skip("skipping without -fail flag") + } + require := require.New(t) + + fp := filepath.Join("testdata", "ls-fail-no-exit-code.yaml") + f, err := os.Open(fp) + require.Nil(err) + + s, err := scenario.FromReader( + f, + scenario.WithPath(fp), + ) + require.Nil(err) + require.NotNil(s) + + ctx := gdtcontext.New(gdtcontext.WithDebug()) + err = s.Run(ctx, t) + require.Nil(err) +} + +func TestExecFailExitCodeNotSpecified(t *testing.T) { + require := require.New(t) + target := os.Args[0] + failArgs := []string{ + "-test.v", + "-test.run=FailExecExitCodeNotSpecified", + "-fail", + } + outerr, err := exec.Command(target, failArgs...).CombinedOutput() + + // The test should have failed... + require.NotNil(err) + debugout := string(outerr) + ec := 2 + // Yay, different exit codes for the same not found error... + if runtime.GOOS == "darwin" { + ec = 1 + } + msg := fmt.Sprintf( + "assertion failed: not equal: expected 0 but got %d", ec, + ) + require.Contains(debugout, msg) +} + func TestShellList(t *testing.T) { require := require.New(t) diff --git a/plugin/exec/testdata/ls-fail-no-exit-code.yaml b/plugin/exec/testdata/ls-fail-no-exit-code.yaml new file mode 100644 index 0000000..35f16af --- /dev/null +++ b/plugin/exec/testdata/ls-fail-no-exit-code.yaml @@ -0,0 +1,4 @@ +name: ls-fail-no-exit-code +description: a scenario that runs the `ls` command with a non-0 exit code and no assertion on exit code +tests: + - exec: ls /this/dir/does/not/exist