Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitdab5a3e

Browse files
authored
GH-89812: Clean up pathlib tests. (#104829)
Clean up pathlib tests.Merge `PurePathTest` into `_BasePurePathTest`, and `PathTest` into`_BasePathTest`.
1 parentb95de96 commitdab5a3e

File tree

1 file changed

+96
-84
lines changed

1 file changed

+96
-84
lines changed

‎Lib/test/test_pathlib.py

Lines changed: 96 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
importerrno
66
importpathlib
77
importpickle
8+
importposixpath
89
importsocket
910
importstat
1011
importtempfile
@@ -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-
defwith_segments(self,*pathsegments):
36-
returntype(self)(*pathsegments,session_id=self.session_id)
3737

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

39-
class_BasePurePathTest(object):
42+
classPurePathTest(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+
deftest_concrete_class(self):
83+
ifself.clsispathlib.PurePath:
84+
expected=pathlib.PureWindowsPathifos.name=='nt'elsepathlib.PurePosixPath
85+
else:
86+
expected=self.cls
87+
p=self.cls('a')
88+
self.assertIs(type(p),expected)
89+
90+
deftest_different_flavours_unequal(self):
91+
p=self.cls('a')
92+
ifp._flavourisposixpath:
93+
q=pathlib.PureWindowsPath('a')
94+
else:
95+
q=pathlib.PurePosixPath('a')
96+
self.assertNotEqual(p,q)
97+
98+
deftest_different_flavours_unordered(self):
99+
p=self.cls('a')
100+
ifp._flavourisposixpath:
101+
q=pathlib.PureWindowsPath('a')
102+
else:
103+
q=pathlib.PurePosixPath('a')
104+
withself.assertRaises(TypeError):
105+
p<q
106+
withself.assertRaises(TypeError):
107+
p<=q
108+
withself.assertRaises(TypeError):
109+
p>q
110+
withself.assertRaises(TypeError):
111+
p>=q
112+
78113
deftest_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
deftest_with_segments_common(self):
125-
classP(_BasePurePathSubclass,self.cls):
126-
pass
160+
classP(self.cls):
161+
def__init__(self,*pathsegments,session_id):
162+
super().__init__(*pathsegments)
163+
self.session_id=session_id
164+
165+
defwith_segments(self,*pathsegments):
166+
returntype(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-
classPurePosixPathTest(_BasePurePathTest,unittest.TestCase):
766+
classPurePosixPathTest(PurePathTest):
727767
cls=pathlib.PurePosixPath
728768

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

819859

820-
classPureWindowsPathTest(_BasePurePathTest,unittest.TestCase):
860+
classPureWindowsPathTest(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-
classPurePathTest(_BasePurePathTest,unittest.TestCase):
1495-
cls=pathlib.PurePath
1496-
1497-
deftest_concrete_class(self):
1498-
p=self.cls('a')
1499-
self.assertIs(type(p),
1500-
pathlib.PureWindowsPathifos.name=='nt'elsepathlib.PurePosixPath)
1501-
1502-
deftest_different_flavours_unequal(self):
1503-
p=pathlib.PurePosixPath('a')
1504-
q=pathlib.PureWindowsPath('a')
1505-
self.assertNotEqual(p,q)
1506-
1507-
deftest_different_flavours_unordered(self):
1508-
p=pathlib.PurePosixPath('a')
1509-
q=pathlib.PureWindowsPath('a')
1510-
withself.assertRaises(TypeError):
1511-
p<q
1512-
withself.assertRaises(TypeError):
1513-
p<=q
1514-
withself.assertRaises(TypeError):
1515-
p>q
1516-
withself.assertRaises(TypeError):
1517-
p>=q
1518-
15191534

1520-
#
1521-
# Tests for the concrete classes.
1522-
#
1535+
classPurePathSubclassTest(PurePathTest):
1536+
classcls(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
classPosixPathAsPureTest(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+
classPathTest(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
defassertEqualNormCase(self,path_a,path_b):
16281643
self.assertEqual(os.path.normcase(path_a),os.path.normcase(path_b))
16291644

1645+
deftest_concrete_class(self):
1646+
ifself.clsispathlib.Path:
1647+
expected=pathlib.WindowsPathifos.name=='nt'elsepathlib.PosixPath
1648+
else:
1649+
expected=self.cls
1650+
p=self.cls('a')
1651+
self.assertIs(type(p),expected)
1652+
1653+
deftest_unsupported_flavour(self):
1654+
ifself.cls._flavourisos.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
deftest_with_segments(self):
1686-
classP(_BasePurePathSubclass,self.cls):
1687-
pass
1715+
classP(self.cls):
1716+
def__init__(self,*pathsegments,session_id):
1717+
super().__init__(*pathsegments)
1718+
self.session_id=session_id
1719+
1720+
defwith_segments(self,*pathsegments):
1721+
returntype(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+
deftest_glob_empty_pattern(self):
1910+
p=self.cls()
1911+
withself.assertRaisesRegex(ValueError,'Unacceptable pattern'):
1912+
list(p.glob(''))
1913+
18751914
deftest_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-
classPathTest(_BasePathTest,unittest.TestCase):
3026-
cls=pathlib.Path
3027-
3028-
deftest_concrete_class(self):
3029-
p=self.cls('a')
3030-
self.assertIs(type(p),
3031-
pathlib.WindowsPathifos.name=='nt'elsepathlib.PosixPath)
3032-
3033-
deftest_unsupported_flavour(self):
3034-
ifos.name=='nt':
3035-
self.assertRaises(NotImplementedError,pathlib.PosixPath)
3036-
else:
3037-
self.assertRaises(NotImplementedError,pathlib.WindowsPath)
3038-
3039-
deftest_glob_empty_pattern(self):
3040-
p=self.cls()
3041-
withself.assertRaisesRegex(ValueError,'Unacceptable pattern'):
3042-
list(p.glob(''))
3043-
3044-
30453064
@only_posix
3046-
classPosixPathTest(_BasePathTest,unittest.TestCase):
3065+
classPosixPathTest(PathTest):
30473066
cls=pathlib.PosixPath
30483067

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

32283247

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

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

33473366

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

3356-
classPathSubclassTest(_BasePathTest,unittest.TestCase):
3368+
classPathSubclassTest(PathTest):
33573369
classcls(pathlib.Path):
33583370
pass
33593371

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp