mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
test: fix TestWatcher_SharedParentRefcount on macOS (#25379)
`TestWatcher_SharedParentRefcount` was deterministically broken on macOS: `t.TempDir()` lives under `/var` which is a symlink to `/private/var`, but the watcher canonicalizes paths via `filepath.EvalSymlinks` before storing them, so the test's `w.dirs[dir]` lookup missed and returned `0` instead of `2`. Adds `testutil.TempDirResolved`, a shared helper that returns `t.TempDir()` with symlinks resolved and falls back to the raw temp dir on error (Windows-friendly). Migrates the matching inline `EvalSymlinks(t.TempDir())` callsites in `agent/agentgit/agentgit_test.go` to use it. Closes https://github.com/coder/internal/issues/1531
This commit is contained in:
@@ -2,6 +2,7 @@ package testutil
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -51,3 +52,22 @@ func CreateTemp(t *testing.T, dir, pattern string) *os.File {
|
||||
})
|
||||
return f
|
||||
}
|
||||
|
||||
// TempDirResolved returns t.TempDir() with symlinks resolved via
|
||||
// filepath.EvalSymlinks. Tests that compare paths against values
|
||||
// processed by EvalSymlinks (directly or indirectly) should use
|
||||
// this helper so the comparison works on macOS, where the default
|
||||
// temp dir lives under /var which is a symlink to /private/var.
|
||||
//
|
||||
// If EvalSymlinks errors (for example on Windows where the temp
|
||||
// path may not resolve cleanly), the raw t.TempDir() result is
|
||||
// returned. This matches the lenient behavior already used in
|
||||
// existing tests.
|
||||
func TempDirResolved(t *testing.T) string {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
if resolved, err := filepath.EvalSymlinks(dir); err == nil {
|
||||
return resolved
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user