Refactor tests to prevent from unnecessary preparations (#32398)

This commit is contained in:
wxiaoguang 2024-11-01 23:18:29 +08:00 committed by GitHub
parent 66971e591e
commit ec2d1593c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 89 additions and 55 deletions

View file

@ -192,32 +192,7 @@ func PrepareAttachmentsStorage(t testing.TB) {
}))
}
func PrepareArtifactsStorage(t testing.TB) {
// prepare actions artifacts directory and files
assert.NoError(t, storage.Clean(storage.ActionsArtifacts))
s, err := storage.NewStorage(setting.LocalStorageType, &setting.Storage{
Path: filepath.Join(filepath.Dir(setting.AppPath), "tests", "testdata", "data", "artifacts"),
})
assert.NoError(t, err)
assert.NoError(t, s.IterateObjects("", func(p string, obj storage.Object) error {
_, err = storage.Copy(storage.ActionsArtifacts, p, s, p)
return err
}))
}
func PrepareTestEnv(t testing.TB, skip ...int) func() {
t.Helper()
ourSkip := 1
if len(skip) > 0 {
ourSkip += skip[0]
}
deferFn := PrintCurrentTest(t, ourSkip)
// load database fixtures
assert.NoError(t, unittest.LoadFixtures())
// load git repo fixtures
func PrepareGitRepoDirectory(t testing.TB) {
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
@ -241,12 +216,25 @@ func PrepareTestEnv(t testing.TB, skip ...int) func() {
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "pull"), 0o755)
}
}
}
// Initialize actions artifact data
PrepareArtifactsStorage(t)
func PrepareArtifactsStorage(t testing.TB) {
// prepare actions artifacts directory and files
assert.NoError(t, storage.Clean(storage.ActionsArtifacts))
s, err := storage.NewStorage(setting.LocalStorageType, &setting.Storage{
Path: filepath.Join(filepath.Dir(setting.AppPath), "tests", "testdata", "data", "artifacts"),
})
assert.NoError(t, err)
assert.NoError(t, s.IterateObjects("", func(p string, obj storage.Object) error {
_, err = storage.Copy(storage.ActionsArtifacts, p, s, p)
return err
}))
}
func PrepareLFSStorage(t testing.TB) {
// load LFS object fixtures
// (LFS storage can be on any of several backends, including remote servers, so we init it with the storage API)
// (LFS storage can be on any of several backends, including remote servers, so init it with the storage API)
lfsFixtures, err := storage.NewStorage(setting.LocalStorageType, &setting.Storage{
Path: filepath.Join(filepath.Dir(setting.AppPath), "tests/gitea-lfs-meta"),
})
@ -256,7 +244,9 @@ func PrepareTestEnv(t testing.TB, skip ...int) func() {
_, err := storage.Copy(storage.LFS, path, lfsFixtures, path)
return err
}))
}
func PrepareCleanPackageData(t testing.TB) {
// clear all package data
assert.NoError(t, db.TruncateBeans(db.DefaultContext,
&packages_model.Package{},
@ -268,17 +258,25 @@ func PrepareTestEnv(t testing.TB, skip ...int) func() {
&packages_model.PackageCleanupRule{},
))
assert.NoError(t, storage.Clean(storage.Packages))
}
func PrepareTestEnv(t testing.TB, skip ...int) func() {
t.Helper()
deferFn := PrintCurrentTest(t, util.OptionalArg(skip)+1)
// load database fixtures
assert.NoError(t, unittest.LoadFixtures())
// do not add more Prepare* functions here, only call necessary ones in the related test functions
PrepareGitRepoDirectory(t)
PrepareLFSStorage(t)
PrepareCleanPackageData(t)
return deferFn
}
func PrintCurrentTest(t testing.TB, skip ...int) func() {
t.Helper()
actualSkip := 1
if len(skip) > 0 {
actualSkip = skip[0] + 1
}
return testlogger.PrintCurrentTest(t, actualSkip)
return testlogger.PrintCurrentTest(t, util.OptionalArg(skip)+1)
}
// Printf takes a format and args and prints the string to os.Stdout