Skip to content

Commit

Permalink
feat: consider untracked/uncommitted in the change detector.
Browse files Browse the repository at this point in the history
Signed-off-by: i4k <[email protected]>
  • Loading branch information
i4ki committed Oct 7, 2024
1 parent 6d7802f commit 0ae438d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 25 deletions.
93 changes: 69 additions & 24 deletions e2etests/core/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1882,27 +1882,32 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {
})
})

t.Run("ensure list is not affected by untracked check", func(t *testing.T) {
t.Run("ensure list **is** affected by untracked check (by default)", func(t *testing.T) {
tmcli := NewCLI(t, s.RootDir())

AssertRun(t, tmcli.Run("list", "--changed"))
AssertRunResult(t, tmcli.Run("list"), RunExpected{
Stdout: nljoin("stack"),
})
AssertRunResult(t, tmcli.Run("list", "--changed"), RunExpected{
Stdout: nljoin("stack"),
})
})

// disabling the check must work for both with and without --changed

t.Run("disable check using deprecated cmd args", func(t *testing.T) {
tmcli := NewCLI(t, s.RootDir(), testEnviron(t)...)
AssertRun(t, tmcli.Run(
AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"--changed",
"--disable-check-git-untracked",
HelperPath,
"cat",
mainTfFileName,
))
), RunExpected{
Stdout: mainTfContents,
})

AssertRunResult(t, tmcli.Run(
"run",
Expand All @@ -1918,14 +1923,17 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {

t.Run("disable check using --disable-safeguards=git-untracked cmd args", func(t *testing.T) {
tmcli := NewCLI(t, s.RootDir(), testEnviron(t)...)
AssertRun(t, tmcli.Run(
AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"--disable-safeguards=git-untracked",
"--changed",
HelperPath,
"cat",
mainTfFileName,
))
), RunExpected{
Stdout: mainTfContents,
})

AssertRunResult(t, tmcli.Run(
"--quiet",
Expand All @@ -1941,14 +1949,17 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {

t.Run("disable check using --disable-safeguards=all cmd args", func(t *testing.T) {
tmcli := NewCLI(t, s.RootDir(), testEnviron(t)...)
AssertRun(t, tmcli.Run(
AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"--disable-safeguards=all",
"--changed",
HelperPath,
"cat",
mainTfFileName,
))
), RunExpected{
Stdout: mainTfContents,
})

AssertRunResult(t, tmcli.Run(
"--quiet",
Expand All @@ -1964,14 +1975,17 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {

t.Run("disable check using -X", func(t *testing.T) {
tmcli := NewCLI(t, s.RootDir(), testEnviron(t)...)
AssertRun(t, tmcli.Run(
AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"-X",
"--changed",
HelperPath,
"cat",
mainTfFileName,
))
), RunExpected{
Stdout: mainTfContents,
})

AssertRunResult(t, tmcli.Run(
"--quiet",
Expand All @@ -1989,13 +2003,16 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {
tmcli := NewCLI(t, s.RootDir(), testEnviron(t)...)
tmcli.AppendEnv = append(tmcli.AppendEnv, "TM_DISABLE_CHECK_GIT_UNTRACKED=true")

AssertRun(t, tmcli.Run(
AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"--changed",
HelperPath,
"cat",
mainTfFileName,
))
), RunExpected{
Stdout: mainTfContents,
})

AssertRunResult(t, tmcli.Run(
"run",
Expand All @@ -2012,13 +2029,16 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {
tmcli := NewCLI(t, s.RootDir(), testEnviron(t)...)
tmcli.AppendEnv = append(tmcli.AppendEnv, "TM_DISABLE_CHECK_GIT_UNTRACKED=1")

AssertRun(t, tmcli.Run(
AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"--changed",
HelperPath,
"cat",
mainTfFileName,
))
), RunExpected{
Stdout: mainTfContents,
})

AssertRunResult(t, tmcli.Run(
"run",
Expand Down Expand Up @@ -2046,13 +2066,16 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {
defer s.RootEntry().RemoveFile(rootConfig)

tmcli := NewCLI(t, s.RootDir(), testEnviron(t)...)
AssertRun(t, tmcli.Run(
AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"--changed",
HelperPath,
"cat",
mainTfFileName,
))
), RunExpected{
Stdout: mainTfContents,
})

AssertRunResult(t, tmcli.Run(
"run",
Expand All @@ -2078,13 +2101,16 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {
defer s.RootEntry().RemoveFile(rootConfig)

tmcli := NewCLI(t, s.RootDir(), testEnviron(t)...)
AssertRun(t, tmcli.Run(
AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"--changed",
HelperPath,
"cat",
mainTfFileName,
))
), RunExpected{
Stdout: mainTfContents,
})

AssertRunResult(t, tmcli.Run(
"run",
Expand All @@ -2110,13 +2136,16 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {
defer s.RootEntry().RemoveFile(rootConfig)

tmcli := NewCLI(t, s.RootDir(), testEnviron(t)...)
AssertRun(t, tmcli.Run(
AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"--changed",
HelperPath,
"cat",
mainTfFileName,
))
), RunExpected{
Stdout: mainTfContents,
})

AssertRunResult(t, tmcli.Run(
"run",
Expand Down Expand Up @@ -2144,14 +2173,17 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {
defer s.RootEntry().RemoveFile(rootConfig)

tmcli := NewCLI(t, s.RootDir(), testEnviron(t)...)
AssertRun(t, tmcli.Run(
AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"--disable-safeguards=git-untracked",
"--changed",
HelperPath,
"cat",
mainTfFileName,
))
), RunExpected{
Stdout: mainTfContents,
})

AssertRunResult(t, tmcli.Run(
"run",
Expand All @@ -2178,19 +2210,23 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {
defer s.RootEntry().RemoveFile(rootConfig)

tmcli := NewCLI(t, s.RootDir(), testEnviron(t)...)
AssertRun(t, tmcli.Run(
AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"--disable-safeguards=git-untracked",
"--changed",
HelperPath,
"cat",
mainTfFileName,
))
), RunExpected{
Stdout: mainTfContents,
})

AssertRunResult(t, tmcli.Run(
"run",
"--disable-safeguards=git-untracked",
"--quiet",
"--",
HelperPath,
"cat",
mainTfFileName,
Expand All @@ -2216,7 +2252,10 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {
tmcli := NewCLI(t, s.RootDir(), testEnviron(t)...)
AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"--disable-safeguards=none",
"--changed",
"--",
HelperPath,
"cat",
mainTfFileName,
Expand All @@ -2227,6 +2266,7 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {

AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"--disable-safeguards=none",
HelperPath,
"cat",
Expand Down Expand Up @@ -2255,6 +2295,9 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {
tmcli.AppendEnv = append(tmcli.AppendEnv, "TM_DISABLE_SAFEGUARDS=none")
AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"--changed",
"--",
HelperPath,
"cat",
mainTfFileName,
Expand All @@ -2265,6 +2308,8 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) {

AssertRunResult(t, tmcli.Run(
"run",
"--quiet",
"--",
HelperPath,
"cat",
mainTfFileName,
Expand Down
14 changes: 13 additions & 1 deletion stack/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,16 @@ func (m *Manager) ListChanged(gitBaseRef string) (*Report, error) {
return nil, errors.E(errListChanged, err)
}

changedFiles, err := m.changedFiles(gitBaseRef)
_, err = m.changedFiles(gitBaseRef)
if err != nil {
return nil, errors.E(errListChanged, err)
}

m.appendChangedFiles(gitBaseRef, checks.UncommittedFiles...)
m.appendChangedFiles(gitBaseRef, checks.UntrackedFiles...)

changedFiles, _ := m.changedFiles(gitBaseRef)

if len(changedFiles) == 0 {
return &Report{
Checks: checks,
Expand Down Expand Up @@ -611,6 +616,13 @@ func (m *Manager) changedFiles(gitBaseRef string) ([]string, error) {
return changedFiles, nil
}

func (m *Manager) appendChangedFiles(gitBaseRef string, files ...string) {
if _, ok := m.cache.changedFiles[gitBaseRef]; !ok {
m.cache.changedFiles[gitBaseRef] = []string{}
}
m.cache.changedFiles[gitBaseRef] = append(m.cache.changedFiles[gitBaseRef], files...)
}

func (m *Manager) tgModuleChanged(
stack *config.Stack, tgMod *tg.Module, gitBaseRef string, stackSet map[project.Path]Entry, tgModuleMap map[project.Path]*tg.Module,
) (changed bool, why string, err error) {
Expand Down

0 comments on commit 0ae438d

Please sign in to comment.