Skip to content

Commit

Permalink
feat: add --disable-check-git-untracked flag to disable untracked saf…
Browse files Browse the repository at this point in the history
…eguard. (#241)
  • Loading branch information
i4ki committed Feb 28, 2022
1 parent da2c37a commit c225175
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
4 changes: 3 additions & 1 deletion cmd/terramate/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type cliSpec struct {
LogLevel string `optional:"true" default:"info" enum:"trace,debug,info,warn,error,fatal" help:"Log level to use: 'trace', 'debug', 'info', 'warn', 'error', or 'fatal'"`
LogFmt string `optional:"true" default:"console" enum:"console,text,json" help:"Log format to use: 'console', 'text', or 'json'."`

DisableCheckGitUntracked bool `optional:"true" default:"false" help:"disable git check for untracked files."`

Run struct {
ContinueOnError bool `default:"false" help:"continue executing in other stacks in case of error."`
DryRun bool `default:"false" help:"plan the execution but do not execute it"`
Expand Down Expand Up @@ -427,7 +429,7 @@ func (c *cli) gitSafeguards(checks terramate.RepoChecks, shouldAbort bool) {
Str("action", "gitSafeguards()").
Logger()

if len(checks.UntrackedFiles) > 0 {
if !c.parsedArgs.DisableCheckGitUntracked && len(checks.UntrackedFiles) > 0 {
if shouldAbort {
logger.Fatal().
Strs("files", checks.UntrackedFiles).
Expand Down
67 changes: 60 additions & 7 deletions cmd/terramate/e2etests/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ func TestRunOrderAllChangedStacksExecuted(t *testing.T) {
), runExpected{Stdout: wantRun})
}

func TestRunFailIfDirtyRepo(t *testing.T) {
func TestRunFailIfGitSafeguardUntracked(t *testing.T) {
const (
mainTfFileName = "main.tf"
mainTfContents = "# some code"
Expand All @@ -757,11 +757,12 @@ func TestRunFailIfDirtyRepo(t *testing.T) {
git.Push("main")
git.CheckoutNew("change-stack")

untracked := stack.CreateFile("untracked-file.txt", `# something`)
stack.CreateFile("untracked-file.txt", `# something`)

cli := newCLI(t, s.RootDir())
cat := test.LookPath(t, "cat")

// check untracked with --changed
assertRunResult(t, cli.run(
"run",
"--changed",
Expand All @@ -772,6 +773,7 @@ func TestRunFailIfDirtyRepo(t *testing.T) {
StderrRegex: "repository has untracked files",
})

// check untracked *without* --changed
assertRunResult(t, cli.run(
"run",
cat,
Expand All @@ -781,21 +783,72 @@ func TestRunFailIfDirtyRepo(t *testing.T) {
StderrRegex: "repository has untracked files",
})

git.Add(untracked.Path())
git.Commit("commit untracked")
// disabling the check must work for both with and without --changed
assertRun(t, cli.run(
"run",
"--changed",
"--disable-check-git-untracked",
cat,
mainTfFileName,
))

assertRunResult(t, cli.run(
"run",
"--disable-check-git-untracked",
cat,
mainTfFileName,
), runExpected{
Stdout: mainTfContents,
})
}

func TestRunFailIfGitSafeguardUncommitted(t *testing.T) {
const (
mainTfFileName = "main.tf"
mainTfInitialContents = "# some code"
mainTfAlteredContents = "# other code"
)

s := sandbox.New(t)

stack := s.CreateStack("stack")
file := stack.CreateFile(mainTfFileName, mainTfInitialContents)

git := s.Git()
git.CommitAll("first commit")

cli := newCLI(t, s.RootDir())
cat := test.LookPath(t, "cat")

// everything commited, repo is clean
assertRunResult(t, cli.run(
"run",
cat,
mainTfFileName,
), runExpected{Stdout: mainTfInitialContents})

assertRunResult(t, cli.run(
"run",
"--changed",
cat,
mainTfFileName,
), runExpected{Stdout: mainTfContents})
), runExpected{Stdout: mainTfInitialContents})

// make it uncommitted
file.Write(mainTfAlteredContents)

assertRunResult(t, cli.run(
"run",
cat,
mainTfFileName,
), runExpected{
Status: defaultErrExitStatus,
StderrRegex: "repository has uncommitted files",
})

// change file, no commit
untracked.Write("# changed")
assertRunResult(t, cli.run(
"run",
"--changed",
cat,
mainTfFileName,
), runExpected{
Expand Down

0 comments on commit c225175

Please sign in to comment.