From 0ae438d88680126788dc42ecb04fc9a04470cf8b Mon Sep 17 00:00:00 2001 From: i4k Date: Mon, 7 Oct 2024 13:04:17 +0100 Subject: [PATCH] feat: consider untracked/uncommitted in the change detector. Signed-off-by: i4k --- e2etests/core/run_test.go | 93 +++++++++++++++++++++++++++++---------- stack/manager.go | 14 +++++- 2 files changed, 82 insertions(+), 25 deletions(-) diff --git a/e2etests/core/run_test.go b/e2etests/core/run_test.go index 507451e87e..a06177f27c 100644 --- a/e2etests/core/run_test.go +++ b/e2etests/core/run_test.go @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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, @@ -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, @@ -2227,6 +2266,7 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) { AssertRunResult(t, tmcli.Run( "run", + "--quiet", "--disable-safeguards=none", HelperPath, "cat", @@ -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, @@ -2265,6 +2308,8 @@ func TestRunFailIfGitSafeguardUntracked(t *testing.T) { AssertRunResult(t, tmcli.Run( "run", + "--quiet", + "--", HelperPath, "cat", mainTfFileName, diff --git a/stack/manager.go b/stack/manager.go index c0c1bb0bc3..0f477e51a9 100644 --- a/stack/manager.go +++ b/stack/manager.go @@ -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, @@ -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) {