Skip to content

Commit

Permalink
opa test: change exit status when tests are skipped (open-policy-agen…
Browse files Browse the repository at this point in the history
…t#3774)

When `-z` is passed to `opa test`, skipped tests will not affect
the exit code. (When not passed, skipped tests produce the same
exit code as failed tests.)

Fixes open-policy-agent#3773.

Signed-off-by: Kirk Patton <kpatton@verizonmedia.com>
Signed-off-by: Dolev Farhi <farhi.dolev@gmail.com>
  • Loading branch information
kirk-patton authored and dolevf committed Nov 4, 2021
1 parent 9a6563f commit fe221d4
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type testCommandParams struct {
runRegex string
count int
target *util.EnumFlag
skipExitZero bool
}

func newTestCommandParams() *testCommandParams {
Expand Down Expand Up @@ -293,9 +294,13 @@ func runTests(ctx context.Context, txn storage.Transaction, runner *tester.Runne
go func() {
defer close(dup)
for tr := range ch {
if !tr.Pass() {
if !tr.Pass() && !testParams.skipExitZero {
exitCode = 2
}
if tr.Skip && exitCode == 0 && testParams.skipExitZero {
// there is a skipped test, adding the flag -z exits 0 if there are no failures
exitCode = 0
}
tr.Trace = filterTrace(testParams, tr.Trace)
dup <- tr
}
Expand Down Expand Up @@ -343,6 +348,7 @@ func filterTrace(params *testCommandParams, trace []*topdown.Event) []*topdown.E
}

func init() {
testCommand.Flags().BoolVarP(&testParams.skipExitZero, "exit-zero-on-skipped", "z", false, "skipped tests return status 0")
testCommand.Flags().BoolVarP(&testParams.verbose, "verbose", "v", false, "set verbose reporting mode")
testCommand.Flags().BoolVarP(&testParams.failureLine, "show-failure-line", "l", false, "show test failure line")
_ = testCommand.Flags().MarkDeprecated("show-failure-line", "use -v instead")
Expand Down

0 comments on commit fe221d4

Please sign in to comment.