package repository import ( "os" "testing" "lokideploy/config" ) // TestMain 为 repository 包预置安全环境变量与临时 DATA_DIR,并管理 JsonDB 生命周期。 // config.GetConfig() 在 JWT_SECRET/ENCRYPTION_KEY 缺失时会 log.Fatal,故须先设置。 func TestMain(m *testing.M) { dir, err := os.MkdirTemp("", "lokideploy-repository-test") if err != nil { panic(err) } os.Setenv("JWT_SECRET", "repo-test-jwt-secret-32chars-min!!") os.Setenv("ENCRYPTION_KEY", "repo-test-encryption-key-32chars!!") os.Setenv("DATA_DIR", dir) if err := InitDB(); err != nil { panic(err) } code := m.Run() _ = Shutdown() // 优雅刷盘 + 停止 saveWorker _ = os.RemoveAll(dir) os.Exit(code) } // dbPath 返回当前测试用的数据库文件路径。 func dbPath() string { return config.GetConfig().DatabasePath }