Skip to content

Commit c28e29f

Browse files
authored
Refactor tests (#26464)
1. Give the global variable clear names 2. Use generic parameter for `onGiteaRun`
1 parent bcccf4c commit c28e29f

12 files changed

+65
-127
lines changed

cmd/web.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ func serveInstalled(ctx *cli.Context) error {
208208
}
209209

210210
// Set up Chi routes
211-
c := routers.NormalRoutes()
212-
err := listen(c, true)
211+
webRoutes := routers.NormalRoutes()
212+
err := listen(webRoutes, true)
213213
<-graceful.GetManager().Done()
214214
log.Info("PID: %d Gitea Web Finished", os.Getpid())
215215
log.GetManager().Close()

tests/e2e/e2e_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"code.gitea.io/gitea/tests"
2929
)
3030

31-
var c *web.Route
31+
var testE2eWebRoutes *web.Route
3232

3333
func TestMain(m *testing.M) {
3434
defer log.GetManager().Close()
@@ -38,7 +38,7 @@ func TestMain(m *testing.M) {
3838
defer cancel()
3939

4040
tests.InitTest(false)
41-
c = routers.NormalRoutes()
41+
testE2eWebRoutes = routers.NormalRoutes()
4242

4343
os.Unsetenv("GIT_AUTHOR_NAME")
4444
os.Unsetenv("GIT_AUTHOR_EMAIL")

tests/e2e/utils_e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func onGiteaRunTB(t testing.TB, callback func(testing.TB, *url.URL), prepare ...
2222
defer tests.PrepareTestEnv(t, 1)()
2323
}
2424
s := http.Server{
25-
Handler: c,
25+
Handler: testE2eWebRoutes,
2626
}
2727

2828
u, err := url.Parse(setting.AppURL)

tests/integration/api_activitypub_person_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import (
2222

2323
func TestActivityPubPerson(t *testing.T) {
2424
setting.Federation.Enabled = true
25-
c = routers.NormalRoutes()
25+
testWebRoutes = routers.NormalRoutes()
2626
defer func() {
2727
setting.Federation.Enabled = false
28-
c = routers.NormalRoutes()
28+
testWebRoutes = routers.NormalRoutes()
2929
}()
3030

3131
onGiteaRun(t, func(*testing.T, *url.URL) {
@@ -60,10 +60,10 @@ func TestActivityPubPerson(t *testing.T) {
6060

6161
func TestActivityPubMissingPerson(t *testing.T) {
6262
setting.Federation.Enabled = true
63-
c = routers.NormalRoutes()
63+
testWebRoutes = routers.NormalRoutes()
6464
defer func() {
6565
setting.Federation.Enabled = false
66-
c = routers.NormalRoutes()
66+
testWebRoutes = routers.NormalRoutes()
6767
}()
6868

6969
onGiteaRun(t, func(*testing.T, *url.URL) {
@@ -75,13 +75,13 @@ func TestActivityPubMissingPerson(t *testing.T) {
7575

7676
func TestActivityPubPersonInbox(t *testing.T) {
7777
setting.Federation.Enabled = true
78-
c = routers.NormalRoutes()
78+
testWebRoutes = routers.NormalRoutes()
7979
defer func() {
8080
setting.Federation.Enabled = false
81-
c = routers.NormalRoutes()
81+
testWebRoutes = routers.NormalRoutes()
8282
}()
8383

84-
srv := httptest.NewServer(c)
84+
srv := httptest.NewServer(testWebRoutes)
8585
defer srv.Close()
8686

8787
onGiteaRun(t, func(*testing.T, *url.URL) {

tests/integration/api_nodeinfo_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import (
1717

1818
func TestNodeinfo(t *testing.T) {
1919
setting.Federation.Enabled = true
20-
c = routers.NormalRoutes()
20+
testWebRoutes = routers.NormalRoutes()
2121
defer func() {
2222
setting.Federation.Enabled = false
23-
c = routers.NormalRoutes()
23+
testWebRoutes = routers.NormalRoutes()
2424
}()
2525

2626
onGiteaRun(t, func(*testing.T, *url.URL) {

tests/integration/api_repo_file_create_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,31 +110,30 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
110110
}
111111

112112
func BenchmarkAPICreateFileSmall(b *testing.B) {
113-
onGiteaRunTB(b, func(t testing.TB, u *url.URL) {
114-
b := t.(*testing.B)
115-
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo1 & repo16
116-
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) // public repo
113+
onGiteaRun(b, func(b *testing.B, u *url.URL) {
114+
user2 := unittest.AssertExistsAndLoadBean(b, &user_model.User{ID: 2}) // owner of the repo1 & repo16
115+
repo1 := unittest.AssertExistsAndLoadBean(b, &repo_model.Repository{ID: 1}) // public repo
117116

117+
b.ResetTimer()
118118
for n := 0; n < b.N; n++ {
119119
treePath := fmt.Sprintf("update/file%d.txt", n)
120-
createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
120+
_, _ = createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
121121
}
122122
})
123123
}
124124

125125
func BenchmarkAPICreateFileMedium(b *testing.B) {
126126
data := make([]byte, 10*1024*1024)
127127

128-
onGiteaRunTB(b, func(t testing.TB, u *url.URL) {
129-
b := t.(*testing.B)
130-
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo1 & repo16
131-
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) // public repo
128+
onGiteaRun(b, func(b *testing.B, u *url.URL) {
129+
user2 := unittest.AssertExistsAndLoadBean(b, &user_model.User{ID: 2}) // owner of the repo1 & repo16
130+
repo1 := unittest.AssertExistsAndLoadBean(b, &repo_model.Repository{ID: 1}) // public repo
132131

133132
b.ResetTimer()
134133
for n := 0; n < b.N; n++ {
135134
treePath := fmt.Sprintf("update/file%d.txt", n)
136135
copy(data, treePath)
137-
createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
136+
_, _ = createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
138137
}
139138
})
140139
}

tests/integration/benchmarks_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ func StringWithCharset(length int, charset string) string {
2424
}
2525

2626
func BenchmarkRepoBranchCommit(b *testing.B) {
27-
onGiteaRunTB(b, func(t testing.TB, u *url.URL) {
28-
b := t.(*testing.B)
29-
27+
onGiteaRun(b, func(b *testing.B, u *url.URL) {
3028
samples := []int64{1, 2, 3}
3129
b.ResetTimer()
3230

tests/integration/create_no_session_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestSessionFileCreation(t *testing.T) {
5656
oldSessionConfig := setting.SessionConfig.ProviderConfig
5757
defer func() {
5858
setting.SessionConfig.ProviderConfig = oldSessionConfig
59-
c = routers.NormalRoutes()
59+
testWebRoutes = routers.NormalRoutes()
6060
}()
6161

6262
var config session.Options
@@ -75,7 +75,7 @@ func TestSessionFileCreation(t *testing.T) {
7575

7676
setting.SessionConfig.ProviderConfig = string(newConfigBytes)
7777

78-
c = routers.NormalRoutes()
78+
testWebRoutes = routers.NormalRoutes()
7979

8080
t.Run("NoSessionOnViewIssue", func(t *testing.T) {
8181
defer tests.PrintCurrentTest(t)()

tests/integration/git_helper_for_declarative_test.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,10 @@ func createSSHUrl(gitPath string, u *url.URL) *url.URL {
5757
return &u2
5858
}
5959

60-
func onGiteaRunTB(t testing.TB, callback func(testing.TB, *url.URL), prepare ...bool) {
61-
if len(prepare) == 0 || prepare[0] {
62-
defer tests.PrepareTestEnv(t, 1)()
63-
}
60+
func onGiteaRun[T testing.TB](t T, callback func(T, *url.URL)) {
61+
defer tests.PrepareTestEnv(t, 1)()
6462
s := http.Server{
65-
Handler: c,
63+
Handler: testWebRoutes,
6664
}
6765

6866
u, err := url.Parse(setting.AppURL)
@@ -89,12 +87,6 @@ func onGiteaRunTB(t testing.TB, callback func(testing.TB, *url.URL), prepare ...
8987
callback(t, u)
9088
}
9189

92-
func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bool) {
93-
onGiteaRunTB(t, func(t testing.TB, u *url.URL) {
94-
callback(t.(*testing.T), u)
95-
}, prepare...)
96-
}
97-
9890
func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
9991
return func(t *testing.T) {
10092
assert.NoError(t, git.CloneWithArgs(context.Background(), git.AllowLFSFiltersArgs(), u.String(), dstLocalPath, git.CloneRepoOptions{}))

0 commit comments

Comments
 (0)