Skip to content

Commit 581110b

Browse files
aledbfclaude
andcommitted
test(devtools): keep the git tests out of the repository they run in
gitRepo inherited GIT_DIR and GIT_WORK_TREE, which git rebase --exec exports, so TestDailyChangedCommand and TestReleaseResolve committed their fixtures into the repository under rebase. gitRepo now clears the variables that locate a repository. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1a0060d commit 581110b

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

‎internal/devtools/command_test.go‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,38 @@ func assertMode(t *testing.T, path string, want os.FileMode) {
7676
}
7777

7878
// gitRepo makes an empty repository in a new directory and enters it, with no
79-
// user or system configuration to sign or hook its commits.
79+
// user or system configuration to sign or hook its commits. The variables git
80+
// exports to hooks and `rebase --exec` are cleared, so the tests never write
81+
// to the repository they run in.
8082
func gitRepo(t *testing.T) {
8183
t.Helper()
8284
t.Chdir(t.TempDir())
85+
for _, k := range []string{"GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_COMMON_DIR", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES", "GIT_PREFIX"} {
86+
t.Setenv(k, "")
87+
os.Unsetenv(k)
88+
}
8389
t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull)
8490
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
8591
git(t, "init", "-q")
8692
}
8793

94+
// A repository named by GIT_DIR, as `git rebase --exec` names the one it
95+
// runs in, is left alone by gitRepo and the commits made after it.
96+
func TestGitRepoLeavesTheOuterRepositoryAlone(t *testing.T) {
97+
outer := t.TempDir()
98+
t.Chdir(outer)
99+
t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull)
100+
git(t, "init", "-q")
101+
t.Setenv("GIT_DIR", filepath.Join(outer, ".git"))
102+
t.Setenv("GIT_WORK_TREE", outer)
103+
104+
gitRepo(t)
105+
commit(t, "a.txt", 1_700_000_000)
106+
if out, err := exec.Command("git", "-C", outer, "rev-list", "--all").Output(); err != nil || len(out) != 0 {
107+
t.Errorf("outer repository commits = %q, err = %v, want none", out, err)
108+
}
109+
}
110+
88111
func git(t *testing.T, args ...string) string {
89112
t.Helper()
90113
cmd := exec.Command("git", args...)

0 commit comments

Comments
 (0)