Files
w3cjandClaude Opus 5.5 c41ca82f6e fix(project): devvm's own folders are compared by identity; a launch warns of a project in an agent's folder
project.Unsafe's Holds and Inside compared a project folder with the home
folder and devvm's config, data and Lima folders by name alone
(paths.Within, paths.Below), so on a volume that ignores case, as a
Mac's does by default, a project at ~/.Config/devvm/layers passed both
and its VM got devvm's settings and layer scripts read-write. They now
compare by identity too: paths.In, the vm package's isIn moved to its
owner, finds the path or a folder above it to be the same folder as the
other, by device and inode, and paths.Under is In for the path's
folder. An empty folder holds nothing, so an unset one can't match the
working folder. sshagent's mountedBy, which refuses a key in a folder a
VM mounts, compared by name in the same way and uses paths.In now.

Each launch (Launch.Up, beside warnMounted) warns once, in one line,
when the project folder it mounts is inside an agent's folder in the
home folder or holds one (project_agent_folder), naming every such
folder from the agents' registry, with agent_import_project_inside's
detection (overlapsAgent). A --clone VM mounts nothing and gets no
warning.

Tests: TestUnsafeIgnoresCase, TestInAndUnder, TestMountedKeyIgnoresCase,
TestWarnAgentFolders; the case checks skip on a volume that tells
names apart by case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-19 09:05:20 -07:00

114 lines
3.4 KiB
Go

package paths
import (
"os"
"path/filepath"
"testing"
)
// Physical resolves every link as far as the path exists; PhysicalParent
// only the folder's.
func TestPhysical(t *testing.T) {
root, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
dir := filepath.Join(root, "dir")
if err := os.MkdirAll(filepath.Join(dir, "sub"), 0o700); err != nil {
t.Fatal(err)
}
link := filepath.Join(root, "link")
if err := os.Symlink(dir, link); err != nil {
t.Fatal(err)
}
for _, tc := range []struct{ in, physical, parent string }{
{link, dir, link},
{filepath.Join(link, "sub"), filepath.Join(dir, "sub"), filepath.Join(dir, "sub")},
{filepath.Join(link, "new", "deeper"), filepath.Join(dir, "new", "deeper"), filepath.Join(link, "new", "deeper")},
{filepath.Join(link, "sub", "..", "sub"), filepath.Join(dir, "sub"), filepath.Join(dir, "sub")},
{"/", "/", "/"},
} {
if got := Physical(tc.in); got != tc.physical {
t.Errorf("Physical(%s) = %s, want %s", tc.in, got, tc.physical)
}
if got := PhysicalParent(tc.in); got != tc.parent {
t.Errorf("PhysicalParent(%s) = %s, want %s", tc.in, got, tc.parent)
}
}
if got := Physical("relative/none"); got != "relative/none" {
t.Errorf("Physical of a path that never resolves = %s", got)
}
}
// In and Under find a path in a folder by name, as Within and Below do,
// and by identity too: on a volume that ignores case, such as macOS's, a
// name in another case is the same folder. An empty folder holds nothing.
func TestInAndUnder(t *testing.T) {
root, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
dir := filepath.Join(root, "dir")
if err := os.MkdirAll(filepath.Join(dir, "sub"), 0o700); err != nil {
t.Fatal(err)
}
cases := []struct {
path, dir string
in, under bool
}{
{dir, dir, true, false},
{filepath.Join(dir, "sub"), dir, true, true},
{filepath.Join(dir, "none", "deeper"), dir, true, true},
{dir + "x", dir, false, false},
{root, dir, false, false},
{"/", "/", true, false},
{dir, "", false, false},
}
if _, err := os.Stat(filepath.Join(root, "DIR")); err == nil {
cases = append(cases, []struct {
path, dir string
in, under bool
}{
{filepath.Join(root, "DIR"), dir, true, false},
{filepath.Join(root, "Dir", "sub"), dir, true, true},
{filepath.Join(root, "Dir", "SUB", "none"), dir, true, true},
{dir, filepath.Join(root, "DIR"), true, false},
{filepath.Join(root, "DIRx"), dir, false, false},
}...)
} else {
t.Log("this volume tells names apart by case: only the cases by name ran")
}
for _, tc := range cases {
if got := In(tc.path, tc.dir); got != tc.in {
t.Errorf("In(%s, %s) = %v", tc.path, tc.dir, got)
}
if got := Under(tc.path, tc.dir); got != tc.under {
t.Errorf("Under(%s, %s) = %v", tc.path, tc.dir, got)
}
}
}
// Within is the folder or inside it; Below only inside it.
func TestWithinAndBelow(t *testing.T) {
for _, tc := range []struct {
path, dir string
within, below bool
}{
{"/a/b", "/a/b", true, false},
{"/a/b/c", "/a/b", true, true},
{"/a/b/c", "/a/b/", true, true},
{"/a/bc", "/a/b", false, false},
{"/a", "/a/b", false, false},
{"/a", "/", true, true},
{"/", "/", true, false},
{"a/b", "a", true, true},
} {
if got := Within(tc.path, tc.dir); got != tc.within {
t.Errorf("Within(%s, %s) = %v", tc.path, tc.dir, got)
}
if got := Below(tc.path, tc.dir); got != tc.below {
t.Errorf("Below(%s, %s) = %v", tc.path, tc.dir, got)
}
}
}