Skip to content

Commit

Permalink
fix: cleanup of orphaned files must respect -C flag.
Browse files Browse the repository at this point in the history
Signed-off-by: i4k <[email protected]>
  • Loading branch information
i4ki committed Sep 25, 2024
1 parent eb496f0 commit ed56345
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 9 deletions.
18 changes: 9 additions & 9 deletions generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func Do(
stackReport := doStackGeneration(root, tree, vendorDir, vendorRequests)
rootReport := doRootGeneration(root, tree)
report := mergeReports(stackReport, rootReport)
return cleanupOrphaned(root, report)
return cleanupOrphaned(root, tree, report)
}

func doStackGeneration(
Expand Down Expand Up @@ -1339,21 +1339,21 @@ func loadStackCodeCfgs(
return genfilesConfigs, nil
}

func cleanupOrphaned(root *config.Root, report Report) Report {
func cleanupOrphaned(root *config.Root, target *config.Tree, report Report) Report {
logger := log.With().
Str("action", "generate.cleanupOrphaned()").
Stringer("dir", target.Dir()).
Logger()
// If the root of the tree is a stack then there is nothing to do
// since there can't be any orphans (the root parent stack owns
// the entire project).
if root.Tree().IsStack() {
logger.Debug().Msg("project root is a stack, nothing to do")

// If the target tree is a stack then there is nothing to do
// as it was already generated at this point.
if target.IsStack() {
return report
}

logger.Debug().Msg("listing orphaned generated files")

orphanedGenFiles, err := ListStackGenFiles(root, root.HostDir())
orphanedGenFiles, err := ListStackGenFiles(root, target.HostDir())
if err != nil {
report.CleanupErr = err
return report
Expand All @@ -1363,7 +1363,7 @@ func cleanupOrphaned(root *config.Root, report Report) Report {
deleteFailures := map[project.Path]*errors.List{}

for _, genfile := range orphanedGenFiles {
genfileAbspath := filepath.Join(root.HostDir(), genfile)
genfileAbspath := filepath.Join(target.HostDir(), genfile)
dir := project.NewPath("/" + filepath.ToSlash(filepath.Dir(genfile)))
if err := os.Remove(genfileAbspath); err != nil {
if deleteFailures[dir] == nil {
Expand Down
91 changes: 91 additions & 0 deletions generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,97 @@ func TestGenerateCleanup(t *testing.T) {
},
},
},
{
name: "workdir is respected in cleanup",
layout: []string{
"s:stacks/stack-1",
"s:stacks/stack-2",
"s:stacks/stack-1/stack-1-a",
"s:stacks/stack-1/stack-1-b",
"s:stacks/stack-3", // only files here will be cleaned up.
genfile("dir/orphan.hcl"),
genfile("stacks/stack-1/a.hcl"),
genfile("stacks/stack-1/subdir/b.hcl"),
genfile("stacks/stack-1/subdir/dir/c.hcl"),
genfile("stacks/stack-1/stack-1-a/e.hcl"),
genfile("stacks/stack-1/stack-1-a/subdir/f.hcl"),
genfile("stacks/stack-1/stack-1-a/subdir/dir/g.hcl"),
genfile("stacks/stack-1/stack-1-b/h.hcl"),
genfile("stacks/stack-2/d.hcl"),
genfile("stacks/stack-3/x.hcl"),
genfile("stacks/stack-3/subdir/y.hcl"),
genfile("stacks/stack-3/subdir/dir/z.hcl"),
},
fromdir: "/stacks/stack-3",
wantReport: generate.Report{
Successes: []generate.Result{
{
Dir: project.NewPath("/stacks/stack-3"),
Deleted: []string{
"x.hcl",
"subdir/y.hcl",
"subdir/dir/z.hcl",
},
},
},
},
want: []generatedFile{
{
dir: "/stacks/stack-1",
files: map[string]fmt.Stringer{
"a.hcl": Doc(),
},
},
{
dir: "/stacks/stack-1/stack-1-a",
files: map[string]fmt.Stringer{
"e.hcl": Doc(),
},
},
{
dir: "/stacks/stack-1/stack-1-a/subdir/dir",
files: map[string]fmt.Stringer{
"g.hcl": Doc(),
},
},
{
dir: "/stacks/stack-1/stack-1-a/subdir",
files: map[string]fmt.Stringer{
"f.hcl": Doc(),
},
},
{
dir: "/stacks/stack-1/stack-1-b",
files: map[string]fmt.Stringer{
"h.hcl": Doc(),
},
},
{
dir: "/stacks/stack-1/subdir",
files: map[string]fmt.Stringer{
"b.hcl": Doc(),
},
},
{
dir: "/stacks/stack-1/subdir/dir",
files: map[string]fmt.Stringer{
"c.hcl": Doc(),
},
},
{
dir: "/stacks/stack-2",
files: map[string]fmt.Stringer{
"d.hcl": Doc(),
},
},
{
dir: "/dir",
files: map[string]fmt.Stringer{
"orphan.hcl": Doc(),
},
},
},
},
{
name: "cleanup ignores dotdirs outside stacks",
layout: []string{
Expand Down

0 comments on commit ed56345

Please sign in to comment.