Skip to content

Commit

Permalink
Add test for snapshot updating
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Dec 13, 2023
1 parent 58805b3 commit dd31114
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
19 changes: 18 additions & 1 deletion features/recipe-tests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Feature: Running tests for a recipe
Scenario: Using CLI to create a new recipe test
Given a recipes directory
And a recipe "foo" that generates file "README.md"
When I create a placeholder test for recipe "foo" using the CLI
When I create a test for recipe "foo"
Then CLI produced an output "Test created"
And no errors were printed
And the file "tests/example/test.yml" exist in the recipe "foo"
Expand All @@ -13,3 +13,20 @@ Feature: Running tests for a recipe
And I run tests for recipe "foo"
Then CLI produced an output "✅: defaults"
And no errors were printed

Scenario: Tests fail if templates changes
Given a recipes directory
When I create a recipe with name "foo"
And I change recipe "foo" template "README.md" to render "New version"
And I run tests for recipe "foo"
Then CLI produced an output "❌: defaults"
And CLI produced an error "did not match: file 'README.md'"

Scenario: Update test file snapshots
Given a recipes directory
When I create a recipe with name "foo"
And I change recipe "foo" template "README.md" to render "New version"
And I run tests for recipe "foo" while updating snapshots
Then CLI produced an output "test snapshots updated"
When I run tests for recipe "foo"
Then no errors were printed
4 changes: 2 additions & 2 deletions internal/cli/cmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestFeatures(t *testing.T) {
s.Step(`^the project directory should contain file "([^"]*)"$`, theProjectDirectoryShouldContainFile)
s.Step(`^the project directory should contain file "([^"]*)" with "([^"]*)"$`, theProjectDirectoryShouldContainFileWith)
s.Step(`^the sauce file contains a sauce in index (\d) which should have property "([^"]*)" with value "([^"]*)"$`, theSauceFileShouldHavePropertyWithValue)
s.Step(`^the sauce file contains a sauce in index (\d) which should have property "([^"]*)" that is a valid UUID$`, theSauceFileFileShouldHavePropertyThatIsAValidUUID)
s.Step(`^the sauce file contains a sauce in index (\d) which should have property "([^"]*)" that is a valid UUID$`, theSauceFileShouldHavePropertyThatIsAValidUUID)
s.Step(`^CLI produced an output "([^"]*)"$`, expectGivenOutput)
s.Step(`^CLI produced an error "([^"]*)"$`, expectGivenError)
s.Step(`^recipe "([^"]*)" ignores pattern "([^"]*)"$`, recipeIgnoresPattern)
Expand Down Expand Up @@ -343,7 +343,7 @@ func theProjectDirectoryShouldContainFileWith(ctx context.Context, filename, sea
return nil
}

func theSauceFileFileShouldHavePropertyThatIsAValidUUID(ctx context.Context, index int, propertyName string) error {
func theSauceFileShouldHavePropertyThatIsAValidUUID(ctx context.Context, index int, propertyName string) error {
recipes, err := readSauceFile(ctx)
if err != nil {
return err
Expand Down
15 changes: 13 additions & 2 deletions internal/cli/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (

func AddTestSteps(s *godog.ScenarioContext) {
s.Step(`^I run tests for recipe "([^"]*)"$`, iRunTest)
s.Step(`^I create a placeholder test for recipe "([^"]*)" using the CLI$`, iCreateRecipeTestUsingCLI)
s.Step(`^I run tests for recipe "([^"]*)" while updating snapshots$`, iRunTestWithSnapshotUpdate)
s.Step(`^I create a test for recipe "([^"]*)"$`, iCreateRecipeTest)
}

func iRunTest(ctx context.Context, recipe string) (context.Context, error) {
Expand All @@ -33,7 +34,7 @@ func iRunTest(ctx context.Context, recipe string) (context.Context, error) {
return ctx, nil
}

func iCreateRecipeTestUsingCLI(ctx context.Context, recipe string) (context.Context, error) {
func iCreateRecipeTest(ctx context.Context, recipe string) (context.Context, error) {
ctx = context.WithValue(
ctx,
cmdAdditionalFlagsCtxKey{},
Expand All @@ -42,3 +43,13 @@ func iCreateRecipeTestUsingCLI(ctx context.Context, recipe string) (context.Cont

return iRunTest(ctx, recipe)
}

func iRunTestWithSnapshotUpdate(ctx context.Context, recipe string) (context.Context, error) {
ctx = context.WithValue(
ctx,
cmdAdditionalFlagsCtxKey{},
map[string]string{"update-snapshots": "true"},
)

return iRunTest(ctx, recipe)
}

0 comments on commit dd31114

Please sign in to comment.