Skip to content

Commit

Permalink
Merge branch 'main' of github.com:terramate-io/terramate into i4k-add…
Browse files Browse the repository at this point in the history
…-create-wants

Signed-off-by: i4k <[email protected]>
  • Loading branch information
i4ki committed Sep 27, 2024
2 parents b2928b0 + 2e10c1f commit 6d8c635
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:

- Add `terramate create --wants ... --wanted-by ...` flags for configuring the `stack.wants` and `stack.wanted_by` attributes, respectively.

### Fixed

- Fix the cleaning up of orphaned files in the `terramate generate` to respect the `-C <dir>` flag.

## v0.10.6

### Fixed
Expand Down
24 changes: 12 additions & 12 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,23 @@ 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")

defer report.sort()

// 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,8 +1365,8 @@ func cleanupOrphaned(root *config.Root, report Report) Report {
deleteFailures := map[project.Path]*errors.List{}

for _, genfile := range orphanedGenFiles {
genfileAbspath := filepath.Join(root.HostDir(), genfile)
dir := project.NewPath("/" + filepath.ToSlash(filepath.Dir(genfile)))
genfileAbspath := filepath.Join(target.HostDir(), genfile)
dir := project.PrjAbsPath(root.HostDir(), filepath.Dir(genfileAbspath))
if err := os.Remove(genfileAbspath); err != nil {
if deleteFailures[dir] == nil {
deleteFailures[dir] = errors.L()
Expand Down Expand Up @@ -1402,7 +1404,5 @@ func cleanupOrphaned(root *config.Root, report Report) Report {
Deleted: deletedFiles,
})
}

report.sort()
return report
}
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{
"subdir/dir/z.hcl",
"subdir/y.hcl",
"x.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 6d8c635

Please sign in to comment.