Skip to content

Commit 0b9b2c6

Browse files
authored
feat: Add show-builtin-errors flag for the verify command (#901)
This is useful to raise config parsing errors when using the parse_config builtins. Previously, the unit test would fail but it was unclear to the user whether that was due to an error in the policy logic or a typo in the config. Signed-off-by: James Alseth <james@jalseth.me>
1 parent 433560f commit 0b9b2c6

File tree

6 files changed

+47
-10
lines changed

6 files changed

+47
-10
lines changed

docs/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ uses when testing configurations, only exposed as a Rego function. The example
127127
below shows how to use this to parse an AWS Terraform configuration and use it
128128
in a unit test.
129129

130+
> **TIP:** It is recommended to use the `--show-builtin-errors` flag when
131+
> using the `parse_config`, `parse_config_file`, and `parse_combined_config_files`
132+
> functions. This way errors encountered during parsing will be raised. This
133+
> flag will be enabled by default in a future release.
134+
130135
**deny.rego**
131136

132137
```rego

internal/commands/verify.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func NewVerifyCommand(ctx context.Context) *cobra.Command {
8080
"capabilities",
8181
"strict",
8282
"proto-file-dirs",
83+
"show-builtin-errors",
8384
}
8485
for _, name := range flagNames {
8586
if err := viper.BindPFlag(name, cmd.Flags().Lookup(name)); err != nil {
@@ -137,6 +138,7 @@ func NewVerifyCommand(ctx context.Context) *cobra.Command {
137138
cmd.Flags().Bool("trace", false, "Enable more verbose trace output for Rego queries")
138139
cmd.Flags().Bool("strict", false, "Enable strict mode for Rego policies")
139140
cmd.Flags().String("report", "", "Shows output for Rego queries as a report with summary. Available options are {full|notes|fails}.")
141+
cmd.Flags().Bool("show-builtin-errors", false, "Collect and return all encountered built-in errors")
140142

141143
cmd.Flags().StringP("output", "o", output.OutputStandard, fmt.Sprintf("Output format for conftest results - valid options are: %s", output.Outputs()))
142144
cmd.Flags().Bool("junit-hide-message", false, "Do not include the violation message in the JUnit test name")

runner/verify.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ import (
1515
// VerifyRunner is the runner for the Verify command, executing
1616
// Rego policy unit-tests.
1717
type VerifyRunner struct {
18-
Capabilities string
19-
Policy []string
20-
Data []string
21-
Output string
22-
NoColor bool `mapstructure:"no-color"`
23-
Trace bool
24-
Strict bool
25-
Report string
26-
Quiet bool
18+
Capabilities string
19+
Policy []string
20+
Data []string
21+
Output string
22+
NoColor bool `mapstructure:"no-color"`
23+
Trace bool
24+
Strict bool
25+
Report string
26+
Quiet bool
27+
ShowBuiltinErrors bool `mapstructure:"show-builtin-errors"`
2728
}
2829

2930
const (
@@ -51,7 +52,8 @@ func (r *VerifyRunner) Run(ctx context.Context) ([]output.CheckResult, []*tester
5152
SetStore(engine.Store()).
5253
SetModules(engine.Modules()).
5354
EnableTracing(enableTracing).
54-
SetRuntime(engine.Runtime())
55+
SetRuntime(engine.Runtime()).
56+
RaiseBuiltinErrors(r.ShowBuiltinErrors)
5557
ch, err := runner.RunTests(ctx, nil)
5658
if err != nil {
5759
return nil, nil, fmt.Errorf("running tests: %w", err)

tests/builtin-errors/policy/main.rego

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
deny[{"msg": msg}] {
4+
input.test_field == 123
5+
msg := "some error"
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
test_deny_valid {
4+
not deny with input as parse_config_file("file_does_not_exist.yaml")
5+
}

tests/builtin-errors/test.bats

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bats
2+
3+
@test "Parsing error without show-builtin-errors flag returns test failed" {
4+
run $CONFTEST verify --show-builtin-errors=false
5+
6+
[ "$status" -eq 1 ]
7+
echo $output
8+
[[ "$output" =~ "1 test, 0 passed, 0 warnings, 1 failure, 0 exceptions, 0 skipped" ]]
9+
}
10+
11+
@test "Parsing error with show-builtin-errors flag returns builtin error" {
12+
run $CONFTEST verify --show-builtin-errors=true
13+
14+
[ "$status" -eq 1 ]
15+
echo $output
16+
[[ "$output" =~ "file_does_not_exist.yaml: no such file or directory" ]]
17+
}

0 commit comments

Comments
 (0)