aboutsummaryrefslogtreecommitdiff
path: root/internal/plan/conflict_test.go
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-09-14 21:27:22 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-09-14 21:27:22 +0200
commitaca389f0e63713b890214cad950ac3aee7e6aaf4 (patch)
treebb4dab94ca594edb339ab8fd1743d583efe3d59c /internal/plan/conflict_test.go
parent3bfafbc8664a2a1ba8efc3f64376ff63c3dc11b9 (diff)
downloadkrino-aca389f0e63713b890214cad950ac3aee7e6aaf4.tar.gz
krino-aca389f0e63713b890214cad950ac3aee7e6aaf4.zip
plan 9: overwrite never trashes a directory or another scanned file
Diffstat (limited to 'internal/plan/conflict_test.go')
-rw-r--r--internal/plan/conflict_test.go32
1 files changed, 31 insertions, 1 deletions
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)
+ }
+}