mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
fix: align test fixtures with v2.10 RPC and Windows path semantics
This commit is contained in:
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -292,11 +292,15 @@ func (m *Manager) AddSource(s Source) (Source, error) {
|
|||||||
|
|
||||||
// RemoveSource removes the source matching path. Path is
|
// RemoveSource removes the source matching path. Path is
|
||||||
// canonicalized before matching. Returns ErrSourceNotFound when
|
// 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 {
|
func (m *Manager) RemoveSource(path string) error {
|
||||||
canonical, err := CanonicalizePath(path)
|
canonical, err := CanonicalizePath(path)
|
||||||
if err != nil {
|
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()
|
m.mu.Lock()
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package agentcontext_test
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -29,22 +30,29 @@ func TestCanonicalizePath_RelativeRejected(t *testing.T) {
|
|||||||
|
|
||||||
//nolint:paralleltest,tparallel // Uses t.Setenv.
|
//nolint:paralleltest,tparallel // Uses t.Setenv.
|
||||||
func TestCanonicalizePath_TildeExpansion(t *testing.T) {
|
func TestCanonicalizePath_TildeExpansion(t *testing.T) {
|
||||||
t.Setenv("HOME", "/tmp/home")
|
home := t.TempDir()
|
||||||
|
switchHomeEnv(t, home)
|
||||||
got, err := agentcontext.CanonicalizePath("~/.coder")
|
got, err := agentcontext.CanonicalizePath("~/.coder")
|
||||||
require.NoError(t, err)
|
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.
|
//nolint:paralleltest,tparallel // Uses t.Setenv.
|
||||||
func TestCanonicalizePath_BareTildeExpandsToHome(t *testing.T) {
|
func TestCanonicalizePath_BareTildeExpandsToHome(t *testing.T) {
|
||||||
t.Setenv("HOME", "/tmp/home")
|
home := t.TempDir()
|
||||||
|
switchHomeEnv(t, home)
|
||||||
got, err := agentcontext.CanonicalizePath("~")
|
got, err := agentcontext.CanonicalizePath("~")
|
||||||
require.NoError(t, err)
|
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) {
|
func TestCanonicalizePath_FollowsSymlinks(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
t.Skip("os.Symlink requires developer mode or admin on Windows")
|
||||||
|
}
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
realDir := filepath.Join(dir, "real")
|
realDir := filepath.Join(dir, "real")
|
||||||
link := filepath.Join(dir, "link")
|
link := filepath.Join(dir, "link")
|
||||||
@@ -62,7 +70,12 @@ func TestCanonicalizePath_FollowsSymlinks(t *testing.T) {
|
|||||||
|
|
||||||
func TestValidateSourcePath_RejectsParentSegments(t *testing.T) {
|
func TestValidateSourcePath_RejectsParentSegments(t *testing.T) {
|
||||||
t.Parallel()
|
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.Error(t, err)
|
||||||
require.Contains(t, err.Error(), "parent traversal")
|
require.Contains(t, err.Error(), "parent traversal")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
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)
|
require.NoError(t, err)
|
||||||
defer func() {
|
defer func() {
|
||||||
cErr := aAPI.DRPCConn().Close()
|
cErr := aAPI.DRPCConn().Close()
|
||||||
|
|||||||
Reference in New Issue
Block a user