vcom/internal/transfer/helpers_test.go
vrubelroman 76ae6c3a87 feat(transfer): request/plan types, FSSource and Prepare (3.1)
Prepare refuses a read-only or non-directory target, a root that is the
same file as its target (also through a symlinked directory: N-P0-6) and a
directory copied into itself; walks the source once (links as links, or
followed with loop detection); lists each existing target directory once
instead of a stat per file (N-P2-4/5); fills Existing and the per-file
Conflicts and computes exact TotalFiles/TotalBytes (N-P1-13).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 03:56:21 +03:00

99 lines
2.2 KiB
Go

package transfer
import (
"context"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"vcom/internal/testutil/fstree"
"vcom/internal/vfs"
"vcom/internal/vfs/localfs"
"vcom/internal/vfs/vfstest"
)
var bg = context.Background()
var local = localfs.New()
// counter is a FaultFS hook that counts operations (and optionally fails).
type counter struct {
mu sync.Mutex
n map[vfstest.Op]int
}
func (c *counter) hook(op vfstest.Op, _ string) error {
c.mu.Lock()
defer c.mu.Unlock()
if c.n == nil {
c.n = map[vfstest.Op]int{}
}
c.n[op]++
return nil
}
func (c *counter) get(op vfstest.Op) int {
c.mu.Lock()
defer c.mu.Unlock()
return c.n[op]
}
// otherVolume makes a local FS look like a different volume (as a remote
// would): no SameConn, no identity checks.
type otherVolume struct{ vfs.FS }
func (otherVolume) Volume() vfs.VolumeID { return "other" }
func (otherVolume) Kind() vfs.Kind { return vfs.KindSFTP }
// layout builds <tmp>/src and <tmp>/dst from specs and returns their paths.
func layout(t *testing.T, src, dst fstree.Spec) (string, string) {
t.Helper()
d := t.TempDir()
s, ds := filepath.Join(d, "src"), filepath.Join(d, "dst")
for _, p := range []string{s, ds} {
if err := os.MkdirAll(p, 0o755); err != nil {
t.Fatal(err)
}
}
fstree.Build(t, s, src)
fstree.Build(t, ds, dst)
return s, ds
}
func req(op Op, from vfs.FS, paths []string, to vfs.FS, toDir string, mut ...func(*Options)) Request {
o := DefaultOptions()
for _, f := range mut {
f(&o)
}
return Request{Op: op, From: FSSource(from, paths, o.Symlinks), To: to, ToDir: toDir, Opts: o}
}
func join(dir string, names ...string) []string {
out := make([]string, len(names))
for i, n := range names {
out[i] = filepath.Join(dir, n)
}
return out
}
// noLeftovers fails if any .vcom-part-* / .vcom-old-* remains under dir.
func noLeftovers(t *testing.T, dir string) {
t.Helper()
_ = filepath.Walk(dir, func(p string, fi os.FileInfo, err error) error {
if err == nil && (strings.HasPrefix(fi.Name(), partPrefix) || strings.HasPrefix(fi.Name(), oldPrefix)) {
t.Errorf("temp file left behind: %s", p)
}
return nil
})
}
func read(t *testing.T, p string) string {
t.Helper()
b, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
return string(b)
}