Skip to content

Commit

Permalink
feat: add disable-check-git-remote (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
katcipis committed May 16, 2022
1 parent cd89f43 commit c71cf34
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 29 deletions.
30 changes: 15 additions & 15 deletions cmd/terramate/cli/cli.go
Expand Up @@ -79,11 +79,12 @@ type cliSpec struct {
} `cmd:"" help:"List stacks"`

Run struct {
DisableCheckGenCode bool `optional:"true" default:"false" help:"Disable outdated generated code check"`
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"`
Reverse bool `default:"false" help:"Reverse the order of execution"`
Command []string `arg:"" name:"cmd" passthrough:"" help:"Command to execute"`
DisableCheckGenCode bool `optional:"true" default:"false" help:"Disable outdated generated code check"`
DisableCheckGitRemote bool `optional:"true" default:"false" help:"Disable checking if local default branch is updated with remote"`
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"`
Reverse bool `default:"false" help:"Reverse the order of execution"`
Command []string `arg:"" name:"cmd" passthrough:"" help:"Command to execute"`
} `cmd:"" help:"Run command in the stacks"`

Generate struct{} `cmd:"" help:"Generate terraform code for stacks"`
Expand Down Expand Up @@ -266,8 +267,7 @@ func newCLI(args []string, stdin io.Reader, stdout, stderr io.Writer) *cli {
Msg("EvalSymlinks() failed")
}

logger.Trace().
Msg("Running in directory")
logger.Trace().Msg("Running in directory")

prj, foundRoot, err := lookupProject(wd)
if err != nil {
Expand Down Expand Up @@ -366,14 +366,12 @@ func (c *cli) checkGit() {
c.prj.baseRef = c.prj.defaultBaseRef()
}

if c.parsedArgs.Changed {
logger.Trace().Msg("Change detection is on: check git default branch was updated")
logger.Trace().Msg("check git default branch is updated")

if err := c.prj.checkLocalDefaultIsUpdated(); err != nil {
log.Fatal().
Err(err).
Msg("checking git default branch was updated.")
}
if err := c.prj.checkLocalDefaultIsUpdated(); err != nil {
log.Fatal().
Err(err).
Msg("checking git default branch was updated.")
}
}

Expand Down Expand Up @@ -821,7 +819,9 @@ func (c *cli) runOnStacks() {
Str("workingDir", c.wd()).
Logger()

c.checkGit()
if !c.parsedArgs.Run.DisableCheckGitRemote {
c.checkGit()
}

if len(c.parsedArgs.Run.Command) == 0 {
logger.Fatal().Msgf("run expects a cmd")
Expand Down
36 changes: 22 additions & 14 deletions cmd/terramate/e2etests/general_test.go
Expand Up @@ -281,23 +281,13 @@ func TestFailsOnChangeDetectionIfCurrentBranchIsMainAndItIsOutdated(t *testing.T
ts := newCLI(t, s.RootDir())

git := s.Git()

git.Add(".")
git.Commit("all")

// dance below makes makes local main branch behind origin/main by 1 commit.
// - a "temp" branch is created to record current commit.
// - go back to main and create 1 additional commit and push to origin/main.
// - switch to "temp" and delete "main" reference.
// - create "main" branch again based on temp.

git.CheckoutNew("temp")
git.Checkout("main")
stack.CreateFile("tempfile", "any content")
git.CommitAll("additional commit")
git.Push("main")
git.Checkout("temp")
git.DeleteBranch("main")
git.CheckoutNew("main")
setupLocalMainBranchBehindOriginMain(git, func() {
stack.CreateFile("tempfile", "any content")
})

wantRes := runExpected{
Status: 1,
Expand All @@ -307,6 +297,7 @@ func TestFailsOnChangeDetectionIfCurrentBranchIsMainAndItIsOutdated(t *testing.T
assertRunResult(t, ts.listChangedStacks(), wantRes)

cat := test.LookPath(t, "cat")

assertRunResult(t, ts.run(
"run",
"--changed",
Expand Down Expand Up @@ -474,3 +465,20 @@ func TestCommandsNotRequiringGitSafeguards(t *testing.T) {
})
}
}

func setupLocalMainBranchBehindOriginMain(git *sandbox.Git, changeFiles func()) {
// dance below makes local main branch behind origin/main by 1 commit.
// - a "temp" branch is created to record current commit.
// - go back to main and create 1 additional commit and push to origin/main.
// - switch to "temp" and delete "main" reference.
// - create "main" branch again based on temp.

git.CheckoutNew("temp")
git.Checkout("main")
changeFiles()
git.CommitAll("additional commit")
git.Push("main")
git.Checkout("temp")
git.DeleteBranch("main")
git.CheckoutNew("main")
}
101 changes: 101 additions & 0 deletions cmd/terramate/e2etests/run_test.go
Expand Up @@ -1181,3 +1181,104 @@ func TestRunContinueOnError(t *testing.T) {
Status: 1,
})
}

func TestRunDisableGitCheckRemote(t *testing.T) {
s := sandbox.New(t)

stack := s.CreateStack("stack")
fileContents := "# whatever"
someFile := stack.CreateFile("main.tf", fileContents)

ts := newCLI(t, s.RootDir())

git := s.Git()

git.Add(".")
git.Commit("all")

setupLocalMainBranchBehindOriginMain(git, func() {
stack.CreateFile("some-new-file", "testing")
})

cat := test.LookPath(t, "cat")
assertRunResult(t, ts.run(
"run",
"--disable-check-git-remote",
cat,
someFile.Path(),
), runExpected{Stdout: fileContents})
}

func TestRunFailsIfCurrentBranchIsMainAndItIsOutdated(t *testing.T) {
s := sandbox.New(t)

stack := s.CreateStack("stack-1")
mainTfFile := stack.CreateFile("main.tf", "# no code")

ts := newCLI(t, s.RootDir())

git := s.Git()

git.Add(".")
git.Commit("all")

setupLocalMainBranchBehindOriginMain(git, func() {
stack.CreateFile("tempfile", "any content")
})

wantRes := runExpected{
Status: 1,
StderrRegex: string(cli.ErrOutdatedLocalRev),
}

cat := test.LookPath(t, "cat")
// terramate run should also check if local default branch is updated with remote
assertRunResult(t, ts.run(
"run",
cat,
mainTfFile.Path(),
), wantRes)
}

func TestRunWithoutGitRemoteCheckWorksWithoutNetworking(t *testing.T) {
// Regression test to guarantee that all git checks
// are disabled and no git operation will be performed on this case.
// So running terramate run --disable-check-git-remote will
// not fail if there is no networking.
// Some people like to get some coding done on airplanes :-)
const (
fileContents = "body"
nonExistentGit = "http://non-existent/terramate.git"
)

s := sandbox.New(t)

stack := s.CreateStack("stack-1")
stackFile := stack.CreateFile("main.tf", fileContents)

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

git.SetRemoteURL("origin", nonExistentGit)

tm := newCLI(t, s.RootDir())

cat := test.LookPath(t, "cat")
assertRunResult(t, tm.run(
"run",
cat,
stackFile.Path(),
), runExpected{
Status: 1,
StderrRegex: "Could not resolve host: non-existent",
})
assertRunResult(t, tm.run(
"run",
"--disable-check-git-remote",
cat,
stackFile.Path(),
), runExpected{
Stdout: fileContents,
})
}

0 comments on commit c71cf34

Please sign in to comment.