Skip to content

Commit bae9481

Browse files
committed
Merge branch 'main' of github.com:terramate-io/terramate into i4k-generate-log-disabling
2 parents a0a753c + 2e10c1f commit bae9481

File tree

3 files changed

+107
-12
lines changed

3 files changed

+107
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:
2222

2323
## Unreleased
2424

25+
### Fixed
26+
27+
- Fix the cleaning up of orphaned files in the `terramate generate` to respect the `-C <dir>` flag.
28+
2529
## v0.10.6
2630

2731
### Fixed

generate/generate.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func Do(
194194
stackReport := doStackGeneration(root, tree, vendorDir, vendorRequests)
195195
rootReport := doRootGeneration(root, tree)
196196
report := mergeReports(stackReport, rootReport)
197-
return cleanupOrphaned(root, report)
197+
return cleanupOrphaned(root, tree, report)
198198
}
199199

200200
func doStackGeneration(
@@ -1339,21 +1339,23 @@ func loadStackCodeCfgs(
13391339
return genfilesConfigs, nil
13401340
}
13411341

1342-
func cleanupOrphaned(root *config.Root, report Report) Report {
1342+
func cleanupOrphaned(root *config.Root, target *config.Tree, report Report) Report {
13431343
logger := log.With().
13441344
Str("action", "generate.cleanupOrphaned()").
1345+
Stringer("dir", target.Dir()).
13451346
Logger()
1346-
// If the root of the tree is a stack then there is nothing to do
1347-
// since there can't be any orphans (the root parent stack owns
1348-
// the entire project).
1349-
if root.Tree().IsStack() {
1350-
logger.Debug().Msg("project root is a stack, nothing to do")
1347+
1348+
defer report.sort()
1349+
1350+
// If the target tree is a stack then there is nothing to do
1351+
// as it was already generated at this point.
1352+
if target.IsStack() {
13511353
return report
13521354
}
13531355

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

1356-
orphanedGenFiles, err := ListStackGenFiles(root, root.HostDir())
1358+
orphanedGenFiles, err := ListStackGenFiles(root, target.HostDir())
13571359
if err != nil {
13581360
report.CleanupErr = err
13591361
return report
@@ -1363,8 +1365,8 @@ func cleanupOrphaned(root *config.Root, report Report) Report {
13631365
deleteFailures := map[project.Path]*errors.List{}
13641366

13651367
for _, genfile := range orphanedGenFiles {
1366-
genfileAbspath := filepath.Join(root.HostDir(), genfile)
1367-
dir := project.NewPath("/" + filepath.ToSlash(filepath.Dir(genfile)))
1368+
genfileAbspath := filepath.Join(target.HostDir(), genfile)
1369+
dir := project.PrjAbsPath(root.HostDir(), filepath.Dir(genfileAbspath))
13681370
if err := os.Remove(genfileAbspath); err != nil {
13691371
if deleteFailures[dir] == nil {
13701372
deleteFailures[dir] = errors.L()
@@ -1402,7 +1404,5 @@ func cleanupOrphaned(root *config.Root, report Report) Report {
14021404
Deleted: deletedFiles,
14031405
})
14041406
}
1405-
1406-
report.sort()
14071407
return report
14081408
}

generate/generate_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,97 @@ func TestGenerateCleanup(t *testing.T) {
798798
},
799799
},
800800
},
801+
{
802+
name: "workdir is respected in cleanup",
803+
layout: []string{
804+
"s:stacks/stack-1",
805+
"s:stacks/stack-2",
806+
"s:stacks/stack-1/stack-1-a",
807+
"s:stacks/stack-1/stack-1-b",
808+
"s:stacks/stack-3", // only files here will be cleaned up.
809+
genfile("dir/orphan.hcl"),
810+
genfile("stacks/stack-1/a.hcl"),
811+
genfile("stacks/stack-1/subdir/b.hcl"),
812+
genfile("stacks/stack-1/subdir/dir/c.hcl"),
813+
genfile("stacks/stack-1/stack-1-a/e.hcl"),
814+
genfile("stacks/stack-1/stack-1-a/subdir/f.hcl"),
815+
genfile("stacks/stack-1/stack-1-a/subdir/dir/g.hcl"),
816+
genfile("stacks/stack-1/stack-1-b/h.hcl"),
817+
genfile("stacks/stack-2/d.hcl"),
818+
genfile("stacks/stack-3/x.hcl"),
819+
genfile("stacks/stack-3/subdir/y.hcl"),
820+
genfile("stacks/stack-3/subdir/dir/z.hcl"),
821+
},
822+
fromdir: "/stacks/stack-3",
823+
wantReport: generate.Report{
824+
Successes: []generate.Result{
825+
{
826+
Dir: project.NewPath("/stacks/stack-3"),
827+
Deleted: []string{
828+
"subdir/dir/z.hcl",
829+
"subdir/y.hcl",
830+
"x.hcl",
831+
},
832+
},
833+
},
834+
},
835+
want: []generatedFile{
836+
{
837+
dir: "/stacks/stack-1",
838+
files: map[string]fmt.Stringer{
839+
"a.hcl": Doc(),
840+
},
841+
},
842+
{
843+
dir: "/stacks/stack-1/stack-1-a",
844+
files: map[string]fmt.Stringer{
845+
"e.hcl": Doc(),
846+
},
847+
},
848+
{
849+
dir: "/stacks/stack-1/stack-1-a/subdir/dir",
850+
files: map[string]fmt.Stringer{
851+
"g.hcl": Doc(),
852+
},
853+
},
854+
{
855+
dir: "/stacks/stack-1/stack-1-a/subdir",
856+
files: map[string]fmt.Stringer{
857+
"f.hcl": Doc(),
858+
},
859+
},
860+
{
861+
dir: "/stacks/stack-1/stack-1-b",
862+
files: map[string]fmt.Stringer{
863+
"h.hcl": Doc(),
864+
},
865+
},
866+
{
867+
dir: "/stacks/stack-1/subdir",
868+
files: map[string]fmt.Stringer{
869+
"b.hcl": Doc(),
870+
},
871+
},
872+
{
873+
dir: "/stacks/stack-1/subdir/dir",
874+
files: map[string]fmt.Stringer{
875+
"c.hcl": Doc(),
876+
},
877+
},
878+
{
879+
dir: "/stacks/stack-2",
880+
files: map[string]fmt.Stringer{
881+
"d.hcl": Doc(),
882+
},
883+
},
884+
{
885+
dir: "/dir",
886+
files: map[string]fmt.Stringer{
887+
"orphan.hcl": Doc(),
888+
},
889+
},
890+
},
891+
},
801892
{
802893
name: "cleanup ignores dotdirs outside stacks",
803894
layout: []string{

0 commit comments

Comments
 (0)