5
5
import errno
6
6
import pathlib
7
7
import pickle
8
+ import posixpath
8
9
import socket
9
10
import stat
10
11
import tempfile
23
24
grp = pwd = None
24
25
25
26
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 )
29
31
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' )
34
36
35
- def with_segments (self , * pathsegments ):
36
- return type (self )(* pathsegments , session_id = self .session_id )
37
37
38
+ #
39
+ # Tests for the pure classes.
40
+ #
38
41
39
- class _BasePurePathTest (object ):
42
+ class PurePathTest (unittest .TestCase ):
43
+ cls = pathlib .PurePath
40
44
41
45
# Keys are canonical paths, values are list of tuples of arguments
42
46
# supposed to produce equal paths.
@@ -75,6 +79,37 @@ def test_constructor_common(self):
75
79
self .assertEqual (P (P ('a' ), P ('b' ), P ('c' )), P (FakePath ("a/b/c" )))
76
80
self .assertEqual (P (P ('./a:b' )), P ('./a:b' ))
77
81
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
+
78
113
def test_bytes (self ):
79
114
P = self .cls
80
115
message = (r"argument should be a str or an os\.PathLike object "
@@ -122,8 +157,13 @@ def test_str_subclass_common(self):
122
157
self ._check_str_subclass ('/a/b.txt' )
123
158
124
159
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 )
127
167
p = P ('foo' , 'bar' , session_id = 42 )
128
168
self .assertEqual (42 , (p / 'foo' ).session_id )
129
169
self .assertEqual (42 , ('foo' / p ).session_id )
@@ -723,7 +763,7 @@ def test_pickling_common(self):
723
763
self .assertEqual (str (pp ), str (p ))
724
764
725
765
726
- class PurePosixPathTest (_BasePurePathTest , unittest . TestCase ):
766
+ class PurePosixPathTest (PurePathTest ):
727
767
cls = pathlib .PurePosixPath
728
768
729
769
def test_drive_root_parts (self ):
@@ -817,10 +857,10 @@ def test_parse_windows_path(self):
817
857
self .assertEqual (p , pp )
818
858
819
859
820
- class PureWindowsPathTest (_BasePurePathTest , unittest . TestCase ):
860
+ class PureWindowsPathTest (PurePathTest ):
821
861
cls = pathlib .PureWindowsPath
822
862
823
- equivalences = _BasePurePathTest .equivalences .copy ()
863
+ equivalences = PurePathTest .equivalences .copy ()
824
864
equivalences .update ({
825
865
'./a:b' : [ ('./a:b' ,) ],
826
866
'c:a' : [ ('c:' , 'a' ), ('c:' , 'a/' ), ('.' , 'c:' , 'a' ) ],
@@ -1491,45 +1531,14 @@ def test_is_reserved(self):
1491
1531
self .assertIs (True , P ('c:/baz/con/NUL' ).is_reserved ())
1492
1532
self .assertIs (False , P ('c:/NUL/con/baz' ).is_reserved ())
1493
1533
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
-
1519
1534
1520
- #
1521
- # Tests for the concrete classes.
1522
- #
1535
+ class PurePathSubclassTest ( PurePathTest ):
1536
+ class cls ( pathlib . PurePath ):
1537
+ pass
1523
1538
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
1528
1541
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' )
1533
1542
1534
1543
@only_posix
1535
1544
class PosixPathAsPureTest (PurePosixPathTest ):
@@ -1550,9 +1559,15 @@ def test_group(self):
1550
1559
P ('c:/' ).group ()
1551
1560
1552
1561
1553
- class _BasePathTest (object ):
1562
+ #
1563
+ # Tests for the concrete classes.
1564
+ #
1565
+
1566
+ class PathTest (unittest .TestCase ):
1554
1567
"""Tests for the FS-accessing functionalities of the Path classes."""
1555
1568
1569
+ cls = pathlib .Path
1570
+
1556
1571
# (BASE)
1557
1572
# |
1558
1573
# |-- brokenLink -> non-existing
@@ -1627,6 +1642,20 @@ def assertFileNotFound(self, func, *args, **kwargs):
1627
1642
def assertEqualNormCase (self , path_a , path_b ):
1628
1643
self .assertEqual (os .path .normcase (path_a ), os .path .normcase (path_b ))
1629
1644
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
+
1630
1659
def _test_cwd (self , p ):
1631
1660
q = self .cls (os .getcwd ())
1632
1661
self .assertEqual (p , q )
@@ -1683,8 +1712,13 @@ def test_home(self):
1683
1712
self ._test_home (self .cls .home ())
1684
1713
1685
1714
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 )
1688
1722
p = P (BASE , session_id = 42 )
1689
1723
self .assertEqual (42 , p .absolute ().session_id )
1690
1724
self .assertEqual (42 , p .resolve ().session_id )
@@ -1872,6 +1906,11 @@ def _check(glob, expected):
1872
1906
else :
1873
1907
_check (p .glob ("*/" ), ["dirA" , "dirB" , "dirC" , "dirE" , "linkB" ])
1874
1908
1909
+ def test_glob_empty_pattern (self ):
1910
+ p = self .cls ()
1911
+ with self .assertRaisesRegex (ValueError , 'Unacceptable pattern' ):
1912
+ list (p .glob ('' ))
1913
+
1875
1914
def test_glob_case_sensitive (self ):
1876
1915
P = self .cls
1877
1916
def _check (path , pattern , case_sensitive , expected ):
@@ -3022,28 +3061,8 @@ def test_walk_above_recursion_limit(self):
3022
3061
list (base .walk (top_down = False ))
3023
3062
3024
3063
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
-
3045
3064
@only_posix
3046
- class PosixPathTest (_BasePathTest , unittest . TestCase ):
3065
+ class PosixPathTest (PathTest ):
3047
3066
cls = pathlib .PosixPath
3048
3067
3049
3068
def test_absolute (self ):
@@ -3227,7 +3246,7 @@ def test_handling_bad_descriptor(self):
3227
3246
3228
3247
3229
3248
@only_nt
3230
- class WindowsPathTest (_BasePathTest , unittest . TestCase ):
3249
+ class WindowsPathTest (PathTest ):
3231
3250
cls = pathlib .WindowsPath
3232
3251
3233
3252
def test_absolute (self ):
@@ -3345,15 +3364,8 @@ def check():
3345
3364
check ()
3346
3365
3347
3366
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
-
3355
3367
3356
- class PathSubclassTest (_BasePathTest , unittest . TestCase ):
3368
+ class PathSubclassTest (PathTest ):
3357
3369
class cls (pathlib .Path ):
3358
3370
pass
3359
3371
0 commit comments