Skip to content

Commit 214136b

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/fsys: use a root other than "." in Walk tests
Fixes #42115 Change-Id: Icf4c9eac5ed3295acbc8377c7a06f82c6bddc747 Reviewed-on: https://go-review.googlesource.com/c/go/+/264177 Trust: Bryan C. Mills <[email protected]> Trust: Jay Conrod <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: David du Colombier <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent bcc3333 commit 214136b

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

src/cmd/go/internal/fsys/fsys_test.go

+40-34
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,11 @@ contents don't matter for this test
487487
}
488488

489489
func TestWalk(t *testing.T) {
490+
// The root of the walk must be a name with an actual basename, not just ".".
491+
// Walk uses Lstat to obtain the name of the root, and Lstat on platforms
492+
// other than Plan 9 reports the name "." instead of the actual base name of
493+
// the directory. (See https://golang.org/issue/42115.)
494+
490495
type file struct {
491496
path string
492497
name string
@@ -502,62 +507,62 @@ func TestWalk(t *testing.T) {
502507
}{
503508
{"no overlay", `
504509
{}
505-
-- file.txt --
510+
-- dir/file.txt --
506511
`,
507-
".",
512+
"dir",
508513
[]file{
509-
{".", ".", 0, fs.ModeDir | 0700, true},
510-
{"file.txt", "file.txt", 0, 0600, false},
514+
{"dir", "dir", 0, fs.ModeDir | 0700, true},
515+
{"dir/file.txt", "file.txt", 0, 0600, false},
511516
},
512517
},
513518
{"overlay with different file", `
514519
{
515520
"Replace": {
516-
"file.txt": "other.txt"
521+
"dir/file.txt": "dir/other.txt"
517522
}
518523
}
519-
-- file.txt --
520-
-- other.txt --
524+
-- dir/file.txt --
525+
-- dir/other.txt --
521526
contents of other file
522527
`,
523-
".",
528+
"dir",
524529
[]file{
525-
{".", ".", 0, fs.ModeDir | 0500, true},
526-
{"file.txt", "file.txt", 23, 0600, false},
527-
{"other.txt", "other.txt", 23, 0600, false},
530+
{"dir", "dir", 0, fs.ModeDir | 0500, true},
531+
{"dir/file.txt", "file.txt", 23, 0600, false},
532+
{"dir/other.txt", "other.txt", 23, 0600, false},
528533
},
529534
},
530535
{"overlay with new file", `
531536
{
532537
"Replace": {
533-
"file.txt": "other.txt"
538+
"dir/file.txt": "dir/other.txt"
534539
}
535540
}
536-
-- other.txt --
541+
-- dir/other.txt --
537542
contents of other file
538543
`,
539-
".",
544+
"dir",
540545
[]file{
541-
{".", ".", 0, fs.ModeDir | 0500, true},
542-
{"file.txt", "file.txt", 23, 0600, false},
543-
{"other.txt", "other.txt", 23, 0600, false},
546+
{"dir", "dir", 0, fs.ModeDir | 0500, true},
547+
{"dir/file.txt", "file.txt", 23, 0600, false},
548+
{"dir/other.txt", "other.txt", 23, 0600, false},
544549
},
545550
},
546551
{"overlay with new directory", `
547552
{
548553
"Replace": {
549-
"dir/file.txt": "other.txt"
554+
"dir/subdir/file.txt": "dir/other.txt"
550555
}
551556
}
552-
-- other.txt --
557+
-- dir/other.txt --
553558
contents of other file
554559
`,
555-
".",
560+
"dir",
556561
[]file{
557-
{".", ".", 0, fs.ModeDir | 0500, true},
558562
{"dir", "dir", 0, fs.ModeDir | 0500, true},
559-
{"dir" + string(filepath.Separator) + "file.txt", "file.txt", 23, 0600, false},
560-
{"other.txt", "other.txt", 23, 0600, false},
563+
{"dir/other.txt", "other.txt", 23, 0600, false},
564+
{"dir/subdir", "subdir", 0, fs.ModeDir | 0500, true},
565+
{"dir/subdir/file.txt", "file.txt", 23, 0600, false},
561566
},
562567
},
563568
}
@@ -576,8 +581,9 @@ contents of other file
576581
t.Errorf("Walk: saw %#v in walk; want %#v", got, tc.wantFiles)
577582
}
578583
for i := 0; i < len(got) && i < len(tc.wantFiles); i++ {
579-
if got[i].path != tc.wantFiles[i].path {
580-
t.Errorf("path of file #%v in walk, got %q, want %q", i, got[i].path, tc.wantFiles[i].path)
584+
wantPath := filepath.FromSlash(tc.wantFiles[i].path)
585+
if got[i].path != wantPath {
586+
t.Errorf("path of file #%v in walk, got %q, want %q", i, got[i].path, wantPath)
581587
}
582588
if got[i].name != tc.wantFiles[i].name {
583589
t.Errorf("name of file #%v in walk, got %q, want %q", i, got[i].name, tc.wantFiles[i].name)
@@ -603,24 +609,24 @@ func TestWalk_SkipDir(t *testing.T) {
603609
initOverlay(t, `
604610
{
605611
"Replace": {
606-
"skipthisdir/file.go": "dummy.txt",
607-
"dontskip/file.go": "dummy.txt",
608-
"dontskip/skip/file.go": "dummy.txt"
612+
"dir/skip/file.go": "dummy.txt",
613+
"dir/dontskip/file.go": "dummy.txt",
614+
"dir/dontskip/skip/file.go": "dummy.txt"
609615
}
610616
}
611617
-- dummy.txt --
612618
`)
613619

614620
var seen []string
615-
Walk(".", func(path string, info fs.FileInfo, err error) error {
616-
seen = append(seen, path)
617-
if path == "skipthisdir" || path == filepath.Join("dontskip", "skip") {
621+
Walk("dir", func(path string, info fs.FileInfo, err error) error {
622+
seen = append(seen, filepath.ToSlash(path))
623+
if info.Name() == "skip" {
618624
return filepath.SkipDir
619625
}
620626
return nil
621627
})
622628

623-
wantSeen := []string{".", "dontskip", filepath.Join("dontskip", "file.go"), filepath.Join("dontskip", "skip"), "dummy.txt", "skipthisdir"}
629+
wantSeen := []string{"dir", "dir/dontskip", "dir/dontskip/file.go", "dir/dontskip/skip", "dir/skip"}
624630

625631
if len(seen) != len(wantSeen) {
626632
t.Errorf("paths seen in walk: got %v entries; want %v entries", len(seen), len(wantSeen))
@@ -675,8 +681,8 @@ func TestWalk_Symlink(t *testing.T) {
675681
wantFiles []string
676682
}{
677683
{"control", "dir", []string{"dir", "dir" + string(filepath.Separator) + "file"}},
678-
// ensure Walk doesn't wolk into the directory pointed to by the symlink
679-
// (because it's supposed to use Lstat instead of Stat.
684+
// ensure Walk doesn't walk into the directory pointed to by the symlink
685+
// (because it's supposed to use Lstat instead of Stat).
680686
{"symlink_to_dir", "symlink", []string{"symlink"}},
681687
{"overlay_to_symlink_to_dir", "overlay_symlink", []string{"overlay_symlink"}},
682688
}

0 commit comments

Comments
 (0)