forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for non-zero exit code (open-policy-agent#1006)
* Add support for non-zero exit code Adds support for non-zero exit code when providing the --fail flag to the eval command. 0 means no error, 1 means undefined result and 2 means an error. Fixes open-policy-agent#981 Signed-off-by: Kim Christensen <[email protected]>
- Loading branch information
1 parent
b952276
commit 58598d7
Showing
2 changed files
with
69 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2018 The OPA Authors. All rights reserved. | ||
// Use of this source code is governed by an Apache2 | ||
// license that can be found in the LICENSE file. | ||
|
||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/open-policy-agent/opa/util" | ||
) | ||
|
||
func TestEvalExitCode(t *testing.T) { | ||
params := evalCommandParams{ | ||
fail: true, | ||
explain: util.NewEnumFlag(explainModeOff, []string{explainModeFull}), | ||
outputFormat: util.NewEnumFlag(evalJSONOutput, []string{evalJSONOutput}), | ||
} | ||
tests := []struct { | ||
note string | ||
query string | ||
expectedCode int | ||
}{ | ||
{"defined result", "true=true", 0}, | ||
{"undefined result", "true = false", 1}, | ||
{"on error", "x = 1/0", 2}, | ||
} | ||
|
||
var b bytes.Buffer | ||
writer := bufio.NewWriter(&b) | ||
for _, tc := range tests { | ||
code, err := eval([]string{tc.query}, params, writer) | ||
if err != nil { | ||
t.Fatalf("%v: Unexpected error %v", tc.note, err) | ||
} | ||
if code != tc.expectedCode { | ||
t.Fatalf("%v: Expected code %v, got %v", tc.note, tc.expectedCode, code) | ||
} | ||
} | ||
} |