Skip to content

Commit

Permalink
test: make sure paths are saved to recipe with slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Nov 18, 2024
1 parent 4e8e035 commit f232f0e
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 68 deletions.
10 changes: 5 additions & 5 deletions internal/cli/create_recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ my-recipe
└── README.md
%[1]s`,
"```",
recipe.RecipeFileName+recipe.YAMLExtension,
recipe.RecipeTemplatesDirName,
recipe.RecipeTestsDirName,
recipe.RecipeTestMetaFileName+recipe.YAMLExtension,
recipe.RecipeTestFilesDirName,
recipe.MetadataFileName+recipe.YAMLExtension,
recipe.TemplatesDirName,
recipe.TestsDirName,
recipe.TestMetaFileName+recipe.YAMLExtension,
recipe.TestFilesDirName,
),
Example: `jalapeno create recipe my-recipe`,
Args: cobra.ExactArgs(1),
Expand Down
26 changes: 13 additions & 13 deletions pkg/recipe/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
)

const (
YAMLExtension = ".yml"
RecipeFileName = "recipe"
RecipeTemplatesDirName = "templates"
RecipeTestsDirName = "tests"
RecipeTestMetaFileName = "test"
RecipeTestFilesDirName = "files"
IgnoreFileName = ".jalapenoignore"
ManifestFileName = "manifest"
YAMLExtension = ".yml"
MetadataFileName = "recipe"
TemplatesDirName = "templates"
TestsDirName = "tests"
TestMetaFileName = "test"
TestFilesDirName = "files"
IgnoreFileName = ".jalapenoignore"
ManifestFileName = "manifest"
)

