Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: terramate experimental clone --no-generate #2036

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:

## v0.11.7

### Added

- Add `--no-generate` to `terramate experimental clone` to skip the generation phase after the clone.

### Fixed

- Fix an edge case crash in `terramate list` when using the `--status` flag.
Expand Down
7 changes: 6 additions & 1 deletion cmd/terramate/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ type cliSpec struct {
SrcDir string `arg:"" name:"srcdir" predictor:"file" help:"Path of the stack being cloned."`
DestDir string `arg:"" name:"destdir" predictor:"file" help:"Path of the new stack."`
SkipChildStacks bool `default:"false" help:"Do not clone nested child stacks."`
NoGenerate bool `help:"Do not run code generation after cloning the stacks."`
} `cmd:"" help:"Clone a stack."`

Trigger struct {
Expand Down Expand Up @@ -1262,8 +1263,12 @@ func (c *cli) cloneStack() {
}

c.output.MsgStdOut("Cloned %d stack(s) from %s to %s with success", n, srcdir, destdir)
c.output.MsgStdOut("Generating code on the new cloned stack(s)")

if c.parsedArgs.Experimental.Clone.NoGenerate {
return
}

c.output.MsgStdOut("Generating code on the new cloned stack(s)")
c.generate()
}

Expand Down
107 changes: 75 additions & 32 deletions e2etests/core/exp_clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,48 +51,91 @@ generate_hcl "test2.hcl" {
}
`
)
s := sandbox.NoGit(t, true)
s.BuildTree([]string{"d:stack"})

stackEntry := s.DirEntry("stack")
stackEntry.CreateFile(stackCfgFilename, fmt.Sprintf(stackCfgTemplate,
stackID, stackName, stackDesc))
setupTest := func(t *testing.T) *sandbox.S {
s := sandbox.NoGit(t, true)
s.BuildTree([]string{"d:stack"})

tmcli := NewCLI(t, s.RootDir())
res := tmcli.Run("experimental", "clone", srcStack, destStack)
stackEntry := s.DirEntry("stack")
stackEntry.CreateFile(stackCfgFilename, fmt.Sprintf(stackCfgTemplate,
stackID, stackName, stackDesc))

AssertRunResult(t, res, RunExpected{
StdoutRegex: cloneSuccessMsg(1, srcStack, destStack),
})

destdir := filepath.Join(s.RootDir(), destStack)
cfg := test.ParseTerramateConfig(t, destdir)
if cfg.Stack == nil {
t.Fatalf("cloned stack has no stack block: %v", cfg)
}
if cfg.Stack.ID == "" {
t.Fatalf("cloned stack has no ID: %v", cfg.Stack)
}
if cfg.Stack.ID == stackID {
t.Fatalf("want cloned stack to have different ID, got %s == %s", cfg.Stack.ID, stackID)
return &s
}

assert.EqualStrings(t, stackName, cfg.Stack.Name)
assert.EqualStrings(t, stackDesc, cfg.Stack.Description)
doTest := func(t *testing.T, generationEnabled bool) {
s := setupTest(t)
tmcli := NewCLI(t, s.RootDir())

args := []string{"experimental", "clone"}
if !generationEnabled {
args = append(args, "--no-generate")
}
args = append(args, srcStack, destStack)
res := tmcli.Run(args...)

AssertRunResult(t, res, RunExpected{
StdoutRegex: cloneSuccessMsg(1, srcStack, destStack),
})

destdir := filepath.Join(s.RootDir(), destStack)
cfg := test.ParseTerramateConfig(t, destdir)
if cfg.Stack == nil {
t.Fatalf("cloned stack has no stack block: %v", cfg)
}
if cfg.Stack.ID == "" {
t.Fatalf("cloned stack has no ID: %v", cfg.Stack)
}
if cfg.Stack.ID == stackID {
t.Fatalf("want cloned stack to have different ID, got %s == %s", cfg.Stack.ID, stackID)
}

want := fmt.Sprintf(stackCfgTemplate, cfg.Stack.ID, stackName, stackDesc)
assert.EqualStrings(t, stackName, cfg.Stack.Name)
assert.EqualStrings(t, stackDesc, cfg.Stack.Description)

clonedStackEntry := s.DirEntry(destStack)
got := string(clonedStackEntry.ReadFile(stackCfgFilename))
want := fmt.Sprintf(stackCfgTemplate, cfg.Stack.ID, stackName, stackDesc)

assert.EqualStrings(t, want, got, "want:\n%s\ngot:\n%s\n", want, got)
clonedStackEntry := s.DirEntry(destStack)
got := string(clonedStackEntry.ReadFile(stackCfgFilename))

// Checking that code was also generated already
genHCL := string(clonedStackEntry.ReadFile("test.hcl"))
genHCL2 := string(clonedStackEntry.ReadFile("test2.hcl"))
assert.EqualStrings(t, want, got, "want:\n%s\ngot:\n%s\n", want, got)

test.AssertGenCodeEquals(t, genHCL, `a = "literal"`)
test.AssertGenCodeEquals(t, genHCL2, `b = null`)
if generationEnabled {
// Checking that code was also generated already
genHCL := string(clonedStackEntry.ReadFile("test.hcl"))
genHCL2 := string(clonedStackEntry.ReadFile("test2.hcl"))

test.AssertGenCodeEquals(t, genHCL, `a = "literal"`)
test.AssertGenCodeEquals(t, genHCL2, `b = null`)

AssertRunResult(t,
tmcli.Run("run", "--quiet", HelperPath, "echo", "outdated safeguard must not trigger"),
RunExpected{
StdoutRegex: "outdated safeguard must not trigger\n",
},
)
} else {
AssertRunResult(t,
tmcli.Run("run", "--quiet", HelperPath, "echo", "outdated safeguard must trigger"),
RunExpected{
Status: 1,
StderrRegex: "outdated code found",
},
)
}
}

t.Run("clone stack with generate", func(t *testing.T) {
t.Parallel()

doTest(t, true)
})

t.Run("clone stack without generate", func(t *testing.T) {
t.Parallel()

doTest(t, false)
})
}

func TestCloneStacksWithChildren(t *testing.T) {
Expand Down
Loading