From 641c7cc3f69579456aca68fb8bc417dd89130f6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Kivim=C3=A4ki?= Date: Mon, 15 Jul 2024 11:02:39 +0300 Subject: [PATCH] chore: allow "create test" to be run multiple times Release-As: 1.11.5 --- internal/cli/create_rtest.go | 35 ++++++++++++++----- pkg/recipe/recipe.go | 6 ++++ pkg/recipe/test_test.go | 3 +- ...{create-recipes.feature => create.feature} | 14 ++++++++ 4 files changed, 49 insertions(+), 9 deletions(-) rename test/features/{create-recipes.feature => create.feature} (52%) diff --git a/internal/cli/create_rtest.go b/internal/cli/create_rtest.go index 00e75226..8e7726e2 100644 --- a/internal/cli/create_rtest.go +++ b/internal/cli/create_rtest.go @@ -43,14 +43,9 @@ func runCreateTest(cmd *cobra.Command, opts createTestOptions) error { return fmt.Errorf("can not load the recipe: %w", err) } - // TODO: What if there already exists a test called "example"? - test := recipeutil.CreateExampleTest("example") - - if len(re.Tests) > 0 { - re.Tests = append(re.Tests, test) - } else { - re.Tests = []recipe.Test{test} - } + testCaseName := generateTestCaseName(re.Tests) + test := recipeutil.CreateExampleTest(testCaseName) + re.Tests = append(re.Tests, test) err = re.Save(opts.Dir) if err != nil { @@ -70,3 +65,27 @@ func runCreateTest(cmd *cobra.Command, opts createTestOptions) error { return nil } + +func generateTestCaseName(tests []recipe.Test) string { + defaultName := "example" + + testNameExists := func(name string, tests []recipe.Test) bool { + for _, t := range tests { + if t.Name == name { + return true + } + } + return false + } + + if !testNameExists(defaultName, tests) { + return defaultName + } + + for i := 1; ; i++ { + name := fmt.Sprintf("%s_%d", defaultName, i) + if !testNameExists(name, tests) { + return name + } + } +} diff --git a/pkg/recipe/recipe.go b/pkg/recipe/recipe.go index a1f3f026..4d5a3d4e 100644 --- a/pkg/recipe/recipe.go +++ b/pkg/recipe/recipe.go @@ -35,7 +35,13 @@ func (re *Recipe) Validate() error { checkDuplicates[v.Name] = true } + duplicateCheck := make(map[string]struct{}) for _, t := range re.Tests { + if _, exists := duplicateCheck[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) } diff --git a/pkg/recipe/test_test.go b/pkg/recipe/test_test.go index aea4781a..2b4a37e7 100644 --- a/pkg/recipe/test_test.go +++ b/pkg/recipe/test_test.go @@ -2,6 +2,7 @@ package recipe import ( "errors" + "fmt" "testing" ) @@ -183,7 +184,7 @@ func TestRecipeTests(t *testing.T) { tests := make([]Test, len(scenario.tests)) for i, t := range scenario.tests { tests[i] = t.Test - tests[i].Name = scenario.name + tests[i].Name = fmt.Sprintf("%s_%d", scenario.name, i+1) } recipe := NewRecipe() recipe.Name = "foo" diff --git a/test/features/create-recipes.feature b/test/features/create.feature similarity index 52% rename from test/features/create-recipes.feature rename to test/features/create.feature index 321a1528..21f13abe 100644 --- a/test/features/create-recipes.feature +++ b/test/features/create.feature @@ -12,6 +12,20 @@ Feature: Create new recipes And no errors were printed And the file "tests/example/test.yml" exist in the recipe "foo" + Scenario: Using CLI to create multiple recipe tests + Given a recipe "foo" + And recipe "foo" generates file "README.md" with content "initial" + When I create a test for recipe "foo" + Then CLI produced an output "Test 'example' created successfully!" + When I create a test for recipe "foo" + Then CLI produced an output "Test 'example_1' created successfully!" + When I create a test for recipe "foo" + Then CLI produced an output "Test 'example_2' created successfully!" + And no errors were printed + And the file "tests/example/test.yml" exist in the recipe "foo" + And the file "tests/example_1/test.yml" exist in the recipe "foo" + And the file "tests/example_2/test.yml" exist in the recipe "foo" + Scenario: Using CLI to create a manifest When I create a manifest with the CLI Then CLI produced an output "Manifest created successfully!"