Skip to content

Commit

Permalink
correct exit code assertion in exec plugin
Browse files Browse the repository at this point in the history
The constructor for assertions in the exec plugin was incorrectly
setting the expected exit code equal to the exit code it received from
the test spec, resulting in script or command executions returning
non-0 exit codes from causing a test.FailNow().

Signed-off-by: Jay Pipes <[email protected]>
  • Loading branch information
jaypipes committed Jul 8, 2024
1 parent 64ace5b commit f942ccf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
6 changes: 5 additions & 1 deletion plugin/exec/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
47 changes: 47 additions & 0 deletions plugin/exec/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"context"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions plugin/exec/testdata/ls-fail-no-exit-code.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f942ccf

Please sign in to comment.