aboutsummaryrefslogtreecommitdiff
path: root/src/path/filepath
diff options
context:
space:
mode:
Diffstat (limited to 'src/path/filepath')
-rw-r--r--src/path/filepath/path_test.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/path/filepath/path_test.go b/src/path/filepath/path_test.go
index e6a9270909..672d7e6261 100644
--- a/src/path/filepath/path_test.go
+++ b/src/path/filepath/path_test.go
@@ -818,6 +818,70 @@ func TestWalkFileError(t *testing.T) {
}
}
+func TestWalkSymlinkRoot(t *testing.T) {
+ testenv.MustHaveSymlink(t)
+
+ td := t.TempDir()
+ dir := filepath.Join(td, "dir")
+ if err := os.MkdirAll(filepath.Join(td, "dir"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ touch(t, filepath.Join(dir, "foo"))
+
+ link := filepath.Join(td, "link")
+ if err := os.Symlink("dir", link); err != nil {
+ t.Fatal(err)
+ }
+
+ // Per https://pubs.opengroup.org/onlinepubs/9699919799.2013edition/basedefs/V1_chap04.html#tag_04_12:
+ // “A pathname that contains at least one non- <slash> character and that ends
+ // with one or more trailing <slash> characters shall not be resolved
+ // successfully unless the last pathname component before the trailing <slash>
+ // characters names an existing directory [...].”
+ //
+ // Since Walk does not traverse symlinks itself, its behavior should depend on
+ // whether the path passed to Walk ends in a slash: if it does not end in a slash,
+ // Walk should report the symlink itself (since it is the last pathname component);
+ // but if it does end in a slash, Walk should walk the directory to which the symlink
+ // refers (since it must be fully resolved before walking).
+ for _, tt := range []struct {
+ desc string
+ root string
+ want []string
+ }{
+ {
+ desc: "no slash",
+ root: link,
+ want: []string{link},
+ },
+ {
+ desc: "slash",
+ root: link + string(filepath.Separator),
+ want: []string{link, filepath.Join(link, "foo")},
+ },
+ } {
+ tt := tt
+ t.Run(tt.desc, func(t *testing.T) {
+ var walked []string
+ err := filepath.Walk(tt.root, func(path string, info fs.FileInfo, err error) error {
+ if err != nil {
+ return err
+ }
+ t.Logf("%#q: %v", path, info.Mode())
+ walked = append(walked, filepath.Clean(path))
+ return nil
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if !reflect.DeepEqual(walked, tt.want) {
+ t.Errorf("Walk(%#q) visited %#q; want %#q", tt.root, walked, tt.want)
+ }
+ })
+ }
+}
+
var basetests = []PathTest{
{"", "."},
{".", "."},