| 1 | package builtin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/tool" |
| 11 | ) |
| 12 | |
| 13 | func TestObservedDeletionCannotBecomeBlindCreate(t *testing.T) { |
| 14 | path := filepath.Join(t.TempDir(), "file") |
| 15 | if err := os.WriteFile(path, []byte("old"), 0o600); err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | ctx := observedContext() |
| 19 | if _, err := (readFile{}).Execute(ctx, argsJSON(t, map[string]any{"path": path})); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | if err := os.Remove(path); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | _, err := (writeFile{}).Execute(ctx, argsJSON(t, map[string]any{"path": path, "content": "new"})) |
| 26 | if !errors.Is(err, ErrFileChanged) { |
| 27 | t.Fatalf("deleted observed source was recreated: %v", err) |
| 28 | } |
| 29 | if _, err := os.Stat(path); !os.IsNotExist(err) { |
| 30 | t.Fatalf("write changed absent target: %v", err) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestDiskWriteDoesNotSwitchToUnobservedOverlay(t *testing.T) { |
| 35 | path := filepath.Join(t.TempDir(), "file") |
| 36 | if err := os.WriteFile(path, []byte("disk"), 0o600); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | ctx := observedContext() |
| 40 | ov := &fakeOverlay{files: map[string]string{}, writes: map[string]string{}} |
| 41 | if _, err := (readFile{overlay: ov}).Execute(ctx, argsJSON(t, map[string]any{"path": path})); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | if _, err := (writeFile{overlay: ov}).Execute(ctx, argsJSON(t, map[string]any{"path": path, "content": "new"})); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if len(ov.writes) != 0 { |
| 48 | t.Fatal("disk observation authorized an overlay write") |
| 49 | } |
| 50 | got, err := os.ReadFile(path) |
| 51 | if err != nil || string(got) != "new" { |
| 52 | t.Fatalf("disk write = %q, %v", got, err) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestSameContentReplacementBeforePublishIsStale(t *testing.T) { |
| 57 | path := filepath.Join(t.TempDir(), "file") |
| 58 | if err := os.WriteFile(path, []byte("old"), 0o600); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | ctx := observedContext() |
| 62 | if _, err := (readFile{}).Execute(ctx, argsJSON(t, map[string]any{"path": path})); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | ctx = tool.WithWriteIntentHook(ctx, func(tool.FileWriteIntent) error { |
| 66 | // Keep the old inode allocated so identity reuse cannot hide replacement. |
| 67 | if err := os.Rename(path, path+".old"); err != nil { |
| 68 | return err |
| 69 | } |
| 70 | return os.WriteFile(path, []byte("old"), 0o600) |
| 71 | }) |
| 72 | _, err := (editFile{}).Execute(ctx, argsJSON(t, map[string]any{"path": path, "old_string": "old", "new_string": "new"})) |
| 73 | if !errors.Is(err, ErrFileChanged) { |
| 74 | t.Fatalf("replacement accepted: %v", err) |
| 75 | } |
| 76 | got, _ := os.ReadFile(path) |
| 77 | if string(got) != "old" { |
| 78 | t.Fatalf("external replacement overwritten: %q", got) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestMovePublicationCannotOverwriteConcurrentCreator(t *testing.T) { |
| 83 | dir := t.TempDir() |
| 84 | src, dst := filepath.Join(dir, "src"), filepath.Join(dir, "dst") |
| 85 | if err := os.WriteFile(src, []byte("source"), 0o600); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | old := renameFile |
| 89 | renameFile = func(from, to string) error { |
| 90 | if err := os.WriteFile(to, []byte("winner"), 0o600); err != nil { |
| 91 | return err |
| 92 | } |
| 93 | return old(from, to) |
| 94 | } |
| 95 | t.Cleanup(func() { renameFile = old }) |
| 96 | _, err := (moveFile{}).Execute(context.Background(), argsJSON(t, map[string]any{"source_path": src, "destination_path": dst})) |
| 97 | if err == nil { |
| 98 | t.Fatal("move overwrote concurrent destination") |
| 99 | } |
| 100 | got, _ := os.ReadFile(dst) |
| 101 | if string(got) != "winner" { |
| 102 | t.Fatalf("destination = %q", got) |
| 103 | } |
| 104 | got, _ = os.ReadFile(src) |
| 105 | if string(got) != "source" { |
| 106 | t.Fatalf("source = %q", got) |
| 107 | } |
| 108 | } |
| 109 |