test(transfer): conflict policies, runtime conflicts and error handling (3.3)

Ask with ApplyToAll, Abort, OverwriteOlder, KeepBoth naming ("name (1).ext",
.tar.gz kept together, dot-files and dirs unsplit), a file/dir type mismatch
is never overwritten, a target that appears after Prepare is a conflict
(NoReplace commit), Retry after a write error, SkipRest/SkipAll/Abort and
the Report counters and Affected list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-11-01 04:01:36 +03:00
parent 2dd42d0e03
commit 88c67435ed

View file

@ -0,0 +1,247 @@
package transfer
import (
"context"
"errors"
"os"
"path/filepath"
"syscall"
"testing"
"time"
"vcom/internal/testutil/fstree"
"vcom/internal/vfs"
"vcom/internal/vfs/vfstest"
)
func TestConflictAskApplyToAll(t *testing.T) {
src, dst := layout(t, fstree.Spec{
fstree.File("d/a", "new-a"), fstree.File("d/b", "new-b"), fstree.File("d/c", "new-c"), fstree.File("d/n", "n"),
}, fstree.Spec{fstree.File("d/a", "old"), fstree.File("d/b", "old"), fstree.File("d/c", "old")})
var asked []string
_, rep := run(t, req(Copy, local, join(src, "d"), local, dst), Hooks{
Resolve: func(_ context.Context, c Conflict) (Resolution, error) {
asked = append(asked, c.Item.Rel)
if c.Item.Existing == nil || c.Kind != FileFile {
t.Errorf("conflict without Existing/kind: %+v", c)
}
if c.Item.Rel == "d/a" {
return Resolution{Action: ResolveSkip}, nil
}
return Resolution{Action: ResolveOverwrite, ApplyToAll: true}, nil
},
})
if len(asked) != 2 || asked[0] != "d/a" || asked[1] != "d/b" {
t.Fatalf("asked %v: ApplyToAll must stop further questions", asked)
}
for name, want := range map[string]string{"a": "old", "b": "new-b", "c": "new-c", "n": "n"} {
if got := read(t, filepath.Join(dst, "d", name)); got != want {
t.Errorf("%s = %q, want %q", name, got, want)
}
}
if rep.Done != 3 || rep.Skipped != 1 {
t.Fatalf("%+v", rep)
}
// Abort stops the transfer.
_, rep = run(t, req(Copy, local, join(src, "d"), local, dst), Hooks{
Resolve: func(context.Context, Conflict) (Resolution, error) { return Resolution{Action: ResolveAbort}, nil },
})
if !errors.Is(rep.Err, ErrAborted) || rep.Done != 0 {
t.Fatalf("abort: %+v", rep)
}
}
func TestConflictTypeMismatchNeverDeletesDir(t *testing.T) {
src, dst := layout(t, fstree.Spec{
fstree.File("x", "file over dir"),
fstree.Dir("y"), fstree.File("y/inner", "dir over file"),
}, fstree.Spec{
fstree.File("x/precious", "keep"),
fstree.File("y", "a file"),
})
for _, pol := range []ConflictPolicy{Overwrite, OverwriteOlder, Ask} {
_, rep := run(t, req(Copy, local, join(src, "x", "y"), local, dst, func(o *Options) { o.Conflict = pol }), Hooks{
Resolve: func(context.Context, Conflict) (Resolution, error) {
return Resolution{Action: ResolveOverwrite}, nil // not allowed for a mismatch
},
})
if rep.Done != 0 || rep.Err != nil {
t.Fatalf("policy %d: %+v", pol, rep)
}
if read(t, filepath.Join(dst, "x", "precious")) != "keep" || read(t, filepath.Join(dst, "y")) != "a file" {
t.Fatalf("policy %d: a type mismatch was overwritten", pol)
}
}
// KeepBoth writes beside it.
_, rep := run(t, req(Copy, local, join(src, "x", "y"), local, dst, func(o *Options) { o.Conflict = KeepBoth }), Hooks{})
if rep.Done != 2 || read(t, filepath.Join(dst, "x (1)")) != "file over dir" || read(t, filepath.Join(dst, "y (1)", "inner")) != "dir over file" {
t.Fatalf("keep both: %+v", rep)
}
}
func TestKeepBothNaming(t *testing.T) {
cases := []struct {
name string
n int
dir bool
want string
}{
{"report.pdf", 1, false, "report (1).pdf"},
{"report.pdf", 3, false, "report (3).pdf"},
{"archive.tar.gz", 1, false, "archive (1).tar.gz"},
{".bashrc", 1, false, ".bashrc (1)"},
{"Makefile", 2, false, "Makefile (2)"},
{"photos.2024", 1, true, "photos.2024 (1)"},
{"данные.txt", 1, false, "данные (1).txt"},
}
for _, c := range cases {
if got := keepBothName(c.name, c.n, c.dir); got != c.want {
t.Errorf("keepBothName(%q, %d) = %q, want %q", c.name, c.n, got, c.want)
}
}
src, dst := layout(t, fstree.Spec{fstree.File("a.txt", "3rd")},
fstree.Spec{fstree.File("a.txt", "1st"), fstree.File("a (1).txt", "2nd")})
_, rep := run(t, req(Copy, local, join(src, "a.txt"), local, dst, func(o *Options) { o.Conflict = KeepBoth }), Hooks{})
if rep.Done != 1 || read(t, filepath.Join(dst, "a (2).txt")) != "3rd" || read(t, filepath.Join(dst, "a.txt")) != "1st" {
t.Fatalf("%+v", rep)
}
}
func TestTargetAppearedAfterPlanIsConflict(t *testing.T) {
src, dst := layout(t, fstree.Spec{fstree.File("f", "mine"), fstree.Dir("d"), fstree.File("d/g", "mine")}, nil)
plan, err := Prepare(bg, req(Copy, local, join(src, "f", "d"), local, dst), nil)
if err != nil {
t.Fatal(err)
}
if len(plan.Conflicts) != 0 {
t.Fatal("no conflicts expected at plan time")
}
// Someone creates the targets between plan and execute.
fstree.Build(t, dst, fstree.Spec{fstree.File("f", "theirs"), fstree.File("d/g", "theirs")})
var asked []string
rep := Execute(bg, plan, Hooks{Resolve: func(_ context.Context, c Conflict) (Resolution, error) {
asked = append(asked, c.Item.Rel)
if c.Item.Existing == nil {
t.Error("runtime conflict without Existing")
}
return Resolution{Action: ResolveSkip}, nil
}})
if len(asked) != 2 {
t.Fatalf("asked %v", asked)
}
if read(t, filepath.Join(dst, "f")) != "theirs" || read(t, filepath.Join(dst, "d", "g")) != "theirs" {
t.Fatal("a target that appeared after the plan was overwritten without asking")
}
if rep.Skipped != 2 || rep.Done != 0 {
t.Fatalf("%+v", rep)
}
noLeftovers(t, dst)
}
func TestErrorRetryThenSuccess(t *testing.T) {
src, dst := layout(t, fstree.Spec{fstree.File("a", "").WithSize(100 << 10), fstree.File("b", "b")}, nil)
to := &vfstest.FaultFS{FS: local, Hook: vfstest.FailNth(vfstest.OpWrite, 1, syscall.EIO)}
var asked []ItemError
_, rep := run(t, req(Copy, local, join(src, "a", "b"), to, dst), Hooks{
OnError: func(_ context.Context, e ItemError) (ErrorAction, error) {
asked = append(asked, e)
return ErrorRetry, nil
},
})
if len(asked) != 1 || asked[0].Item.Rel != "a" || !errors.Is(asked[0].Err, syscall.EIO) {
t.Fatalf("asked %+v", asked)
}
if rep.Done != 2 || rep.Failed != 0 || len(rep.Errors) != 0 || rep.Bytes != 100<<10+1 {
t.Fatalf("%+v", rep)
}
if fi, _ := os.Stat(filepath.Join(dst, "a")); fi.Size() != 100<<10 {
t.Fatal("retried file incomplete")
}
noLeftovers(t, dst)
}
func TestErrorSkipAllContinues(t *testing.T) {
src, dst := layout(t, fstree.Spec{fstree.File("a", "1"), fstree.File("b", "2"), fstree.File("c", "3"), fstree.File("d", "4")}, nil)
fail := map[string]bool{"a": true, "b": true, "c": true}
to := &vfstest.FaultFS{FS: local, Hook: func(op vfstest.Op, p string) error {
if op == vfstest.OpCreate {
for n := range fail {
if hasSuffixName(p, n) {
return syscall.EACCES
}
}
}
return nil
}}
asks := 0
_, rep := run(t, req(Copy, local, join(src, "a", "b", "c", "d"), to, dst), Hooks{
OnError: func(context.Context, ItemError) (ErrorAction, error) {
asks++
return ErrorSkipRest, nil
},
})
if asks != 1 {
t.Fatalf("asked %d times after SkipAll", asks)
}
if rep.Failed != 3 || rep.Done != 1 || len(rep.Errors) != 3 {
t.Fatalf("%+v", rep)
}
if read(t, filepath.Join(dst, "d")) != "4" {
t.Fatal("transfer did not continue")
}
// Policy-level SkipAll never asks; Abort stops at the first error.
_, dst2 := layout(t, nil, nil)
_, rep = run(t, req(Copy, local, join(src, "a", "b", "c", "d"), to, dst2, func(o *Options) { o.OnError = ErrorAbort }), Hooks{})
if !errors.Is(rep.Err, ErrAborted) || rep.Failed != 1 || rep.Done != 0 {
t.Fatalf("abort: %+v", rep)
}
}
// hasSuffixName: temp files are ".vcom-part-xxxxxxxx-<name>".
func hasSuffixName(p, name string) bool {
b := filepath.Base(p)
return len(b) > len(name) && b[len(b)-len(name)-1:] == "-"+name
}
func TestReportCounts(t *testing.T) {
old := time.Date(2001, 1, 1, 0, 0, 0, 0, time.UTC)
src, dst := layout(t, fstree.Spec{
fstree.File("d/new", "12345"),
fstree.File("d/older", "xx").WithMTime(old),
fstree.File("d/newer", "yyy"),
fstree.File("d/broken", "zz"),
fstree.FIFO("d/fifo"),
fstree.Symlink("d/ln", "new"),
}, fstree.Spec{
fstree.File("d/older", "dst-newer"),
fstree.File("d/newer", "dst-old").WithMTime(old),
})
to := &vfstest.FaultFS{FS: local, Hook: func(op vfstest.Op, p string) error {
if op == vfstest.OpCreate && hasSuffixName(p, "broken") {
return syscall.EIO
}
return nil
}}
plan, rep := run(t, req(Copy, local, join(src, "d"), to, dst, skipAll, func(o *Options) { o.Conflict = OverwriteOlder }), Hooks{})
// new, newer, ln copied; older skipped (target is newer); fifo skipped; broken failed.
if rep.Done != 3 || rep.Skipped != 2 || rep.Failed != 1 || rep.Bytes != 5+3 {
t.Fatalf("report %+v", rep)
}
if plan.TotalFiles != 5 || plan.TotalBytes != 5+2+3+2 {
t.Fatalf("plan %d/%d", plan.TotalFiles, plan.TotalBytes)
}
if read(t, filepath.Join(dst, "d", "older")) != "dst-newer" || read(t, filepath.Join(dst, "d", "newer")) != "yyy" {
t.Fatal("OverwriteOlder decided wrong")
}
wantAffected := map[string]bool{dst: true, filepath.Join(dst, "d"): true}
for _, l := range rep.Affected {
if !wantAffected[l.Path] || l.FS != vfs.FS(to) {
t.Fatalf("affected %v", rep.Affected)
}
delete(wantAffected, l.Path)
}
if len(wantAffected) != 0 {
t.Fatalf("missing affected %v", wantAffected)
}
}