package web import ( "os" "path/filepath" "runtime" "testing" ) // moduleRoot 返回仓库根目录(含 go.mod 的那层)。 // // 不依赖当前工作目录:测试进程的 cwd 会被 newMovementFixture → chdirModuleRoot 改成仓库根, // 而用例执行顺序并不保证——于是「相对路径」到底相对谁,取决于这条用例有没有跟夹具用例一起跑。 // 读源码的静态守卫一旦踩上这个,单跑是绿的、混跑就报「找不到文件」,或者更糟: // filepath.Walk(".") 扫的子树悄悄从 internal/web 变成整个仓库,覆盖面变了却没人知道。 // 一律从本测试文件的真实位置往上找 go.mod。 func moduleRoot(t *testing.T) string { t.Helper() _, this, _, ok := runtime.Caller(0) if !ok { t.Fatal("拿不到本测试文件的路径") } dir := filepath.Dir(this) for { if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { return dir } parent := filepath.Dir(dir) if parent == dir { t.Fatal("找不到 go.mod,无法定位仓库根目录") } dir = parent } } // pkgFile 返回本包(internal/web)内某个源文件的绝对路径。 func pkgFile(t *testing.T, name string) string { t.Helper() return filepath.Join(moduleRoot(t), "internal", "web", name) }