Skip to content

Commit

Permalink
fix: check for extra variables defined in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Jul 18, 2024
1 parent 01074df commit f51a639
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
1 change: 0 additions & 1 deletion examples/with-tests/tests/bar/test.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
values:
MY_VAR: bar
ignoreExtraFiles: false
1 change: 0 additions & 1 deletion examples/with-tests/tests/foo/test.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
values:
MY_VAR: foo
ignoreExtraFiles: false
30 changes: 22 additions & 8 deletions pkg/recipe/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,41 @@ func (re *Recipe) Validate() error {
return err
}

checkDuplicates := make(map[string]bool)
varDuplicateCheck := make(map[string]struct{})
for _, v := range re.Variables {
if _, exists := varDuplicateCheck[v.Name]; exists {
return fmt.Errorf("variable %s has been declared multiple times", v.Name)
}
if err := v.Validate(); err != nil {
return fmt.Errorf("error on variable %s: %w", v.Name, err)
}
if _, exists := checkDuplicates[v.Name]; exists {
return fmt.Errorf("variable %s has been declared multiple times", v.Name)
}
checkDuplicates[v.Name] = true
varDuplicateCheck[v.Name] = struct{}{}
}

duplicateCheck := make(map[string]struct{})
testDuplicateCheck := make(map[string]struct{})
for _, t := range re.Tests {
if _, exists := duplicateCheck[t.Name]; exists {
if _, exists := testDuplicateCheck[t.Name]; exists {
return fmt.Errorf("test case %s has been declared multiple times", t.Name)
}

duplicateCheck[t.Name] = struct{}{}
if err := t.Validate(); err != nil {
return fmt.Errorf("error when validating recipe test case %s: %w", t.Name, err)
}
testDuplicateCheck[t.Name] = struct{}{}

for vName := range t.Values {
found := false
for _, v := range re.Variables {
if v.Name == vName {
found = true
break
}
}

if !found {
return fmt.Errorf("test case %s references an unknown variable %s", t.Name, vName)
}
}
}

return nil
Expand Down
8 changes: 8 additions & 0 deletions pkg/recipe/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ func TestRecipeTests(t *testing.T) {
recipe.Tests = tests
recipe.Templates = scenario.templates

for _, t := range recipe.Tests {
for vName := range t.Values {
recipe.Variables = append(recipe.Variables, Variable{
Name: vName,
})
}
}

errs := recipe.RunTests()
for i, err := range errs {
expectedErr := scenario.tests[i].ExpectedError
Expand Down

0 comments on commit f51a639

Please sign in to comment.