Skip to content

Commit

Permalink
Merge pull request #50 from jaypipes/exec-err
Browse files Browse the repository at this point in the history
correct exit code assertion in exec plugin
  • Loading branch information
jaypipes authored Jul 8, 2024
2 parents 64ace5b + df4c3d6 commit 754a377
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/fmtcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: harden runner
uses: step-security/harden-runner@55d479fb1c5bcad5a4f9099a5d9f37c8857b2845 # v2.4.1
uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1
with:
egress-policy: block
disable-sudo: true
Expand All @@ -26,8 +26,9 @@ jobs:
raw.githubusercontent.com:443
objects.githubusercontent.com:443
proxy.golang.org:443
blob.core.windows.net:443
- name: checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: setup go
uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
with:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
raw.githubusercontent.com:443
objects.githubusercontent.com:443
proxy.golang.org:443
blob.core.windows.net:443
- name: checkout code
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: setup go
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: harden runner
uses: step-security/harden-runner@55d479fb1c5bcad5a4f9099a5d9f37c8857b2845 # v2.4.1
uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1
with:
egress-policy: block
disable-sudo: true
Expand All @@ -29,8 +29,9 @@ jobs:
raw.githubusercontent.com:443
objects.githubusercontent.com:443
proxy.golang.org:443
blob.core.windows.net:443
- name: checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: setup go
uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
with:
Expand Down
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 754a377

Please sign in to comment.