Skip to content

Commit

Permalink
Refactor tests (#26464)
Browse files Browse the repository at this point in the history
1. Give the global variable clear names
2. Use generic parameter for `onGiteaRun`
  • Loading branch information
wxiaoguang authored Aug 12, 2023
1 parent bcccf4c commit c28e29f
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 127 deletions.
4 changes: 2 additions & 2 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ func serveInstalled(ctx *cli.Context) error {
}

// Set up Chi routes
c := routers.NormalRoutes()
err := listen(c, true)
webRoutes := routers.NormalRoutes()
err := listen(webRoutes, true)
<-graceful.GetManager().Done()
log.Info("PID: %d Gitea Web Finished", os.Getpid())
log.GetManager().Close()
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"code.gitea.io/gitea/tests"
)

var c *web.Route
var testE2eWebRoutes *web.Route

func TestMain(m *testing.M) {
defer log.GetManager().Close()
Expand All @@ -38,7 +38,7 @@ func TestMain(m *testing.M) {
defer cancel()

tests.InitTest(false)
c = routers.NormalRoutes()
testE2eWebRoutes = routers.NormalRoutes()

os.Unsetenv("GIT_AUTHOR_NAME")
os.Unsetenv("GIT_AUTHOR_EMAIL")
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/utils_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func onGiteaRunTB(t testing.TB, callback func(testing.TB, *url.URL), prepare ...
defer tests.PrepareTestEnv(t, 1)()
}
s := http.Server{
Handler: c,
Handler: testE2eWebRoutes,
}

u, err := url.Parse(setting.AppURL)
Expand Down
14 changes: 7 additions & 7 deletions tests/integration/api_activitypub_person_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (

func TestActivityPubPerson(t *testing.T) {
setting.Federation.Enabled = true
c = routers.NormalRoutes()
testWebRoutes = routers.NormalRoutes()
defer func() {
setting.Federation.Enabled = false
c = routers.NormalRoutes()
testWebRoutes = routers.NormalRoutes()
}()

onGiteaRun(t, func(*testing.T, *url.URL) {
Expand Down Expand Up @@ -60,10 +60,10 @@ func TestActivityPubPerson(t *testing.T) {

func TestActivityPubMissingPerson(t *testing.T) {
setting.Federation.Enabled = true
c = routers.NormalRoutes()
testWebRoutes = routers.NormalRoutes()
defer func() {
setting.Federation.Enabled = false
c = routers.NormalRoutes()
testWebRoutes = routers.NormalRoutes()
}()

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

func TestActivityPubPersonInbox(t *testing.T) {
setting.Federation.Enabled = true
c = routers.NormalRoutes()
testWebRoutes = routers.NormalRoutes()
defer func() {
setting.Federation.Enabled = false
c = routers.NormalRoutes()
testWebRoutes = routers.NormalRoutes()
}()

srv := httptest.NewServer(c)
srv := httptest.NewServer(testWebRoutes)
defer srv.Close()

onGiteaRun(t, func(*testing.T, *url.URL) {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/api_nodeinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (

func TestNodeinfo(t *testing.T) {
setting.Federation.Enabled = true
c = routers.NormalRoutes()
testWebRoutes = routers.NormalRoutes()
defer func() {
setting.Federation.Enabled = false
c = routers.NormalRoutes()
testWebRoutes = routers.NormalRoutes()
}()

onGiteaRun(t, func(*testing.T, *url.URL) {
Expand Down
19 changes: 9 additions & 10 deletions tests/integration/api_repo_file_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,31 +110,30 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
}

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

b.ResetTimer()
for n := 0; n < b.N; n++ {
treePath := fmt.Sprintf("update/file%d.txt", n)
createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
_, _ = createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
}
})
}

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

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

b.ResetTimer()
for n := 0; n < b.N; n++ {
treePath := fmt.Sprintf("update/file%d.txt", n)
copy(data, treePath)
createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
_, _ = createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
}
})
}
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ func StringWithCharset(length int, charset string) string {
}

func BenchmarkRepoBranchCommit(b *testing.B) {
onGiteaRunTB(b, func(t testing.TB, u *url.URL) {
b := t.(*testing.B)

onGiteaRun(b, func(b *testing.B, u *url.URL) {
samples := []int64{1, 2, 3}
b.ResetTimer()

Expand Down
4 changes: 2 additions & 2 deletions tests/integration/create_no_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestSessionFileCreation(t *testing.T) {
oldSessionConfig := setting.SessionConfig.ProviderConfig
defer func() {
setting.SessionConfig.ProviderConfig = oldSessionConfig
c = routers.NormalRoutes()
testWebRoutes = routers.NormalRoutes()
}()

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

setting.SessionConfig.ProviderConfig = string(newConfigBytes)

c = routers.NormalRoutes()
testWebRoutes = routers.NormalRoutes()

t.Run("NoSessionOnViewIssue", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
Expand Down
14 changes: 3 additions & 11 deletions tests/integration/git_helper_for_declarative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ func createSSHUrl(gitPath string, u *url.URL) *url.URL {
return &u2
}

func onGiteaRunTB(t testing.TB, callback func(testing.TB, *url.URL), prepare ...bool) {
if len(prepare) == 0 || prepare[0] {
defer tests.PrepareTestEnv(t, 1)()
}
func onGiteaRun[T testing.TB](t T, callback func(T, *url.URL)) {
defer tests.PrepareTestEnv(t, 1)()
s := http.Server{
Handler: c,
Handler: testWebRoutes,
}

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

func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bool) {
onGiteaRunTB(t, func(t testing.TB, u *url.URL) {
callback(t.(*testing.T), u)
}, prepare...)
}

func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
return func(t *testing.T) {
assert.NoError(t, git.CloneWithArgs(context.Background(), git.AllowLFSFiltersArgs(), u.String(), dstLocalPath, git.CloneRepoOptions{}))
Expand Down
Loading

0 comments on commit c28e29f

Please sign in to comment.