Skip to content

Commit

Permalink
chore: allow "create test" to be run multiple times
Browse files Browse the repository at this point in the history
Release-As: 1.11.5
  • Loading branch information
majori committed Jul 15, 2024
1 parent 1670411 commit 641c7cc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
35 changes: 27 additions & 8 deletions internal/cli/create_rtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
}
6 changes: 6 additions & 0 deletions pkg/recipe/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/recipe/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package recipe

import (
"errors"
"fmt"
"testing"
)

Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down

0 comments on commit 641c7cc

Please sign in to comment.