diff options
Diffstat (limited to 'tests/test_utils.go')
-rw-r--r-- | tests/test_utils.go | 66 |
1 files changed, 32 insertions, 34 deletions
diff --git a/tests/test_utils.go b/tests/test_utils.go index b31491caf2..e6ce3cce0e 100644 --- a/tests/test_utils.go +++ b/tests/test_utils.go @@ -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 |