Skip to content

Commit f51a639

Browse files
committed
fix: check for extra variables defined in tests
1 parent 01074df commit f51a639

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed
-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
values:
22
MY_VAR: bar
3-
ignoreExtraFiles: false
-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
values:
22
MY_VAR: foo
3-
ignoreExtraFiles: false

pkg/recipe/recipe.go

+22-8
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,41 @@ func (re *Recipe) Validate() error {
2424
return err
2525
}
2626

27-
checkDuplicates := make(map[string]bool)
27+
varDuplicateCheck := make(map[string]struct{})
2828
for _, v := range re.Variables {
29+
if _, exists := varDuplicateCheck[v.Name]; exists {
30+
return fmt.Errorf("variable %s has been declared multiple times", v.Name)
31+
}
2932
if err := v.Validate(); err != nil {
3033
return fmt.Errorf("error on variable %s: %w", v.Name, err)
3134
}
32-
if _, exists := checkDuplicates[v.Name]; exists {
33-
return fmt.Errorf("variable %s has been declared multiple times", v.Name)
34-
}
35-
checkDuplicates[v.Name] = true
35+
varDuplicateCheck[v.Name] = struct{}{}
3636
}
3737

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

44-
duplicateCheck[t.Name] = struct{}{}
4544
if err := t.Validate(); err != nil {
4645
return fmt.Errorf("error when validating recipe test case %s: %w", t.Name, err)
4746
}
47+
testDuplicateCheck[t.Name] = struct{}{}
48+
49+
for vName := range t.Values {
50+
found := false
51+
for _, v := range re.Variables {
52+
if v.Name == vName {
53+
found = true
54+
break
55+
}
56+
}
57+
58+
if !found {
59+
return fmt.Errorf("test case %s references an unknown variable %s", t.Name, vName)
60+
}
61+
}
4862
}
4963

5064
return nil

pkg/recipe/test_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ func TestRecipeTests(t *testing.T) {
192192
recipe.Tests = tests
193193
recipe.Templates = scenario.templates
194194

195+
for _, t := range recipe.Tests {
196+
for vName := range t.Values {
197+
recipe.Variables = append(recipe.Variables, Variable{
198+
Name: vName,
199+
})
200+
}
201+
}
202+
195203
errs := recipe.RunTests()
196204
for i, err := range errs {
197205
expectedErr := scenario.tests[i].ExpectedError

0 commit comments

Comments
 (0)