Skip to content

Commit

Permalink
Move a couple of misplaced pathlib tests
Browse files Browse the repository at this point in the history
`PurePathBase` does not define `__eq__()`, and so we have no business
checking path equality in `test_equivalences`. The test only passes at the
moment because we define the test class's `__eq__()` for use in other
tests.

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.
  • Loading branch information
barneygale committed Dec 27, 2023
1 parent 1b19d73 commit 7ae4ebf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 59 deletions.
59 changes: 59 additions & 0 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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
Expand Down Expand Up @@ -94,6 +110,31 @@ def test_constructor_nested(self):
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_join_nested(self):
P = self.cls
p = P('a/b').joinpath(P('c'))
Expand Down Expand Up @@ -167,6 +208,24 @@ def test_as_bytes_common(self):
P = self.cls
self.assertEqual(bytes(P('a/b')), b'a' + sep + b'b')

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):
Expand Down
59 changes: 0 additions & 59 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,6 @@ class DummyPurePathTest(unittest.TestCase):
# Use a base path that's unrelated to any real filesystem path.
base = f'/this/path/kills/fascists/{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 setUp(self):
p = self.cls('a')
self.pathmod = p.pathmod
Expand Down Expand Up @@ -130,31 +114,6 @@ def with_segments(self, *pathsegments):
for parent in p.parents:
self.assertEqual(42, parent.session_id)

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_join_common(self):
P = self.cls
p = P('a/b')
Expand Down Expand Up @@ -294,24 +253,6 @@ def test_parts_common(self):
parts = p.parts
self.assertEqual(parts, (sep, 'a', 'b'))

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_parent_common(self):
# Relative
P = self.cls
Expand Down

0 comments on commit 7ae4ebf

Please sign in to comment.