Skip to content

Commit

Permalink
Fix issue with repeated output
Browse files Browse the repository at this point in the history
If you used the same name for multiple tests, ie. called them all deny,
you got repeated output.

The test commands needs some refactoring, to split out the utilities and
test them independently. For the moment to get this fix out quickly I've
just added an acceptance test which works but is a little too coupled to
the current output.
  • Loading branch information
garethr committed Jul 24, 2019
1 parent 2a3f17b commit 6ebddd7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 6 additions & 0 deletions acceptance.bats
Expand Up @@ -69,3 +69,9 @@
[ "$status" -eq 0 ]
[[ "$output" != *"[33m"* ]]
}

@test "Output results only once" {
run ./conftest test -p examples/kubernetes/policy examples/kubernetes/deployment.yaml
count="${#lines[@]}"
[ "$count" -eq 4 ]
}
17 changes: 15 additions & 2 deletions pkg/commands/test/test.go
Expand Up @@ -151,7 +151,7 @@ func processFile(ctx context.Context, fileName string, compiler *ast.Compiler) (
return failuresList.ErrorOrNil(), warningsList.ErrorOrNil()
}

// finds all queries in the compiler supported by the
// finds all queries in the compiler
func getRules(ctx context.Context, re *regexp.Regexp, compiler *ast.Compiler) []string {

var res []string
Expand All @@ -160,14 +160,27 @@ func getRules(ctx context.Context, re *regexp.Regexp, compiler *ast.Compiler) []
for _, r := range m.Rules {
n := r.Head.Name.String()
if re.MatchString(n) {
res = append(res, n)
// the same rule names can be used multiple times, but
// we only want to run the query and report results once
if !stringInSlice(n, res) {
res = append(res, n)
}
}
}
}

return res
}

func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}

func makeQuery(rule string) string {
return fmt.Sprintf("data.%s.%s", viper.GetString("namespace"), rule)
}
Expand Down

0 comments on commit 6ebddd7

Please sign in to comment.