From aca389f0e63713b890214cad950ac3aee7e6aaf4 Mon Sep 17 00:00:00 2001 From: Lukasz Kasprzak Date: Mon, 14 Sep 2026 21:27:22 +0200 Subject: plan 9: overwrite never trashes a directory or another scanned file --- internal/plan/conflict_test.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'internal/plan/conflict_test.go') diff --git a/internal/plan/conflict_test.go b/internal/plan/conflict_test.go index bdc37af..289cd14 100644 --- a/internal/plan/conflict_test.go +++ b/internal/plan/conflict_test.go @@ -16,9 +16,11 @@ import ( type fakeDisk struct { exists map[string]bool same map[[2]string]bool + dirs map[string]bool // existing paths that are directories } -func (f fakeDisk) Exists(p string) bool { return f.exists[p] } +func (f fakeDisk) Exists(p string) bool { return f.exists[p] } +func (f fakeDisk) Regular(p string) bool { return f.exists[p] && !f.dirs[p] } func (f fakeDisk) SameContent(a, b string) (bool, error) { return f.same[[2]string{a, b}], nil } @@ -208,6 +210,7 @@ func TestSameContentReal(t *testing.T) { type stubAlwaysExists struct{} func (stubAlwaysExists) Exists(string) bool { return true } +func (stubAlwaysExists) Regular(string) bool { return true } func (stubAlwaysExists) SameContent(string, string) (bool, error) { return false, nil } // TestSuffixedCapsAttempts is C2: suffixed() gives up after @@ -222,3 +225,30 @@ func TestSuffixedCapsAttempts(t *testing.T) { t.Errorf("step = %+v; want Skip \"too many conflicting names\" and empty Dst", s) } } + +// TestOverwriteNeverDisplacesADirectory: a hostile file named like an +// existing directory must not have that directory trashed in its place +// (review M4). +func TestOverwriteNeverDisplacesADirectory(t *testing.T) { + dir := "/home/x/Documents/Invoices" + d := fakeDisk{exists: map[string]bool{dir: true}, dirs: map[string]bool{dir: true}} + _, skip, displaces := resolveConflict(Move, config.ConflictOverwrite, "/r/Invoices", dir, d, claimed{}) + if displaces != "" || skip != "target is not a regular file" { + t.Errorf("skip %q displaces %q; want skipped, nothing displaced", skip, displaces) + } +} + +// TestOverwriteNeverDisplacesAnotherScannedFile: a file of the same plan is +// not trashed to make room - it may be about to move away or be acted on - +// so the step takes a free name instead (review M4). +func TestOverwriteNeverDisplacesAnotherScannedFile(t *testing.T) { + d := fakeDisk{exists: map[string]bool{"/r/a.pdf": true, "/r/b.pdf": true}} + in := []Input{ + {File: file("/r", "a.pdf"), Rules: []RuleMatch{{Name: "r", Settings: config.Resolved{OnConflict: config.ConflictOverwrite}, Actions: []config.Action{act(config.Rename, "b.pdf")}}}}, + {File: file("/r", "b.pdf"), Rules: []RuleMatch{{Name: "m", Actions: []config.Action{act(config.Move, "Out")}}}}, + } + s := Build("/r", in, time.Now(), d, NewClaims())[0].Steps[0] + if s.Displaces != "" || s.Dst != "/r/b_1.pdf" || s.Skip != "" { + t.Errorf("Displaces %q Dst %q Skip %q; want no displacement and /r/b_1.pdf", s.Displaces, s.Dst, s.Skip) + } +} -- cgit v1.3