Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dab5a3e

Browse filesBrowse files
authored
GH-89812: Clean up pathlib tests. (#104829)
Clean up pathlib tests. Merge `PurePathTest` into `_BasePurePathTest`, and `PathTest` into `_BasePathTest`.
1 parent b95de96 commit dab5a3e
Copy full SHA for dab5a3e

File tree

1 file changed

+96
-84
lines changed
Filter options

1 file changed

+96
-84
lines changed

‎Lib/test/test_pathlib.py

Copy file name to clipboardExpand all lines: Lib/test/test_pathlib.py
+96-84Lines changed: 96 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import errno
66
import pathlib
77
import pickle
8+
import posixpath
89
import socket
910
import stat
1011
import tempfile
@@ -23,20 +24,23 @@
2324
grp = pwd = None
2425

2526

26-
#
27-
# Tests for the pure classes.
28-
#
27+
# Make sure any symbolic links in the base test path are resolved.
28+
BASE = os.path.realpath(TESTFN)
29+
join = lambda *x: os.path.join(BASE, *x)
30+
rel_join = lambda *x: os.path.join(TESTFN, *x)
2931

30-
class _BasePurePathSubclass(object):
31-
def __init__(self, *pathsegments, session_id):
32-
super().__init__(*pathsegments)
33-
self.session_id = session_id
32+
only_nt = unittest.skipIf(os.name != 'nt',
33+
'test requires a Windows-compatible system')
34+
only_posix = unittest.skipIf(os.name == 'nt',
35+
'test requires a POSIX-compatible system')
3436

35-
def with_segments(self, *pathsegments):
36-
return type(self)(*pathsegments, session_id=self.session_id)
3737

38+
#
39+
# Tests for the pure classes.
40+
#
3841

39-
class _BasePurePathTest(object):
42+
class PurePathTest(unittest.TestCase):
43+
cls = pathlib.PurePath
4044

4145
# Keys are canonical paths, values are list of tuples of arguments
4246
# supposed to produce equal paths.
@@ -75,6 +79,37 @@ def test_constructor_common(self):
7579
self.assertEqual(P(P('a'), P('b'), P('c')), P(FakePath("a/b/c")))
7680
self.assertEqual(P(P('./a:b')), P('./a:b'))
7781

82+
def test_concrete_class(self):
83+
if self.cls is pathlib.PurePath:
84+
expected = pathlib.PureWindowsPath if os.name == 'nt' else pathlib.PurePosixPath
85+
else:
86+
expected = self.cls
87+
p = self.cls('a')
88+
self.assertIs(type(p), expected)
89+
90+
def test_different_flavours_unequal(self):
91+
p = self.cls('a')
92+
if p._flavour is posixpath:
93+
q = pathlib.PureWindowsPath('a')
94+
else:
95+
q = pathlib.PurePosixPath('a')
96+
self.assertNotEqual(p, q)
97+
98+
def test_different_flavours_unordered(self):
99+
p = self.cls('a')
100+
if p._flavour is posixpath:
101+
q = pathlib.PureWindowsPath('a')
102+
else:
103+
q = pathlib.PurePosixPath('a')
104+
with self.assertRaises(TypeError):
105+
p < q
106+
with self.assertRaises(TypeError):
107+
p <= q
108+
with self.assertRaises(TypeError):
109+
p > q
110+
with self.assertRaises(TypeError):
111+
p >= q
112+
78113
def test_bytes(self):
79114
P = self.cls
80115
message = (r"argument should be a str or an os\.PathLike object "
@@ -122,8 +157,13 @@ def test_str_subclass_common(self):
122157
self._check_str_subclass('/a/b.txt')
123158

124159
def test_with_segments_common(self):
125-
class P(_BasePurePathSubclass, self.cls):
126-
pass
160+
class P(self.cls):
161+
def __init__(self, *pathsegments, session_id):
162+
super().__init__(*pathsegments)
163+
self.session_id = session_id
164+
165+
def with_segments(self, *pathsegments):
166+
return type(self)(*pathsegments, session_id=self.session_id)
127167
p = P('foo', 'bar', session_id=42)
128168
self.assertEqual(42, (p / 'foo').session_id)
129169
self.assertEqual(42, ('foo' / p).session_id)
@@ -723,7 +763,7 @@ def test_pickling_common(self):
723763
self.assertEqual(str(pp), str(p))
724764

725765

726-
class PurePosixPathTest(_BasePurePathTest, unittest.TestCase):
766+
class PurePosixPathTest(PurePathTest):
727767
cls = pathlib.PurePosixPath
728768

729769
def test_drive_root_parts(self):
@@ -817,10 +857,10 @@ def test_parse_windows_path(self):
817857
self.assertEqual(p, pp)
818858

819859

820-
class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
860+
class PureWindowsPathTest(PurePathTest):
821861
cls = pathlib.PureWindowsPath
822862

823-
equivalences = _BasePurePathTest.equivalences.copy()
863+
equivalences = PurePathTest.equivalences.copy()
824864
equivalences.update({
825865
'./a:b': [ ('./a:b',) ],
826866
'c:a': [ ('c:', 'a'), ('c:', 'a/'), ('.', 'c:', 'a') ],
@@ -1491,45 +1531,14 @@ def test_is_reserved(self):
14911531
self.assertIs(True, P('c:/baz/con/NUL').is_reserved())
14921532
self.assertIs(False, P('c:/NUL/con/baz').is_reserved())
14931533

1494-
class PurePathTest(_BasePurePathTest, unittest.TestCase):
1495-
cls = pathlib.PurePath
1496-
1497-
def test_concrete_class(self):
1498-
p = self.cls('a')
1499-
self.assertIs(type(p),
1500-
pathlib.PureWindowsPath if os.name == 'nt' else pathlib.PurePosixPath)
1501-
1502-
def test_different_flavours_unequal(self):
1503-
p = pathlib.PurePosixPath('a')
1504-
q = pathlib.PureWindowsPath('a')
1505-
self.assertNotEqual(p, q)
1506-
1507-
def test_different_flavours_unordered(self):
1508-
p = pathlib.PurePosixPath('a')
1509-
q = pathlib.PureWindowsPath('a')
1510-
with self.assertRaises(TypeError):
1511-
p < q
1512-
with self.assertRaises(TypeError):
1513-
p <= q
1514-
with self.assertRaises(TypeError):
1515-
p > q
1516-
with self.assertRaises(TypeError):
1517-
p >= q
1518-
15191534

1520-
#
1521-
# Tests for the concrete classes.
1522-
#
1535+
class PurePathSubclassTest(PurePathTest):
1536+
class cls(pathlib.PurePath):
1537+
pass
15231538

1524-
# Make sure any symbolic links in the base test path are resolved.
1525-
BASE = os.path.realpath(TESTFN)
1526-
join = lambda *x: os.path.join(BASE, *x)
1527-
rel_join = lambda *x: os.path.join(TESTFN, *x)
1539+
# repr() roundtripping is not supported in custom subclass.
1540+
test_repr_roundtrips = None
15281541

1529-
only_nt = unittest.skipIf(os.name != 'nt',
1530-
'test requires a Windows-compatible system')
1531-
only_posix = unittest.skipIf(os.name == 'nt',
1532-
'test requires a POSIX-compatible system')
15331542

15341543
@only_posix
15351544
class PosixPathAsPureTest(PurePosixPathTest):
@@ -1550,9 +1559,15 @@ def test_group(self):
15501559
P('c:/').group()
15511560

15521561

1553-
class _BasePathTest(object):
1562+
#
1563+
# Tests for the concrete classes.
1564+
#
1565+
1566+
class PathTest(unittest.TestCase):
15541567
"""Tests for the FS-accessing functionalities of the Path classes."""
15551568

1569+
cls = pathlib.Path
1570+
15561571
# (BASE)
15571572
# |
15581573
# |-- brokenLink -> non-existing
@@ -1627,6 +1642,20 @@ def assertFileNotFound(self, func, *args, **kwargs):
16271642
def assertEqualNormCase(self, path_a, path_b):
16281643
self.assertEqual(os.path.normcase(path_a), os.path.normcase(path_b))
16291644

1645+
def test_concrete_class(self):
1646+
if self.cls is pathlib.Path:
1647+
expected = pathlib.WindowsPath if os.name == 'nt' else pathlib.PosixPath
1648+
else:
1649+
expected = self.cls
1650+
p = self.cls('a')
1651+
self.assertIs(type(p), expected)
1652+
1653+
def test_unsupported_flavour(self):
1654+
if self.cls._flavour is os.path:
1655+
self.skipTest("path flavour is supported")
1656+
else:
1657+
self.assertRaises(NotImplementedError, self.cls)
1658+
16301659
def _test_cwd(self, p):
16311660
q = self.cls(os.getcwd())
16321661
self.assertEqual(p, q)
@@ -1683,8 +1712,13 @@ def test_home(self):
16831712
self._test_home(self.cls.home())
16841713

16851714
def test_with_segments(self):
1686-
class P(_BasePurePathSubclass, self.cls):
1687-
pass
1715+
class P(self.cls):
1716+
def __init__(self, *pathsegments, session_id):
1717+
super().__init__(*pathsegments)
1718+
self.session_id = session_id
1719+
1720+
def with_segments(self, *pathsegments):
1721+
return type(self)(*pathsegments, session_id=self.session_id)
16881722
p = P(BASE, session_id=42)
16891723
self.assertEqual(42, p.absolute().session_id)
16901724
self.assertEqual(42, p.resolve().session_id)
@@ -1872,6 +1906,11 @@ def _check(glob, expected):
18721906
else:
18731907
_check(p.glob("*/"), ["dirA", "dirB", "dirC", "dirE", "linkB"])
18741908

1909+
def test_glob_empty_pattern(self):
1910+
p = self.cls()
1911+
with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'):
1912+
list(p.glob(''))
1913+
18751914
def test_glob_case_sensitive(self):
18761915
P = self.cls
18771916
def _check(path, pattern, case_sensitive, expected):
@@ -3022,28 +3061,8 @@ def test_walk_above_recursion_limit(self):
30223061
list(base.walk(top_down=False))
30233062

30243063

3025-
class PathTest(_BasePathTest, unittest.TestCase):
3026-
cls = pathlib.Path
3027-
3028-
def test_concrete_class(self):
3029-
p = self.cls('a')
3030-
self.assertIs(type(p),
3031-
pathlib.WindowsPath if os.name == 'nt' else pathlib.PosixPath)
3032-
3033-
def test_unsupported_flavour(self):
3034-
if os.name == 'nt':
3035-
self.assertRaises(NotImplementedError, pathlib.PosixPath)
3036-
else:
3037-
self.assertRaises(NotImplementedError, pathlib.WindowsPath)
3038-
3039-
def test_glob_empty_pattern(self):
3040-
p = self.cls()
3041-
with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'):
3042-
list(p.glob(''))
3043-
3044-
30453064
@only_posix
3046-
class PosixPathTest(_BasePathTest, unittest.TestCase):
3065+
class PosixPathTest(PathTest):
30473066
cls = pathlib.PosixPath
30483067

30493068
def test_absolute(self):
@@ -3227,7 +3246,7 @@ def test_handling_bad_descriptor(self):
32273246

32283247

32293248
@only_nt
3230-
class WindowsPathTest(_BasePathTest, unittest.TestCase):
3249+
class WindowsPathTest(PathTest):
32313250
cls = pathlib.WindowsPath
32323251

32333252
def test_absolute(self):
@@ -3345,15 +3364,8 @@ def check():
33453364
check()
33463365

33473366

3348-
class PurePathSubclassTest(_BasePurePathTest, unittest.TestCase):
3349-
class cls(pathlib.PurePath):
3350-
pass
3351-
3352-
# repr() roundtripping is not supported in custom subclass.
3353-
test_repr_roundtrips = None
3354-
33553367

3356-
class PathSubclassTest(_BasePathTest, unittest.TestCase):
3368+
class PathSubclassTest(PathTest):
33573369
class cls(pathlib.Path):
33583370
pass
33593371

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.