vcom/internal/vfs/vfs_test.go
vrubelroman 2db51e9385 feat(vfs): define the vfs.FS contract, sentinels, PathOps and ReadOnly wrapper (2.0)
Types and interfaces from design §2.1: FS, File, FileInfo, Caps, VolumeID,
RenameMode, CreateMode, PathOps (posix and native), Location.Same, the
optional Trasher/BirthTimer/Execer interfaces and ReadOnly(fs).

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

189 lines
6 KiB
Go

package vfs
import (
"context"
"errors"
"io/fs"
"testing"
"time"
)
// recFS is a fake FS that records which methods reached it.
type recFS struct{ calls []string }
func (r *recFS) rec(op string) { r.calls = append(r.calls, op) }
func (r *recFS) Kind() Kind { return KindLocal }
func (r *recFS) Volume() VolumeID { return "fake" }
func (r *recFS) Caps() Caps { return Caps{Trash: true, Exec: true, Chmod: true} }
func (r *recFS) Label() string { return "fake" }
func (r *recFS) Paths() PathOps { return PosixPaths }
func (r *recFS) Stat(context.Context, string) (FileInfo, error) {
r.rec("stat")
return FileInfo{}, nil
}
func (r *recFS) Lstat(context.Context, string) (FileInfo, error) {
r.rec("lstat")
return FileInfo{}, nil
}
func (r *recFS) ReadDir(context.Context, string) ([]FileInfo, error) {
r.rec("readdir")
return nil, nil
}
func (r *recFS) ReadLink(context.Context, string) (string, error) { r.rec("readlink"); return "", nil }
func (r *recFS) RealPath(context.Context, string) (string, error) { r.rec("realpath"); return "", nil }
func (r *recFS) Open(context.Context, string) (File, error) { r.rec("open"); return fakeFile{}, nil }
func (r *recFS) Create(context.Context, string, fs.FileMode, CreateMode) (File, error) {
r.rec("create")
return fakeFile{}, nil
}
func (r *recFS) Mkdir(context.Context, string, fs.FileMode) error { r.rec("mkdir"); return nil }
func (r *recFS) Remove(context.Context, string) error { r.rec("remove"); return nil }
func (r *recFS) Rename(context.Context, string, string, RenameMode) error {
r.rec("rename")
return nil
}
func (r *recFS) Symlink(context.Context, string, string) error { r.rec("symlink"); return nil }
func (r *recFS) Chmod(context.Context, string, fs.FileMode) error { r.rec("chmod"); return nil }
func (r *recFS) Chtimes(context.Context, string, time.Time, time.Time) error {
r.rec("chtimes")
return nil
}
func (r *recFS) Trash(context.Context, string) error { r.rec("trash"); return nil }
type fakeFile struct{}
func (fakeFile) Read([]byte) (int, error) { return 0, nil }
func (fakeFile) Write(b []byte) (int, error) { return len(b), nil }
func (fakeFile) ReadAt([]byte, int64) (int, error) { return 0, nil }
func (fakeFile) Close() error { return nil }
func (fakeFile) Stat() (FileInfo, error) { return FileInfo{}, nil }
func (fakeFile) Sync() error { return nil }
func TestReadOnlyRejectsMutations(t *testing.T) {
ctx := context.Background()
inner := &recFS{}
ro := ReadOnly(inner)
if !ro.Caps().ReadOnly || ro.Caps().Trash || ro.Caps().Exec {
t.Fatalf("caps = %+v, want ReadOnly and no Trash/Exec", ro.Caps())
}
if !ro.Caps().Chmod {
t.Fatalf("other caps must pass through: %+v", ro.Caps())
}
if _, ok := ro.(Trasher); ok {
t.Fatal("read-only wrapper must not expose Trasher")
}
_, errCreate := ro.Create(ctx, "/a", 0o644, CreateExcl)
muts := map[string]error{
"create": errCreate,
"mkdir": ro.Mkdir(ctx, "/a", 0o755),
"remove": ro.Remove(ctx, "/a"),
"rename": ro.Rename(ctx, "/a", "/b", Replace),
"symlink": ro.Symlink(ctx, "/a", "/b"),
"chmod": ro.Chmod(ctx, "/a", 0o600),
"chtimes": ro.Chtimes(ctx, "/a", time.Now(), time.Now()),
}
for op, err := range muts {
if !errors.Is(err, ErrReadOnly) {
t.Errorf("%s: err = %v, want ErrReadOnly", op, err)
}
}
if len(inner.calls) != 0 {
t.Fatalf("mutations reached the inner FS: %v", inner.calls)
}
// Reads pass through; files opened through the wrapper refuse writes.
if _, err := ro.Stat(ctx, "/a"); err != nil {
t.Fatal(err)
}
if _, err := ro.ReadDir(ctx, "/"); err != nil {
t.Fatal(err)
}
f, err := ro.Open(ctx, "/a")
if err != nil {
t.Fatal(err)
}
if _, err := f.Write([]byte("x")); !errors.Is(err, ErrReadOnly) {
t.Fatalf("write through read-only open: %v", err)
}
if got := inner.calls; len(got) != 3 || got[0] != "stat" || got[1] != "readdir" || got[2] != "open" {
t.Fatalf("inner calls = %v", got)
}
if ReadOnly(ro) != ro {
t.Fatal("ReadOnly must not double-wrap")
}
}
func TestPosixPathOps(t *testing.T) {
p := PosixPaths
if p.Sep() != '/' {
t.Fatal("sep")
}
cases := []struct{ got, want string }{
{p.Join("/a", "b", "../c"), "/a/c"},
{p.Join("a", "", "b"), "a/b"},
{p.Dir("/a/b/c"), "/a/b"},
{p.Dir("/"), "/"},
{p.Dir("file"), "."},
{p.Base("/a/b/"), "b"},
{p.Base("/"), "/"},
{p.Clean("/a//b/./c/.."), "/a/b"},
{p.Clean(""), "."},
{p.Join("/srv", `back\slash`), `/srv/back\slash`}, // backslash is a normal char in posix
}
for i, c := range cases {
if c.got != c.want {
t.Errorf("case %d: got %q, want %q", i, c.got, c.want)
}
}
if !p.IsAbs("/x") || p.IsAbs("x") {
t.Error("IsAbs")
}
within := []struct {
child, parent string
want bool
}{
{"/a/b", "/a", true},
{"/a", "/a/", true},
{"/ab", "/a", false},
{"/a/../b", "/a", false},
{"/anything", "/", true},
}
for _, c := range within {
if got := IsWithin(p, c.child, c.parent); got != c.want {
t.Errorf("IsWithin(%q, %q) = %v", c.child, c.parent, got)
}
}
}
func TestLocationSame(t *testing.T) {
a := &recFS{}
l1 := Location{FS: a, Path: "/x/y/"}
l2 := Location{FS: ReadOnly(a), Path: "/x/./y"}
if !l1.Same(l2) {
t.Fatal("same volume and cleaned path must be Same")
}
if l1.Same(Location{FS: a, Path: "/x"}) {
t.Fatal("different paths")
}
}
func TestFileInfoPredicates(t *testing.T) {
dir := FileInfo{Mode: fs.ModeDir | 0o755}
file := FileInfo{Mode: 0o644}
linkDir := FileInfo{Mode: fs.ModeSymlink, Target: &dir}
linkFile := FileInfo{Mode: fs.ModeSymlink, Target: &file}
broken := FileInfo{Mode: fs.ModeSymlink, LinkErr: ErrNotExist}
fifo := FileInfo{Mode: fs.ModeNamedPipe}
if !dir.NavigableDir() || !linkDir.NavigableDir() || linkFile.NavigableDir() || broken.NavigableDir() {
t.Error("NavigableDir")
}
if linkDir.IsDir() || !linkDir.IsSymlink() {
t.Error("a link to a dir is not itself a dir")
}
if !file.IsRegularLike() || !linkFile.IsRegularLike() || fifo.IsRegularLike() || broken.IsRegularLike() {
t.Error("IsRegularLike")
}
}