From 81fcfab0e35877262a84a14dfaffcd92ff42ece0 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 25 Apr 2026 19:45:37 +0800 Subject: [PATCH] fix --- Makefile | 2 +- cmd/migrate_storage_test.go | 2 -- models/unittest/testdb.go | 19 ++++--------------- modules/queue/manager_test.go | 1 + modules/setting/server.go | 5 ----- modules/setting/testenv.go | 20 ++------------------ 6 files changed, 8 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 3c0e2a3e731..e621cc362fc 100644 --- a/Makefile +++ b/Makefile @@ -411,7 +411,7 @@ coverage: .PHONY: unit-test-coverage unit-test-coverage: @echo "Running unit-test-coverage $(GOTESTFLAGS) -tags '$(TEST_TAGS)'..." - @$(GO) test $(GOTESTFLAGS) -timeout=20m -tags='$(TEST_TAGS)' -v -cover -coverprofile coverage.out $(GO_TEST_PACKAGES) && echo "\n==>\033[32m Ok\033[m\n" || exit 1 + @$(GO) test $(GOTESTFLAGS) -timeout=20m -tags='$(TEST_TAGS)' -cover -coverprofile coverage.out $(GO_TEST_PACKAGES) && echo "\n==>\033[32m Ok\033[m\n" || exit 1 .PHONY: tidy tidy: ## run go mod tidy diff --git a/cmd/migrate_storage_test.go b/cmd/migrate_storage_test.go index 6c985146f85..3ea193eb1ea 100644 --- a/cmd/migrate_storage_test.go +++ b/cmd/migrate_storage_test.go @@ -22,8 +22,6 @@ import ( func TestMigratePackages(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - t.Log("DebugLogs", setting.DebugGetLogs()) - creator := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) content := "package main\n\nfunc main() {\nfmt.Println(\"hi\")\n}\n" diff --git a/models/unittest/testdb.go b/models/unittest/testdb.go index 01191eb3d0f..82c8d181ac2 100644 --- a/models/unittest/testdb.go +++ b/models/unittest/testdb.go @@ -47,24 +47,13 @@ func mainTest(m *testing.M, testOptsArg ...*TestOptions) int { testOpts := util.OptionalArg(testOptsArg, &TestOptions{}) - appDataPath, appDataCleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("unit-test-data-") + tempWorkPath, tempCleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("unit-test-dir-") if err != nil { - return reportError("Failed to create temp dir for app data path: %v", err) + return reportError("Failed to create temp dir for unit test: %v", err) } + defer tempCleanup() - // FIXME: debug - _, _ = fmt.Fprintf(os.Stderr, "Prepare APP_DATA_PATH for SetupGiteaTestEnv: %s\n", appDataPath) - setting.DebugAppendLog("Prepare APP_DATA_PATH for SetupGiteaTestEnv: %s", appDataPath) - defer func() { - _, _ = fmt.Fprintf(os.Stderr, "Remove APP_DATA_PATH for SetupGiteaTestEnv: %s\n", appDataPath) - setting.DebugAppendLog("Remove APP_DATA_PATH for SetupGiteaTestEnv: %s", appDataPath) - appDataCleanup() - }() - - _ = os.Setenv("GITEA_TEST_CONF_CONTENT", ` -[server] -APP_DATA_PATH = `+appDataPath+` -`) + defer setting.MockBuiltinPaths(tempWorkPath, "", "")() setting.SetupGiteaTestEnv() if setting.RepoRootPath == "" || setting.AppDataPath == "" { return reportError("SetupGiteaTestEnv failed, paths are not initialized") diff --git a/modules/queue/manager_test.go b/modules/queue/manager_test.go index ee6ccc46ff3..f9f9b7310be 100644 --- a/modules/queue/manager_test.go +++ b/modules/queue/manager_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/modules/setting" + "github.com/stretchr/testify/assert" ) diff --git a/modules/setting/server.go b/modules/setting/server.go index 7d9b8215be5..dc58e43c435 100644 --- a/modules/setting/server.go +++ b/modules/setting/server.go @@ -278,11 +278,6 @@ func loadServerFrom(rootCfg ConfigProvider) { StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(StaticRootPath) StaticCacheTime = sec.Key("STATIC_CACHE_TIME").MustDuration(6 * time.Hour) AppDataPath = sec.Key("APP_DATA_PATH").MustString(filepath.Join(AppWorkPath, "data")) - - // FIXME: debug - bufCustomConf, _ := os.ReadFile(CustomConf) - log.Error("Set AppDataPath=%s (CustomConf=%s)\n%s\n%s\n", AppDataPath, CustomConf, string(bufCustomConf), log.Stack(2)) - DebugAppendLog("Set AppDataPath=%s (CustomConf=%s)\n%s\n%s\n", AppDataPath, CustomConf, string(bufCustomConf), log.Stack(2)) if !filepath.IsAbs(AppDataPath) { AppDataPath = filepath.ToSlash(filepath.Join(AppWorkPath, AppDataPath)) } diff --git a/modules/setting/testenv.go b/modules/setting/testenv.go index 1d9efc9ed8b..3ffb22109e5 100644 --- a/modules/setting/testenv.go +++ b/modules/setting/testenv.go @@ -21,19 +21,6 @@ func GetGiteaTestSourceRoot() string { return *giteaTestSourceRoot } -func DebugAppendLog(msg string, a ...any) { - f, _ := os.OpenFile("/tmp/debug.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644) - if f != nil { - defer f.Close() - fmt.Fprintf(f, msg+"\n", a...) - } -} - -func DebugGetLogs() string { - buf, _ := os.ReadFile("/tmp/debug.log") - return string(buf) -} - func SetupGiteaTestEnv() { if giteaTestSourceRoot != nil { return // already initialized @@ -75,14 +62,11 @@ func SetupGiteaTestEnv() { giteaConf := os.Getenv("GITEA_TEST_CONF") if giteaConf == "" { // if no GITEA_TEST_CONF, then it is in unit test, use a temp (non-existing / empty) config file + // do not really use such config file, the test can run concurrently, using the same config file will cause data-race between tests giteaConf = "custom/conf/app-test-tmp.ini" customConfBuiltin = filepath.Join(AppWorkPath, giteaConf) CustomConf = customConfBuiltin - _ = os.WriteFile(CustomConf, []byte(os.Getenv("GITEA_TEST_CONF_CONTENT")), 0o644) - - // FIXME: debug - log.Error("SetupGiteaTestEnv CustomConf=%s\n%s\n%s\n", CustomConf, os.Getenv("GITEA_TEST_CONF_CONTENT"), log.Stack(2)) - DebugAppendLog("SetupGiteaTestEnv CustomConf=%s\n%s\n%s\n", CustomConf, os.Getenv("GITEA_TEST_CONF_CONTENT"), log.Stack(2)) + _ = os.Remove(CustomConf) } else { // CustomConf must be absolute path to make tests pass, CustomConf = filepath.Join(AppWorkPath, giteaConf)