Improve testing init, clean up webhook tests (#37412)
Avoid webhook test fixtures affect other tests (be triggered) Also fixed more testing problems including path init, global config pollution & conflict --------- Signed-off-by: silverwind <me@silverwind.io> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: wxiaoguang <2114189+wxiaoguang@users.noreply.github.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
+16
-39
@@ -42,12 +42,20 @@ func MainTest(m *testing.M, testOptsArg ...*TestOptions) {
|
||||
|
||||
func mainTest(m *testing.M, testOptsArg ...*TestOptions) int {
|
||||
testOpts := util.OptionalArg(testOptsArg, &TestOptions{})
|
||||
|
||||
tempWorkPath, tempCleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("unit-test-dir-")
|
||||
if err != nil {
|
||||
return testlogger.MainErrorf("Failed to create temp dir for unit test: %v", err)
|
||||
}
|
||||
defer tempCleanup()
|
||||
|
||||
defer setting.MockBuiltinPaths(tempWorkPath, "", "")()
|
||||
setting.SetupGiteaTestEnv()
|
||||
|
||||
giteaRoot := setting.GetGiteaTestSourceRoot()
|
||||
fixturesOpts := FixturesOptions{Dir: filepath.Join(giteaRoot, "models", "fixtures"), Files: testOpts.FixtureFiles}
|
||||
if err := CreateTestEngine(fixturesOpts); err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Error creating test database engine: %v\n", err)
|
||||
os.Exit(1)
|
||||
return testlogger.MainErrorf("Error creating test database engine: %v", err)
|
||||
}
|
||||
|
||||
setting.AppURL = "https://try.gitea.io/"
|
||||
@@ -59,59 +67,28 @@ func mainTest(m *testing.M, testOptsArg ...*TestOptions) int {
|
||||
setting.SSH.Domain = "try.gitea.io"
|
||||
setting.Database.Type = "sqlite3"
|
||||
setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master"
|
||||
repoRootPath, cleanup1, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("repos")
|
||||
if err != nil {
|
||||
testlogger.Panicf("TempDir: %v\n", err)
|
||||
}
|
||||
defer cleanup1()
|
||||
|
||||
setting.RepoRootPath = repoRootPath
|
||||
appDataPath, cleanup2, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("appdata")
|
||||
if err != nil {
|
||||
testlogger.Panicf("TempDir: %v\n", err)
|
||||
}
|
||||
defer cleanup2()
|
||||
|
||||
setting.AppDataPath = appDataPath
|
||||
setting.GravatarSource = "https://secure.gravatar.com/avatar/"
|
||||
|
||||
setting.Attachment.Storage.Path = filepath.Join(setting.AppDataPath, "attachments")
|
||||
|
||||
setting.LFS.Storage.Path = filepath.Join(setting.AppDataPath, "lfs")
|
||||
|
||||
setting.Avatar.Storage.Path = filepath.Join(setting.AppDataPath, "avatars")
|
||||
|
||||
setting.RepoAvatar.Storage.Path = filepath.Join(setting.AppDataPath, "repo-avatars")
|
||||
|
||||
setting.RepoArchive.Storage.Path = filepath.Join(setting.AppDataPath, "repo-archive")
|
||||
|
||||
setting.Packages.Storage.Path = filepath.Join(setting.AppDataPath, "packages")
|
||||
|
||||
setting.Actions.LogStorage.Path = filepath.Join(setting.AppDataPath, "actions_log")
|
||||
|
||||
setting.Git.HomePath = filepath.Join(setting.AppDataPath, "home")
|
||||
|
||||
setting.IncomingEmail.ReplyToAddress = "incoming+%{token}@localhost"
|
||||
|
||||
config.SetDynGetter(system.NewDatabaseDynKeyGetter())
|
||||
|
||||
if err = cache.Init(); err != nil {
|
||||
testlogger.Panicf("cache.Init: %v\n", err)
|
||||
return testlogger.MainErrorf("cache.Init: %v", err)
|
||||
}
|
||||
if err = storage.Init(); err != nil {
|
||||
testlogger.Panicf("storage.Init: %v\n", err)
|
||||
return testlogger.MainErrorf("storage.Init: %v", err)
|
||||
}
|
||||
if err = SyncDirs(filepath.Join(giteaRoot, "tests", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
|
||||
testlogger.Panicf("util.SyncDirs: %v\n", err)
|
||||
return testlogger.MainErrorf("util.SyncDirs: %v", err)
|
||||
}
|
||||
|
||||
if err = git.InitFull(); err != nil {
|
||||
testlogger.Panicf("git.Init: %v\n", err)
|
||||
return testlogger.MainErrorf("git.Init: %v", err)
|
||||
}
|
||||
|
||||
if testOpts.SetUp != nil {
|
||||
if err := testOpts.SetUp(); err != nil {
|
||||
testlogger.Panicf("set up failed: %v\n", err)
|
||||
return testlogger.MainErrorf("set up failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +96,7 @@ func mainTest(m *testing.M, testOptsArg ...*TestOptions) int {
|
||||
|
||||
if testOpts.TearDown != nil {
|
||||
if err := testOpts.TearDown(); err != nil {
|
||||
testlogger.Panicf("tear down failed: %v\n", err)
|
||||
return testlogger.MainErrorf("tear down failed: %v", err)
|
||||
}
|
||||
}
|
||||
return exitStatus
|
||||
|
||||
Reference in New Issue
Block a user