From d1520cf08da06b10e4544678303d2926d41eef2a Mon Sep 17 00:00:00 2001 From: Gusted Date: Thu, 14 Nov 2024 02:13:22 +0100 Subject: [PATCH] chore: improve preparing tests - Only prepare repositories once. - Move the repositories to temporary directories (these should usually be stored in memory) which are recreated for each test to avoid persistentance between tests. Doing some dirty profiling suggests that the preparing test functions from 140-100ms to 70-40ms --- tests/integration/api_wiki_test.go | 45 ++++++------- tests/integration/easymde_test.go | 3 + tests/integration/git_clone_wiki_test.go | 3 - .../git_helper_for_declarative_test.go | 9 +++ tests/test_utils.go | 66 ++++++++++++------- 5 files changed, 79 insertions(+), 47 deletions(-) diff --git a/tests/integration/api_wiki_test.go b/tests/integration/api_wiki_test.go index e5eb7a52c1..b930791969 100644 --- a/tests/integration/api_wiki_test.go +++ b/tests/integration/api_wiki_test.go @@ -9,6 +9,7 @@ import ( "encoding/base64" "fmt" "net/http" + "net/url" "testing" auth_model "code.gitea.io/gitea/models/auth" @@ -206,11 +207,11 @@ func TestAPIListWikiPages(t *testing.T) { } func TestAPINewWikiPage(t *testing.T) { + defer tests.PrepareTestEnv(t)() for _, title := range []string{ "New page", "&&&&", } { - defer tests.PrepareTestEnv(t)() username := "user2" session := loginUser(t, username) token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) @@ -386,26 +387,26 @@ func TestAPIListPageRevisions(t *testing.T) { } func TestAPIWikiNonMasterBranch(t *testing.T) { - defer tests.PrepareTestEnv(t)() - - user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) - repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{ - WikiBranch: optional.Some("main"), - }) - defer f() - - uris := []string{ - "revisions/Home", - "pages", - "page/Home", - } - baseURL := fmt.Sprintf("/api/v1/repos/%s/wiki", repo.FullName()) - for _, uri := range uris { - t.Run(uri, func(t *testing.T) { - defer tests.PrintCurrentTest(t)() - - req := NewRequestf(t, "GET", "%s/%s", baseURL, uri) - MakeRequest(t, req, http.StatusOK) + onGiteaRun(t, func(t *testing.T, _ *url.URL) { + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) + repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{ + WikiBranch: optional.Some("main"), }) - } + defer f() + + uris := []string{ + "revisions/Home", + "pages", + "page/Home", + } + baseURL := fmt.Sprintf("/api/v1/repos/%s/wiki", repo.FullName()) + for _, uri := range uris { + t.Run(uri, func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + req := NewRequestf(t, "GET", "%s/%s", baseURL, uri) + MakeRequest(t, req, http.StatusOK) + }) + } + }) } diff --git a/tests/integration/easymde_test.go b/tests/integration/easymde_test.go index 97f61c8449..2b4630d8b4 100644 --- a/tests/integration/easymde_test.go +++ b/tests/integration/easymde_test.go @@ -6,9 +6,12 @@ package integration import ( "net/http" "testing" + + "code.gitea.io/gitea/tests" ) func TestEasyMDESwitch(t *testing.T) { + defer tests.PrepareTestEnv(t)() session := loginUser(t, "user2") testEasyMDESwitch(t, session, "user2/glob/issues/1", false) testEasyMDESwitch(t, session, "user2/glob/issues/new", false) diff --git a/tests/integration/git_clone_wiki_test.go b/tests/integration/git_clone_wiki_test.go index ec99374c81..df260258de 100644 --- a/tests/integration/git_clone_wiki_test.go +++ b/tests/integration/git_clone_wiki_test.go @@ -13,7 +13,6 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/util" - "code.gitea.io/gitea/tests" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -33,8 +32,6 @@ func assertFileEqual(t *testing.T, p string, content []byte) { func TestRepoCloneWiki(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { - defer tests.PrepareTestEnv(t)() - dstPath := t.TempDir() r := fmt.Sprintf("%suser2/repo1.wiki.git", u.String()) diff --git a/tests/integration/git_helper_for_declarative_test.go b/tests/integration/git_helper_for_declarative_test.go index 490d4caae0..8bcab1b150 100644 --- a/tests/integration/git_helper_for_declarative_test.go +++ b/tests/integration/git_helper_for_declarative_test.go @@ -12,6 +12,7 @@ import ( "os" "path" "path/filepath" + "regexp" "strconv" "testing" "time" @@ -58,6 +59,8 @@ func createSSHUrl(gitPath string, u *url.URL) *url.URL { return &u2 } +var rootPathRe = regexp.MustCompile("\\[repository\\]\nROOT\\s=\\s.*") + func onGiteaRun[T testing.TB](t T, callback func(T, *url.URL)) { defer tests.PrepareTestEnv(t, 1)() s := http.Server{ @@ -76,7 +79,13 @@ func onGiteaRun[T testing.TB](t T, callback func(T, *url.URL)) { require.NoError(t, err) u.Host = listener.Addr().String() + // Override repository root in config. + conf, err := os.ReadFile(setting.CustomConf) + require.NoError(t, err) + require.NoError(t, os.WriteFile(setting.CustomConf, rootPathRe.ReplaceAll(conf, []byte("[repository]\nROOT = "+setting.RepoRootPath)), os.ModePerm)) + defer func() { + require.NoError(t, os.WriteFile(setting.CustomConf, conf, os.ModePerm)) ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) s.Shutdown(ctx) cancel() diff --git a/tests/test_utils.go b/tests/test_utils.go index d6516dd99a..e83f19700a 100644 --- a/tests/test_utils.go +++ b/tests/test_utils.go @@ -48,6 +48,8 @@ func exitf(format string, args ...any) { os.Exit(1) } +var preparedDir string + func InitTest(requireGitea bool) { log.RegisterEventWriter("test", testlogger.NewTestLoggerWriter) @@ -177,6 +179,44 @@ func InitTest(requireGitea bool) { } } + setting.Repository.Local.LocalCopyPath = os.TempDir() + dir, err := os.MkdirTemp("", "prepared-forgejo") + if err != nil { + log.Fatal("os.MkdirTemp: %v", err) + } + preparedDir = dir + + setting.Repository.Local.LocalCopyPath, err = os.MkdirTemp("", "local-upload") + if err != nil { + log.Fatal("os.MkdirTemp: %v", err) + } + + if err := unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), dir); err != nil { + log.Fatal("os.RemoveAll: %v", err) + } + ownerDirs, err := os.ReadDir(dir) + if err != nil { + log.Fatal("os.ReadDir: %v", err) + } + fmt.Println(ownerDirs) + + for _, ownerDir := range ownerDirs { + if !ownerDir.Type().IsDir() { + continue + } + repoDirs, err := os.ReadDir(filepath.Join(dir, ownerDir.Name())) + if err != nil { + log.Fatal("os.ReadDir: %v", err) + } + for _, repoDir := range repoDirs { + _ = os.MkdirAll(filepath.Join(dir, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0o755) + _ = os.MkdirAll(filepath.Join(dir, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0o755) + _ = os.MkdirAll(filepath.Join(dir, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0o755) + _ = os.MkdirAll(filepath.Join(dir, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0o755) + _ = os.MkdirAll(filepath.Join(dir, ownerDir.Name(), repoDir.Name(), "refs", "pull"), 0o755) + } + } + routers.InitWebInstalled(graceful.GetManager().HammerContext()) } @@ -225,28 +265,10 @@ func cancelProcesses(t testing.TB, delay time.Duration) { } func PrepareGitRepoDirectory(t testing.TB) { - require.NoError(t, util.RemoveAll(setting.RepoRootPath)) - require.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath)) - ownerDirs, err := os.ReadDir(setting.RepoRootPath) - if err != nil { - require.NoError(t, err, "unable to read the new repo root: %v\n", err) - } - for _, ownerDir := range ownerDirs { - if !ownerDir.Type().IsDir() { - continue - } - repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name())) - if err != nil { - require.NoError(t, err, "unable to read the new repo root: %v\n", err) - } - for _, repoDir := range repoDirs { - _ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0o755) - _ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0o755) - _ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0o755) - _ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0o755) - _ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "pull"), 0o755) - } - } + var err error + setting.RepoRootPath, err = os.MkdirTemp(t.TempDir(), "forgejo-repo-rooth") + require.NoError(t, err) + require.NoError(t, unittest.CopyDir(preparedDir, setting.RepoRootPath)) } func PrepareArtifactsStorage(t testing.TB) {