mirror of
1
Fork 0

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
This commit is contained in:
Gusted 2024-11-14 02:13:22 +01:00
parent 9e95f80d94
commit d1520cf08d
No known key found for this signature in database
GPG Key ID: FD821B732837125F
5 changed files with 79 additions and 47 deletions

View File

@ -9,6 +9,7 @@ import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"testing" "testing"
auth_model "code.gitea.io/gitea/models/auth" auth_model "code.gitea.io/gitea/models/auth"
@ -206,11 +207,11 @@ func TestAPIListWikiPages(t *testing.T) {
} }
func TestAPINewWikiPage(t *testing.T) { func TestAPINewWikiPage(t *testing.T) {
defer tests.PrepareTestEnv(t)()
for _, title := range []string{ for _, title := range []string{
"New page", "New page",
"&&&&", "&&&&",
} { } {
defer tests.PrepareTestEnv(t)()
username := "user2" username := "user2"
session := loginUser(t, username) session := loginUser(t, username)
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
@ -386,26 +387,26 @@ func TestAPIListPageRevisions(t *testing.T) {
} }
func TestAPIWikiNonMasterBranch(t *testing.T) { func TestAPIWikiNonMasterBranch(t *testing.T) {
defer tests.PrepareTestEnv(t)() onGiteaRun(t, func(t *testing.T, _ *url.URL) {
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{
repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{ WikiBranch: optional.Some("main"),
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)
}) })
} 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)
})
}
})
} }

View File

@ -6,9 +6,12 @@ package integration
import ( import (
"net/http" "net/http"
"testing" "testing"
"code.gitea.io/gitea/tests"
) )
func TestEasyMDESwitch(t *testing.T) { func TestEasyMDESwitch(t *testing.T) {
defer tests.PrepareTestEnv(t)()
session := loginUser(t, "user2") session := loginUser(t, "user2")
testEasyMDESwitch(t, session, "user2/glob/issues/1", false) testEasyMDESwitch(t, session, "user2/glob/issues/1", false)
testEasyMDESwitch(t, session, "user2/glob/issues/new", false) testEasyMDESwitch(t, session, "user2/glob/issues/new", false)

View File

@ -13,7 +13,6 @@ import (
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -33,8 +32,6 @@ func assertFileEqual(t *testing.T, p string, content []byte) {
func TestRepoCloneWiki(t *testing.T) { func TestRepoCloneWiki(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) { onGiteaRun(t, func(t *testing.T, u *url.URL) {
defer tests.PrepareTestEnv(t)()
dstPath := t.TempDir() dstPath := t.TempDir()
r := fmt.Sprintf("%suser2/repo1.wiki.git", u.String()) r := fmt.Sprintf("%suser2/repo1.wiki.git", u.String())

View File

@ -12,6 +12,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
"strconv" "strconv"
"testing" "testing"
"time" "time"
@ -58,6 +59,8 @@ func createSSHUrl(gitPath string, u *url.URL) *url.URL {
return &u2 return &u2
} }
var rootPathRe = regexp.MustCompile("\\[repository\\]\nROOT\\s=\\s.*")
func onGiteaRun[T testing.TB](t T, callback func(T, *url.URL)) { func onGiteaRun[T testing.TB](t T, callback func(T, *url.URL)) {
defer tests.PrepareTestEnv(t, 1)() defer tests.PrepareTestEnv(t, 1)()
s := http.Server{ s := http.Server{
@ -76,7 +79,13 @@ func onGiteaRun[T testing.TB](t T, callback func(T, *url.URL)) {
require.NoError(t, err) require.NoError(t, err)
u.Host = listener.Addr().String() 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() { defer func() {
require.NoError(t, os.WriteFile(setting.CustomConf, conf, os.ModePerm))
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
s.Shutdown(ctx) s.Shutdown(ctx)
cancel() cancel()

View File

@ -48,6 +48,8 @@ func exitf(format string, args ...any) {
os.Exit(1) os.Exit(1)
} }
var preparedDir string
func InitTest(requireGitea bool) { func InitTest(requireGitea bool) {
log.RegisterEventWriter("test", testlogger.NewTestLoggerWriter) 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()) routers.InitWebInstalled(graceful.GetManager().HammerContext())
} }
@ -225,28 +265,10 @@ func cancelProcesses(t testing.TB, delay time.Duration) {
} }
func PrepareGitRepoDirectory(t testing.TB) { func PrepareGitRepoDirectory(t testing.TB) {
require.NoError(t, util.RemoveAll(setting.RepoRootPath)) var err error
require.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath)) setting.RepoRootPath, err = os.MkdirTemp(t.TempDir(), "forgejo-repo-rooth")
ownerDirs, err := os.ReadDir(setting.RepoRootPath) require.NoError(t, err)
if err != nil { require.NoError(t, unittest.CopyDir(preparedDir, setting.RepoRootPath))
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)
}
}
} }
func PrepareArtifactsStorage(t testing.TB) { func PrepareArtifactsStorage(t testing.TB) {