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"
"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,8 +387,7 @@ func TestAPIListPageRevisions(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})
repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{
WikiBranch: optional.Some("main"),
@ -408,4 +408,5 @@ func TestAPIWikiNonMasterBranch(t *testing.T) {
MakeRequest(t, req, http.StatusOK)
})
}
})
}

View File

@ -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)

View File

@ -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())

View File

@ -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()

View File

@ -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) {