Skip to content

Commit 6c8f664

Browse files
committed
Ensure git tag tests and other create test repos in tmpdir
There are a few places where tests appear to reuse testing repos which causes random CI failures. This PR simply changes these tests to ensure that cloning always happens into new temporary directories. Fix #18444 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 726715f commit 6c8f664

File tree

3 files changed

+205
-60
lines changed

3 files changed

+205
-60
lines changed

modules/git/commit_info_test.go

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@ const (
2121
benchmarkReposDir = "benchmark/repos/"
2222
)
2323

24-
func cloneRepo(url, dir, name string) (string, error) {
25-
repoDir := filepath.Join(dir, name)
26-
if _, err := os.Stat(repoDir); err == nil {
27-
return repoDir, nil
24+
func cloneRepo(url, name string) (string, error) {
25+
repoDir, err := os.MkdirTemp("", name)
26+
if err != nil {
27+
return "", err
2828
}
29-
return repoDir, Clone(DefaultContext, url, repoDir, CloneRepoOptions{
29+
if err := Clone(DefaultContext, url, repoDir, CloneRepoOptions{
3030
Mirror: false,
3131
Bare: false,
3232
Quiet: true,
3333
Timeout: 5 * time.Minute,
34-
})
34+
}); err != nil {
35+
_ = util.RemoveAll(repoDir)
36+
return "", err
37+
}
38+
return repoDir, nil
3539
}
3640

3741
func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
@@ -61,20 +65,35 @@ func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
6165
}
6266
for _, testCase := range testCases {
6367
commit, err := repo1.GetCommit(testCase.CommitID)
64-
assert.NoError(t, err)
68+
if err != nil {
69+
assert.NoError(t, err, "Unable to get commit: %s from testcase due to error: %v", testCase.CommitID, err)
70+
// no point trying to do anything else for this test.
71+
continue
72+
}
6573
assert.NotNil(t, commit)
6674
assert.NotNil(t, commit.Tree)
6775
assert.NotNil(t, commit.Tree.repo)
6876

6977
tree, err := commit.Tree.SubTree(testCase.Path)
78+
if err != nil {
79+
assert.NoError(t, err, "Unable to get subtree: %s of commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err)
80+
// no point trying to do anything else for this test.
81+
continue
82+
}
83+
7084
assert.NotNil(t, tree, "tree is nil for testCase CommitID %s in Path %s", testCase.CommitID, testCase.Path)
7185
assert.NotNil(t, tree.repo, "repo is nil for testCase CommitID %s in Path %s", testCase.CommitID, testCase.Path)
7286

73-
assert.NoError(t, err)
7487
entries, err := tree.ListEntries()
75-
assert.NoError(t, err)
76-
commitsInfo, treeCommit, err := entries.GetCommitsInfo(context.Background(), commit, testCase.Path, nil)
77-
assert.NoError(t, err)
88+
if err != nil {
89+
assert.NoError(t, err, "Unable to get entries of subtree: %s in commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err)
90+
// no point trying to do anything else for this test.
91+
continue
92+
}
93+
94+
// FIXME: Context.TODO() - if graceful has started we should use its Shutdown context otherwise use install signals in TestMain.
95+
commitsInfo, treeCommit, err := entries.GetCommitsInfo(context.TODO(), commit, testCase.Path, nil)
96+
assert.NoError(t, err, "Unable to get commit information for entries of subtree: %s in commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err)
7897
if err != nil {
7998
t.FailNow()
8099
}
@@ -100,40 +119,52 @@ func TestEntries_GetCommitsInfo(t *testing.T) {
100119

101120
testGetCommitsInfo(t, bareRepo1)
102121

103-
clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestEntries_GetCommitsInfo")
104-
assert.NoError(t, err)
122+
clonedPath, err := cloneRepo(bareRepo1Path, "repo1_TestEntries_GetCommitsInfo")
123+
if err != nil {
124+
assert.NoError(t, err)
125+
}
105126
defer util.RemoveAll(clonedPath)
106127
clonedRepo1, err := OpenRepository(clonedPath)
107-
assert.NoError(t, err)
128+
if err != nil {
129+
assert.NoError(t, err)
130+
}
108131
defer clonedRepo1.Close()
109132

110133
testGetCommitsInfo(t, clonedRepo1)
111134
}
112135

113136
func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
114-
benchmarks := []struct {
137+
type benchmarkType struct {
115138
url string
116139
name string
117-
}{
140+
}
141+
142+
benchmarks := []benchmarkType{
118143
{url: "https://github.com/go-gitea/gitea.git", name: "gitea"},
119144
{url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"},
120145
{url: "https://github.com/moby/moby.git", name: "moby"},
121146
{url: "https://github.com/golang/go.git", name: "go"},
122147
{url: "https://github.com/torvalds/linux.git", name: "linux"},
123148
}
124-
for _, benchmark := range benchmarks {
149+
150+
doBenchmark := func(benchmark benchmarkType) {
125151
var commit *Commit
126152
var entries Entries
127153
var repo *Repository
128-
if repoPath, err := cloneRepo(benchmark.url, benchmarkReposDir, benchmark.name); err != nil {
154+
repoPath, err := cloneRepo(benchmark.url, benchmark.name)
155+
if err != nil {
129156
b.Fatal(err)
130-
} else if repo, err = OpenRepository(repoPath); err != nil {
157+
}
158+
defer util.RemoveAll(repoPath)
159+
160+
if repo, err = OpenRepository(repoPath); err != nil {
131161
b.Fatal(err)
132-
} else if commit, err = repo.GetBranchCommit("master"); err != nil {
133-
repo.Close()
162+
}
163+
defer repo.Close()
164+
165+
if commit, err = repo.GetBranchCommit("master"); err != nil {
134166
b.Fatal(err)
135167
} else if entries, err = commit.Tree.ListEntries(); err != nil {
136-
repo.Close()
137168
b.Fatal(err)
138169
}
139170
entries.Sort()
@@ -146,6 +177,9 @@ func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
146177
}
147178
}
148179
})
149-
repo.Close()
180+
}
181+
182+
for _, benchmark := range benchmarks {
183+
doBenchmark(benchmark)
150184
}
151185
}

modules/git/repo_compare_test.go

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,33 @@ import (
1717

1818
func TestGetFormatPatch(t *testing.T) {
1919
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
20-
clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestGetFormatPatch")
20+
clonedPath, err := cloneRepo(bareRepo1Path, "repo1_TestGetFormatPatch")
21+
if err != nil {
22+
assert.NoError(t, err)
23+
return
24+
}
2125
defer util.RemoveAll(clonedPath)
22-
assert.NoError(t, err)
26+
2327
repo, err := OpenRepository(clonedPath)
28+
if err != nil {
29+
assert.NoError(t, err)
30+
return
31+
}
2432
defer repo.Close()
25-
assert.NoError(t, err)
33+
2634
rd := &bytes.Buffer{}
2735
err = repo.GetPatch("8d92fc95^", "8d92fc95", rd)
28-
assert.NoError(t, err)
36+
if err != nil {
37+
assert.NoError(t, err)
38+
return
39+
}
40+
2941
patchb, err := io.ReadAll(rd)
30-
assert.NoError(t, err)
42+
if err != nil {
43+
assert.NoError(t, err)
44+
return
45+
}
46+
3147
patch := string(patchb)
3248
assert.Regexp(t, "^From 8d92fc95", patch)
3349
assert.Contains(t, patch, "Subject: [PATCH] Add file2.txt")
@@ -37,17 +53,25 @@ func TestReadPatch(t *testing.T) {
3753
// Ensure we can read the patch files
3854
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
3955
repo, err := OpenRepository(bareRepo1Path)
56+
if err != nil {
57+
assert.NoError(t, err)
58+
return
59+
}
4060
defer repo.Close()
41-
assert.NoError(t, err)
4261
// This patch doesn't exist
4362
noFile, err := repo.ReadPatchCommit(0)
4463
assert.Error(t, err)
64+
4565
// This patch is an empty one (sometimes it's a 404)
4666
noCommit, err := repo.ReadPatchCommit(1)
4767
assert.Error(t, err)
68+
4869
// This patch is legit and should return a commit
4970
oldCommit, err := repo.ReadPatchCommit(2)
50-
assert.NoError(t, err)
71+
if err != nil {
72+
assert.NoError(t, err)
73+
return
74+
}
5175

5276
assert.Empty(t, noFile)
5377
assert.Empty(t, noCommit)
@@ -58,23 +82,45 @@ func TestReadPatch(t *testing.T) {
5882
func TestReadWritePullHead(t *testing.T) {
5983
// Ensure we can write SHA1 head corresponding to PR and open them
6084
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
61-
repo, err := OpenRepository(bareRepo1Path)
62-
assert.NoError(t, err)
85+
86+
// As we are writing we should clone the repository first
87+
clonedPath, err := cloneRepo(bareRepo1Path, "TestReadWritePullHead")
88+
if err != nil {
89+
assert.NoError(t, err)
90+
return
91+
}
92+
defer util.RemoveAll(clonedPath)
93+
94+
repo, err := OpenRepository(clonedPath)
95+
if err != nil {
96+
assert.NoError(t, err)
97+
return
98+
}
6399
defer repo.Close()
100+
64101
// Try to open non-existing Pull
65102
_, err = repo.GetRefCommitID(PullPrefix + "0/head")
66103
assert.Error(t, err)
104+
67105
// Write a fake sha1 with only 40 zeros
68106
newCommit := "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"
69107
err = repo.SetReference(PullPrefix+"1/head", newCommit)
70-
assert.NoError(t, err)
71-
// Remove file after the test
72-
defer func() {
73-
_ = repo.RemoveReference(PullPrefix + "1/head")
74-
}()
108+
if err != nil {
109+
assert.NoError(t, err)
110+
return
111+
}
112+
75113
// Read the file created
76114
headContents, err := repo.GetRefCommitID(PullPrefix + "1/head")
77-
assert.NoError(t, err)
115+
if err != nil {
116+
assert.NoError(t, err)
117+
return
118+
}
119+
78120
assert.Len(t, string(headContents), 40)
79121
assert.True(t, string(headContents) == newCommit)
122+
123+
// Remove file after the test
124+
err = repo.RemoveReference(PullPrefix + "1/head")
125+
assert.NoError(t, err)
80126
}

0 commit comments

Comments
 (0)