Skip to content

Commit f942ccf

Browse files
committed
correct exit code assertion in exec plugin
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]>
1 parent 64ace5b commit f942ccf

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

plugin/exec/assertions.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,13 @@ func newAssertions(
166166
outPipe *bytes.Buffer,
167167
errPipe *bytes.Buffer,
168168
) api.Assertions {
169+
expExitCode := 0
170+
if e != nil {
171+
expExitCode = e.ExitCode
172+
}
169173
a := &assertions{
170174
failures: []error{},
171-
expExitCode: exitCode,
175+
expExitCode: expExitCode,
172176
exitCode: exitCode,
173177
}
174178
if e != nil {

plugin/exec/eval_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"bytes"
1010
"context"
1111
"flag"
12+
"fmt"
1213
"os"
1314
"os/exec"
1415
"path/filepath"
@@ -71,6 +72,52 @@ func TestExitCode(t *testing.T) {
7172
require.Nil(err)
7273
}
7374

75+
func TestFailExecExitCodeNotSpecified(t *testing.T) {
76+
if !*failFlag {
77+
t.Skip("skipping without -fail flag")
78+
}
79+
require := require.New(t)
80+
81+
fp := filepath.Join("testdata", "ls-fail-no-exit-code.yaml")
82+
f, err := os.Open(fp)
83+
require.Nil(err)
84+
85+
s, err := scenario.FromReader(
86+
f,
87+
scenario.WithPath(fp),
88+
)
89+
require.Nil(err)
90+
require.NotNil(s)
91+
92+
ctx := gdtcontext.New(gdtcontext.WithDebug())
93+
err = s.Run(ctx, t)
94+
require.Nil(err)
95+
}
96+
97+
func TestExecFailExitCodeNotSpecified(t *testing.T) {
98+
require := require.New(t)
99+
target := os.Args[0]
100+
failArgs := []string{
101+
"-test.v",
102+
"-test.run=FailExecExitCodeNotSpecified",
103+
"-fail",
104+
}
105+
outerr, err := exec.Command(target, failArgs...).CombinedOutput()
106+
107+
// The test should have failed...
108+
require.NotNil(err)
109+
debugout := string(outerr)
110+
ec := 2
111+
// Yay, different exit codes for the same not found error...
112+
if runtime.GOOS == "darwin" {
113+
ec = 1
114+
}
115+
msg := fmt.Sprintf(
116+
"assertion failed: not equal: expected 0 but got %d", ec,
117+
)
118+
require.Contains(debugout, msg)
119+
}
120+
74121
func TestShellList(t *testing.T) {
75122
require := require.New(t)
76123

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: ls-fail-no-exit-code
2+
description: a scenario that runs the `ls` command with a non-0 exit code and no assertion on exit code
3+
tests:
4+
- exec: ls /this/dir/does/not/exist

0 commit comments

Comments
 (0)