This commit is contained in:
wxiaoguang
2026-04-25 20:24:43 +08:00
parent 0b7aee8147
commit e86d62e497
8 changed files with 113 additions and 108 deletions

View File

@@ -192,13 +192,13 @@ func RunGitTests(m interface{ Run() int }) {
func runGitTests(m interface{ Run() int }) int {
gitHomePath, cleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("git-home")
if err != nil {
testlogger.Panicf("unable to create temp dir: %s", err.Error())
return testlogger.MainErrorf("unable to create temp dir: %v", err)
}
defer cleanup()
setting.Git.HomePath = gitHomePath
if err = InitFull(); err != nil {
testlogger.Panicf("failed to call Init: %s", err.Error())
return testlogger.MainErrorf("failed to call Init: %s", err)
}
return m.Run()
}

View File

@@ -24,7 +24,7 @@ func testMain(m *testing.M) int {
// "setting.Git.HomePath" is initialized in "git" package but really used in "gitcmd" package
gitHomePath, cleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("git-home")
if err != nil {
testlogger.Panicf("failed to create temp dir: %v", err)
return testlogger.MainErrorf("failed to create temp dir: %v", err)
}
defer cleanup()

View File

@@ -101,6 +101,10 @@ func SetupGiteaTestEnv() {
cleanUpEnv()
initWorkPathAndConfig()
if RepoRootPath == "" || AppDataPath == "" {
panic("SetupGiteaTestEnv failed, paths are not initialized")
}
// TODO: some git repo hooks (test fixtures) still use these env variables, need to be refactored in the future
_ = os.Setenv("GITEA_ROOT", giteaRoot)
_ = os.Setenv("GITEA_CONF", giteaConf) // test fixture git hooks use "$GITEA_ROOT/$GITEA_CONF" in their scripts

View File

@@ -173,7 +173,8 @@ func Init() {
log.RegisterEventWriter("test", newTestLoggerWriter)
}
func Panicf(format string, args ...any) {
// don't call os.Exit, otherwise the "defer" functions won't be executed
panic(fmt.Sprintf(format, args...))
// MainErrorf is used to report an error from TestMain and return a non-zero value to indicate the failure
func MainErrorf(msg string, a ...any) int {
_, _ = fmt.Fprintf(os.Stderr, msg+"\n", a...)
return 1
}