Repository navigation
Expand file tree
/
Copy pathfs_test.go
More file actions
339 lines (311 loc) · 9.09 KB
/
Copy pathfs_test.go
File metadata and controls
339 lines (311 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"bytes"
"encoding/json"
"io/fs"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
)
// fs_test.go — Tier-1 contracts for fs.go exercised against the real
// filesystem. No mocks or stubs.
// fsAssertMode skips on Windows where POSIX permission bits are not enforced.
func fsAssertMode(t *testing.T, path string, want os.FileMode) {
t.Helper()
if runtime.GOOS == "windows" {
return
}
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if got := info.Mode().Perm(); got != want {
t.Fatalf("%s mode = %o, want %o", path, got, want)
}
}
func TestCanonical(t *testing.T) {
root := canonical(t.TempDir())
t.Run("clean", func(t *testing.T) {
sep := string(filepath.Separator)
got := canonical(root + sep + "a" + sep + ".." + sep + "x")
if want := filepath.Join(root, "x"); got != want {
t.Fatalf("canonical = %q, want %q", got, want)
}
})
t.Run("idempotent", func(t *testing.T) {
once := canonical(filepath.Join(root, "missing"))
if twice := canonical(once); twice != once {
t.Fatalf("canonical not idempotent: %q -> %q", once, twice)
}
})
t.Run("symlinked ancestor preserves non-existent tail", func(t *testing.T) {
base := t.TempDir()
real := filepath.Join(base, "real")
if err := os.MkdirAll(filepath.Join(real, "kid"), 0o755); err != nil {
t.Fatal(err)
}
link := filepath.Join(base, "link")
mustSymlink(t, real, link)
got := canonical(filepath.Join(link, "gone"))
if want := filepath.Join(canonical(real), "gone"); got != want {
t.Fatalf("canonical = %q, want %q", got, want)
}
})
t.Run("relative path", func(t *testing.T) {
t.Chdir(root)
got := canonical("sub/./gone")
if want := filepath.Join("sub", "gone"); got != want {
t.Fatalf("canonical = %q, want %q", got, want)
}
})
}
// chmodTest pins permission bits regardless of umask, failing on error.
func chmodTest(t *testing.T, path string, mode os.FileMode) {
t.Helper()
if err := os.Chmod(path, mode); err != nil {
t.Fatal(err)
}
}
func TestCopyFile(t *testing.T) {
src := filepath.Join(t.TempDir(), "src.txt")
writeFile(t, src, "hello", 0o600)
chmodTest(t, src, 0o600)
dst := filepath.Join(t.TempDir(), "dst.txt")
if err := copyFile(src, dst); err != nil {
t.Fatalf("copyFile: %v", err)
}
if b, _ := os.ReadFile(dst); string(b) != "hello" {
t.Fatalf("content = %q, want hello", b)
}
fsAssertMode(t, dst, 0o600)
writeFile(t, dst, "old", 0o644)
if err := copyFile(src, dst); err != nil {
t.Fatalf("copyFile overwrite: %v", err)
}
if b, _ := os.ReadFile(dst); string(b) != "hello" {
t.Fatalf("overwritten content = %q, want hello", b)
}
}
func TestCopyLink(t *testing.T) {
src := filepath.Join(t.TempDir(), "link")
mustSymlink(t, "target-file", src)
dst := filepath.Join(t.TempDir(), "out", "link")
if err := copyLink(src, dst); err != nil {
t.Fatalf("copyLink: %v", err)
}
if got, err := os.Readlink(dst); err != nil || got != "target-file" {
t.Fatalf("readlink = (%q,%v), want target-file", got, err)
}
// Dangling targets must survive without dereferencing.
dangle := filepath.Join(t.TempDir(), "dangle")
mustSymlink(t, "does-not-exist", dangle)
dst2 := filepath.Join(t.TempDir(), "d2")
if err := copyLink(dangle, dst2); err != nil {
t.Fatalf("copyLink dangling: %v", err)
}
if got, _ := os.Readlink(dst2); got != "does-not-exist" {
t.Fatalf("dangling readlink = %q", got)
}
}
// fsWriteTree seeds a tree with live daemon sidecars (top-level and nested)
// plus sibling files that must survive every copy.
func fsWriteTree(t *testing.T, root string) {
t.Helper()
for _, rel := range []string{
"a.txt", "keep.txt", "daemon.log",
"sub/b.txt", "sub/daemon.log",
"watchman/state", "sub/watchman/state",
".watchman/state", "sub/.watchman/state",
} {
writeFile(t, filepath.Join(root, filepath.FromSlash(rel)), rel, 0o644)
}
}
func fsExcludedRels() []string {
return []string{"daemon.log", "sub/daemon.log", "watchman", "sub/watchman", ".watchman", "sub/.watchman"}
}
func TestCopyTree(t *testing.T) {
t.Run("dirs files and modes", func(t *testing.T) {
src := t.TempDir()
dst := filepath.Join(t.TempDir(), "dst")
fsWriteTree(t, src)
chmodTest(t, filepath.Join(src, "sub"), 0o700)
chmodTest(t, filepath.Join(src, "a.txt"), 0o640)
if err := copyTree(src, dst); err != nil {
t.Fatalf("copyTree: %v", err)
}
for _, rel := range []string{"a.txt", "keep.txt", "sub/b.txt"} {
b, _ := os.ReadFile(filepath.Join(dst, filepath.FromSlash(rel)))
if string(b) != rel {
t.Fatalf("%s content = %q, want %q", rel, b, rel)
}
}
fsAssertMode(t, filepath.Join(dst, "sub"), 0o700)
fsAssertMode(t, filepath.Join(dst, "a.txt"), 0o640)
})
t.Run("excludes daemon sidecars including nested", func(t *testing.T) {
src := t.TempDir()
dst := filepath.Join(t.TempDir(), "dst")
fsWriteTree(t, src)
if err := copyTree(src, dst); err != nil {
t.Fatalf("copyTree: %v", err)
}
for _, rel := range fsExcludedRels() {
if exists(filepath.Join(dst, filepath.FromSlash(rel))) {
t.Fatalf("excluded %s was copied", rel)
}
}
if !exists(filepath.Join(dst, "keep.txt")) || !exists(filepath.Join(dst, "sub", "b.txt")) {
t.Fatal("siblings of excluded entries must survive")
}
})
t.Run("does not dereference symlinks", func(t *testing.T) {
src := t.TempDir()
dst := filepath.Join(t.TempDir(), "dst")
writeFile(t, filepath.Join(src, "a.txt"), "A", 0o644)
mustSymlink(t, "a.txt", filepath.Join(src, "link"))
if err := copyTree(src, dst); err != nil {
t.Fatalf("copyTree: %v", err)
}
info, err := os.Lstat(filepath.Join(dst, "link"))
if err != nil {
t.Fatal(err)
}
if info.Mode()&fs.ModeSymlink == 0 {
t.Fatal("link was dereferenced")
}
if got, _ := os.Readlink(filepath.Join(dst, "link")); got != "a.txt" {
t.Fatalf("readlink = %q, want a.txt", got)
}
})
}
// fsWalkFiles snapshots a tree as rel-path -> content, "dir" or "link:<target>".
func fsWalkFiles(t *testing.T, root string) map[string]string {
t.Helper()
m := map[string]string{}
err := filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
if err != nil || p == root {
return err
}
rel, err := filepath.Rel(root, p)
if err != nil {
return err
}
rel = filepath.ToSlash(rel)
switch {
case d.Type()&fs.ModeSymlink != 0:
target, err := os.Readlink(p)
if err != nil {
return err
}
m[rel] = "link:" + target
case d.IsDir():
m[rel] = "dir"
default:
b, err := os.ReadFile(p)
if err != nil {
return err
}
m[rel] = string(b)
}
return nil
})
if err != nil {
t.Fatal(err)
}
return m
}
// fsDropExcluded removes the entries copyTree/cloneDir must never carry over.
func fsDropExcluded(m map[string]string) {
for k := range m {
for _, seg := range strings.Split(k, "/") {
if seg == "daemon.log" || seg == "watchman" || seg == ".watchman" {
delete(m, k)
break
}
}
}
}
func TestCloneDir(t *testing.T) {
src := t.TempDir()
fsWriteTree(t, src)
dst := filepath.Join(t.TempDir(), "clone")
if err := cloneDir(src, dst); err != nil {
t.Fatalf("cloneDir: %v", err)
}
want := fsWalkFiles(t, src)
fsDropExcluded(want)
if got := fsWalkFiles(t, dst); !reflect.DeepEqual(got, want) {
t.Fatalf("clone tree = %#v, want %#v", got, want)
}
for _, rel := range fsExcludedRels() {
if exists(filepath.Join(dst, filepath.FromSlash(rel))) {
t.Fatalf("clone kept excluded %s", rel)
}
}
}
func TestRemoveExcluded(t *testing.T) {
root := t.TempDir()
fsWriteTree(t, root)
if err := removeExcluded(root); err != nil {
t.Fatalf("removeExcluded: %v", err)
}
for _, rel := range fsExcludedRels() {
if exists(filepath.Join(root, filepath.FromSlash(rel))) {
t.Fatalf("excluded %s survived", rel)
}
}
for _, rel := range []string{"a.txt", "keep.txt", "sub/b.txt"} {
if !exists(filepath.Join(root, filepath.FromSlash(rel))) {
t.Fatalf("kept %s was removed", rel)
}
}
}
func TestWriteJSONContract(t *testing.T) {
p := filepath.Join(t.TempDir(), "out.json")
in := map[string]any{"known": 1, "extra": "x"}
if err := writeJSON(p, in); err != nil {
t.Fatalf("writeJSON: %v", err)
}
b, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
if !bytes.HasSuffix(b, []byte("\n")) {
t.Fatalf("must end with newline: %q", b)
}
if !bytes.Contains(b, []byte("\n \"known\"")) || bytes.Contains(b, []byte("\t")) {
t.Fatalf("must use 2-space indent, got %q", b)
}
fsAssertMode(t, p, 0o644)
var out map[string]json.RawMessage
if err := readJSON(p, &out); err != nil {
t.Fatalf("readJSON round-trip: %v", err)
}
if string(out["known"]) != "1" || string(out["extra"]) != `"x"` {
t.Fatalf("round-trip lost keys: %v", out)
}
}
func TestPrintJSON(t *testing.T) {
out := captureStdout(t, func() {
if err := printJSON(map[string]any{"a": 1}); err != nil {
t.Errorf("printJSON: %v", err)
}
})
if want := "{\n \"a\": 1\n}\n"; out != want {
t.Fatalf("printJSON = %q, want %q", out, want)
}
}
func TestExists(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, "f")
writeFile(t, file, "x", 0o644)
if !exists(file) || !exists(dir) {
t.Fatal("existing file/dir must report true")
}
if exists(filepath.Join(dir, "missing")) {
t.Fatal("missing path must report false")
}
}