fix: align test fixtures with v2.10 RPC and Windows path semantics

This commit is contained in:
Kyle Carberry
2026-06-02 14:55:35 +00:00
parent 37d8cb136a
commit 0d382264e5
4 changed files with 44 additions and 8 deletions
+19
View File
@@ -0,0 +1,19 @@
package agentcontext_test
import (
"runtime"
"testing"
)
// switchHomeEnv overrides the platform-specific environment variable
// consulted by os.UserHomeDir for the duration of the test. Windows
// reads USERPROFILE; Linux and macOS read HOME.
func switchHomeEnv(t *testing.T, dir string) {
t.Helper()
switch runtime.GOOS {
case "windows":
t.Setenv("USERPROFILE", dir)
default:
t.Setenv("HOME", dir)
}
}
+6 -2
View File
@@ -292,11 +292,15 @@ func (m *Manager) AddSource(s Source) (Source, error) {
// RemoveSource removes the source matching path. Path is
// canonicalized before matching. Returns ErrSourceNotFound when
// no such source exists.
// no such source exists or when the path cannot be canonicalized.
func (m *Manager) RemoveSource(path string) error {
canonical, err := CanonicalizePath(path)
if err != nil {
return xerrors.Errorf("canonicalize: %w", err)
// A path that does not canonicalize cannot match any
// existing source. Mirror HasSource semantics by
// reporting not-found rather than leaking the
// canonicalize error to API callers.
return ErrSourceNotFound
}
m.mu.Lock()
+18 -5
View File
@@ -3,6 +3,7 @@ package agentcontext_test
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
@@ -29,22 +30,29 @@ func TestCanonicalizePath_RelativeRejected(t *testing.T) {
//nolint:paralleltest,tparallel // Uses t.Setenv.
func TestCanonicalizePath_TildeExpansion(t *testing.T) {
t.Setenv("HOME", "/tmp/home")
home := t.TempDir()
switchHomeEnv(t, home)
got, err := agentcontext.CanonicalizePath("~/.coder")
require.NoError(t, err)
require.Equal(t, "/tmp/home/.coder", got)
require.Equal(t, filepath.Join(home, ".coder"), got)
}
//nolint:paralleltest,tparallel // Uses t.Setenv.
func TestCanonicalizePath_BareTildeExpandsToHome(t *testing.T) {
t.Setenv("HOME", "/tmp/home")
home := t.TempDir()
switchHomeEnv(t, home)
got, err := agentcontext.CanonicalizePath("~")
require.NoError(t, err)
require.Equal(t, "/tmp/home", got)
want, err := filepath.EvalSymlinks(home)
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestCanonicalizePath_FollowsSymlinks(t *testing.T) {
t.Parallel()
if runtime.GOOS == "windows" {
t.Skip("os.Symlink requires developer mode or admin on Windows")
}
dir := t.TempDir()
realDir := filepath.Join(dir, "real")
link := filepath.Join(dir, "link")
@@ -62,7 +70,12 @@ func TestCanonicalizePath_FollowsSymlinks(t *testing.T) {
func TestValidateSourcePath_RejectsParentSegments(t *testing.T) {
t.Parallel()
err := agentcontext.ValidateSourcePath("/a/../b", []string{"/a"})
root := t.TempDir()
// Build /a/../b underneath a real allowed root so the path is
// absolute on every platform. Validation must still reject the
// embedded ".." segment before it ever touches allowedRoots.
bad := filepath.Join(root, "a") + string(os.PathSeparator) + ".." + string(os.PathSeparator) + "b"
err := agentcontext.ValidateSourcePath(bad, []string{root})
require.Error(t, err)
require.Contains(t, err.Error(), "parent traversal")
}
+1 -1
View File
@@ -3184,7 +3184,7 @@ func requireGetManifest(ctx context.Context, t testing.TB, aAPI agentproto.DRPCA
}
func postStartup(ctx context.Context, t testing.TB, client agent.Client, startup *agentproto.Startup) error {
aAPI, _, err := client.ConnectRPC29(ctx)
aAPI, _, err := client.ConnectRPC210(ctx)
require.NoError(t, err)
defer func() {
cErr := aAPI.DRPCConn().Close()