Skip to content

Commit 013d383

Browse files
barneygaleaisk
authored andcommitted
pythonGH-113528: Slightly improve pathlib.Path.glob() tests for symlink loop handling (python#113763)
Slightly improve `pathlib.Path.glob()` tests for symlink loop handling When filtering results, ignore paths with more than one `linkD/` segment, rather than all paths below the first `linkD/` segment. This allows us to test that other paths under `linkD/` are correctly returned.
1 parent 19553e7 commit 013d383

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ def test_glob_follow_symlinks_common(self):
10861086
self.skipTest("symlinks required")
10871087
def _check(path, glob, expected):
10881088
actual = {path for path in path.glob(glob, follow_symlinks=True)
1089-
if "linkD" not in path.parent.parts} # exclude symlink loop.
1089+
if path.parts.count("linkD") <= 1} # exclude symlink loop.
10901090
self.assertEqual(actual, { P(self.base, q) for q in expected })
10911091
P = self.cls
10921092
p = P(self.base)
@@ -1096,13 +1096,15 @@ def _check(path, glob, expected):
10961096
_check(p, "*B/*", ["dirB/fileB", "dirB/linkD", "linkB/fileB", "linkB/linkD"])
10971097
_check(p, "*/fileB", ["dirB/fileB", "linkB/fileB"])
10981098
_check(p, "*/", ["dirA/", "dirB/", "dirC/", "dirE/", "linkB/"])
1099-
_check(p, "dir*/*/..", ["dirC/dirD/..", "dirA/linkC/.."])
1099+
_check(p, "dir*/*/..", ["dirC/dirD/..", "dirA/linkC/..", "dirB/linkD/.."])
11001100
_check(p, "dir*/**/", ["dirA/", "dirA/linkC/", "dirA/linkC/linkD/", "dirB/", "dirB/linkD/",
11011101
"dirC/", "dirC/dirD/", "dirE/"])
11021102
_check(p, "dir*/**/..", ["dirA/..", "dirA/linkC/..", "dirB/..",
1103+
"dirB/linkD/..", "dirA/linkC/linkD/..",
11031104
"dirC/..", "dirC/dirD/..", "dirE/.."])
11041105
_check(p, "dir*/*/**/", ["dirA/linkC/", "dirA/linkC/linkD/", "dirB/linkD/", "dirC/dirD/"])
1105-
_check(p, "dir*/*/**/..", ["dirA/linkC/..", "dirC/dirD/.."])
1106+
_check(p, "dir*/*/**/..", ["dirA/linkC/..", "dirA/linkC/linkD/..",
1107+
"dirB/linkD/..", "dirC/dirD/.."])
11061108
_check(p, "dir*/**/fileC", ["dirC/fileC"])
11071109
_check(p, "dir*/*/../dirD/**/", ["dirC/dirD/../dirD/"])
11081110
_check(p, "*/dirD/**/", ["dirC/dirD/"])
@@ -1157,7 +1159,7 @@ def _check(glob, expected):
11571159
"dirA/", "dirA/linkC/", "dirB/", "dirB/linkD/", "dirC/",
11581160
"dirC/dirD/", "dirE/", "linkB/",
11591161
])
1160-
_check(p.rglob(""), ["./", "dirA/", "dirB/", "dirC/", "dirE/", "dirC/dirD/"])
1162+
_check(p.rglob(""), ["", "dirA/", "dirB/", "dirC/", "dirE/", "dirC/dirD/"])
11611163

11621164
p = P(self.base, "dirC")
11631165
_check(p.rglob("*"), ["dirC/fileC", "dirC/novel.txt",
@@ -1178,18 +1180,21 @@ def test_rglob_follow_symlinks_common(self):
11781180
self.skipTest("symlinks required")
11791181
def _check(path, glob, expected):
11801182
actual = {path for path in path.rglob(glob, follow_symlinks=True)
1181-
if 'linkD' not in path.parent.parts} # exclude symlink loop.
1183+
if path.parts.count("linkD") <= 1} # exclude symlink loop.
11821184
self.assertEqual(actual, { P(self.base, q) for q in expected })
11831185
P = self.cls
11841186
p = P(self.base)
1185-
_check(p, "fileB", ["dirB/fileB", "dirA/linkC/fileB", "linkB/fileB"])
1187+
_check(p, "fileB", ["dirB/fileB", "dirA/linkC/fileB", "linkB/fileB",
1188+
"dirA/linkC/linkD/fileB", "dirB/linkD/fileB", "linkB/linkD/fileB"])
11861189
_check(p, "*/fileA", [])
1187-
_check(p, "*/fileB", ["dirB/fileB", "dirA/linkC/fileB", "linkB/fileB"])
1190+
_check(p, "*/fileB", ["dirB/fileB", "dirA/linkC/fileB", "linkB/fileB",
1191+
"dirA/linkC/linkD/fileB", "dirB/linkD/fileB", "linkB/linkD/fileB"])
11881192
_check(p, "file*", ["fileA", "dirA/linkC/fileB", "dirB/fileB",
1193+
"dirA/linkC/linkD/fileB", "dirB/linkD/fileB", "linkB/linkD/fileB",
11891194
"dirC/fileC", "dirC/dirD/fileD", "linkB/fileB"])
11901195
_check(p, "*/", ["dirA/", "dirA/linkC/", "dirA/linkC/linkD/", "dirB/", "dirB/linkD/",
11911196
"dirC/", "dirC/dirD/", "dirE/", "linkB/", "linkB/linkD/"])
1192-
_check(p, "", ["./", "dirA/", "dirA/linkC/", "dirA/linkC/linkD/", "dirB/", "dirB/linkD/",
1197+
_check(p, "", ["", "dirA/", "dirA/linkC/", "dirA/linkC/linkD/", "dirB/", "dirB/linkD/",
11931198
"dirC/", "dirE/", "dirC/dirD/", "linkB/", "linkB/linkD/"])
11941199

11951200
p = P(self.base, "dirC")
@@ -1216,7 +1221,7 @@ def _check(path, glob, expected):
12161221
_check(p, "*/fileB", ["dirB/fileB"])
12171222
_check(p, "file*", ["fileA", "dirB/fileB", "dirC/fileC", "dirC/dirD/fileD", ])
12181223
_check(p, "*/", ["dirA/", "dirB/", "dirC/", "dirC/dirD/", "dirE/"])
1219-
_check(p, "", ["./", "dirA/", "dirB/", "dirC/", "dirE/", "dirC/dirD/"])
1224+
_check(p, "", ["", "dirA/", "dirB/", "dirC/", "dirE/", "dirC/dirD/"])
12201225

12211226
p = P(self.base, "dirC")
12221227
_check(p, "*", ["dirC/fileC", "dirC/novel.txt",

0 commit comments

Comments
 (0)