vcom/internal/vfs/helpers_test.go
vrubelroman 5132daa292 feat(vfs): Walk, RemoveTree, DirSize, SameFile and Within helpers (2A.2)
One implementation for every FS. Walk and RemoveTree never follow symlinks,
so deleting a link to a directory removes only the link (N-P0-3). DirSize
returns the root error and counts unreadable subdirectories instead of
reporting a silent 0 B (N-P1-14). SameFile compares inodes locally and
RealPath on other volumes, so an alias directory is recognised (N-P0-6);
Within resolves symlinks on both sides and accepts a not-yet-existing child.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 23:24:12 +03:00

268 lines
8.1 KiB
Go

package vfs_test
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"vcom/internal/testutil/fstree"
"vcom/internal/vfs"
"vcom/internal/vfs/localfs"
)
var ctx = context.Background()
func TestRemoveTreeSymlinkToDirKeepsTarget(t *testing.T) {
dir := t.TempDir()
fstree.Build(t, dir, fstree.Spec{
fstree.File("data/precious", "keep me"),
fstree.File("data/sub/also", "keep"),
fstree.Symlink("link", "data"),
fstree.File("tree/f", "x"),
fstree.Symlink("tree/inner", "../data"),
})
fsys := localfs.New()
// Root is a symlink to a directory: only the link goes.
if err := vfs.RemoveTree(ctx, fsys, filepath.Join(dir, "link"), nil); err != nil {
t.Fatal(err)
}
// A tree containing a link to a directory: the link is unlinked, not walked.
if err := vfs.RemoveTree(ctx, fsys, filepath.Join(dir, "tree"), nil); err != nil {
t.Fatal(err)
}
fstree.Equal(t, fstree.FromSpec(fstree.Spec{
fstree.File("data/precious", "keep me"),
fstree.File("data/sub/also", "keep"),
}), fstree.Snapshot(t, dir), fstree.Opts{Links: true})
}
func TestRemoveTreeCancel(t *testing.T) {
dir := t.TempDir()
var spec fstree.Spec
for i := range 20 {
spec = append(spec, fstree.File(fmt.Sprintf("big/f%02d", i), "x"))
}
fstree.Build(t, dir, spec)
c, cancel := context.WithCancel(ctx)
defer cancel()
removed := 0
err := vfs.RemoveTree(c, localfs.New(), filepath.Join(dir, "big"), func(n int) {
removed = n
if n == 3 {
cancel()
}
})
if !errors.Is(err, vfs.ErrCancelled) {
t.Fatalf("err = %v, want ErrCancelled", err)
}
if removed != 3 {
t.Fatalf("removed %d after cancel, want 3", removed)
}
left, _ := os.ReadDir(filepath.Join(dir, "big"))
if len(left) != 17 {
t.Fatalf("%d entries left, want 17", len(left))
}
}
func TestRemoveTreeProgressAndNonEmpty(t *testing.T) {
dir := t.TempDir()
fstree.Build(t, dir, fstree.Spec{fstree.File("a/b/c", ""), fstree.File("a/d", "")})
var last int
if err := vfs.RemoveTree(ctx, localfs.New(), filepath.Join(dir, "a"), func(n int) { last = n }); err != nil {
t.Fatal(err)
}
if last != 4 { // c, b, d, a
t.Fatalf("progress = %d, want 4", last)
}
if _, err := os.Lstat(filepath.Join(dir, "a")); !errors.Is(err, os.ErrNotExist) {
t.Fatal("tree still there")
}
if err := vfs.RemoveTree(ctx, localfs.New(), filepath.Join(dir, "missing"), nil); !errors.Is(err, vfs.ErrNotExist) {
t.Fatalf("missing root: %v", err)
}
}
func TestDirSizeRootError(t *testing.T) {
dir := t.TempDir()
_, err := vfs.DirSize(ctx, localfs.New(), filepath.Join(dir, "missing"), nil)
if !errors.Is(err, vfs.ErrNotExist) {
t.Fatalf("missing root: err = %v, want ErrNotExist (not a silent 0 B)", err)
}
if os.Geteuid() != 0 {
fstree.Build(t, dir, fstree.Spec{fstree.File("locked/f", "x")})
lock(t, filepath.Join(dir, "locked"))
if _, err := vfs.DirSize(ctx, localfs.New(), filepath.Join(dir, "locked"), nil); !errors.Is(err, vfs.ErrPermission) {
t.Fatalf("unreadable root: err = %v, want ErrPermission", err)
}
}
c, cancel := context.WithCancel(ctx)
cancel()
if _, err := vfs.DirSize(c, localfs.New(), dir, nil); !errors.Is(err, vfs.ErrCancelled) {
t.Fatalf("cancelled: %v", err)
}
}
// lock makes dir unreadable (mode 0) until the test ends.
func lock(t *testing.T, dir string) {
t.Helper()
if err := os.Chmod(dir, 0); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Chmod(dir, 0o755) })
}
func TestDirSizeCountsUnreadable(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("root reads mode-0 directories")
}
dir := t.TempDir()
fstree.Build(t, dir, fstree.Spec{
fstree.File("a.bin", "").WithSize(100),
fstree.File("ok/b.bin", "").WithSize(20),
fstree.File("locked/hidden", "").WithSize(1000),
fstree.Symlink("ln", "a.bin"),
})
lock(t, filepath.Join(dir, "locked"))
var calls int
r, err := vfs.DirSize(ctx, localfs.New(), dir, func(vfs.SizeResult) { calls++ })
if err != nil {
t.Fatal(err)
}
want := vfs.SizeResult{Bytes: 100 + 20 + int64(len("a.bin")), Files: 3, Dirs: 2, Unreadable: 1}
if r != want {
t.Fatalf("got %+v, want %+v", r, want)
}
if calls == 0 {
t.Fatal("no progress")
}
}
func TestSameFileSymlinkAlias(t *testing.T) {
home := t.TempDir()
fstree.Build(t, home, fstree.Spec{
fstree.File("real/f.txt", "data"),
fstree.File("other/f.txt", "data"),
fstree.Symlink("alias", "real"),
})
fsys := localfs.New()
same, err := vfs.SameFile(ctx, fsys, filepath.Join(home, "real", "f.txt"), fsys, filepath.Join(home, "alias", "f.txt"))
if err != nil || !same {
t.Fatalf("~/real/f.txt vs ~/alias/f.txt: same=%v err=%v, want true (N-P0-6)", same, err)
}
same, err = vfs.SameFile(ctx, fsys, filepath.Join(home, "real"), fsys, filepath.Join(home, "alias"))
if err != nil || !same {
t.Fatalf("dir vs its alias: %v %v", same, err)
}
same, err = vfs.SameFile(ctx, fsys, filepath.Join(home, "real", "f.txt"), fsys, filepath.Join(home, "other", "f.txt"))
if err != nil || same {
t.Fatalf("equal content is not identity: %v %v", same, err)
}
same, err = vfs.SameFile(ctx, fsys, filepath.Join(home, "real", "f.txt"), fsys, filepath.Join(home, "nope"))
if err != nil || same {
t.Fatalf("missing: %v %v", same, err)
}
// Different volume is never the same, even with an identical path.
ro := otherVolume{vfs.ReadOnly(fsys)}
same, err = vfs.SameFile(ctx, fsys, filepath.Join(home, "real"), ro, filepath.Join(home, "real"))
if err != nil || same {
t.Fatalf("different volumes: %v %v", same, err)
}
// Same volume, non-local kind: RealPath comparison.
rp := realPathOnly{vfs.ReadOnly(fsys)}
same, err = vfs.SameFile(ctx, rp, filepath.Join(home, "real", "f.txt"), rp, filepath.Join(home, "alias", "f.txt"))
if err != nil || !same {
t.Fatalf("realpath comparison: %v %v", same, err)
}
}
type otherVolume struct{ vfs.FS }
func (otherVolume) Volume() vfs.VolumeID { return "other" }
type realPathOnly struct{ vfs.FS }
func (realPathOnly) Kind() vfs.Kind { return vfs.KindSFTP }
func TestWithinViaSymlink(t *testing.T) {
home := t.TempDir()
fstree.Build(t, home, fstree.Spec{
fstree.Dir("src/sub"),
fstree.Dir("srcsibling"),
fstree.Symlink("alias", "src"),
fstree.Symlink("sublink", "src/sub"),
})
fsys := localfs.New()
j := func(p ...string) string { return filepath.Join(append([]string{home}, p...)...) }
cases := []struct {
child, parent string
want bool
}{
{j("alias", "sub"), j("src"), true}, // through an alias of the parent
{j("sublink"), j("src"), true}, // a link pointing inside
{j("src"), j("alias"), true}, // equal after resolution
{j("alias", "sub", "new", "deeper"), j("src"), true}, // child does not exist yet
{j("srcsibling"), j("src"), false}, // prefix is not containment
{j("src"), j("src", "sub"), false},
}
for _, c := range cases {
got, err := vfs.Within(ctx, fsys, c.child, c.parent)
if err != nil || got != c.want {
t.Errorf("Within(%s, %s) = %v, %v; want %v",
strings.TrimPrefix(c.child, home), strings.TrimPrefix(c.parent, home), got, err, c.want)
}
}
}
func TestWalkDoesNotFollowLinks(t *testing.T) {
dir := t.TempDir()
fstree.Build(t, dir, fstree.Spec{
fstree.File("root/b", ""),
fstree.File("root/a/x", ""),
fstree.File("root/c/skipme", ""),
fstree.Symlink("root/loop", "."),
fstree.Symlink("root/out", "../outside"),
fstree.File("outside/secret", ""),
})
root := filepath.Join(dir, "root")
var got []string
err := vfs.Walk(ctx, localfs.New(), root, func(p string, fi vfs.FileInfo, err error) error {
if err != nil {
return err
}
rel, _ := filepath.Rel(root, p)
kind := "f"
switch {
case fi.IsSymlink():
kind = "l"
case fi.IsDir():
kind = "d"
}
got = append(got, kind+":"+filepath.ToSlash(rel))
if fi.Name == "c" {
return vfs.SkipDir
}
return nil
})
if err != nil {
t.Fatal(err)
}
want := "d:. d:a f:a/x f:b d:c l:loop l:out"
if strings.Join(got, " ") != want {
t.Fatalf("walk order:\n got %s\nwant %s", strings.Join(got, " "), want)
}
// Missing root: fn gets the error.
var rootErr error
_ = vfs.Walk(ctx, localfs.New(), filepath.Join(dir, "missing"), func(_ string, _ vfs.FileInfo, err error) error {
rootErr = err
return nil
})
if !errors.Is(rootErr, vfs.ErrNotExist) {
t.Fatalf("root error = %v", rootErr)
}
}