Skip to content

Commit

Permalink
fix: make sure that the filenames are created with slash to recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Nov 15, 2024
1 parent 54c7af2 commit a8a95e5
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion pkg/recipe/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package recipe
import (
"errors"
"maps"
"path/filepath"
"strings"

"github.com/gofrs/uuid"
Expand Down Expand Up @@ -72,7 +73,7 @@ func (re *Recipe) Execute(engine RenderEngine, values VariableValues, id uuid.UU
continue
}

filename = strings.TrimSuffix(filename, re.TemplateExtension)
filename = filepath.ToSlash(strings.TrimSuffix(filename, re.TemplateExtension))

sauce.Files[filename] = NewFile(content)
idx += 1
Expand Down
5 changes: 3 additions & 2 deletions pkg/recipeutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package recipeutil

import (
"fmt"
fp "path/filepath"
"slices"
"strings"

Expand All @@ -27,12 +28,12 @@ func CreateFileTree(root string, files map[string]FileStatus) string {
slices.Sort(filepaths)

for _, filepath := range filepaths {
filepathSegments := strings.Split(filepath, "/")
filepathSegments := strings.Split(filepath, string(fp.Separator))

// If the last segment is empty, it means the filepath should represent a directory
if filepathSegments[len(filepathSegments)-1] == "" {
filepathSegments = filepathSegments[:len(filepathSegments)-1]
filepathSegments[len(filepathSegments)-1] += "/"
filepathSegments[len(filepathSegments)-1] += string(fp.Separator)
}

cursor := tree
Expand Down
2 changes: 1 addition & 1 deletion test/features/execute-recipes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Feature: Execute recipes
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 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$"

Expand Down
6 changes: 3 additions & 3 deletions test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func iClearTheOutput(ctx context.Context) (context.Context, error) {

func theProjectDirectoryShouldContainFile(ctx context.Context, filename string) error {
dir := ctx.Value(projectDirectoryPathCtxKey{}).(string)
info, err := os.Stat(filepath.Join(dir, filename))
info, err := os.Stat(filepath.Join(dir, filepath.Clean(filename)))
if err == nil && !info.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file", filename)
}
Expand All @@ -446,11 +446,11 @@ func theProjectDirectoryShouldContainFile(ctx context.Context, filename string)

func iCreateAFileWithContentsToTheProjectDir(ctx context.Context, filename, contents string) error {
dir := ctx.Value(projectDirectoryPathCtxKey{}).(string)
return os.WriteFile(filepath.Join(dir, filename), []byte(contents), 0644)
return os.WriteFile(filepath.Join(dir, filepath.Clean(filename)), []byte(contents), 0644)
}

func theProjectDirectoryShouldContainFileWith(ctx context.Context, filename, searchTerm string) error {
content, err := readProjectDirectoryFile(ctx, filename)
content, err := readProjectDirectoryFile(ctx, filepath.Clean(filename))
if err != nil {
return err
}
Expand Down

0 comments on commit a8a95e5

Please sign in to comment.