Skip to content

Commit

Permalink
feat: added quiet command to standard test output (#785)
Browse files Browse the repository at this point in the history
* feat: added quiet command to standard test output

Added quiet output similar to verify to disable all success cases.
This is done because at work we have a lot of policies and we
only want to show the warning and failures to people and not
all the correct ones

Signed-off-by: Alexander Rasmussen <[email protected]>

* feat: Updated quiet to match verify and added Suppress-no-policies-found

Signed-off-by: Alexander Rasmussen <[email protected]>

* feat: Added acceptance tests and change bool

Signed-off-by: Alexander Rasmussen <[email protected]>

* fix: removed supress-no-policies-found

Signed-off-by: Alexander Rasmussen <[email protected]>

---------

Signed-off-by: Alexander Rasmussen <[email protected]>
  • Loading branch information
AlexanderRasmussen authored Apr 25, 2023
1 parent 5760ca6 commit 33ce843
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
13 changes: 13 additions & 0 deletions acceptance.bats
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,21 @@ EOF"
[[ "$output" =~ "2 tests, 2 passed" ]]
}

@test "Should not show any output with quiet flag and all tests succeeds" {
run ./conftest test -p examples/kubernetes/policy/deny.rego examples/kubernetes/deployment.yaml --quiet
[ "$status" -eq 0 ]
[[ "$output" = "" ]]
}

@test "Should show output because of failure" {
run ./conftest test -p examples/kubernetes/policy/ examples/kubernetes/deployment.yaml --all-namespaces --quiet
[ "$status" -eq 1 ]
[[ "$output" =~ "5 tests, 1 passed, 0 warnings, 4 failures, 0 exceptions" ]]
}

@test "Should fail evaluation if a builtin function returns error" {
run ./conftest test -p examples/builtin-errors/invalid-dns.rego examples/kubernetes/deployment.yaml
[ "$status" -eq 1 ]
[[ "$output" =~ "built-in error" ]]
}

36 changes: 20 additions & 16 deletions internal/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func NewTestCommand(ctx context.Context) *cobra.Command {
"strict",
"update",
"junit-hide-message",
"quiet",
}
for _, name := range flagNames {
if err := viper.BindPFlag(name, cmd.Flags().Lookup(name)); err != nil {
Expand All @@ -127,29 +128,31 @@ func NewTestCommand(ctx context.Context) *cobra.Command {
return fmt.Errorf("running test: %w", err)
}

outputter := output.Get(runner.Output, output.Options{
NoColor: runner.NoColor,
SuppressExceptions: runner.SuppressExceptions,
Tracing: runner.Trace,
JUnitHideMessage: viper.GetBool("junit-hide-message"),
})
if err := outputter.Output(results); err != nil {
return fmt.Errorf("output results: %w", err)
}

// When the no-fail parameter is set, there is no need to figure out the error code
// as we always want to return zero.
if runner.NoFail {
return nil
}

var exitCode int
if runner.FailOnWarn {
exitCode = output.ExitCodeFailOnWarn(results)
} else {
exitCode = output.ExitCode(results)
}

if !runner.Quiet || exitCode != 0 {
outputter := output.Get(runner.Output, output.Options{
NoColor: runner.NoColor,
SuppressExceptions: runner.SuppressExceptions,
Tracing: runner.Trace,
JUnitHideMessage: viper.GetBool("junit-hide-message"),
})
if err := outputter.Output(results); err != nil {
return fmt.Errorf("output results: %w", err)
}

// When the no-fail parameter is set, there is no need to figure out the error code
// as we always want to return zero.
if runner.NoFail {
return nil
}
}

os.Exit(exitCode)
return nil
},
Expand All @@ -160,6 +163,7 @@ func NewTestCommand(ctx context.Context) *cobra.Command {
cmd.Flags().Bool("no-color", false, "Disable color when printing")
cmd.Flags().Bool("suppress-exceptions", false, "Do not include exceptions in output")
cmd.Flags().Bool("all-namespaces", false, "Test policies found in all namespaces")
cmd.Flags().Bool("quiet", false, "Disable successful test output")

cmd.Flags().BoolP("trace", "", false, "Enable more verbose trace output for Rego queries")
cmd.Flags().BoolP("strict", "", false, "Enable strict mode for Rego policies")
Expand Down
1 change: 1 addition & 0 deletions runner/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type TestRunner struct {
NoFail bool `mapstructure:"no-fail"`
SuppressExceptions bool `mapstructure:"suppress-exceptions"`
Combine bool
Quiet bool
Output string
}

Expand Down

0 comments on commit 33ce843

Please sign in to comment.