Skip to content

Commit

Permalink
🐛 fix rule parsing when sub-trees contain tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Gaikwad <pgaikwad@redhat.com>
  • Loading branch information
pranavgaikwad committed May 22, 2024
1 parent 8af6f5c commit 5798dba
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 24 deletions.
11 changes: 6 additions & 5 deletions parser/rule_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func (r *RuleParser) LoadRules(filepath string) ([]engine.RuleSet, map[string]pr
}
var ruleSet *engine.RuleSet
rules := []engine.Rule{}
foundTree := false
parserErr := &parserErrors{}
for _, f := range files {
info, err := os.Stat(path.Join(filepath, f.Name()))
Expand All @@ -122,7 +121,6 @@ func (r *RuleParser) LoadRules(filepath string) ([]engine.RuleSet, map[string]pr
continue
}
if info.IsDir() {
foundTree = true
r, m, err := r.LoadRules(path.Join(filepath, f.Name()))
if err != nil {
parserErr.errs = append(parserErr.errs, err)
Expand All @@ -141,6 +139,12 @@ func (r *RuleParser) LoadRules(filepath string) ([]engine.RuleSet, map[string]pr
ruleSet = r.loadRuleSet(filepath)
continue
}
// skip rule tests
if strings.HasSuffix(f.Name(), ".test.yaml") ||
strings.HasSuffix(f.Name(), ".test.yml") {
r.Log.V(7).Info("excluding test file from parsing", "file", f.Name())
continue
}
r, m, err := r.LoadRule(path.Join(filepath, f.Name()))
if err != nil {
parserErr.errs = append(parserErr.errs, err)
Expand All @@ -154,9 +158,6 @@ func (r *RuleParser) LoadRules(filepath string) ([]engine.RuleSet, map[string]pr
}
}

if ruleSet == nil && !foundTree {
return nil, nil, fmt.Errorf("unable to find %v", RULE_SET_GOLDEN_FILE_NAME)
}
if ruleSet != nil {
ruleSet.Rules = rules
ruleSets = append(ruleSets, *ruleSet)
Expand Down
18 changes: 0 additions & 18 deletions parser/rule_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,24 +621,6 @@ func TestLoadRules(t *testing.T) {
},
},
},
{
Name: "failure-no-ruleset",
testFileName: "no-ruleset",
ShouldErr: true,
ErrorMessage: "unable to find ruleset.yaml",
providerNameClient: map[string]provider.InternalProviderClient{
"builtin": testProvider{
caps: []provider.Capability{{
Name: "file",
}},
},
"notadded": testProvider{
caps: []provider.Capability{{
Name: "fake",
}},
},
},
},
{
Name: "handle not-valid category",
testFileName: "invalid-category.yaml",
Expand Down
2 changes: 1 addition & 1 deletion provider/internal/builtin/service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,6 @@ func (b *builtinServiceClient) isFileIncluded(absolutePath string) bool {
return true
}
}
b.log.V(5).Info("excluding file from search", "file", absolutePath)
b.log.V(7).Info("excluding file from search", "file", absolutePath)
return false
}

0 comments on commit 5798dba

Please sign in to comment.