A failed write whose temp file cannot be removed (the connection is gone) is remembered by the executor and removed again on Retry (typically after the pool reconnected inside OnError) and at the end of Execute; whatever still cannot be removed is named in Report.Warnings. Prepare now always lists ToDir once (instead of a Lstat per root), so leftovers next to a single top-level file are reported too. TestOverwriteThenDrop checks this strictly, TestOverwriteThenDropRetryLeavesNoTemp covers the retry path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
227 lines
7.6 KiB
Go
227 lines
7.6 KiB
Go
package transfer
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"vcom/internal/testutil/fstree"
|
|
"vcom/internal/vfs"
|
|
"vcom/internal/vfs/vfstest"
|
|
)
|
|
|
|
func TestPrepareCountsBytes(t *testing.T) {
|
|
src, dst := layout(t, fstree.Spec{
|
|
fstree.File("dir/a", "1234567890"),
|
|
fstree.File("dir/sub/b", "").WithSize(1000),
|
|
fstree.Symlink("dir/ln", "a"),
|
|
fstree.FIFO("dir/fifo"),
|
|
fstree.File("top.txt", "abc"),
|
|
}, nil)
|
|
var last Progress
|
|
plan, err := Prepare(bg, req(Copy, local, join(src, "dir", "top.txt"), local, dst), func(p Progress) { last = p })
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if plan.TotalBytes != 10+1000+3 || plan.TotalFiles != 4 {
|
|
t.Fatalf("totals: files=%d bytes=%d (N-P1-13: dir bytes must be counted)", plan.TotalFiles, plan.TotalBytes)
|
|
}
|
|
if last.Phase != Scanning || last.BytesTotal != plan.TotalBytes {
|
|
t.Fatalf("final scanning progress %+v", last)
|
|
}
|
|
if len(plan.Warnings) != 1 || plan.Warnings[0] != "dir/fifo: special file skipped" {
|
|
t.Fatalf("warnings %v", plan.Warnings)
|
|
}
|
|
var rels []string
|
|
for _, it := range plan.Items {
|
|
rels = append(rels, it.Rel+":"+it.Kind.String())
|
|
}
|
|
want := "[dir:dir dir/a:file dir/fifo:special dir/ln:symlink dir/sub:dir dir/sub/b:file top.txt:file]"
|
|
if fmt.Sprint(rels) != want {
|
|
t.Fatalf("items %v", rels)
|
|
}
|
|
if !plan.SameConn {
|
|
t.Fatal("local→local is SameConn")
|
|
}
|
|
}
|
|
|
|
func TestPrepareRejectsSameFile(t *testing.T) {
|
|
src, _ := layout(t, fstree.Spec{fstree.File("real/f.txt", "ONLY COPY"), fstree.Symlink("alias", "real")}, nil)
|
|
cases := map[string]Request{
|
|
"same dir": req(Copy, local, join(src, "real/f.txt"), local, filepath.Join(src, "real")),
|
|
"via dir alias": req(Copy, local, join(src, "real/f.txt"), local, filepath.Join(src, "alias")),
|
|
"dir onto alias": req(Move, local, join(src, "real"), local, src),
|
|
}
|
|
for name, r := range cases {
|
|
if _, err := Prepare(bg, r, nil); !errors.Is(err, ErrSameFile) {
|
|
t.Errorf("%s: err = %v, want ErrSameFile (N-P0-6)", name, err)
|
|
}
|
|
}
|
|
if read(t, filepath.Join(src, "real", "f.txt")) != "ONLY COPY" {
|
|
t.Fatal("source touched")
|
|
}
|
|
}
|
|
|
|
func TestPrepareRejectsIntoItself(t *testing.T) {
|
|
src, _ := layout(t, fstree.Spec{fstree.Dir("proj/sub/deep"), fstree.Symlink("plink", "proj/sub")}, nil)
|
|
for _, to := range []string{
|
|
filepath.Join(src, "proj"),
|
|
filepath.Join(src, "proj", "sub", "deep"),
|
|
filepath.Join(src, "plink"), // inside through a symlink
|
|
} {
|
|
if _, err := Prepare(bg, req(Copy, local, join(src, "proj"), local, to), nil); !errors.Is(err, ErrIntoItself) {
|
|
t.Errorf("to %s: err = %v, want ErrIntoItself", to, err)
|
|
}
|
|
}
|
|
// A sibling with a common prefix is fine.
|
|
fstree.Build(t, src, fstree.Spec{fstree.Dir("project")})
|
|
if _, err := Prepare(bg, req(Copy, local, join(src, "proj"), local, filepath.Join(src, "project")), nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestPrepareConflictsPerFile(t *testing.T) {
|
|
src, dst := layout(t, fstree.Spec{
|
|
fstree.File("proj/same.txt", "new"),
|
|
fstree.File("proj/new.txt", "n"),
|
|
fstree.File("proj/isdir", "file in src"),
|
|
fstree.Dir("proj/isfile"),
|
|
fstree.File("proj/isfile/x", ""),
|
|
fstree.File("proj/sub/deeper.txt", "d"),
|
|
}, fstree.Spec{
|
|
fstree.File("proj/same.txt", "old"),
|
|
fstree.Dir("proj/isdir"),
|
|
fstree.File("proj/isfile", "file in dst"),
|
|
fstree.File("proj/sub/deeper.txt", "old"),
|
|
fstree.File("proj/only-in-dst", ""),
|
|
fstree.File("proj/.vcom-part-deadbeef-x", ""),
|
|
})
|
|
plan, err := Prepare(bg, req(Copy, local, join(src, "proj"), local, dst), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := map[string]ConflictKind{}
|
|
for _, c := range plan.Conflicts {
|
|
got[c.Item.Rel] = c.Kind
|
|
}
|
|
want := map[string]ConflictKind{
|
|
"proj/same.txt": FileFile,
|
|
"proj/isdir": TypeMismatch,
|
|
"proj/isfile": TypeMismatch,
|
|
"proj/sub/deeper.txt": FileFile,
|
|
}
|
|
if fmt.Sprint(got) != fmt.Sprint(want) {
|
|
t.Fatalf("conflicts %v, want %v", got, want)
|
|
}
|
|
for _, it := range plan.Items {
|
|
if (it.Rel == "proj/new.txt" || it.Rel == "proj/isfile/x") && it.Existing != nil {
|
|
t.Errorf("%s has Existing", it.Rel)
|
|
}
|
|
if it.Rel == "proj" && (it.Existing == nil || !it.Existing.IsDir()) {
|
|
t.Error("root dir merge target not found")
|
|
}
|
|
}
|
|
if len(plan.Warnings) != 1 {
|
|
t.Fatalf("leftover warning expected: %v", plan.Warnings)
|
|
}
|
|
}
|
|
|
|
func TestPrepareReadDirPerDirNotPerFile(t *testing.T) {
|
|
var spec, dspec fstree.Spec
|
|
for i := range 1000 {
|
|
spec = append(spec, fstree.File(fmt.Sprintf("d/f%04d", i), ""))
|
|
if i%2 == 0 {
|
|
dspec = append(dspec, fstree.File(fmt.Sprintf("d/f%04d", i), ""))
|
|
}
|
|
}
|
|
src, dst := layout(t, spec, dspec)
|
|
c := &counter{}
|
|
to := &vfstest.FaultFS{FS: local, Hook: c.hook}
|
|
plan, err := Prepare(bg, req(Copy, local, join(src, "d"), to, dst), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(plan.Conflicts) != 500 {
|
|
t.Fatalf("%d conflicts", len(plan.Conflicts))
|
|
}
|
|
if n := c.get(vfstest.OpReadDir); n != 2 { // ToDir and d
|
|
t.Fatalf("%d ReadDir calls on the target, want 2: one per directory (N-P2-4/5)", n)
|
|
}
|
|
if n := c.get(vfstest.OpLstat) + c.get(vfstest.OpStat); n > 4 {
|
|
t.Fatalf("%d stat calls on the target", n)
|
|
}
|
|
|
|
// Many roots: one listing of ToDir, no stat per root.
|
|
var roots fstree.Spec
|
|
var names []string
|
|
for i := range 50 {
|
|
n := fmt.Sprintf("r%02d", i)
|
|
roots = append(roots, fstree.File(n, ""))
|
|
names = append(names, n)
|
|
}
|
|
src, dst = layout(t, roots, nil)
|
|
c = &counter{}
|
|
to = &vfstest.FaultFS{FS: local, Hook: c.hook}
|
|
if _, err := Prepare(bg, req(Copy, local, join(src, names...), to, dst), nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.get(vfstest.OpReadDir) != 1 || c.get(vfstest.OpLstat) > 0 {
|
|
t.Fatalf("many roots: readdir=%d lstat=%d", c.get(vfstest.OpReadDir), c.get(vfstest.OpLstat))
|
|
}
|
|
}
|
|
|
|
func TestPrepareCancel(t *testing.T) {
|
|
var spec fstree.Spec
|
|
for i := range 50 {
|
|
spec = append(spec, fstree.File(fmt.Sprintf("d/s%02d/f", i), ""))
|
|
}
|
|
src, dst := layout(t, spec, nil)
|
|
ctx, cancel := context.WithCancel(bg)
|
|
n := 0
|
|
from := &vfstest.FaultFS{FS: local, Hook: func(op vfstest.Op, _ string) error {
|
|
if op == vfstest.OpReadDir {
|
|
if n++; n == 5 {
|
|
cancel()
|
|
}
|
|
}
|
|
return nil
|
|
}}
|
|
_, err := Prepare(ctx, req(Copy, from, join(src, "d"), local, dst), nil)
|
|
if !errors.Is(err, vfs.ErrCancelled) {
|
|
t.Fatalf("err = %v, want cancelled", err)
|
|
}
|
|
if n > 6 {
|
|
t.Fatalf("walk continued after cancel: %d ReadDirs", n)
|
|
}
|
|
}
|
|
|
|
func TestPrepareRejectsBadTargets(t *testing.T) {
|
|
src, dst := layout(t, fstree.Spec{fstree.File("a", ""), fstree.File("x/a", "")}, fstree.Spec{fstree.File("file", "")})
|
|
if _, err := Prepare(bg, req(Copy, local, join(src, "a"), vfs.ReadOnly(local), dst), nil); !errors.Is(err, ErrReadOnly) {
|
|
t.Fatalf("read-only target: %v", err)
|
|
}
|
|
if _, err := Prepare(bg, req(Copy, local, join(src, "a"), local, filepath.Join(dst, "file")), nil); !errors.Is(err, ErrTargetNotDir) {
|
|
t.Fatalf("file target: %v", err)
|
|
}
|
|
if _, err := Prepare(bg, req(Copy, local, join(src, "a", "x/a"), local, dst), nil); !errors.Is(err, ErrDuplicateName) {
|
|
t.Fatalf("duplicate names: %v", err)
|
|
}
|
|
if _, err := Prepare(bg, req(Move, vfs.ReadOnly(local), join(src, "a"), local, dst), nil); !errors.Is(err, ErrReadOnly) {
|
|
t.Fatalf("move from read-only: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPrepareWarnsAboutLeftoversInToDir(t *testing.T) {
|
|
src, dst := layout(t, fstree.Spec{fstree.File("important.db", "new")},
|
|
fstree.Spec{fstree.File("important.db", "old"), fstree.File(".vcom-part-0badc0de-important.db", "half")})
|
|
plan, err := Prepare(bg, req(Copy, local, join(src, "important.db"), local, dst, overwrite), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(plan.Warnings) != 1 || !strings.Contains(plan.Warnings[0], ".vcom-part-0badc0de-important.db") {
|
|
t.Fatalf("warnings %v: a leftover next to a single root must be reported", plan.Warnings)
|
|
}
|
|
}
|