| 1 |
// Copyright 2020 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package fsys |
| 6 |
|
| 7 |
import ( |
| 8 |
"errors" |
| 9 |
"internal/testenv" |
| 10 |
"internal/txtar" |
| 11 |
"io" |
| 12 |
"io/fs" |
| 13 |
"os" |
| 14 |
"path/filepath" |
| 15 |
"reflect" |
| 16 |
"runtime" |
| 17 |
"slices" |
| 18 |
"strings" |
| 19 |
"sync" |
| 20 |
"testing" |
| 21 |
) |
| 22 |
|
| 23 |
func resetForTesting() { |
| 24 |
cwd = sync.OnceValue(cwdOnce) |
| 25 |
overlay = nil |
| 26 |
binds = nil |
| 27 |
} |
| 28 |
|
| 29 |
// initOverlay resets the overlay state to reflect the config. |
| 30 |
// config should be a text archive string. The comment is the overlay config |
| 31 |
// json, and the files, in the archive are laid out in a temp directory |
| 32 |
// that cwd is set to. |
| 33 |
func initOverlay(t *testing.T, config string) { |
| 34 |
t.Helper() |
| 35 |
t.Chdir(t.TempDir()) |
| 36 |
resetForTesting() |
| 37 |
t.Cleanup(resetForTesting) |
| 38 |
cwd := cwd() |
| 39 |
|
| 40 |
a := txtar.Parse([]byte(config)) |
| 41 |
for _, f := range a.Files { |
| 42 |
name := filepath.Join(cwd, f.Name) |
| 43 |
if err := os.MkdirAll(filepath.Dir(name), 0777); err != nil { |
| 44 |
t.Fatal(err) |
| 45 |
} |
| 46 |
if err := os.WriteFile(name, f.Data, 0666); err != nil { |
| 47 |
t.Fatal(err) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if err := initFromJSON(a.Comment); err != nil { |
| 52 |
t.Fatal(err) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
var statInfoOverlay = `{"Replace": { |
| 57 |
"x": "replace/x", |
| 58 |
"a/b/c": "replace/c", |
| 59 |
"d/e": "" |
| 60 |
}}` |
| 61 |
|
| 62 |
var statInfoTests = []struct { |
| 63 |
path string |
| 64 |
info info |
| 65 |
}{ |
| 66 |
{"foo", info{abs: "/tmp/foo", actual: "foo"}}, |
| 67 |
{"foo/bar/baz/quux", info{abs: "/tmp/foo/bar/baz/quux", actual: "foo/bar/baz/quux"}}, |
| 68 |
{"x", info{abs: "/tmp/x", replaced: true, file: true, actual: "/tmp/replace/x"}}, |
| 69 |
{"/tmp/x", info{abs: "/tmp/x", replaced: true, file: true, actual: "/tmp/replace/x"}}, |
| 70 |
{"x/y", info{abs: "/tmp/x/y", deleted: true}}, |
| 71 |
{"a", info{abs: "/tmp/a", replaced: true, dir: true, actual: "a"}}, |
| 72 |
{"a/b", info{abs: "/tmp/a/b", replaced: true, dir: true, actual: "a/b"}}, |
| 73 |
{"a/b/c", info{abs: "/tmp/a/b/c", replaced: true, file: true, actual: "/tmp/replace/c"}}, |
| 74 |
{"d/e", info{abs: "/tmp/d/e", deleted: true}}, |
| 75 |
{"d", info{abs: "/tmp/d", replaced: true, dir: true, actual: "d"}}, |
| 76 |
} |
| 77 |
|
| 78 |
var statInfoChildrenTests = []struct { |
| 79 |
path string |
| 80 |
children []info |
| 81 |
}{ |
| 82 |
{"foo", nil}, |
| 83 |
{"foo/bar", nil}, |
| 84 |
{"foo/bar/baz", nil}, |
| 85 |
{"x", nil}, |
| 86 |
{"x/y", nil}, |
| 87 |
{"a", []info{{abs: "/tmp/a/b", replaced: true, dir: true, actual: ""}}}, |
| 88 |
{"a/b", []info{{abs: "/tmp/a/b/c", replaced: true, actual: "/tmp/replace/c"}}}, |
| 89 |
{"d", []info{{abs: "/tmp/d/e", deleted: true}}}, |
| 90 |
{"d/e", nil}, |
| 91 |
{".", []info{ |
| 92 |
{abs: "/tmp/a", replaced: true, dir: true, actual: ""}, |
| 93 |
// {abs: "/tmp/d", replaced: true, dir: true, actual: ""}, |
| 94 |
{abs: "/tmp/x", replaced: true, actual: "/tmp/replace/x"}, |
| 95 |
}}, |
| 96 |
} |
| 97 |
|
| 98 |
func TestStatInfo(t *testing.T) { |
| 99 |
tmp := "/tmp" |
| 100 |
if runtime.GOOS == "windows" { |
| 101 |
tmp = `C:\tmp` |
| 102 |
} |
| 103 |
cwd = sync.OnceValue(func() string { return tmp }) |
| 104 |
|
| 105 |
winFix := func(s string) string { |
| 106 |
if runtime.GOOS == "windows" { |
| 107 |
s = strings.ReplaceAll(s, `/tmp`, tmp) // fix tmp |
| 108 |
s = strings.ReplaceAll(s, `/`, `\`) // use backslashes |
| 109 |
} |
| 110 |
return s |
| 111 |
} |
| 112 |
|
| 113 |
overlay := statInfoOverlay |
| 114 |
overlay = winFix(overlay) |
| 115 |
overlay = strings.ReplaceAll(overlay, `\`, `\\`) // JSON escaping |
| 116 |
if err := initFromJSON([]byte(overlay)); err != nil { |
| 117 |
t.Fatal(err) |
| 118 |
} |
| 119 |
|
| 120 |
for _, tt := range statInfoTests { |
| 121 |
tt.path = winFix(tt.path) |
| 122 |
tt.info.abs = winFix(tt.info.abs) |
| 123 |
tt.info.actual = winFix(tt.info.actual) |
| 124 |
info := stat(tt.path) |
| 125 |
if info != tt.info { |
| 126 |
t.Errorf("stat(%#q):\nhave %+v\nwant %+v", tt.path, info, tt.info) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
for _, tt := range statInfoChildrenTests { |
| 131 |
tt.path = winFix(tt.path) |
| 132 |
for i, info := range tt.children { |
| 133 |
info.abs = winFix(info.abs) |
| 134 |
info.actual = winFix(info.actual) |
| 135 |
tt.children[i] = info |
| 136 |
} |
| 137 |
parent := stat(winFix(tt.path)) |
| 138 |
var children []info |
| 139 |
for name, child := range parent.children() { |
| 140 |
if name != filepath.Base(child.abs) { |
| 141 |
t.Errorf("stat(%#q): child %#q has inconsistent abs %#q", tt.path, name, child.abs) |
| 142 |
} |
| 143 |
children = append(children, child) |
| 144 |
} |
| 145 |
slices.SortFunc(children, func(x, y info) int { return cmp(x.abs, y.abs) }) |
| 146 |
if !slices.Equal(children, tt.children) { |
| 147 |
t.Errorf("stat(%#q) children:\nhave %+v\nwant %+v", tt.path, children, tt.children) |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
func TestIsDir(t *testing.T) { |
| 153 |
initOverlay(t, ` |
| 154 |
{ |
| 155 |
"Replace": { |
| 156 |
"subdir2/file2.txt": "overlayfiles/subdir2_file2.txt", |
| 157 |
"subdir4": "overlayfiles/subdir4", |
| 158 |
"subdir3/file3b.txt": "overlayfiles/subdir3_file3b.txt", |
| 159 |
"subdir5": "", |
| 160 |
"subdir6": "" |
| 161 |
} |
| 162 |
} |
| 163 |
-- subdir1/file1.txt -- |
| 164 |
|
| 165 |
-- subdir3/file3a.txt -- |
| 166 |
33 |
| 167 |
-- subdir4/file4.txt -- |
| 168 |
444 |
| 169 |
-- overlayfiles/subdir2_file2.txt -- |
| 170 |
2 |
| 171 |
-- overlayfiles/subdir3_file3b.txt -- |
| 172 |
66666 |
| 173 |
-- overlayfiles/subdir4 -- |
| 174 |
x |
| 175 |
-- subdir6/file6.txt -- |
| 176 |
six |
| 177 |
`) |
| 178 |
|
| 179 |
cwd := cwd() |
| 180 |
testCases := []struct { |
| 181 |
path string |
| 182 |
want, wantErr bool |
| 183 |
}{ |
| 184 |
{"", true, true}, |
| 185 |
{".", true, false}, |
| 186 |
{cwd, true, false}, |
| 187 |
{cwd + string(filepath.Separator), true, false}, |
| 188 |
// subdir1 is only on disk |
| 189 |
{filepath.Join(cwd, "subdir1"), true, false}, |
| 190 |
{"subdir1", true, false}, |
| 191 |
{"subdir1" + string(filepath.Separator), true, false}, |
| 192 |
{"subdir1/file1.txt", false, false}, |
| 193 |
{"subdir1/doesntexist.txt", false, true}, |
| 194 |
{"doesntexist", false, true}, |
| 195 |
// subdir2 is only in overlay |
| 196 |
{filepath.Join(cwd, "subdir2"), true, false}, |
| 197 |
{"subdir2", true, false}, |
| 198 |
{"subdir2" + string(filepath.Separator), true, false}, |
| 199 |
{"subdir2/file2.txt", false, false}, |
| 200 |
{"subdir2/doesntexist.txt", false, true}, |
| 201 |
// subdir3 has files on disk and in overlay |
| 202 |
{filepath.Join(cwd, "subdir3"), true, false}, |
| 203 |
{"subdir3", true, false}, |
| 204 |
{"subdir3" + string(filepath.Separator), true, false}, |
| 205 |
{"subdir3/file3a.txt", false, false}, |
| 206 |
{"subdir3/file3b.txt", false, false}, |
| 207 |
{"subdir3/doesntexist.txt", false, true}, |
| 208 |
// subdir4 is overlaid with a file |
| 209 |
{filepath.Join(cwd, "subdir4"), false, false}, |
| 210 |
{"subdir4", false, false}, |
| 211 |
{"subdir4" + string(filepath.Separator), false, false}, |
| 212 |
{"subdir4/file4.txt", false, false}, |
| 213 |
{"subdir4/doesntexist.txt", false, false}, |
| 214 |
// subdir5 doesn't exist, and is overlaid with a "delete" entry |
| 215 |
{filepath.Join(cwd, "subdir5"), false, false}, |
| 216 |
{"subdir5", false, false}, |
| 217 |
{"subdir5" + string(filepath.Separator), false, false}, |
| 218 |
{"subdir5/file5.txt", false, false}, |
| 219 |
{"subdir5/doesntexist.txt", false, false}, |
| 220 |
// subdir6 does exist, and is overlaid with a "delete" entry |
| 221 |
{filepath.Join(cwd, "subdir6"), false, false}, |
| 222 |
{"subdir6", false, false}, |
| 223 |
{"subdir6" + string(filepath.Separator), false, false}, |
| 224 |
{"subdir6/file6.txt", false, false}, |
| 225 |
{"subdir6/doesntexist.txt", false, false}, |
| 226 |
} |
| 227 |
|
| 228 |
for _, tc := range testCases { |
| 229 |
got, err := IsDir(tc.path) |
| 230 |
if err != nil { |
| 231 |
if !tc.wantErr { |
| 232 |
t.Errorf("IsDir(%q): got error with string %q, want no error", tc.path, err.Error()) |
| 233 |
} |
| 234 |
continue |
| 235 |
} |
| 236 |
if tc.wantErr { |
| 237 |
t.Errorf("IsDir(%q): got no error, want error", tc.path) |
| 238 |
} |
| 239 |
if tc.want != got { |
| 240 |
t.Errorf("IsDir(%q) = %v, want %v", tc.path, got, tc.want) |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
const readDirOverlay = ` |
| 246 |
{ |
| 247 |
"Replace": { |
| 248 |
"subdir2/file2.txt": "overlayfiles/subdir2_file2.txt", |
| 249 |
"subdir4": "overlayfiles/subdir4", |
| 250 |
"subdir3/file3b.txt": "overlayfiles/subdir3_file3b.txt", |
| 251 |
"subdir5": "", |
| 252 |
"subdir6/asubsubdir/afile.txt": "overlayfiles/subdir6_asubsubdir_afile.txt", |
| 253 |
"subdir6/asubsubdir/zfile.txt": "overlayfiles/subdir6_asubsubdir_zfile.txt", |
| 254 |
"subdir6/zsubsubdir/file.txt": "overlayfiles/subdir6_zsubsubdir_file.txt", |
| 255 |
"subdir7/asubsubdir/file.txt": "overlayfiles/subdir7_asubsubdir_file.txt", |
| 256 |
"subdir7/zsubsubdir/file.txt": "overlayfiles/subdir7_zsubsubdir_file.txt", |
| 257 |
"subdir8/doesntexist": "this_file_doesnt_exist_anywhere", |
| 258 |
"other/pointstodir": "overlayfiles/this_is_a_directory", |
| 259 |
"parentoverwritten/subdir1": "overlayfiles/parentoverwritten_subdir1", |
| 260 |
"subdir9/this_file_is_overlaid.txt": "overlayfiles/subdir9_this_file_is_overlaid.txt", |
| 261 |
"subdir10/only_deleted_file.txt": "", |
| 262 |
"subdir11/deleted.txt": "", |
| 263 |
"subdir11": "overlayfiles/subdir11", |
| 264 |
"textfile.txt/file.go": "overlayfiles/textfile_txt_file.go" |
| 265 |
} |
| 266 |
} |
| 267 |
-- subdir1/file1.txt -- |
| 268 |
|
| 269 |
-- subdir3/file3a.txt -- |
| 270 |
33 |
| 271 |
-- subdir4/file4.txt -- |
| 272 |
444 |
| 273 |
-- subdir6/file.txt -- |
| 274 |
-- subdir6/asubsubdir/file.txt -- |
| 275 |
-- subdir6/anothersubsubdir/file.txt -- |
| 276 |
-- subdir9/this_file_is_overlaid.txt -- |
| 277 |
-- subdir10/only_deleted_file.txt -- |
| 278 |
this will be deleted in overlay |
| 279 |
-- subdir11/deleted.txt -- |
| 280 |
-- parentoverwritten/subdir1/subdir2/subdir3/file.txt -- |
| 281 |
-- textfile.txt -- |
| 282 |
this will be overridden by textfile.txt/file.go |
| 283 |
-- overlayfiles/subdir2_file2.txt -- |
| 284 |
2 |
| 285 |
-- overlayfiles/subdir3_file3b.txt -- |
| 286 |
66666 |
| 287 |
-- overlayfiles/subdir4 -- |
| 288 |
x |
| 289 |
-- overlayfiles/subdir6_asubsubdir_afile.txt -- |
| 290 |
-- overlayfiles/subdir6_asubsubdir_zfile.txt -- |
| 291 |
-- overlayfiles/subdir6_zsubsubdir_file.txt -- |
| 292 |
-- overlayfiles/subdir7_asubsubdir_file.txt -- |
| 293 |
-- overlayfiles/subdir7_zsubsubdir_file.txt -- |
| 294 |
-- overlayfiles/parentoverwritten_subdir1 -- |
| 295 |
x |
| 296 |
-- overlayfiles/subdir9_this_file_is_overlaid.txt -- |
| 297 |
99999999 |
| 298 |
-- overlayfiles/subdir11 -- |
| 299 |
-- overlayfiles/this_is_a_directory/file.txt -- |
| 300 |
-- overlayfiles/textfile_txt_file.go -- |
| 301 |
x |
| 302 |
` |
| 303 |
|
| 304 |
func TestReadDir(t *testing.T) { |
| 305 |
initOverlay(t, readDirOverlay) |
| 306 |
|
| 307 |
type entry struct { |
| 308 |
name string |
| 309 |
size int64 |
| 310 |
isDir bool |
| 311 |
} |
| 312 |
|
| 313 |
testCases := []struct { |
| 314 |
dir string |
| 315 |
want []entry |
| 316 |
}{ |
| 317 |
{ |
| 318 |
".", []entry{ |
| 319 |
{"other", 0, true}, |
| 320 |
{"overlayfiles", 0, true}, |
| 321 |
{"parentoverwritten", 0, true}, |
| 322 |
{"subdir1", 0, true}, |
| 323 |
{"subdir10", 0, true}, |
| 324 |
{"subdir11", 0, false}, |
| 325 |
{"subdir2", 0, true}, |
| 326 |
{"subdir3", 0, true}, |
| 327 |
{"subdir4", 2, false}, |
| 328 |
// no subdir5. |
| 329 |
{"subdir6", 0, true}, |
| 330 |
{"subdir7", 0, true}, |
| 331 |
{"subdir8", 0, true}, |
| 332 |
{"subdir9", 0, true}, |
| 333 |
{"textfile.txt", 0, true}, |
| 334 |
}, |
| 335 |
}, |
| 336 |
{ |
| 337 |
"subdir1", []entry{ |
| 338 |
{"file1.txt", 1, false}, |
| 339 |
}, |
| 340 |
}, |
| 341 |
{ |
| 342 |
"subdir2", []entry{ |
| 343 |
{"file2.txt", 2, false}, |
| 344 |
}, |
| 345 |
}, |
| 346 |
{ |
| 347 |
"subdir3", []entry{ |
| 348 |
{"file3a.txt", 3, false}, |
| 349 |
{"file3b.txt", 6, false}, |
| 350 |
}, |
| 351 |
}, |
| 352 |
{ |
| 353 |
"subdir6", []entry{ |
| 354 |
{"anothersubsubdir", 0, true}, |
| 355 |
{"asubsubdir", 0, true}, |
| 356 |
{"file.txt", 0, false}, |
| 357 |
{"zsubsubdir", 0, true}, |
| 358 |
}, |
| 359 |
}, |
| 360 |
{ |
| 361 |
"subdir6/asubsubdir", []entry{ |
| 362 |
{"afile.txt", 0, false}, |
| 363 |
{"file.txt", 0, false}, |
| 364 |
{"zfile.txt", 0, false}, |
| 365 |
}, |
| 366 |
}, |
| 367 |
{ |
| 368 |
"subdir8", []entry{ |
| 369 |
{"doesntexist", 0, false}, // entry is returned even if destination file doesn't exist |
| 370 |
}, |
| 371 |
}, |
| 372 |
{ |
| 373 |
// check that read dir actually redirects files that already exist |
| 374 |
// the original this_file_is_overlaid.txt is empty |
| 375 |
"subdir9", []entry{ |
| 376 |
{"this_file_is_overlaid.txt", 9, false}, |
| 377 |
}, |
| 378 |
}, |
| 379 |
{ |
| 380 |
"subdir10", []entry{}, |
| 381 |
}, |
| 382 |
{ |
| 383 |
"parentoverwritten", []entry{ |
| 384 |
{"subdir1", 2, false}, |
| 385 |
}, |
| 386 |
}, |
| 387 |
{ |
| 388 |
"textfile.txt", []entry{ |
| 389 |
{"file.go", 2, false}, |
| 390 |
}, |
| 391 |
}, |
| 392 |
} |
| 393 |
|
| 394 |
for _, tc := range testCases { |
| 395 |
dir, want := tc.dir, tc.want |
| 396 |
infos, err := ReadDir(dir) |
| 397 |
if err != nil { |
| 398 |
t.Errorf("ReadDir(%q): %v", dir, err) |
| 399 |
continue |
| 400 |
} |
| 401 |
// Sorted diff of want and infos. |
| 402 |
for len(infos) > 0 || len(want) > 0 { |
| 403 |
switch { |
| 404 |
case len(want) == 0 || len(infos) > 0 && infos[0].Name() < want[0].name: |
| 405 |
t.Errorf("ReadDir(%q): unexpected entry: %s IsDir=%v", dir, infos[0].Name(), infos[0].IsDir()) |
| 406 |
infos = infos[1:] |
| 407 |
case len(infos) == 0 || len(want) > 0 && want[0].name < infos[0].Name(): |
| 408 |
t.Errorf("ReadDir(%q): missing entry: %s IsDir=%v", dir, want[0].name, want[0].isDir) |
| 409 |
want = want[1:] |
| 410 |
default: |
| 411 |
if infos[0].IsDir() != want[0].isDir { |
| 412 |
t.Errorf("ReadDir(%q): %s: IsDir=%v, want IsDir=%v", dir, want[0].name, infos[0].IsDir(), want[0].isDir) |
| 413 |
} |
| 414 |
infos = infos[1:] |
| 415 |
want = want[1:] |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
errCases := []string{ |
| 421 |
"subdir1/file1.txt", // regular file on disk |
| 422 |
"subdir2/file2.txt", // regular file in overlay |
| 423 |
"subdir4", // directory overlaid with regular file |
| 424 |
"subdir5", // directory deleted in overlay |
| 425 |
"parentoverwritten/subdir1/subdir2/subdir3", // parentoverwritten/subdir1 overlaid with regular file |
| 426 |
"parentoverwritten/subdir1/subdir2", // parentoverwritten/subdir1 overlaid with regular file |
| 427 |
"subdir11", // directory with deleted child, overlaid with regular file |
| 428 |
"other/pointstodir", |
| 429 |
} |
| 430 |
|
| 431 |
for _, dir := range errCases { |
| 432 |
_, err := ReadDir(dir) |
| 433 |
if _, ok := err.(*fs.PathError); !ok { |
| 434 |
t.Errorf("ReadDir(%q): err = %T (%v), want fs.PathError", dir, err, err) |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
func TestGlob(t *testing.T) { |
| 440 |
initOverlay(t, readDirOverlay) |
| 441 |
|
| 442 |
testCases := []struct { |
| 443 |
pattern string |
| 444 |
match []string |
| 445 |
}{ |
| 446 |
{ |
| 447 |
"*o*", |
| 448 |
[]string{ |
| 449 |
"other", |
| 450 |
"overlayfiles", |
| 451 |
"parentoverwritten", |
| 452 |
}, |
| 453 |
}, |
| 454 |
{ |
| 455 |
"subdir2/file2.txt", |
| 456 |
[]string{ |
| 457 |
"subdir2/file2.txt", |
| 458 |
}, |
| 459 |
}, |
| 460 |
{ |
| 461 |
"*/*.txt", |
| 462 |
[]string{ |
| 463 |
"overlayfiles/subdir2_file2.txt", |
| 464 |
"overlayfiles/subdir3_file3b.txt", |
| 465 |
"overlayfiles/subdir6_asubsubdir_afile.txt", |
| 466 |
"overlayfiles/subdir6_asubsubdir_zfile.txt", |
| 467 |
"overlayfiles/subdir6_zsubsubdir_file.txt", |
| 468 |
"overlayfiles/subdir7_asubsubdir_file.txt", |
| 469 |
"overlayfiles/subdir7_zsubsubdir_file.txt", |
| 470 |
"overlayfiles/subdir9_this_file_is_overlaid.txt", |
| 471 |
"subdir1/file1.txt", |
| 472 |
"subdir2/file2.txt", |
| 473 |
"subdir3/file3a.txt", |
| 474 |
"subdir3/file3b.txt", |
| 475 |
"subdir6/file.txt", |
| 476 |
"subdir9/this_file_is_overlaid.txt", |
| 477 |
}, |
| 478 |
}, |
| 479 |
} |
| 480 |
|
| 481 |
for _, tc := range testCases { |
| 482 |
pattern := tc.pattern |
| 483 |
match, err := Glob(pattern) |
| 484 |
if err != nil { |
| 485 |
t.Errorf("Glob(%q): %v", pattern, err) |
| 486 |
continue |
| 487 |
} |
| 488 |
want := tc.match |
| 489 |
for i, name := range want { |
| 490 |
if name != tc.pattern { |
| 491 |
want[i] = filepath.FromSlash(name) |
| 492 |
} |
| 493 |
} |
| 494 |
for len(match) > 0 || len(want) > 0 { |
| 495 |
switch { |
| 496 |
case len(match) == 0 || len(want) > 0 && want[0] < match[0]: |
| 497 |
t.Errorf("Glob(%q): missing match: %s", pattern, want[0]) |
| 498 |
want = want[1:] |
| 499 |
case len(want) == 0 || len(match) > 0 && match[0] < want[0]: |
| 500 |
t.Errorf("Glob(%q): extra match: %s", pattern, match[0]) |
| 501 |
match = match[1:] |
| 502 |
default: |
| 503 |
want = want[1:] |
| 504 |
match = match[1:] |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
func TestActual(t *testing.T) { |
| 511 |
initOverlay(t, ` |
| 512 |
{ |
| 513 |
"Replace": { |
| 514 |
"subdir2/file2.txt": "overlayfiles/subdir2_file2.txt", |
| 515 |
"subdir3/doesntexist": "this_file_doesnt_exist_anywhere", |
| 516 |
"subdir4/this_file_is_overlaid.txt": "overlayfiles/subdir4_this_file_is_overlaid.txt", |
| 517 |
"subdir5/deleted.txt": "", |
| 518 |
"parentoverwritten/subdir1": "" |
| 519 |
} |
| 520 |
} |
| 521 |
-- subdir1/file1.txt -- |
| 522 |
file 1 |
| 523 |
-- subdir4/this_file_is_overlaid.txt -- |
| 524 |
these contents are replaced by the overlay |
| 525 |
-- parentoverwritten/subdir1/subdir2/subdir3/file.txt -- |
| 526 |
-- subdir5/deleted.txt -- |
| 527 |
deleted |
| 528 |
-- overlayfiles/subdir2_file2.txt -- |
| 529 |
file 2 |
| 530 |
-- overlayfiles/subdir4_this_file_is_overlaid.txt -- |
| 531 |
99999999 |
| 532 |
`) |
| 533 |
|
| 534 |
cwd := cwd() |
| 535 |
testCases := []struct { |
| 536 |
path string |
| 537 |
wantPath string |
| 538 |
wantOK bool |
| 539 |
}{ |
| 540 |
{"subdir1/file1.txt", "subdir1/file1.txt", false}, |
| 541 |
// Actual returns false for directories |
| 542 |
{"subdir2", "subdir2", false}, |
| 543 |
{"subdir2/file2.txt", filepath.Join(cwd, "overlayfiles/subdir2_file2.txt"), true}, |
| 544 |
// Actual doesn't stat a file to see if it exists, so it happily returns |
| 545 |
// the 'to' path and true even if the 'to' path doesn't exist on disk. |
| 546 |
{"subdir3/doesntexist", filepath.Join(cwd, "this_file_doesnt_exist_anywhere"), true}, |
| 547 |
// Like the subdir2/file2.txt case above, but subdir4 exists on disk, but subdir2 does not. |
| 548 |
{"subdir4/this_file_is_overlaid.txt", filepath.Join(cwd, "overlayfiles/subdir4_this_file_is_overlaid.txt"), true}, |
| 549 |
{"subdir5", "subdir5", false}, |
| 550 |
{"subdir5/deleted.txt", "", true}, |
| 551 |
} |
| 552 |
|
| 553 |
for _, tc := range testCases { |
| 554 |
path := Actual(tc.path) |
| 555 |
ok := Replaced(tc.path) |
| 556 |
|
| 557 |
if path != tc.wantPath { |
| 558 |
t.Errorf("Actual(%q) = %q, want %q", tc.path, path, tc.wantPath) |
| 559 |
} |
| 560 |
if ok != tc.wantOK { |
| 561 |
t.Errorf("Replaced(%q) = %v, want %v", tc.path, ok, tc.wantOK) |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
func TestOpen(t *testing.T) { |
| 567 |
initOverlay(t, ` |
| 568 |
{ |
| 569 |
"Replace": { |
| 570 |
"subdir2/file2.txt": "overlayfiles/subdir2_file2.txt", |
| 571 |
"subdir3/doesntexist": "this_file_doesnt_exist_anywhere", |
| 572 |
"subdir4/this_file_is_overlaid.txt": "overlayfiles/subdir4_this_file_is_overlaid.txt", |
| 573 |
"subdir5/deleted.txt": "", |
| 574 |
"parentoverwritten/subdir1": "", |
| 575 |
"childoverlay/subdir1.txt/child.txt": "overlayfiles/child.txt", |
| 576 |
"subdir11/deleted.txt": "", |
| 577 |
"subdir11": "overlayfiles/subdir11", |
| 578 |
"parentdeleted": "", |
| 579 |
"parentdeleted/file.txt": "overlayfiles/parentdeleted_file.txt" |
| 580 |
} |
| 581 |
} |
| 582 |
-- subdir11/deleted.txt -- |
| 583 |
-- subdir1/file1.txt -- |
| 584 |
file 1 |
| 585 |
-- subdir4/this_file_is_overlaid.txt -- |
| 586 |
these contents are replaced by the overlay |
| 587 |
-- parentoverwritten/subdir1/subdir2/subdir3/file.txt -- |
| 588 |
-- childoverlay/subdir1.txt -- |
| 589 |
this file doesn't exist because the path |
| 590 |
childoverlay/subdir1.txt/child.txt is in the overlay |
| 591 |
-- subdir5/deleted.txt -- |
| 592 |
deleted |
| 593 |
-- parentdeleted -- |
| 594 |
this will be deleted so that parentdeleted/file.txt can exist |
| 595 |
-- overlayfiles/subdir2_file2.txt -- |
| 596 |
file 2 |
| 597 |
-- overlayfiles/subdir4_this_file_is_overlaid.txt -- |
| 598 |
99999999 |
| 599 |
-- overlayfiles/child.txt -- |
| 600 |
-- overlayfiles/subdir11 -- |
| 601 |
11 |
| 602 |
-- overlayfiles/parentdeleted_file.txt -- |
| 603 |
this can exist because the parent directory is deleted |
| 604 |
`) |
| 605 |
|
| 606 |
testCases := []struct { |
| 607 |
path string |
| 608 |
wantContents string |
| 609 |
isErr bool |
| 610 |
}{ |
| 611 |
{"subdir1/file1.txt", "file 1\n", false}, |
| 612 |
{"subdir2/file2.txt", "file 2\n", false}, |
| 613 |
{"subdir3/doesntexist", "", true}, |
| 614 |
{"subdir4/this_file_is_overlaid.txt", "99999999\n", false}, |
| 615 |
{"subdir5/deleted.txt", "", true}, |
| 616 |
{"parentoverwritten/subdir1/subdir2/subdir3/file.txt", "", true}, |
| 617 |
{"childoverlay/subdir1.txt", "", true}, |
| 618 |
{"subdir11", "11\n", false}, |
| 619 |
{"parentdeleted/file.txt", "this can exist because the parent directory is deleted\n", false}, |
| 620 |
} |
| 621 |
|
| 622 |
for _, tc := range testCases { |
| 623 |
f, err := Open(tc.path) |
| 624 |
if tc.isErr { |
| 625 |
if err == nil { |
| 626 |
f.Close() |
| 627 |
t.Errorf("Open(%q): got no error, but want error", tc.path) |
| 628 |
} |
| 629 |
continue |
| 630 |
} |
| 631 |
if err != nil { |
| 632 |
t.Errorf("Open(%q): got error %v, want nil", tc.path, err) |
| 633 |
continue |
| 634 |
} |
| 635 |
contents, err := io.ReadAll(f) |
| 636 |
if err != nil { |
| 637 |
t.Errorf("unexpected error reading contents of file: %v", err) |
| 638 |
} |
| 639 |
if string(contents) != tc.wantContents { |
| 640 |
t.Errorf("contents of file opened with Open(%q): got %q, want %q", |
| 641 |
tc.path, contents, tc.wantContents) |
| 642 |
} |
| 643 |
f.Close() |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
func TestIsGoDir(t *testing.T) { |
| 648 |
initOverlay(t, ` |
| 649 |
{ |
| 650 |
"Replace": { |
| 651 |
"goinoverlay/file.go": "dummy", |
| 652 |
"directory/removed/by/file": "dummy", |
| 653 |
"directory_with_go_dir/dir.go/file.txt": "dummy", |
| 654 |
"otherdirectory/deleted.go": "", |
| 655 |
"nonexistentdirectory/deleted.go": "", |
| 656 |
"textfile.txt/file.go": "dummy" |
| 657 |
} |
| 658 |
} |
| 659 |
-- dummy -- |
| 660 |
a destination file for the overlay entries to point to |
| 661 |
contents don't matter for this test |
| 662 |
-- nogo/file.txt -- |
| 663 |
-- goondisk/file.go -- |
| 664 |
-- goinoverlay/file.txt -- |
| 665 |
-- directory/removed/by/file/in/overlay/file.go -- |
| 666 |
-- otherdirectory/deleted.go -- |
| 667 |
-- textfile.txt -- |
| 668 |
`) |
| 669 |
|
| 670 |
testCases := []struct { |
| 671 |
dir string |
| 672 |
want bool |
| 673 |
wantErr bool |
| 674 |
}{ |
| 675 |
{"nogo", false, false}, |
| 676 |
{"goondisk", true, false}, |
| 677 |
{"goinoverlay", true, false}, |
| 678 |
{"directory/removed/by/file/in/overlay", false, false}, |
| 679 |
{"directory_with_go_dir", false, false}, |
| 680 |
{"otherdirectory", false, false}, |
| 681 |
{"nonexistentdirectory", false, false}, |
| 682 |
{"textfile.txt", true, false}, |
| 683 |
} |
| 684 |
|
| 685 |
for _, tc := range testCases { |
| 686 |
got, gotErr := IsGoDir(tc.dir) |
| 687 |
if tc.wantErr { |
| 688 |
if gotErr == nil { |
| 689 |
t.Errorf("IsGoDir(%q): got %v, %v; want non-nil error", tc.dir, got, gotErr) |
| 690 |
} |
| 691 |
continue |
| 692 |
} |
| 693 |
if gotErr != nil { |
| 694 |
t.Errorf("IsGoDir(%q): got %v, %v; want nil error", tc.dir, got, gotErr) |
| 695 |
} |
| 696 |
if got != tc.want { |
| 697 |
t.Errorf("IsGoDir(%q) = %v; want %v", tc.dir, got, tc.want) |
| 698 |
} |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
func TestWalk(t *testing.T) { |
| 703 |
// The root of the walk must be a name with an actual basename, not just ".". |
| 704 |
// Walk uses Lstat to obtain the name of the root, and Lstat on platforms |
| 705 |
// other than Plan 9 reports the name "." instead of the actual base name of |
| 706 |
// the directory. (See https://golang.org/issue/42115.) |
| 707 |
|
| 708 |
type file struct { |
| 709 |
path string |
| 710 |
name string |
| 711 |
size int64 |
| 712 |
mode fs.FileMode |
| 713 |
isDir bool |
| 714 |
} |
| 715 |
testCases := []struct { |
| 716 |
name string |
| 717 |
overlay string |
| 718 |
root string |
| 719 |
wantFiles []file |
| 720 |
}{ |
| 721 |
{"no overlay", ` |
| 722 |
{} |
| 723 |
-- dir/file.txt -- |
| 724 |
`, |
| 725 |
"dir", |
| 726 |
[]file{ |
| 727 |
{"dir", "dir", 0, fs.ModeDir | 0700, true}, |
| 728 |
{"dir/file.txt", "file.txt", 0, 0600, false}, |
| 729 |
}, |
| 730 |
}, |
| 731 |
{"overlay with different file", ` |
| 732 |
{ |
| 733 |
"Replace": { |
| 734 |
"dir/file.txt": "dir/other.txt" |
| 735 |
} |
| 736 |
} |
| 737 |
-- dir/file.txt -- |
| 738 |
-- dir/other.txt -- |
| 739 |
contents of other file |
| 740 |
`, |
| 741 |
"dir", |
| 742 |
[]file{ |
| 743 |
{"dir", "dir", 0, fs.ModeDir | 0500, true}, |
| 744 |
{"dir/file.txt", "file.txt", 23, 0600, false}, |
| 745 |
{"dir/other.txt", "other.txt", 23, 0600, false}, |
| 746 |
}, |
| 747 |
}, |
| 748 |
{"overlay with new file", ` |
| 749 |
{ |
| 750 |
"Replace": { |
| 751 |
"dir/file.txt": "dir/other.txt" |
| 752 |
} |
| 753 |
} |
| 754 |
-- dir/other.txt -- |
| 755 |
contents of other file |
| 756 |
`, |
| 757 |
"dir", |
| 758 |
[]file{ |
| 759 |
{"dir", "dir", 0, fs.ModeDir | 0500, true}, |
| 760 |
{"dir/file.txt", "file.txt", 23, 0600, false}, |
| 761 |
{"dir/other.txt", "other.txt", 23, 0600, false}, |
| 762 |
}, |
| 763 |
}, |
| 764 |
{"overlay with new directory", ` |
| 765 |
{ |
| 766 |
"Replace": { |
| 767 |
"dir/subdir/file.txt": "dir/other.txt" |
| 768 |
} |
| 769 |
} |
| 770 |
-- dir/other.txt -- |
| 771 |
contents of other file |
| 772 |
`, |
| 773 |
"dir", |
| 774 |
[]file{ |
| 775 |
{"dir", "dir", 0, fs.ModeDir | 0500, true}, |
| 776 |
{"dir/other.txt", "other.txt", 23, 0600, false}, |
| 777 |
{"dir/subdir", "subdir", 0, fs.ModeDir | 0500, true}, |
| 778 |
{"dir/subdir/file.txt", "file.txt", 23, 0600, false}, |
| 779 |
}, |
| 780 |
}, |
| 781 |
} |
| 782 |
|
| 783 |
for _, tc := range testCases { |
| 784 |
t.Run(tc.name, func(t *testing.T) { |
| 785 |
initOverlay(t, tc.overlay) |
| 786 |
|
| 787 |
var got []file |
| 788 |
WalkDir(tc.root, func(path string, d fs.DirEntry, err error) error { |
| 789 |
info, err := d.Info() |
| 790 |
if err != nil { |
| 791 |
t.Fatal(err) |
| 792 |
} |
| 793 |
if info.Name() != d.Name() { |
| 794 |
t.Errorf("walk %s: d.Name() = %q, but info.Name() = %q", path, d.Name(), info.Name()) |
| 795 |
} |
| 796 |
if info.IsDir() != d.IsDir() { |
| 797 |
t.Errorf("walk %s: d.IsDir() = %v, but info.IsDir() = %v", path, d.IsDir(), info.IsDir()) |
| 798 |
} |
| 799 |
if info.Mode().Type() != d.Type() { |
| 800 |
t.Errorf("walk %s: d.Type() = %v, but info.Mode().Type() = %v", path, d.Type(), info.Mode().Type()) |
| 801 |
} |
| 802 |
got = append(got, file{path, d.Name(), info.Size(), info.Mode(), d.IsDir()}) |
| 803 |
return nil |
| 804 |
}) |
| 805 |
|
| 806 |
if len(got) != len(tc.wantFiles) { |
| 807 |
t.Errorf("Walk: saw %#v in walk; want %#v", got, tc.wantFiles) |
| 808 |
} |
| 809 |
for i := 0; i < len(got) && i < len(tc.wantFiles); i++ { |
| 810 |
wantPath := filepath.FromSlash(tc.wantFiles[i].path) |
| 811 |
if got[i].path != wantPath { |
| 812 |
t.Errorf("walk #%d: path = %q, want %q", i, got[i].path, wantPath) |
| 813 |
} |
| 814 |
if got[i].name != tc.wantFiles[i].name { |
| 815 |
t.Errorf("walk %s: Name = %q, want %q", got[i].path, got[i].name, tc.wantFiles[i].name) |
| 816 |
} |
| 817 |
if got[i].mode&(fs.ModeDir|0700) != tc.wantFiles[i].mode { |
| 818 |
t.Errorf("walk %s: Mode = %q, want %q", got[i].path, got[i].mode&(fs.ModeDir|0700), tc.wantFiles[i].mode) |
| 819 |
} |
| 820 |
if got[i].isDir != tc.wantFiles[i].isDir { |
| 821 |
t.Errorf("walk %s: IsDir = %v, want %v", got[i].path, got[i].isDir, tc.wantFiles[i].isDir) |
| 822 |
} |
| 823 |
} |
| 824 |
}) |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
func TestWalkSkipDir(t *testing.T) { |
| 829 |
initOverlay(t, ` |
| 830 |
{ |
| 831 |
"Replace": { |
| 832 |
"dir/skip/file.go": "dummy.txt", |
| 833 |
"dir/dontskip/file.go": "dummy.txt", |
| 834 |
"dir/dontskip/skip/file.go": "dummy.txt" |
| 835 |
} |
| 836 |
} |
| 837 |
-- dummy.txt -- |
| 838 |
`) |
| 839 |
|
| 840 |
var seen []string |
| 841 |
WalkDir("dir", func(path string, d fs.DirEntry, err error) error { |
| 842 |
seen = append(seen, filepath.ToSlash(path)) |
| 843 |
if d.Name() == "skip" { |
| 844 |
return filepath.SkipDir |
| 845 |
} |
| 846 |
return nil |
| 847 |
}) |
| 848 |
|
| 849 |
wantSeen := []string{"dir", "dir/dontskip", "dir/dontskip/file.go", "dir/dontskip/skip", "dir/skip"} |
| 850 |
|
| 851 |
if len(seen) != len(wantSeen) { |
| 852 |
t.Errorf("paths seen in walk: got %v entries; want %v entries", len(seen), len(wantSeen)) |
| 853 |
} |
| 854 |
|
| 855 |
for i := 0; i < len(seen) && i < len(wantSeen); i++ { |
| 856 |
if seen[i] != wantSeen[i] { |
| 857 |
t.Errorf("path #%v seen walking tree: want %q, got %q", i, seen[i], wantSeen[i]) |
| 858 |
} |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
func TestWalkSkipAll(t *testing.T) { |
| 863 |
initOverlay(t, ` |
| 864 |
{ |
| 865 |
"Replace": { |
| 866 |
"dir/subdir1/foo1": "dummy.txt", |
| 867 |
"dir/subdir1/foo2": "dummy.txt", |
| 868 |
"dir/subdir1/foo3": "dummy.txt", |
| 869 |
"dir/subdir2/foo4": "dummy.txt", |
| 870 |
"dir/zzlast": "dummy.txt" |
| 871 |
} |
| 872 |
} |
| 873 |
-- dummy.txt -- |
| 874 |
`) |
| 875 |
|
| 876 |
var seen []string |
| 877 |
WalkDir("dir", func(path string, d fs.DirEntry, err error) error { |
| 878 |
seen = append(seen, filepath.ToSlash(path)) |
| 879 |
if d.Name() == "foo2" { |
| 880 |
return filepath.SkipAll |
| 881 |
} |
| 882 |
return nil |
| 883 |
}) |
| 884 |
|
| 885 |
wantSeen := []string{"dir", "dir/subdir1", "dir/subdir1/foo1", "dir/subdir1/foo2"} |
| 886 |
|
| 887 |
if len(seen) != len(wantSeen) { |
| 888 |
t.Errorf("paths seen in walk: got %v entries; want %v entries", len(seen), len(wantSeen)) |
| 889 |
} |
| 890 |
|
| 891 |
for i := 0; i < len(seen) && i < len(wantSeen); i++ { |
| 892 |
if seen[i] != wantSeen[i] { |
| 893 |
t.Errorf("path %#v seen walking tree: got %q, want %q", i, seen[i], wantSeen[i]) |
| 894 |
} |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
func TestWalkError(t *testing.T) { |
| 899 |
initOverlay(t, "{}") |
| 900 |
|
| 901 |
alreadyCalled := false |
| 902 |
err := WalkDir("foo", func(path string, d fs.DirEntry, err error) error { |
| 903 |
if alreadyCalled { |
| 904 |
t.Fatal("expected walk function to be called exactly once, but it was called more than once") |
| 905 |
} |
| 906 |
alreadyCalled = true |
| 907 |
return errors.New("returned from function") |
| 908 |
}) |
| 909 |
if !alreadyCalled { |
| 910 |
t.Fatal("expected walk function to be called exactly once, but it was never called") |
| 911 |
|
| 912 |
} |
| 913 |
if err == nil { |
| 914 |
t.Fatalf("Walk: got no error, want error") |
| 915 |
} |
| 916 |
if err.Error() != "returned from function" { |
| 917 |
t.Fatalf("Walk: got error %v, want \"returned from function\" error", err) |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
func TestWalkSymlink(t *testing.T) { |
| 922 |
testenv.MustHaveSymlink(t) |
| 923 |
|
| 924 |
initOverlay(t, `{ |
| 925 |
"Replace": {"overlay_symlink/file": "symlink/file"} |
| 926 |
} |
| 927 |
-- dir/file --`) |
| 928 |
|
| 929 |
// Create symlink |
| 930 |
if err := os.Symlink("dir", "symlink"); err != nil { |
| 931 |
t.Error(err) |
| 932 |
} |
| 933 |
|
| 934 |
testCases := []struct { |
| 935 |
name string |
| 936 |
dir string |
| 937 |
wantFiles []string |
| 938 |
}{ |
| 939 |
{"control", "dir", []string{"dir", filepath.Join("dir", "file")}}, |
| 940 |
// ensure Walk doesn't walk into the directory pointed to by the symlink |
| 941 |
// (because it's supposed to use Lstat instead of Stat). |
| 942 |
{"symlink_to_dir", "symlink", []string{"symlink"}}, |
| 943 |
{"overlay_to_symlink_to_dir", "overlay_symlink", []string{"overlay_symlink", filepath.Join("overlay_symlink", "file")}}, |
| 944 |
|
| 945 |
// However, adding filepath.Separator should cause the link to be resolved. |
| 946 |
{"symlink_with_slash", "symlink" + string(filepath.Separator), []string{"symlink" + string(filepath.Separator), filepath.Join("symlink", "file")}}, |
| 947 |
{"overlay_to_symlink_to_dir", "overlay_symlink" + string(filepath.Separator), []string{"overlay_symlink" + string(filepath.Separator), filepath.Join("overlay_symlink", "file")}}, |
| 948 |
} |
| 949 |
|
| 950 |
for _, tc := range testCases { |
| 951 |
t.Run(tc.name, func(t *testing.T) { |
| 952 |
var got []string |
| 953 |
|
| 954 |
err := WalkDir(tc.dir, func(path string, d fs.DirEntry, err error) error { |
| 955 |
t.Logf("walk %q", path) |
| 956 |
got = append(got, path) |
| 957 |
if err != nil { |
| 958 |
t.Errorf("walkfn: got non nil err argument: %v, want nil err argument", err) |
| 959 |
} |
| 960 |
return nil |
| 961 |
}) |
| 962 |
if err != nil { |
| 963 |
t.Errorf("Walk: got error %q, want nil", err) |
| 964 |
} |
| 965 |
|
| 966 |
if !reflect.DeepEqual(got, tc.wantFiles) { |
| 967 |
t.Errorf("files examined by walk: got %v, want %v", got, tc.wantFiles) |
| 968 |
} |
| 969 |
}) |
| 970 |
} |
| 971 |
|
| 972 |
} |
| 973 |
|
| 974 |
func TestLstat(t *testing.T) { |
| 975 |
type file struct { |
| 976 |
name string |
| 977 |
size int64 |
| 978 |
mode fs.FileMode // mode & (fs.ModeDir|0x700): only check 'user' permissions |
| 979 |
isDir bool |
| 980 |
} |
| 981 |
|
| 982 |
testCases := []struct { |
| 983 |
name string |
| 984 |
overlay string |
| 985 |
path string |
| 986 |
|
| 987 |
want file |
| 988 |
wantErr bool |
| 989 |
}{ |
| 990 |
{ |
| 991 |
"regular_file", |
| 992 |
`{} |
| 993 |
-- file.txt -- |
| 994 |
contents`, |
| 995 |
"file.txt", |
| 996 |
file{"file.txt", 9, 0600, false}, |
| 997 |
false, |
| 998 |
}, |
| 999 |
{ |
| 1000 |
"new_file_in_overlay", |
| 1001 |
`{"Replace": {"file.txt": "dummy.txt"}} |
| 1002 |
-- dummy.txt -- |
| 1003 |
contents`, |
| 1004 |
"file.txt", |
| 1005 |
file{"file.txt", 9, 0600, false}, |
| 1006 |
false, |
| 1007 |
}, |
| 1008 |
{ |
| 1009 |
"file_replaced_in_overlay", |
| 1010 |
`{"Replace": {"file.txt": "dummy.txt"}} |
| 1011 |
-- file.txt -- |
| 1012 |
-- dummy.txt -- |
| 1013 |
contents`, |
| 1014 |
"file.txt", |
| 1015 |
file{"file.txt", 9, 0600, false}, |
| 1016 |
false, |
| 1017 |
}, |
| 1018 |
{ |
| 1019 |
"file_cant_exist", |
| 1020 |
`{"Replace": {"deleted": "dummy.txt"}} |
| 1021 |
-- deleted/file.txt -- |
| 1022 |
-- dummy.txt -- |
| 1023 |
`, |
| 1024 |
"deleted/file.txt", |
| 1025 |
file{}, |
| 1026 |
true, |
| 1027 |
}, |
| 1028 |
{ |
| 1029 |
"deleted", |
| 1030 |
`{"Replace": {"deleted": ""}} |
| 1031 |
-- deleted -- |
| 1032 |
`, |
| 1033 |
"deleted", |
| 1034 |
file{}, |
| 1035 |
true, |
| 1036 |
}, |
| 1037 |
{ |
| 1038 |
"dir_on_disk", |
| 1039 |
`{} |
| 1040 |
-- dir/foo.txt -- |
| 1041 |
`, |
| 1042 |
"dir", |
| 1043 |
file{"dir", 0, 0700 | fs.ModeDir, true}, |
| 1044 |
false, |
| 1045 |
}, |
| 1046 |
{ |
| 1047 |
"dir_in_overlay", |
| 1048 |
`{"Replace": {"dir/file.txt": "dummy.txt"}} |
| 1049 |
-- dummy.txt -- |
| 1050 |
`, |
| 1051 |
"dir", |
| 1052 |
file{"dir", 0, 0500 | fs.ModeDir, true}, |
| 1053 |
false, |
| 1054 |
}, |
| 1055 |
} |
| 1056 |
|
| 1057 |
for _, tc := range testCases { |
| 1058 |
t.Run(tc.name, func(t *testing.T) { |
| 1059 |
initOverlay(t, tc.overlay) |
| 1060 |
got, err := Lstat(tc.path) |
| 1061 |
if tc.wantErr { |
| 1062 |
if err == nil { |
| 1063 |
t.Errorf("lstat(%q): got no error, want error", tc.path) |
| 1064 |
} |
| 1065 |
return |
| 1066 |
} |
| 1067 |
if err != nil { |
| 1068 |
t.Fatalf("lstat(%q): got error %v, want no error", tc.path, err) |
| 1069 |
} |
| 1070 |
if got.Name() != tc.want.name { |
| 1071 |
t.Errorf("lstat(%q).Name(): got %q, want %q", tc.path, got.Name(), tc.want.name) |
| 1072 |
} |
| 1073 |
if got.Mode()&(fs.ModeDir|0700) != tc.want.mode { |
| 1074 |
t.Errorf("lstat(%q).Mode()&(fs.ModeDir|0700): got %v, want %v", tc.path, got.Mode()&(fs.ModeDir|0700), tc.want.mode) |
| 1075 |
} |
| 1076 |
if got.IsDir() != tc.want.isDir { |
| 1077 |
t.Errorf("lstat(%q).IsDir(): got %v, want %v", tc.path, got.IsDir(), tc.want.isDir) |
| 1078 |
} |
| 1079 |
if tc.want.isDir { |
| 1080 |
return // don't check size for directories |
| 1081 |
} |
| 1082 |
if got.Size() != tc.want.size { |
| 1083 |
t.Errorf("lstat(%q).Size(): got %v, want %v", tc.path, got.Size(), tc.want.size) |
| 1084 |
} |
| 1085 |
}) |
| 1086 |
} |
| 1087 |
} |
| 1088 |
|
| 1089 |
func TestStat(t *testing.T) { |
| 1090 |
testenv.MustHaveSymlink(t) |
| 1091 |
|
| 1092 |
type file struct { |
| 1093 |
name string |
| 1094 |
size int64 |
| 1095 |
mode os.FileMode // mode & (os.ModeDir|0x700): only check 'user' permissions |
| 1096 |
isDir bool |
| 1097 |
} |
| 1098 |
|
| 1099 |
testCases := []struct { |
| 1100 |
name string |
| 1101 |
overlay string |
| 1102 |
path string |
| 1103 |
|
| 1104 |
want file |
| 1105 |
wantErr bool |
| 1106 |
}{ |
| 1107 |
{ |
| 1108 |
"regular_file", |
| 1109 |
`{} |
| 1110 |
-- file.txt -- |
| 1111 |
contents`, |
| 1112 |
"file.txt", |
| 1113 |
file{"file.txt", 9, 0600, false}, |
| 1114 |
false, |
| 1115 |
}, |
| 1116 |
{ |
| 1117 |
"new_file_in_overlay", |
| 1118 |
`{"Replace": {"file.txt": "dummy.txt"}} |
| 1119 |
-- dummy.txt -- |
| 1120 |
contents`, |
| 1121 |
"file.txt", |
| 1122 |
file{"file.txt", 9, 0600, false}, |
| 1123 |
false, |
| 1124 |
}, |
| 1125 |
{ |
| 1126 |
"file_replaced_in_overlay", |
| 1127 |
`{"Replace": {"file.txt": "dummy.txt"}} |
| 1128 |
-- file.txt -- |
| 1129 |
-- dummy.txt -- |
| 1130 |
contents`, |
| 1131 |
"file.txt", |
| 1132 |
file{"file.txt", 9, 0600, false}, |
| 1133 |
false, |
| 1134 |
}, |
| 1135 |
{ |
| 1136 |
"file_cant_exist", |
| 1137 |
`{"Replace": {"deleted": "dummy.txt"}} |
| 1138 |
-- deleted/file.txt -- |
| 1139 |
-- dummy.txt -- |
| 1140 |
`, |
| 1141 |
"deleted/file.txt", |
| 1142 |
file{}, |
| 1143 |
true, |
| 1144 |
}, |
| 1145 |
{ |
| 1146 |
"deleted", |
| 1147 |
`{"Replace": {"deleted": ""}} |
| 1148 |
-- deleted -- |
| 1149 |
`, |
| 1150 |
"deleted", |
| 1151 |
file{}, |
| 1152 |
true, |
| 1153 |
}, |
| 1154 |
{ |
| 1155 |
"dir_on_disk", |
| 1156 |
`{} |
| 1157 |
-- dir/foo.txt -- |
| 1158 |
`, |
| 1159 |
"dir", |
| 1160 |
file{"dir", 0, 0700 | os.ModeDir, true}, |
| 1161 |
false, |
| 1162 |
}, |
| 1163 |
{ |
| 1164 |
"dir_in_overlay", |
| 1165 |
`{"Replace": {"dir/file.txt": "dummy.txt"}} |
| 1166 |
-- dummy.txt -- |
| 1167 |
`, |
| 1168 |
"dir", |
| 1169 |
file{"dir", 0, 0500 | os.ModeDir, true}, |
| 1170 |
false, |
| 1171 |
}, |
| 1172 |
} |
| 1173 |
|
| 1174 |
for _, tc := range testCases { |
| 1175 |
t.Run(tc.name, func(t *testing.T) { |
| 1176 |
initOverlay(t, tc.overlay) |
| 1177 |
got, err := Stat(tc.path) |
| 1178 |
if tc.wantErr { |
| 1179 |
if err == nil { |
| 1180 |
t.Errorf("Stat(%q): got no error, want error", tc.path) |
| 1181 |
} |
| 1182 |
return |
| 1183 |
} |
| 1184 |
if err != nil { |
| 1185 |
t.Fatalf("Stat(%q): got error %v, want no error", tc.path, err) |
| 1186 |
} |
| 1187 |
if got.Name() != tc.want.name { |
| 1188 |
t.Errorf("Stat(%q).Name(): got %q, want %q", tc.path, got.Name(), tc.want.name) |
| 1189 |
} |
| 1190 |
if got.Mode()&(os.ModeDir|0700) != tc.want.mode { |
| 1191 |
t.Errorf("Stat(%q).Mode()&(os.ModeDir|0700): got %v, want %v", tc.path, got.Mode()&(os.ModeDir|0700), tc.want.mode) |
| 1192 |
} |
| 1193 |
if got.IsDir() != tc.want.isDir { |
| 1194 |
t.Errorf("Stat(%q).IsDir(): got %v, want %v", tc.path, got.IsDir(), tc.want.isDir) |
| 1195 |
} |
| 1196 |
if tc.want.isDir { |
| 1197 |
return // don't check size for directories |
| 1198 |
} |
| 1199 |
if got.Size() != tc.want.size { |
| 1200 |
t.Errorf("Stat(%q).Size(): got %v, want %v", tc.path, got.Size(), tc.want.size) |
| 1201 |
} |
| 1202 |
}) |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
func TestStatSymlink(t *testing.T) { |
| 1207 |
testenv.MustHaveSymlink(t) |
| 1208 |
|
| 1209 |
initOverlay(t, `{ |
| 1210 |
"Replace": {"file.go": "symlink"} |
| 1211 |
} |
| 1212 |
-- to.go -- |
| 1213 |
0123456789 |
| 1214 |
`) |
| 1215 |
|
| 1216 |
// Create symlink |
| 1217 |
if err := os.Symlink("to.go", "symlink"); err != nil { |
| 1218 |
t.Error(err) |
| 1219 |
} |
| 1220 |
|
| 1221 |
f := "file.go" |
| 1222 |
fi, err := Stat(f) |
| 1223 |
if err != nil { |
| 1224 |
t.Errorf("Stat(%q): got error %q, want nil error", f, err) |
| 1225 |
} |
| 1226 |
|
| 1227 |
if !fi.Mode().IsRegular() { |
| 1228 |
t.Errorf("Stat(%q).Mode(): got %v, want regular mode", f, fi.Mode()) |
| 1229 |
} |
| 1230 |
|
| 1231 |
if fi.Size() != 11 { |
| 1232 |
t.Errorf("Stat(%q).Size(): got %v, want 11", f, fi.Size()) |
| 1233 |
} |
| 1234 |
} |
| 1235 |
|
| 1236 |
func TestBindOverlay(t *testing.T) { |
| 1237 |
initOverlay(t, `{"Replace": {"mtpt/x.go": "xx.go"}} |
| 1238 |
-- mtpt/x.go -- |
| 1239 |
mtpt/x.go |
| 1240 |
-- mtpt/y.go -- |
| 1241 |
mtpt/y.go |
| 1242 |
-- mtpt2/x.go -- |
| 1243 |
mtpt/x.go |
| 1244 |
-- replaced/x.go -- |
| 1245 |
replaced/x.go |
| 1246 |
-- replaced/x/y/z.go -- |
| 1247 |
replaced/x/y/z.go |
| 1248 |
-- xx.go -- |
| 1249 |
xx.go |
| 1250 |
`) |
| 1251 |
|
| 1252 |
testReadFile(t, "mtpt/x.go", "xx.go\n") |
| 1253 |
|
| 1254 |
Bind("replaced", "mtpt") |
| 1255 |
testReadFile(t, "mtpt/x.go", "replaced/x.go\n") |
| 1256 |
testReadDir(t, "mtpt/x", "y/") |
| 1257 |
testReadDir(t, "mtpt/x/y", "z.go") |
| 1258 |
testReadFile(t, "mtpt/x/y/z.go", "replaced/x/y/z.go\n") |
| 1259 |
testReadFile(t, "mtpt/y.go", "ERROR") |
| 1260 |
|
| 1261 |
Bind("replaced", "mtpt2/a/b") |
| 1262 |
testReadDir(t, "mtpt2", "a/", "x.go") |
| 1263 |
testReadDir(t, "mtpt2/a", "b/") |
| 1264 |
testReadDir(t, "mtpt2/a/b", "x/", "x.go") |
| 1265 |
testReadFile(t, "mtpt2/a/b/x.go", "replaced/x.go\n") |
| 1266 |
} |
| 1267 |
|
| 1268 |
var badOverlayTests = []struct { |
| 1269 |
json string |
| 1270 |
err string |
| 1271 |
}{ |
| 1272 |
{`{`, |
| 1273 |
"parsing overlay JSON: unexpected end of JSON input"}, |
| 1274 |
{`{"Replace": {"":"a"}}`, |
| 1275 |
"empty string key in overlay map"}, |
| 1276 |
{`{"Replace": {"/tmp/x": "y", "x": "y"}}`, |
| 1277 |
`duplicate paths /tmp/x and x in overlay map`}, |
| 1278 |
{`{"Replace": {"/tmp/x/z": "z", "x":"y"}}`, |
| 1279 |
`inconsistent files /tmp/x and /tmp/x/z in overlay map`}, |
| 1280 |
{`{"Replace": {"/tmp/x/z/z2": "z", "x":"y"}}`, |
| 1281 |
`inconsistent files /tmp/x and /tmp/x/z/z2 in overlay map`}, |
| 1282 |
{`{"Replace": {"/tmp/x": "y", "x/z/z2": "z"}}`, |
| 1283 |
`inconsistent files /tmp/x and /tmp/x/z/z2 in overlay map`}, |
| 1284 |
} |
| 1285 |
|
| 1286 |
func TestBadOverlay(t *testing.T) { |
| 1287 |
tmp := "/tmp" |
| 1288 |
if runtime.GOOS == "windows" { |
| 1289 |
tmp = `C:\tmp` |
| 1290 |
} |
| 1291 |
cwd = sync.OnceValue(func() string { return tmp }) |
| 1292 |
defer resetForTesting() |
| 1293 |
|
| 1294 |
for i, tt := range badOverlayTests { |
| 1295 |
if runtime.GOOS == "windows" { |
| 1296 |
tt.json = strings.ReplaceAll(tt.json, `/tmp`, tmp) // fix tmp |
| 1297 |
tt.json = strings.ReplaceAll(tt.json, `/`, `\`) // use backslashes |
| 1298 |
tt.json = strings.ReplaceAll(tt.json, `\`, `\\`) // JSON escaping |
| 1299 |
tt.err = strings.ReplaceAll(tt.err, `/tmp`, tmp) // fix tmp |
| 1300 |
tt.err = strings.ReplaceAll(tt.err, `/`, `\`) // use backslashes |
| 1301 |
} |
| 1302 |
err := initFromJSON([]byte(tt.json)) |
| 1303 |
if err == nil || err.Error() != tt.err { |
| 1304 |
t.Errorf("#%d: err=%v, want %q", i, err, tt.err) |
| 1305 |
} |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
func testReadFile(t *testing.T, name string, want string) { |
| 1310 |
t.Helper() |
| 1311 |
data, err := ReadFile(name) |
| 1312 |
if want == "ERROR" { |
| 1313 |
if data != nil || err == nil { |
| 1314 |
t.Errorf("ReadFile(%q) = %q, %v, want nil, error", name, data, err) |
| 1315 |
} |
| 1316 |
return |
| 1317 |
} |
| 1318 |
if string(data) != want || err != nil { |
| 1319 |
t.Errorf("ReadFile(%q) = %q, %v, want %q, nil", name, data, err, want) |
| 1320 |
} |
| 1321 |
} |
| 1322 |
|
| 1323 |
func testReadDir(t *testing.T, name string, want ...string) { |
| 1324 |
t.Helper() |
| 1325 |
dirs, err := ReadDir(name) |
| 1326 |
var names []string |
| 1327 |
for _, d := range dirs { |
| 1328 |
name := d.Name() |
| 1329 |
if d.IsDir() { |
| 1330 |
name += "/" |
| 1331 |
} |
| 1332 |
names = append(names, name) |
| 1333 |
} |
| 1334 |
if !slices.Equal(names, want) || err != nil { |
| 1335 |
t.Errorf("ReadDir(%q) = %q, %v, want %q, nil", name, names, err, want) |
| 1336 |
} |
| 1337 |
} |