Skip to content

Commit 673cf6a

Browse files
lunnywxiaoguang
andauthored
make writing main test easier (#27270)
This PR removed `unittest.MainTest` the second parameter `TestOptions.GiteaRoot`. Now it detects the root directory by current working directory. --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent e74a3b1 commit 673cf6a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+81
-222
lines changed

cmd/main_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ import (
2020
)
2121

2222
func TestMain(m *testing.M) {
23-
unittest.MainTest(m, &unittest.TestOptions{
24-
GiteaRootPath: "..",
25-
})
23+
unittest.MainTest(m)
2624
}
2725

2826
func makePathOutput(workPath, customPath, customConf string) string {

models/activities/main_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package activities_test
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
@@ -14,7 +13,5 @@ import (
1413
)
1514

1615
func TestMain(m *testing.M) {
17-
unittest.MainTest(m, &unittest.TestOptions{
18-
GiteaRootPath: filepath.Join("..", ".."),
19-
})
16+
unittest.MainTest(m)
2017
}

models/asymkey/main_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
package asymkey
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
1110
)
1211

1312
func TestMain(m *testing.M) {
1413
unittest.MainTest(m, &unittest.TestOptions{
15-
GiteaRootPath: filepath.Join("..", ".."),
1614
FixtureFiles: []string{
1715
"gpg_key.yml",
1816
"public_key.yml",

models/auth/main_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package auth_test
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
@@ -17,7 +16,5 @@ import (
1716
)
1817

1918
func TestMain(m *testing.M) {
20-
unittest.MainTest(m, &unittest.TestOptions{
21-
GiteaRootPath: filepath.Join("..", ".."),
22-
})
19+
unittest.MainTest(m)
2320
}

models/avatars/main_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package avatars_test
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
@@ -15,7 +14,5 @@ import (
1514
)
1615

1716
func TestMain(m *testing.M) {
18-
unittest.MainTest(m, &unittest.TestOptions{
19-
GiteaRootPath: filepath.Join("..", ".."),
20-
})
17+
unittest.MainTest(m)
2118
}

models/db/main_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package db_test
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
@@ -14,7 +13,5 @@ import (
1413
)
1514

1615
func TestMain(m *testing.M) {
17-
unittest.MainTest(m, &unittest.TestOptions{
18-
GiteaRootPath: filepath.Join("..", ".."),
19-
})
16+
unittest.MainTest(m)
2017
}

models/db/paginator/main_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
package paginator
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
1110
)
1211

1312
func TestMain(m *testing.M) {
14-
unittest.MainTest(m, &unittest.TestOptions{
15-
GiteaRootPath: filepath.Join("..", "..", ".."),
16-
})
13+
unittest.MainTest(m)
1714
}

models/dbfs/main_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
package dbfs
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
1110
)
1211

1312
func TestMain(m *testing.M) {
14-
unittest.MainTest(m, &unittest.TestOptions{
15-
GiteaRootPath: filepath.Join("..", ".."),
16-
})
13+
unittest.MainTest(m)
1714
}

models/git/main_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package git_test
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
@@ -15,7 +14,5 @@ import (
1514
)
1615

1716
func TestMain(m *testing.M) {
18-
unittest.MainTest(m, &unittest.TestOptions{
19-
GiteaRootPath: filepath.Join("..", ".."),
20-
})
17+
unittest.MainTest(m)
2118
}

models/issues/main_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package issues_test
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
issues_model "code.gitea.io/gitea/models/issues"
@@ -30,7 +29,5 @@ func TestFixturesAreConsistent(t *testing.T) {
3029
}
3130

3231
func TestMain(m *testing.M) {
33-
unittest.MainTest(m, &unittest.TestOptions{
34-
GiteaRootPath: filepath.Join("..", ".."),
35-
})
32+
unittest.MainTest(m)
3633
}

models/main_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,5 @@ func TestFixturesAreConsistent(t *testing.T) {
2929
}
3030

3131
func TestMain(m *testing.M) {
32-
unittest.MainTest(m, &unittest.TestOptions{
33-
GiteaRootPath: "..",
34-
})
32+
unittest.MainTest(m)
3533
}

models/organization/main_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package organization_test
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
@@ -18,7 +17,5 @@ import (
1817
)
1918

2019
func TestMain(m *testing.M) {
21-
unittest.MainTest(m, &unittest.TestOptions{
22-
GiteaRootPath: filepath.Join("..", ".."),
23-
})
20+
unittest.MainTest(m)
2421
}

models/packages/package_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package packages_test
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/db"
@@ -20,9 +19,7 @@ import (
2019
)
2120

2221
func TestMain(m *testing.M) {
23-
unittest.MainTest(m, &unittest.TestOptions{
24-
GiteaRootPath: filepath.Join("..", ".."),
25-
})
22+
unittest.MainTest(m)
2623
}
2724

2825
func TestHasOwnerPackages(t *testing.T) {

models/perm/access/main_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package access_test
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
@@ -17,7 +16,5 @@ import (
1716
)
1817

1918
func TestMain(m *testing.M) {
20-
unittest.MainTest(m, &unittest.TestOptions{
21-
GiteaRootPath: filepath.Join("..", "..", ".."),
22-
})
19+
unittest.MainTest(m)
2320
}

models/project/main_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package project
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
@@ -14,7 +13,6 @@ import (
1413

1514
func TestMain(m *testing.M) {
1615
unittest.MainTest(m, &unittest.TestOptions{
17-
GiteaRootPath: filepath.Join("..", ".."),
1816
FixtureFiles: []string{
1917
"project.yml",
2018
"project_board.yml",

models/repo/main_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package repo_test
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
@@ -18,7 +17,5 @@ import (
1817
)
1918

2019
func TestMain(m *testing.M) {
21-
unittest.MainTest(m, &unittest.TestOptions{
22-
GiteaRootPath: filepath.Join("..", ".."),
23-
})
20+
unittest.MainTest(m)
2421
}

models/system/main_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package system_test
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
@@ -16,7 +15,5 @@ import (
1615
)
1716

1817
func TestMain(m *testing.M) {
19-
unittest.MainTest(m, &unittest.TestOptions{
20-
GiteaRootPath: filepath.Join("..", ".."),
21-
})
18+
unittest.MainTest(m)
2219
}

models/unittest/testdb.go

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,35 +62,46 @@ func InitSettings(extraConfigs ...string) {
6262

6363
// TestOptions represents test options
6464
type TestOptions struct {
65-
GiteaRootPath string
66-
FixtureFiles []string
67-
SetUp func() error // SetUp will be executed before all tests in this package
68-
TearDown func() error // TearDown will be executed after all tests in this package
65+
FixtureFiles []string
66+
SetUp func() error // SetUp will be executed before all tests in this package
67+
TearDown func() error // TearDown will be executed after all tests in this package
6968
}
7069

7170
// MainTest a reusable TestMain(..) function for unit tests that need to use a
7271
// test database. Creates the test database, and sets necessary settings.
73-
func MainTest(m *testing.M, testOpts *TestOptions) {
74-
setting.CustomPath = filepath.Join(testOpts.GiteaRootPath, "custom")
75-
InitSettings()
76-
77-
var err error
72+
func MainTest(m *testing.M, testOpts ...*TestOptions) {
73+
searchDir, _ := os.Getwd()
74+
for searchDir != "" {
75+
if _, err := os.Stat(filepath.Join(searchDir, "go.mod")); err == nil {
76+
break // The "go.mod" should be the one for Gitea repository
77+
}
78+
if dir := filepath.Dir(searchDir); dir == searchDir {
79+
searchDir = "" // reaches the root of filesystem
80+
} else {
81+
searchDir = dir
82+
}
83+
}
84+
if searchDir == "" {
85+
panic("The tests should run in a Gitea repository, there should be a 'go.mod' in the root")
86+
}
7887

79-
giteaRoot = testOpts.GiteaRootPath
80-
fixturesDir = filepath.Join(testOpts.GiteaRootPath, "models", "fixtures")
88+
giteaRoot = searchDir
89+
setting.CustomPath = filepath.Join(giteaRoot, "custom")
90+
InitSettings()
8191

92+
fixturesDir = filepath.Join(giteaRoot, "models", "fixtures")
8293
var opts FixturesOptions
83-
if len(testOpts.FixtureFiles) == 0 {
94+
if len(testOpts) == 0 || len(testOpts[0].FixtureFiles) == 0 {
8495
opts.Dir = fixturesDir
8596
} else {
86-
for _, f := range testOpts.FixtureFiles {
97+
for _, f := range testOpts[0].FixtureFiles {
8798
if len(f) != 0 {
8899
opts.Files = append(opts.Files, filepath.Join(fixturesDir, f))
89100
}
90101
}
91102
}
92103

93-
if err = CreateTestEngine(opts); err != nil {
104+
if err := CreateTestEngine(opts); err != nil {
94105
fatalTestError("Error creating test engine: %v\n", err)
95106
}
96107

@@ -112,8 +123,8 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
112123
fatalTestError("TempDir: %v\n", err)
113124
}
114125
setting.AppDataPath = appDataPath
115-
setting.AppWorkPath = testOpts.GiteaRootPath
116-
setting.StaticRootPath = testOpts.GiteaRootPath
126+
setting.AppWorkPath = giteaRoot
127+
setting.StaticRootPath = giteaRoot
117128
setting.GravatarSource = "https://secure.gravatar.com/avatar/"
118129

119130
setting.Attachment.Storage.Path = filepath.Join(setting.AppDataPath, "attachments")
@@ -144,7 +155,7 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
144155
if err = util.RemoveAll(repoRootPath); err != nil {
145156
fatalTestError("util.RemoveAll: %v\n", err)
146157
}
147-
if err = CopyDir(filepath.Join(testOpts.GiteaRootPath, "tests", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
158+
if err = CopyDir(filepath.Join(giteaRoot, "tests", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
148159
fatalTestError("util.CopyDir: %v\n", err)
149160
}
150161

@@ -171,16 +182,16 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
171182
}
172183
}
173184

174-
if testOpts.SetUp != nil {
175-
if err := testOpts.SetUp(); err != nil {
185+
if len(testOpts) > 0 && testOpts[0].SetUp != nil {
186+
if err := testOpts[0].SetUp(); err != nil {
176187
fatalTestError("set up failed: %v\n", err)
177188
}
178189
}
179190

180191
exitStatus := m.Run()
181192

182-
if testOpts.TearDown != nil {
183-
if err := testOpts.TearDown(); err != nil {
193+
if len(testOpts) > 0 && testOpts[0].TearDown != nil {
194+
if err := testOpts[0].TearDown(); err != nil {
184195
fatalTestError("tear down failed: %v\n", err)
185196
}
186197
}

models/user/main_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package user_test
55

66
import (
7-
"path/filepath"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
@@ -16,7 +15,5 @@ import (
1615
)
1716

1817
func TestMain(m *testing.M) {
19-
unittest.MainTest(m, &unittest.TestOptions{
20-
GiteaRootPath: filepath.Join("..", ".."),
21-
})
18+
unittest.MainTest(m)
2219
}

0 commit comments

Comments
 (0)