From f51a639f04a2d05d0e8863e3a4f5acfc81d87915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Kivim=C3=A4ki?= Date: Thu, 18 Jul 2024 13:32:01 +0300 Subject: [PATCH] fix: check for extra variables defined in tests --- examples/with-tests/tests/bar/test.yml | 1 - examples/with-tests/tests/foo/test.yml | 1 - pkg/recipe/recipe.go | 30 +++++++++++++++++++------- pkg/recipe/test_test.go | 8 +++++++ 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/examples/with-tests/tests/bar/test.yml b/examples/with-tests/tests/bar/test.yml index 81cbd157..b98bfb98 100644 --- a/examples/with-tests/tests/bar/test.yml +++ b/examples/with-tests/tests/bar/test.yml @@ -1,3 +1,2 @@ values: MY_VAR: bar -ignoreExtraFiles: false diff --git a/examples/with-tests/tests/foo/test.yml b/examples/with-tests/tests/foo/test.yml index fa19b1c2..77e30de9 100644 --- a/examples/with-tests/tests/foo/test.yml +++ b/examples/with-tests/tests/foo/test.yml @@ -1,3 +1,2 @@ values: MY_VAR: foo -ignoreExtraFiles: false diff --git a/pkg/recipe/recipe.go b/pkg/recipe/recipe.go index 4d5a3d4e..f0d6894b 100644 --- a/pkg/recipe/recipe.go +++ b/pkg/recipe/recipe.go @@ -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 diff --git a/pkg/recipe/test_test.go b/pkg/recipe/test_test.go index 2b4a37e7..ff03acbe 100644 --- a/pkg/recipe/test_test.go +++ b/pkg/recipe/test_test.go @@ -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