var (
Expand All @@ -36,7 +36,7 @@ func LoadRecipe(path string) (*Recipe, error) {
return nil, err
}

recipeFile := filepath.Join(rootDir, RecipeFileName+YAMLExtension)
recipeFile := filepath.Join(rootDir, MetadataFileName+YAMLExtension)
dat, err := os.ReadFile(recipeFile)
if err != nil {
return nil, err
Expand All @@ -48,12 +48,12 @@ func LoadRecipe(path string) (*Recipe, error) {
return nil, err
}

recipe.Templates, err = loadTemplates(filepath.Join(rootDir, RecipeTemplatesDirName))
recipe.Templates, err = loadTemplates(filepath.Join(rootDir, TemplatesDirName))
if err != nil {
return nil, fmt.Errorf("error when loading recipe templates: %w", err)
}

recipe.Tests, err = loadTests(filepath.Join(rootDir, RecipeTestsDirName))
recipe.Tests, err = loadTests(filepath.Join(rootDir, TestsDirName))
if err != nil {
return nil, fmt.Errorf("error when loading recipe tests: %w", err)
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func loadTests(path string) ([]Test, error) {

test := Test{}
testDirPath := filepath.Join(path, dir.Name())
contents, err := os.ReadFile(filepath.Join(testDirPath, RecipeTestMetaFileName+YAMLExtension))
contents, err := os.ReadFile(filepath.Join(testDirPath, TestMetaFileName+YAMLExtension))
if err != nil {
return nil, err
}
Expand All @@ -129,7 +129,7 @@ func loadTests(path string) ([]Test, error) {

test.Name = dir.Name()
test.Files = make(map[string]File)
testFileDirPath := filepath.Join(testDirPath, RecipeTestFilesDirName)
testFileDirPath := filepath.Join(testDirPath, TestFilesDirName)

walk := func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions pkg/recipe/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ func TestLoadTests(t *testing.T) {
}
defer os.RemoveAll(dir)

if err = os.MkdirAll(filepath.Join(dir, RecipeTemplatesDirName), 0755); err != nil {
if err = os.MkdirAll(filepath.Join(dir, TemplatesDirName), 0755); err != nil {
t.Fatalf("cannot create templates dir: %s", err)
}

contents := "# file"
if err = os.WriteFile(filepath.Join(dir, RecipeTemplatesDirName, "file.md"), []byte(contents), 0644); err != nil {
if err = os.WriteFile(filepath.Join(dir, TemplatesDirName, "file.md"), []byte(contents), 0644); err != nil {
t.Fatalf("cannot write rendered template: %s", err)
}

Expand All @@ -154,24 +154,24 @@ version: v1.0.0
description: foo recipe
`

if err = os.WriteFile(filepath.Join(dir, RecipeFileName+YAMLExtension), []byte(recipe), 0644); err != nil {
if err = os.WriteFile(filepath.Join(dir, MetadataFileName+YAMLExtension), []byte(recipe), 0644); err != nil {
t.Fatal("cannot write recipe file", err)
}

testMetaFile := "values: {}"
if err = os.MkdirAll(filepath.Join(dir, RecipeTestsDirName, "test_foo"), 0755); err != nil {
if err = os.MkdirAll(filepath.Join(dir, TestsDirName, "test_foo"), 0755); err != nil {
t.Fatalf("cannot create test dir: %s", err)
}

if err = os.WriteFile(filepath.Join(dir, RecipeTestsDirName, "test_foo", RecipeTestMetaFileName+YAMLExtension), []byte(testMetaFile), 0644); err != nil {
if err = os.WriteFile(filepath.Join(dir, TestsDirName, "test_foo", TestMetaFileName+YAMLExtension), []byte(testMetaFile), 0644); err != nil {
t.Fatalf("cannot write recipe test file: %s", err)
}

if err = os.MkdirAll(filepath.Join(dir, RecipeTestsDirName, "test_foo", RecipeTestFilesDirName), 0755); err != nil {
if err = os.MkdirAll(filepath.Join(dir, TestsDirName, "test_foo", TestFilesDirName), 0755); err != nil {
t.Fatalf("cannot create test file dir: %s", err)
}

if err = os.WriteFile(filepath.Join(dir, RecipeTestsDirName, "test_foo", RecipeTestFilesDirName, "file.md"), []byte(contents), 0644); err != nil {
if err = os.WriteFile(filepath.Join(dir, TestsDirName, "test_foo", TestFilesDirName, "file.md"), []byte(contents), 0644); err != nil {
t.Fatalf("cannot create test file dir: %s", err)
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/recipe/saver.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (re *Recipe) Save(dest string) error {
return fmt.Errorf("can not create directory %s: %v", dest, err)
}

recipeFilepath := filepath.Join(dest, RecipeFileName+YAMLExtension)
recipeFilepath := filepath.Join(dest, MetadataFileName+YAMLExtension)
file, err := os.Create(recipeFilepath)
if err != nil {
return fmt.Errorf("failed to create recipe file: %w", err)
Expand Down Expand Up @@ -80,7 +80,7 @@ func (re *Recipe) saveTests(dest string) error {
return nil
}

testRootDir := filepath.Join(dest, RecipeTestsDirName)
testRootDir := filepath.Join(dest, TestsDirName)

err := os.MkdirAll(testRootDir, defaultFileMode)
if err != nil {
Expand All @@ -94,7 +94,7 @@ func (re *Recipe) saveTests(dest string) error {
return fmt.Errorf("failed to create test directory for test '%s': %w", test.Name, err)
}

meta, err := os.Create(filepath.Join(testDirPath, RecipeTestMetaFileName+YAMLExtension))
meta, err := os.Create(filepath.Join(testDirPath, TestMetaFileName+YAMLExtension))
if err != nil {
return fmt.Errorf("failed to create recipe test file: %w", err)
}
Expand All @@ -108,7 +108,7 @@ func (re *Recipe) saveTests(dest string) error {
return fmt.Errorf("failed to write recipe test to a file: %w", err)
}

testFileDirPath := filepath.Join(testDirPath, RecipeTestFilesDirName)
testFileDirPath := filepath.Join(testDirPath, TestFilesDirName)
err = os.RemoveAll(testFileDirPath)
if err != nil {
return fmt.Errorf("failed to clean up test file directory for test '%s': %w", test.Name, err)
Expand All @@ -131,7 +131,7 @@ func (re *Recipe) saveTests(dest string) error {
}

func (re *Recipe) saveTemplates(dest string) error {
templateDir := filepath.Join(dest, RecipeTemplatesDirName)
templateDir := filepath.Join(dest, TemplatesDirName)
err := os.MkdirAll(templateDir, defaultFileMode)
if err != nil {
return fmt.Errorf("can not save templates to the directory: %w", err)
Expand Down
10 changes: 5 additions & 5 deletions pkg/recipe/saver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func TestSaveRecipe(t *testing.T) {
}

expectedFiles := []string{
filepath.Join(RecipeFileName + YAMLExtension),
filepath.Join(MetadataFileName + YAMLExtension),
filepath.Join("templates", "foo.md"),
filepath.Join("templates", "foo", "bar.md"),
filepath.Join("templates", "foo", "bar", "baz.md"),
filepath.Join("tests", re.Tests[0].Name, RecipeTestMetaFileName+YAMLExtension),
filepath.Join("tests", re.Tests[0].Name, RecipeTestFilesDirName, "foo.md"),
filepath.Join("tests", re.Tests[0].Name, RecipeTestFilesDirName, "foo", "bar.md"),
filepath.Join("tests", re.Tests[0].Name, RecipeTestFilesDirName, "foo", "bar", "baz.md"),
filepath.Join("tests", re.Tests[0].Name, TestMetaFileName+YAMLExtension),
filepath.Join("tests", re.Tests[0].Name, TestFilesDirName, "foo.md"),
filepath.Join("tests", re.Tests[0].Name, TestFilesDirName, "foo", "bar.md"),
filepath.Join("tests", re.Tests[0].Name, TestFilesDirName, "foo", "bar", "baz.md"),
}

checkFiles(t, dir, expectedFiles)
Expand Down
2 changes: 1 addition & 1 deletion test/features/check-recipes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Feature: Check for new recipe versions
And the recipe "foo" is pushed to the local OCI repository "foo:v0.0.2"
And I check new versions for recipe "foo" from the local OCI repository "foo"
Then CLI produced an output "new versions found: v0\.0\.2"
And the sauce in index 0 which should have property "CheckFrom" with value "^oci://localhost:\d+/foo$"
And the sauce in index 0 should have property "CheckFrom" with value "^oci://localhost:\d+/foo$"

@docker
Scenario: Find and upgrade newer version for recipes
Expand Down
44 changes: 26 additions & 18 deletions test/features/execute-recipes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ Feature: Execute recipes
When I execute recipe "foo"
Then execution of the recipe has succeeded
And the project directory should contain file "README.md"
And the sauce in index 0 which should have property "Recipe.Name" with value "^foo$"
And the sauce in index 0 which has a valid ID
And the sauce in index 0 should have property "Recipe::Name" with value "^foo$"
And the sauce in index 0 has a valid ID

Scenario: Execute single recipe with directory hierarcy
Given a recipe "foo"
And recipe "foo" generates file "foo/bar.md" with content "initial"
When I execute recipe "foo"
Then execution of the recipe has succeeded
And the project directory should contain file "foo/bar.md"
And the sauce in index 0 should have property "Files::foo/bar.md"

@docker
Scenario: Execute single recipe from remote registry
Expand All @@ -20,7 +28,7 @@ Feature: Execute recipes
Then execution of the recipe has succeeded
And no errors were printed
And the project directory should contain file "README.md"
And the sauce in index 0 which should have property "CheckFrom" with value "^oci://.+/foo$"
And the sauce in index 0 should have property "CheckFrom" with value "^oci://.+/foo$"

Scenario: Execute multiple recipes
Given a recipe "foo"
Expand All @@ -34,8 +42,8 @@ Feature: Execute recipes
And no errors were printed
And the project directory should contain file "README.md"
And the project directory should contain file "Taskfile.yml"
And the sauce in index 0 which should have property "Recipe.Name" with value "^foo$"
And the sauce in index 1 which should have property "Recipe.Name" with value "^bar$"
And the sauce in index 0 should have property "Recipe::Name" with value "^foo$"
And the sauce in index 1 should have property "Recipe::Name" with value "^bar$"

Scenario: New recipe conflicts with the previous recipe
Given a recipe "foo"
Expand All @@ -55,19 +63,19 @@ Feature: Execute recipes

Scenario: Execute single recipe to a subpath
Given a recipe "foo"
And recipe "foo" generates file "README" with content "initial"
And recipes will be executed to the subpath "docs"
And recipe "foo" generates file "README.md" with content "initial"
And recipes will be executed to the subpath "docs/pages"
When I execute recipe "foo"
And no errors were printed
Then execution of the recipe has succeeded
And CLI produced an output "docs[\S\s]+└── README"
And the project directory should contain file "docs/README"
And the sauce in index 0 which should have property "Files.README"
And the sauce in index 0 which should have property "SubPath" with value "^docs$"
And CLI produced an output "docs[\S\s]+└── pages[\S\s]+└── README\.md"
And the project directory should contain file "docs/pages/README.md"
And the sauce in index 0 should have property "Files::README.md"
And the sauce in index 0 should have property "SubPath" with value "^docs/pages$"

Scenario: Execute multiple recipes to different subpaths
Given a recipe "foo"
And recipe "foo" generates file "README" with content "initial"
And recipe "foo" generates file "README.md" with content "initial"
And recipes will be executed to the subpath "foo"
When I execute recipe "foo"
Then no errors were printed
Expand All @@ -76,12 +84,12 @@ Feature: Execute recipes
And I execute recipe "foo"
Then no errors were printed
And execution of the recipe has succeeded
And the project directory should contain file "foo/README"
And the project directory should contain file "bar/README"
And the sauce in index 0 which should have property "Files.README"
And the sauce in index 0 which should have property "SubPath" with value "^foo$"
And the sauce in index 1 which should have property "Files.README"
And the sauce in index 1 which should have property "SubPath" with value "^bar$"
And the project directory should contain file "foo/README.md"
And the project directory should contain file "bar/README.md"
And the sauce in index 0 should have property "Files::README.md"
And the sauce in index 0 should have property "SubPath" with value "^foo$"
And the sauce in index 1 should have property "Files::README.md"
And the sauce in index 1 should have property "SubPath" with value "^bar$"

Scenario: Try to execute recipe which escapes the project root
Given a recipe "foo"
Expand Down
2 changes: 1 addition & 1 deletion test/features/jalapenoignore.feature
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ Feature: Jalapenoignore
Then no conflicts were reported
And no errors were printed
And the project directory should contain file "will-be-removed-in-next-version" with "modified"
And the sauce in index 0 which should not have property "Files.will-be-removed-in-next-version"
And the sauce in index 0 should not have property "Files::will-be-removed-in-next-version"
8 changes: 4 additions & 4 deletions test/features/upgrade-recipe.feature
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ Feature: Upgrade sauce
And recipes will be executed to the subpath "foo"
And I execute recipe "shared"
Then execution of the recipe has succeeded
And the sauce in index 0 which should have property "SubPath" with value "^foo$"
And the sauce in index 0 should have property "SubPath" with value "^foo$"
And the project directory should contain file "./foo/README.md"
When recipes will be executed to the subpath "bar"
And I execute recipe "shared"
Then execution of the recipe has succeeded
And the sauce in index 1 which should have property "SubPath" with value "^bar$"
And the sauce in index 1 should have property "SubPath" with value "^bar$"
And the project directory should contain file "./bar/README.md"
When I change recipe "shared" to version "v0.0.2"
And I change recipe "shared" template "README.md" to render "New version"
Expand All @@ -79,12 +79,12 @@ Feature: Upgrade sauce
And recipes will be executed to the subpath "foo"
And I execute recipe "shared"
Then execution of the recipe has succeeded
And the sauce in index 0 which should have property "SubPath" with value "^foo$"
And the sauce in index 0 should have property "SubPath" with value "^foo$"
And the project directory should contain file "./foo/README.md"
When recipes will be executed to the subpath "bar"
And I execute recipe "shared"
Then execution of the recipe has succeeded
And the sauce in index 1 which should have property "SubPath" with value "^bar$"
And the sauce in index 1 should have property "SubPath" with value "^bar$"
And the project directory should contain file "./bar/README.md"
When I change recipe "shared" to version "v0.0.2"
And I change recipe "shared" template "README.md" to render "New version"
Expand Down
28 changes: 19 additions & 9 deletions test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ func AddCommonSteps(s *godog.ScenarioContext) {
s.Step(`^no errors were printed$`, noErrorsWerePrinted)
s.Step(`^CLI produced an output "([^"]*)"$`, expectGivenOutput)
s.Step(`^CLI produced an error "(.*)"$`, expectGivenError)
s.Step(`^the sauce in index (\d) which should have property "([^"]*)"$`, theSauceShouldHaveProperty)
s.Step(`^the sauce in index (\d) which should not have property "([^"]*)"$`, theSauceFileShouldNotHaveProperty)
s.Step(`^the sauce in index (\d) which should have property "([^"]*)" with value "([^"]*)"$`, theSauceFileShouldHavePropertyWithValue)
s.Step(`^the sauce in index (\d) which has a valid ID$`, theSauceFileShouldHasAValidID)
s.Step(`^the sauce in index (\d) should have property "([^"]*)"$`, theSauceShouldHaveProperty)
s.Step(`^the sauce in index (\d) should not have property "([^"]*)"$`, theSauceFileShouldNotHaveProperty)
s.Step(`^the sauce in index (\d) should have property "([^"]*)" with value "([^"]*)"$`, theSauceFileShouldHavePropertyWithValue)
s.Step(`^the sauce in index (\d) has a valid ID$`, theSauceFileShouldHasAValidID)
s.Step(`^the project directory should contain file "([^"]*)"$`, theProjectDirectoryShouldContainFile)
s.Step(`^the project directory should contain file "([^"]*)" with "([^"]*)"$`, theProjectDirectoryShouldContainFileWith)
s.Step(`^the project directory should not contain file "([^"]*)"$`, theProjectDirectoryShouldNotContainFile)
Expand Down Expand Up @@ -318,14 +318,24 @@ func aRecipe(ctx context.Context, recipeName string) (context.Context, error) {

func recipeGeneratesFileWithContent(ctx context.Context, recipeName, filename, content string) (context.Context, error) {
dir := ctx.Value(recipesDirectoryPathCtxKey{}).(string)
re, err := recipe.LoadRecipe(filepath.Join(dir, recipeName))

recipePath := filepath.Join(dir, recipeName)
filePath := filepath.Join(recipePath, recipe.TemplatesDirName, filename)

err := os.MkdirAll(filepath.Dir(filePath), 0700)
if err != nil {
return ctx, err
}

file, err := os.Create(filepath.Join(recipePath, recipe.TemplatesDirName, filename))
if err != nil {
return ctx, err
}

re.Templates[filename] = recipe.NewFile([]byte(content))
defer file.Close()

if err := re.Save(filepath.Join(dir, recipeName)); err != nil {
_, err = file.WriteString(content)
if err != nil {
return ctx, err
}

Expand All @@ -335,7 +345,7 @@ func recipeGeneratesFileWithContent(ctx context.Context, recipeName, filename, c
func iRemoveFileFromTheRecipe(ctx context.Context, filename, recipeName string) (context.Context, error) {
recipesDir := ctx.Value(recipesDirectoryPathCtxKey{}).(string)

templateDir := filepath.Join(recipesDir, recipeName, recipe.RecipeTemplatesDirName)
templateDir := filepath.Join(recipesDir, recipeName, recipe.TemplatesDirName)

err := os.Remove(filepath.Join(templateDir, filename))
return ctx, err
Expand Down Expand Up @@ -757,7 +767,7 @@ func addRegistryRelatedFlags(ctx context.Context) context.Context {

func getDeepPropertyFromStruct(v any, key string) reflect.Value {
r := reflect.ValueOf(v)
for _, k := range strings.Split(key, ".") {
for _, k := range strings.Split(key, "::") {
switch reflect.Indirect(r).Kind() {
case reflect.Struct:
r = reflect.Indirect(r).FieldByName(k)
Expand Down

0 comments on commit f232f0e

Please sign in to comment.