Skip to content

Commit 9cf32ba

Browse files
committed
Ensure git tag tests and others create test repos in tmpdir (go-gitea#18447)
Backport go-gitea#18447 * 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 go-gitea#18444 * Change log root for integration tests to use the REPO_TEST_DIR There is a potential race in the drone integration tests whereby test-mysql etc will start writing to log files causing make test-check fail. Fix go-gitea#18077 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 69a158d commit 9cf32ba

8 files changed

+213
-67
lines changed

integrations/mssql.ini.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-mssql/data/sessions
8383

8484
[log]
8585
MODE = test,file
86-
ROOT_PATH = mssql-log
86+
ROOT_PATH = {{REPO_TEST_DIR}}mssql-log
8787
ROUTER = ,
8888
XORM = file
8989
ENABLE_SSH_LOG = true

integrations/mysql.ini.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-mysql/data/sessions
101101

102102
[log]
103103
MODE = test,file
104-
ROOT_PATH = mysql-log
104+
ROOT_PATH = {{REPO_TEST_DIR}}mysql-log
105105
ROUTER = ,
106106
XORM = file
107107
ENABLE_SSH_LOG = true

integrations/mysql8.ini.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-mysql8/data/sessions
8080

8181
[log]
8282
MODE = test,file
83-
ROOT_PATH = mysql8-log
83+
ROOT_PATH = {{REPO_TEST_DIR}}mysql8-log
8484
ROUTER = ,
8585
XORM = file
8686
ENABLE_SSH_LOG = true

integrations/pgsql.ini.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-pgsql/data/sessions
8484

8585
[log]
8686
MODE = test,file
87-
ROOT_PATH = pgsql-log
87+
ROOT_PATH = {{REPO_TEST_DIR}}pgsql-log
8888
ROUTER = ,
8989
XORM = file
9090
ENABLE_SSH_LOG = true

integrations/sqlite.ini.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-sqlite/data/sessions
7979

8080
[log]
8181
MODE = test,file
82-
ROOT_PATH = sqlite-log
82+
ROOT_PATH = {{REPO_TEST_DIR}}sqlite-log
8383
ROUTER = ,
8484
XORM = file
8585
ENABLE_SSH_LOG = true

modules/git/commit_info_test.go

+60-25
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,25 @@ import (
1616
"github.com/stretchr/testify/assert"
1717
)
1818

19-
const testReposDir = "tests/repos/"
20-
const benchmarkReposDir = "benchmark/repos/"
19+
const (
20+
testReposDir = "tests/repos/"
21+
)
2122

22-
func cloneRepo(url, dir, name string) (string, error) {
23-
repoDir := filepath.Join(dir, name)
24-
if _, err := os.Stat(repoDir); err == nil {
25-
return repoDir, nil
23+
func cloneRepo(url, name string) (string, error) {
24+
repoDir, err := os.MkdirTemp("", name)
25+
if err != nil {
26+
return "", err
2627
}
27-
return repoDir, Clone(url, repoDir, CloneRepoOptions{
28+
if err := Clone(url, repoDir, CloneRepoOptions{
2829
Mirror: false,
2930
Bare: false,
3031
Quiet: true,
3132
Timeout: 5 * time.Minute,
32-
})
33+
}); err != nil {
34+
_ = util.RemoveAll(repoDir)
35+
return "", err
36+
}
37+
return repoDir, nil
3338
}
3439

3540
func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
@@ -59,20 +64,35 @@ func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
5964
}
6065
for _, testCase := range testCases {
6166
commit, err := repo1.GetCommit(testCase.CommitID)
62-
assert.NoError(t, err)
67+
if err != nil {
68+
assert.NoError(t, err, "Unable to get commit: %s from testcase due to error: %v", testCase.CommitID, err)
69+
// no point trying to do anything else for this test.
70+
continue
71+
}
6372
assert.NotNil(t, commit)
6473
assert.NotNil(t, commit.Tree)
6574
assert.NotNil(t, commit.Tree.repo)
6675

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

71-
assert.NoError(t, err)
7286
entries, err := tree.ListEntries()
73-
assert.NoError(t, err)
74-
commitsInfo, treeCommit, err := entries.GetCommitsInfo(context.Background(), commit, testCase.Path, nil)
75-
assert.NoError(t, err)
87+
if err != nil {
88+
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)
89+
// no point trying to do anything else for this test.
90+
continue
91+
}
92+
93+
// FIXME: Context.TODO() - if graceful has started we should use its Shutdown context otherwise use install signals in TestMain.
94+
commitsInfo, treeCommit, err := entries.GetCommitsInfo(context.TODO(), commit, testCase.Path, nil)
95+
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)
7696
if err != nil {
7797
t.FailNow()
7898
}
@@ -98,40 +118,52 @@ func TestEntries_GetCommitsInfo(t *testing.T) {
98118

99119
testGetCommitsInfo(t, bareRepo1)
100120

101-
clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestEntries_GetCommitsInfo")
102-
assert.NoError(t, err)
121+
clonedPath, err := cloneRepo(bareRepo1Path, "repo1_TestEntries_GetCommitsInfo")
122+
if err != nil {
123+
assert.NoError(t, err)
124+
}
103125
defer util.RemoveAll(clonedPath)
104126
clonedRepo1, err := OpenRepository(clonedPath)
105-
assert.NoError(t, err)
127+
if err != nil {
128+
assert.NoError(t, err)
129+
}
106130
defer clonedRepo1.Close()
107131

108132
testGetCommitsInfo(t, clonedRepo1)
109133
}
110134

111135
func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
112-
benchmarks := []struct {
136+
type benchmarkType struct {
113137
url string
114138
name string
115-
}{
139+
}
140+
141+
benchmarks := []benchmarkType{
116142
{url: "https://github.com/go-gitea/gitea.git", name: "gitea"},
117143
{url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"},
118144
{url: "https://github.com/moby/moby.git", name: "moby"},
119145
{url: "https://github.com/golang/go.git", name: "go"},
120146
{url: "https://github.com/torvalds/linux.git", name: "linux"},
121147
}
122-
for _, benchmark := range benchmarks {
148+
149+
doBenchmark := func(benchmark benchmarkType) {
123150
var commit *Commit
124151
var entries Entries
125152
var repo *Repository
126-
if repoPath, err := cloneRepo(benchmark.url, benchmarkReposDir, benchmark.name); err != nil {
153+
repoPath, err := cloneRepo(benchmark.url, benchmark.name)
154+
if err != nil {
127155
b.Fatal(err)
128-
} else if repo, err = OpenRepository(repoPath); err != nil {
156+
}
157+
defer util.RemoveAll(repoPath)
158+
159+
if repo, err = OpenRepository(repoPath); err != nil {
129160
b.Fatal(err)
130-
} else if commit, err = repo.GetBranchCommit("master"); err != nil {
131-
repo.Close()
161+
}
162+
defer repo.Close()
163+
164+
if commit, err = repo.GetBranchCommit("master"); err != nil {
132165
b.Fatal(err)
133166
} else if entries, err = commit.Tree.ListEntries(); err != nil {
134-
repo.Close()
135167
b.Fatal(err)
136168
}
137169
entries.Sort()
@@ -144,6 +176,9 @@ func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
144176
}
145177
}
146178
})
147-
repo.Close()
179+
}
180+
181+
for _, benchmark := range benchmarks {
182+
doBenchmark(benchmark)
148183
}
149184
}

modules/git/repo_compare_test.go

+61-15
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)