aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_pathlib/test_pathlib.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-01-08 19:17:18 +0000
committerGitHub <noreply@github.com>2024-01-08 19:17:18 +0000
commita9df076d7d5e113aab4dfd32118a14b62537a8a2 (patch)
tree06b060f293770ba7cd1913639e0329d8394bef4c /Lib/test/test_pathlib/test_pathlib.py
parentaef375f56ec93740f0a9b5031c3d2063c553fc12 (diff)
downloadcpython-a9df076d7d5e113aab4dfd32118a14b62537a8a2.tar.gz
cpython-a9df076d7d5e113aab4dfd32118a14b62537a8a2.zip
GH-113528: Move a few misplaced pathlib tests (#113527)
`PurePathBase` does not define `__eq__()`, and so we have no business checking path equality in `test_eq_common` and `test_equivalences`. The tests only pass at the moment because we define the test class's `__eq__()` for use elsewhere. Also move `test_parse_path_common` into the main pathlib test suite. It exercises a private `_parse_path()` method that will be moved to `PurePath` soon. Lastly move a couple more tests concerned with optimisations and path normalisation.
Diffstat (limited to 'Lib/test/test_pathlib/test_pathlib.py')
-rw-r--r--Lib/test/test_pathlib/test_pathlib.py86
1 files changed, 86 insertions, 0 deletions
diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py
index 93fe327a0d3..6e421222126 100644
--- a/Lib/test/test_pathlib/test_pathlib.py
+++ b/Lib/test/test_pathlib/test_pathlib.py
@@ -45,6 +45,22 @@ class PurePathTest(test_pathlib_abc.DummyPurePathTest):
# Make sure any symbolic links in the base test path are resolved.
base = os.path.realpath(TESTFN)
+ # Keys are canonical paths, values are list of tuples of arguments
+ # supposed to produce equal paths.
+ equivalences = {
+ 'a/b': [
+ ('a', 'b'), ('a/', 'b'), ('a', 'b/'), ('a/', 'b/'),
+ ('a/b/',), ('a//b',), ('a//b//',),
+ # Empty components get removed.
+ ('', 'a', 'b'), ('a', '', 'b'), ('a', 'b', ''),
+ ],
+ '/b/c/d': [
+ ('a', '/b/c', 'd'), ('/a', '/b/c', 'd'),
+ # Empty components get removed.
+ ('/', 'b', '', 'c/d'), ('/', '', 'b/c/d'), ('', '/b/c/d'),
+ ],
+ }
+
def test_concrete_class(self):
if self.cls is pathlib.PurePath:
expected = pathlib.PureWindowsPath if os.name == 'nt' else pathlib.PurePosixPath
@@ -95,6 +111,45 @@ class PurePathTest(test_pathlib_abc.DummyPurePathTest):
self.assertEqual(P(P('a'), P('b'), P('c')), P(FakePath("a/b/c")))
self.assertEqual(P(P('./a:b')), P('./a:b'))
+ def _check_parse_path(self, raw_path, *expected):
+ sep = self.pathmod.sep
+ actual = self.cls._parse_path(raw_path.replace('/', sep))
+ self.assertEqual(actual, expected)
+ if altsep := self.pathmod.altsep:
+ actual = self.cls._parse_path(raw_path.replace('/', altsep))
+ self.assertEqual(actual, expected)
+
+ def test_parse_path_common(self):
+ check = self._check_parse_path
+ sep = self.pathmod.sep
+ check('', '', '', [])
+ check('a', '', '', ['a'])
+ check('a/', '', '', ['a'])
+ check('a/b', '', '', ['a', 'b'])
+ check('a/b/', '', '', ['a', 'b'])
+ check('a/b/c/d', '', '', ['a', 'b', 'c', 'd'])
+ check('a/b//c/d', '', '', ['a', 'b', 'c', 'd'])
+ check('a/b/c/d', '', '', ['a', 'b', 'c', 'd'])
+ check('.', '', '', [])
+ check('././b', '', '', ['b'])
+ check('a/./b', '', '', ['a', 'b'])
+ check('a/./.', '', '', ['a'])
+ check('/a/b', '', sep, ['a', 'b'])
+
+ def test_empty_path(self):
+ # The empty path points to '.'
+ p = self.cls('')
+ self.assertEqual(str(p), '.')
+
+ def test_parts_interning(self):
+ P = self.cls
+ p = P('/usr/bin/foo')
+ q = P('/usr/local/bin')
+ # 'usr'
+ self.assertIs(p.parts[1], q.parts[1])
+ # 'bin'
+ self.assertIs(p.parts[2], q.parts[3])
+
def test_join_nested(self):
P = self.cls
p = P('a/b').joinpath(P('c'))
@@ -168,6 +223,37 @@ class PurePathTest(test_pathlib_abc.DummyPurePathTest):
P = self.cls
self.assertEqual(bytes(P('a/b')), b'a' + sep + b'b')
+ def test_eq_common(self):
+ P = self.cls
+ self.assertEqual(P('a/b'), P('a/b'))
+ self.assertEqual(P('a/b'), P('a', 'b'))
+ self.assertNotEqual(P('a/b'), P('a'))
+ self.assertNotEqual(P('a/b'), P('/a/b'))
+ self.assertNotEqual(P('a/b'), P())
+ self.assertNotEqual(P('/a/b'), P('/'))
+ self.assertNotEqual(P(), P('/'))
+ self.assertNotEqual(P(), "")
+ self.assertNotEqual(P(), {})
+ self.assertNotEqual(P(), int)
+
+ def test_equivalences(self):
+ for k, tuples in self.equivalences.items():
+ canon = k.replace('/', self.sep)
+ posix = k.replace(self.sep, '/')
+ if canon != posix:
+ tuples = tuples + [
+ tuple(part.replace('/', self.sep) for part in t)
+ for t in tuples
+ ]
+ tuples.append((posix, ))
+ pcanon = self.cls(canon)
+ for t in tuples:
+ p = self.cls(*t)
+ self.assertEqual(p, pcanon, "failed with args {}".format(t))
+ self.assertEqual(hash(p), hash(pcanon))
+ self.assertEqual(str(p), canon)
+ self.assertEqual(p.as_posix(), posix)
+
def test_ordering_common(self):
# Ordering is tuple-alike.
def assertLess(a, b):