1 | n/a | import collections |
---|
2 | n/a | import io |
---|
3 | n/a | import os |
---|
4 | n/a | import errno |
---|
5 | n/a | import pathlib |
---|
6 | n/a | import pickle |
---|
7 | n/a | import socket |
---|
8 | n/a | import stat |
---|
9 | n/a | import tempfile |
---|
10 | n/a | import unittest |
---|
11 | n/a | |
---|
12 | n/a | from test import support |
---|
13 | n/a | android_not_root = support.android_not_root |
---|
14 | n/a | TESTFN = support.TESTFN |
---|
15 | n/a | |
---|
16 | n/a | try: |
---|
17 | n/a | import grp, pwd |
---|
18 | n/a | except ImportError: |
---|
19 | n/a | grp = pwd = None |
---|
20 | n/a | |
---|
21 | n/a | |
---|
22 | n/a | class _BaseFlavourTest(object): |
---|
23 | n/a | |
---|
24 | n/a | def _check_parse_parts(self, arg, expected): |
---|
25 | n/a | f = self.flavour.parse_parts |
---|
26 | n/a | sep = self.flavour.sep |
---|
27 | n/a | altsep = self.flavour.altsep |
---|
28 | n/a | actual = f([x.replace('/', sep) for x in arg]) |
---|
29 | n/a | self.assertEqual(actual, expected) |
---|
30 | n/a | if altsep: |
---|
31 | n/a | actual = f([x.replace('/', altsep) for x in arg]) |
---|
32 | n/a | self.assertEqual(actual, expected) |
---|
33 | n/a | |
---|
34 | n/a | def test_parse_parts_common(self): |
---|
35 | n/a | check = self._check_parse_parts |
---|
36 | n/a | sep = self.flavour.sep |
---|
37 | n/a | # Unanchored parts |
---|
38 | n/a | check([], ('', '', [])) |
---|
39 | n/a | check(['a'], ('', '', ['a'])) |
---|
40 | n/a | check(['a/'], ('', '', ['a'])) |
---|
41 | n/a | check(['a', 'b'], ('', '', ['a', 'b'])) |
---|
42 | n/a | # Expansion |
---|
43 | n/a | check(['a/b'], ('', '', ['a', 'b'])) |
---|
44 | n/a | check(['a/b/'], ('', '', ['a', 'b'])) |
---|
45 | n/a | check(['a', 'b/c', 'd'], ('', '', ['a', 'b', 'c', 'd'])) |
---|
46 | n/a | # Collapsing and stripping excess slashes |
---|
47 | n/a | check(['a', 'b//c', 'd'], ('', '', ['a', 'b', 'c', 'd'])) |
---|
48 | n/a | check(['a', 'b/c/', 'd'], ('', '', ['a', 'b', 'c', 'd'])) |
---|
49 | n/a | # Eliminating standalone dots |
---|
50 | n/a | check(['.'], ('', '', [])) |
---|
51 | n/a | check(['.', '.', 'b'], ('', '', ['b'])) |
---|
52 | n/a | check(['a', '.', 'b'], ('', '', ['a', 'b'])) |
---|
53 | n/a | check(['a', '.', '.'], ('', '', ['a'])) |
---|
54 | n/a | # The first part is anchored |
---|
55 | n/a | check(['/a/b'], ('', sep, [sep, 'a', 'b'])) |
---|
56 | n/a | check(['/a', 'b'], ('', sep, [sep, 'a', 'b'])) |
---|
57 | n/a | check(['/a/', 'b'], ('', sep, [sep, 'a', 'b'])) |
---|
58 | n/a | # Ignoring parts before an anchored part |
---|
59 | n/a | check(['a', '/b', 'c'], ('', sep, [sep, 'b', 'c'])) |
---|
60 | n/a | check(['a', '/b', '/c'], ('', sep, [sep, 'c'])) |
---|
61 | n/a | |
---|
62 | n/a | |
---|
63 | n/a | class PosixFlavourTest(_BaseFlavourTest, unittest.TestCase): |
---|
64 | n/a | flavour = pathlib._posix_flavour |
---|
65 | n/a | |
---|
66 | n/a | def test_parse_parts(self): |
---|
67 | n/a | check = self._check_parse_parts |
---|
68 | n/a | # Collapsing of excess leading slashes, except for the double-slash |
---|
69 | n/a | # special case. |
---|
70 | n/a | check(['//a', 'b'], ('', '//', ['//', 'a', 'b'])) |
---|
71 | n/a | check(['///a', 'b'], ('', '/', ['/', 'a', 'b'])) |
---|
72 | n/a | check(['////a', 'b'], ('', '/', ['/', 'a', 'b'])) |
---|
73 | n/a | # Paths which look like NT paths aren't treated specially |
---|
74 | n/a | check(['c:a'], ('', '', ['c:a'])) |
---|
75 | n/a | check(['c:\\a'], ('', '', ['c:\\a'])) |
---|
76 | n/a | check(['\\a'], ('', '', ['\\a'])) |
---|
77 | n/a | |
---|
78 | n/a | def test_splitroot(self): |
---|
79 | n/a | f = self.flavour.splitroot |
---|
80 | n/a | self.assertEqual(f(''), ('', '', '')) |
---|
81 | n/a | self.assertEqual(f('a'), ('', '', 'a')) |
---|
82 | n/a | self.assertEqual(f('a/b'), ('', '', 'a/b')) |
---|
83 | n/a | self.assertEqual(f('a/b/'), ('', '', 'a/b/')) |
---|
84 | n/a | self.assertEqual(f('/a'), ('', '/', 'a')) |
---|
85 | n/a | self.assertEqual(f('/a/b'), ('', '/', 'a/b')) |
---|
86 | n/a | self.assertEqual(f('/a/b/'), ('', '/', 'a/b/')) |
---|
87 | n/a | # The root is collapsed when there are redundant slashes |
---|
88 | n/a | # except when there are exactly two leading slashes, which |
---|
89 | n/a | # is a special case in POSIX. |
---|
90 | n/a | self.assertEqual(f('//a'), ('', '//', 'a')) |
---|
91 | n/a | self.assertEqual(f('///a'), ('', '/', 'a')) |
---|
92 | n/a | self.assertEqual(f('///a/b'), ('', '/', 'a/b')) |
---|
93 | n/a | # Paths which look like NT paths aren't treated specially |
---|
94 | n/a | self.assertEqual(f('c:/a/b'), ('', '', 'c:/a/b')) |
---|
95 | n/a | self.assertEqual(f('\\/a/b'), ('', '', '\\/a/b')) |
---|
96 | n/a | self.assertEqual(f('\\a\\b'), ('', '', '\\a\\b')) |
---|
97 | n/a | |
---|
98 | n/a | |
---|
99 | n/a | class NTFlavourTest(_BaseFlavourTest, unittest.TestCase): |
---|
100 | n/a | flavour = pathlib._windows_flavour |
---|
101 | n/a | |
---|
102 | n/a | def test_parse_parts(self): |
---|
103 | n/a | check = self._check_parse_parts |
---|
104 | n/a | # First part is anchored |
---|
105 | n/a | check(['c:'], ('c:', '', ['c:'])) |
---|
106 | n/a | check(['c:/'], ('c:', '\\', ['c:\\'])) |
---|
107 | n/a | check(['/'], ('', '\\', ['\\'])) |
---|
108 | n/a | check(['c:a'], ('c:', '', ['c:', 'a'])) |
---|
109 | n/a | check(['c:/a'], ('c:', '\\', ['c:\\', 'a'])) |
---|
110 | n/a | check(['/a'], ('', '\\', ['\\', 'a'])) |
---|
111 | n/a | # UNC paths |
---|
112 | n/a | check(['//a/b'], ('\\\\a\\b', '\\', ['\\\\a\\b\\'])) |
---|
113 | n/a | check(['//a/b/'], ('\\\\a\\b', '\\', ['\\\\a\\b\\'])) |
---|
114 | n/a | check(['//a/b/c'], ('\\\\a\\b', '\\', ['\\\\a\\b\\', 'c'])) |
---|
115 | n/a | # Second part is anchored, so that the first part is ignored |
---|
116 | n/a | check(['a', 'Z:b', 'c'], ('Z:', '', ['Z:', 'b', 'c'])) |
---|
117 | n/a | check(['a', 'Z:/b', 'c'], ('Z:', '\\', ['Z:\\', 'b', 'c'])) |
---|
118 | n/a | # UNC paths |
---|
119 | n/a | check(['a', '//b/c', 'd'], ('\\\\b\\c', '\\', ['\\\\b\\c\\', 'd'])) |
---|
120 | n/a | # Collapsing and stripping excess slashes |
---|
121 | n/a | check(['a', 'Z://b//c/', 'd/'], ('Z:', '\\', ['Z:\\', 'b', 'c', 'd'])) |
---|
122 | n/a | # UNC paths |
---|
123 | n/a | check(['a', '//b/c//', 'd'], ('\\\\b\\c', '\\', ['\\\\b\\c\\', 'd'])) |
---|
124 | n/a | # Extended paths |
---|
125 | n/a | check(['//?/c:/'], ('\\\\?\\c:', '\\', ['\\\\?\\c:\\'])) |
---|
126 | n/a | check(['//?/c:/a'], ('\\\\?\\c:', '\\', ['\\\\?\\c:\\', 'a'])) |
---|
127 | n/a | check(['//?/c:/a', '/b'], ('\\\\?\\c:', '\\', ['\\\\?\\c:\\', 'b'])) |
---|
128 | n/a | # Extended UNC paths (format is "\\?\UNC\server\share") |
---|
129 | n/a | check(['//?/UNC/b/c'], ('\\\\?\\UNC\\b\\c', '\\', ['\\\\?\\UNC\\b\\c\\'])) |
---|
130 | n/a | check(['//?/UNC/b/c/d'], ('\\\\?\\UNC\\b\\c', '\\', ['\\\\?\\UNC\\b\\c\\', 'd'])) |
---|
131 | n/a | # Second part has a root but not drive |
---|
132 | n/a | check(['a', '/b', 'c'], ('', '\\', ['\\', 'b', 'c'])) |
---|
133 | n/a | check(['Z:/a', '/b', 'c'], ('Z:', '\\', ['Z:\\', 'b', 'c'])) |
---|
134 | n/a | check(['//?/Z:/a', '/b', 'c'], ('\\\\?\\Z:', '\\', ['\\\\?\\Z:\\', 'b', 'c'])) |
---|
135 | n/a | |
---|
136 | n/a | def test_splitroot(self): |
---|
137 | n/a | f = self.flavour.splitroot |
---|
138 | n/a | self.assertEqual(f(''), ('', '', '')) |
---|
139 | n/a | self.assertEqual(f('a'), ('', '', 'a')) |
---|
140 | n/a | self.assertEqual(f('a\\b'), ('', '', 'a\\b')) |
---|
141 | n/a | self.assertEqual(f('\\a'), ('', '\\', 'a')) |
---|
142 | n/a | self.assertEqual(f('\\a\\b'), ('', '\\', 'a\\b')) |
---|
143 | n/a | self.assertEqual(f('c:a\\b'), ('c:', '', 'a\\b')) |
---|
144 | n/a | self.assertEqual(f('c:\\a\\b'), ('c:', '\\', 'a\\b')) |
---|
145 | n/a | # Redundant slashes in the root are collapsed |
---|
146 | n/a | self.assertEqual(f('\\\\a'), ('', '\\', 'a')) |
---|
147 | n/a | self.assertEqual(f('\\\\\\a/b'), ('', '\\', 'a/b')) |
---|
148 | n/a | self.assertEqual(f('c:\\\\a'), ('c:', '\\', 'a')) |
---|
149 | n/a | self.assertEqual(f('c:\\\\\\a/b'), ('c:', '\\', 'a/b')) |
---|
150 | n/a | # Valid UNC paths |
---|
151 | n/a | self.assertEqual(f('\\\\a\\b'), ('\\\\a\\b', '\\', '')) |
---|
152 | n/a | self.assertEqual(f('\\\\a\\b\\'), ('\\\\a\\b', '\\', '')) |
---|
153 | n/a | self.assertEqual(f('\\\\a\\b\\c\\d'), ('\\\\a\\b', '\\', 'c\\d')) |
---|
154 | n/a | # These are non-UNC paths (according to ntpath.py and test_ntpath) |
---|
155 | n/a | # However, command.com says such paths are invalid, so it's |
---|
156 | n/a | # difficult to know what the right semantics are |
---|
157 | n/a | self.assertEqual(f('\\\\\\a\\b'), ('', '\\', 'a\\b')) |
---|
158 | n/a | self.assertEqual(f('\\\\a'), ('', '\\', 'a')) |
---|
159 | n/a | |
---|
160 | n/a | |
---|
161 | n/a | # |
---|
162 | n/a | # Tests for the pure classes |
---|
163 | n/a | # |
---|
164 | n/a | |
---|
165 | n/a | class _BasePurePathTest(object): |
---|
166 | n/a | |
---|
167 | n/a | # keys are canonical paths, values are list of tuples of arguments |
---|
168 | n/a | # supposed to produce equal paths |
---|
169 | n/a | equivalences = { |
---|
170 | n/a | 'a/b': [ |
---|
171 | n/a | ('a', 'b'), ('a/', 'b'), ('a', 'b/'), ('a/', 'b/'), |
---|
172 | n/a | ('a/b/',), ('a//b',), ('a//b//',), |
---|
173 | n/a | # empty components get removed |
---|
174 | n/a | ('', 'a', 'b'), ('a', '', 'b'), ('a', 'b', ''), |
---|
175 | n/a | ], |
---|
176 | n/a | '/b/c/d': [ |
---|
177 | n/a | ('a', '/b/c', 'd'), ('a', '///b//c', 'd/'), |
---|
178 | n/a | ('/a', '/b/c', 'd'), |
---|
179 | n/a | # empty components get removed |
---|
180 | n/a | ('/', 'b', '', 'c/d'), ('/', '', 'b/c/d'), ('', '/b/c/d'), |
---|
181 | n/a | ], |
---|
182 | n/a | } |
---|
183 | n/a | |
---|
184 | n/a | def setUp(self): |
---|
185 | n/a | p = self.cls('a') |
---|
186 | n/a | self.flavour = p._flavour |
---|
187 | n/a | self.sep = self.flavour.sep |
---|
188 | n/a | self.altsep = self.flavour.altsep |
---|
189 | n/a | |
---|
190 | n/a | def test_constructor_common(self): |
---|
191 | n/a | P = self.cls |
---|
192 | n/a | p = P('a') |
---|
193 | n/a | self.assertIsInstance(p, P) |
---|
194 | n/a | class PathLike: |
---|
195 | n/a | def __fspath__(self): |
---|
196 | n/a | return "a/b/c" |
---|
197 | n/a | P('a', 'b', 'c') |
---|
198 | n/a | P('/a', 'b', 'c') |
---|
199 | n/a | P('a/b/c') |
---|
200 | n/a | P('/a/b/c') |
---|
201 | n/a | P(PathLike()) |
---|
202 | n/a | self.assertEqual(P(P('a')), P('a')) |
---|
203 | n/a | self.assertEqual(P(P('a'), 'b'), P('a/b')) |
---|
204 | n/a | self.assertEqual(P(P('a'), P('b')), P('a/b')) |
---|
205 | n/a | self.assertEqual(P(P('a'), P('b'), P('c')), P(PathLike())) |
---|
206 | n/a | |
---|
207 | n/a | def _check_str_subclass(self, *args): |
---|
208 | n/a | # Issue #21127: it should be possible to construct a PurePath object |
---|
209 | n/a | # from a str subclass instance, and it then gets converted to |
---|
210 | n/a | # a pure str object. |
---|
211 | n/a | class StrSubclass(str): |
---|
212 | n/a | pass |
---|
213 | n/a | P = self.cls |
---|
214 | n/a | p = P(*(StrSubclass(x) for x in args)) |
---|
215 | n/a | self.assertEqual(p, P(*args)) |
---|
216 | n/a | for part in p.parts: |
---|
217 | n/a | self.assertIs(type(part), str) |
---|
218 | n/a | |
---|
219 | n/a | def test_str_subclass_common(self): |
---|
220 | n/a | self._check_str_subclass('') |
---|
221 | n/a | self._check_str_subclass('.') |
---|
222 | n/a | self._check_str_subclass('a') |
---|
223 | n/a | self._check_str_subclass('a/b.txt') |
---|
224 | n/a | self._check_str_subclass('/a/b.txt') |
---|
225 | n/a | |
---|
226 | n/a | def test_join_common(self): |
---|
227 | n/a | P = self.cls |
---|
228 | n/a | p = P('a/b') |
---|
229 | n/a | pp = p.joinpath('c') |
---|
230 | n/a | self.assertEqual(pp, P('a/b/c')) |
---|
231 | n/a | self.assertIs(type(pp), type(p)) |
---|
232 | n/a | pp = p.joinpath('c', 'd') |
---|
233 | n/a | self.assertEqual(pp, P('a/b/c/d')) |
---|
234 | n/a | pp = p.joinpath(P('c')) |
---|
235 | n/a | self.assertEqual(pp, P('a/b/c')) |
---|
236 | n/a | pp = p.joinpath('/c') |
---|
237 | n/a | self.assertEqual(pp, P('/c')) |
---|
238 | n/a | |
---|
239 | n/a | def test_div_common(self): |
---|
240 | n/a | # Basically the same as joinpath() |
---|
241 | n/a | P = self.cls |
---|
242 | n/a | p = P('a/b') |
---|
243 | n/a | pp = p / 'c' |
---|
244 | n/a | self.assertEqual(pp, P('a/b/c')) |
---|
245 | n/a | self.assertIs(type(pp), type(p)) |
---|
246 | n/a | pp = p / 'c/d' |
---|
247 | n/a | self.assertEqual(pp, P('a/b/c/d')) |
---|
248 | n/a | pp = p / 'c' / 'd' |
---|
249 | n/a | self.assertEqual(pp, P('a/b/c/d')) |
---|
250 | n/a | pp = 'c' / p / 'd' |
---|
251 | n/a | self.assertEqual(pp, P('c/a/b/d')) |
---|
252 | n/a | pp = p / P('c') |
---|
253 | n/a | self.assertEqual(pp, P('a/b/c')) |
---|
254 | n/a | pp = p/ '/c' |
---|
255 | n/a | self.assertEqual(pp, P('/c')) |
---|
256 | n/a | |
---|
257 | n/a | def _check_str(self, expected, args): |
---|
258 | n/a | p = self.cls(*args) |
---|
259 | n/a | self.assertEqual(str(p), expected.replace('/', self.sep)) |
---|
260 | n/a | |
---|
261 | n/a | def test_str_common(self): |
---|
262 | n/a | # Canonicalized paths roundtrip |
---|
263 | n/a | for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'): |
---|
264 | n/a | self._check_str(pathstr, (pathstr,)) |
---|
265 | n/a | # Special case for the empty path |
---|
266 | n/a | self._check_str('.', ('',)) |
---|
267 | n/a | # Other tests for str() are in test_equivalences() |
---|
268 | n/a | |
---|
269 | n/a | def test_as_posix_common(self): |
---|
270 | n/a | P = self.cls |
---|
271 | n/a | for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'): |
---|
272 | n/a | self.assertEqual(P(pathstr).as_posix(), pathstr) |
---|
273 | n/a | # Other tests for as_posix() are in test_equivalences() |
---|
274 | n/a | |
---|
275 | n/a | def test_as_bytes_common(self): |
---|
276 | n/a | sep = os.fsencode(self.sep) |
---|
277 | n/a | P = self.cls |
---|
278 | n/a | self.assertEqual(bytes(P('a/b')), b'a' + sep + b'b') |
---|
279 | n/a | |
---|
280 | n/a | def test_as_uri_common(self): |
---|
281 | n/a | P = self.cls |
---|
282 | n/a | with self.assertRaises(ValueError): |
---|
283 | n/a | P('a').as_uri() |
---|
284 | n/a | with self.assertRaises(ValueError): |
---|
285 | n/a | P().as_uri() |
---|
286 | n/a | |
---|
287 | n/a | def test_repr_common(self): |
---|
288 | n/a | for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'): |
---|
289 | n/a | p = self.cls(pathstr) |
---|
290 | n/a | clsname = p.__class__.__name__ |
---|
291 | n/a | r = repr(p) |
---|
292 | n/a | # The repr() is in the form ClassName("forward-slashes path") |
---|
293 | n/a | self.assertTrue(r.startswith(clsname + '('), r) |
---|
294 | n/a | self.assertTrue(r.endswith(')'), r) |
---|
295 | n/a | inner = r[len(clsname) + 1 : -1] |
---|
296 | n/a | self.assertEqual(eval(inner), p.as_posix()) |
---|
297 | n/a | # The repr() roundtrips |
---|
298 | n/a | q = eval(r, pathlib.__dict__) |
---|
299 | n/a | self.assertIs(q.__class__, p.__class__) |
---|
300 | n/a | self.assertEqual(q, p) |
---|
301 | n/a | self.assertEqual(repr(q), r) |
---|
302 | n/a | |
---|
303 | n/a | def test_eq_common(self): |
---|
304 | n/a | P = self.cls |
---|
305 | n/a | self.assertEqual(P('a/b'), P('a/b')) |
---|
306 | n/a | self.assertEqual(P('a/b'), P('a', 'b')) |
---|
307 | n/a | self.assertNotEqual(P('a/b'), P('a')) |
---|
308 | n/a | self.assertNotEqual(P('a/b'), P('/a/b')) |
---|
309 | n/a | self.assertNotEqual(P('a/b'), P()) |
---|
310 | n/a | self.assertNotEqual(P('/a/b'), P('/')) |
---|
311 | n/a | self.assertNotEqual(P(), P('/')) |
---|
312 | n/a | self.assertNotEqual(P(), "") |
---|
313 | n/a | self.assertNotEqual(P(), {}) |
---|
314 | n/a | self.assertNotEqual(P(), int) |
---|
315 | n/a | |
---|
316 | n/a | def test_match_common(self): |
---|
317 | n/a | P = self.cls |
---|
318 | n/a | self.assertRaises(ValueError, P('a').match, '') |
---|
319 | n/a | self.assertRaises(ValueError, P('a').match, '.') |
---|
320 | n/a | # Simple relative pattern |
---|
321 | n/a | self.assertTrue(P('b.py').match('b.py')) |
---|
322 | n/a | self.assertTrue(P('a/b.py').match('b.py')) |
---|
323 | n/a | self.assertTrue(P('/a/b.py').match('b.py')) |
---|
324 | n/a | self.assertFalse(P('a.py').match('b.py')) |
---|
325 | n/a | self.assertFalse(P('b/py').match('b.py')) |
---|
326 | n/a | self.assertFalse(P('/a.py').match('b.py')) |
---|
327 | n/a | self.assertFalse(P('b.py/c').match('b.py')) |
---|
328 | n/a | # Wilcard relative pattern |
---|
329 | n/a | self.assertTrue(P('b.py').match('*.py')) |
---|
330 | n/a | self.assertTrue(P('a/b.py').match('*.py')) |
---|
331 | n/a | self.assertTrue(P('/a/b.py').match('*.py')) |
---|
332 | n/a | self.assertFalse(P('b.pyc').match('*.py')) |
---|
333 | n/a | self.assertFalse(P('b./py').match('*.py')) |
---|
334 | n/a | self.assertFalse(P('b.py/c').match('*.py')) |
---|
335 | n/a | # Multi-part relative pattern |
---|
336 | n/a | self.assertTrue(P('ab/c.py').match('a*/*.py')) |
---|
337 | n/a | self.assertTrue(P('/d/ab/c.py').match('a*/*.py')) |
---|
338 | n/a | self.assertFalse(P('a.py').match('a*/*.py')) |
---|
339 | n/a | self.assertFalse(P('/dab/c.py').match('a*/*.py')) |
---|
340 | n/a | self.assertFalse(P('ab/c.py/d').match('a*/*.py')) |
---|
341 | n/a | # Absolute pattern |
---|
342 | n/a | self.assertTrue(P('/b.py').match('/*.py')) |
---|
343 | n/a | self.assertFalse(P('b.py').match('/*.py')) |
---|
344 | n/a | self.assertFalse(P('a/b.py').match('/*.py')) |
---|
345 | n/a | self.assertFalse(P('/a/b.py').match('/*.py')) |
---|
346 | n/a | # Multi-part absolute pattern |
---|
347 | n/a | self.assertTrue(P('/a/b.py').match('/a/*.py')) |
---|
348 | n/a | self.assertFalse(P('/ab.py').match('/a/*.py')) |
---|
349 | n/a | self.assertFalse(P('/a/b/c.py').match('/a/*.py')) |
---|
350 | n/a | |
---|
351 | n/a | def test_ordering_common(self): |
---|
352 | n/a | # Ordering is tuple-alike |
---|
353 | n/a | def assertLess(a, b): |
---|
354 | n/a | self.assertLess(a, b) |
---|
355 | n/a | self.assertGreater(b, a) |
---|
356 | n/a | P = self.cls |
---|
357 | n/a | a = P('a') |
---|
358 | n/a | b = P('a/b') |
---|
359 | n/a | c = P('abc') |
---|
360 | n/a | d = P('b') |
---|
361 | n/a | assertLess(a, b) |
---|
362 | n/a | assertLess(a, c) |
---|
363 | n/a | assertLess(a, d) |
---|
364 | n/a | assertLess(b, c) |
---|
365 | n/a | assertLess(c, d) |
---|
366 | n/a | P = self.cls |
---|
367 | n/a | a = P('/a') |
---|
368 | n/a | b = P('/a/b') |
---|
369 | n/a | c = P('/abc') |
---|
370 | n/a | d = P('/b') |
---|
371 | n/a | assertLess(a, b) |
---|
372 | n/a | assertLess(a, c) |
---|
373 | n/a | assertLess(a, d) |
---|
374 | n/a | assertLess(b, c) |
---|
375 | n/a | assertLess(c, d) |
---|
376 | n/a | with self.assertRaises(TypeError): |
---|
377 | n/a | P() < {} |
---|
378 | n/a | |
---|
379 | n/a | def test_parts_common(self): |
---|
380 | n/a | # `parts` returns a tuple |
---|
381 | n/a | sep = self.sep |
---|
382 | n/a | P = self.cls |
---|
383 | n/a | p = P('a/b') |
---|
384 | n/a | parts = p.parts |
---|
385 | n/a | self.assertEqual(parts, ('a', 'b')) |
---|
386 | n/a | # The object gets reused |
---|
387 | n/a | self.assertIs(parts, p.parts) |
---|
388 | n/a | # When the path is absolute, the anchor is a separate part |
---|
389 | n/a | p = P('/a/b') |
---|
390 | n/a | parts = p.parts |
---|
391 | n/a | self.assertEqual(parts, (sep, 'a', 'b')) |
---|
392 | n/a | |
---|
393 | n/a | def test_fspath_common(self): |
---|
394 | n/a | P = self.cls |
---|
395 | n/a | p = P('a/b') |
---|
396 | n/a | self._check_str(p.__fspath__(), ('a/b',)) |
---|
397 | n/a | self._check_str(os.fspath(p), ('a/b',)) |
---|
398 | n/a | |
---|
399 | n/a | def test_equivalences(self): |
---|
400 | n/a | for k, tuples in self.equivalences.items(): |
---|
401 | n/a | canon = k.replace('/', self.sep) |
---|
402 | n/a | posix = k.replace(self.sep, '/') |
---|
403 | n/a | if canon != posix: |
---|
404 | n/a | tuples = tuples + [ |
---|
405 | n/a | tuple(part.replace('/', self.sep) for part in t) |
---|
406 | n/a | for t in tuples |
---|
407 | n/a | ] |
---|
408 | n/a | tuples.append((posix, )) |
---|
409 | n/a | pcanon = self.cls(canon) |
---|
410 | n/a | for t in tuples: |
---|
411 | n/a | p = self.cls(*t) |
---|
412 | n/a | self.assertEqual(p, pcanon, "failed with args {}".format(t)) |
---|
413 | n/a | self.assertEqual(hash(p), hash(pcanon)) |
---|
414 | n/a | self.assertEqual(str(p), canon) |
---|
415 | n/a | self.assertEqual(p.as_posix(), posix) |
---|
416 | n/a | |
---|
417 | n/a | def test_parent_common(self): |
---|
418 | n/a | # Relative |
---|
419 | n/a | P = self.cls |
---|
420 | n/a | p = P('a/b/c') |
---|
421 | n/a | self.assertEqual(p.parent, P('a/b')) |
---|
422 | n/a | self.assertEqual(p.parent.parent, P('a')) |
---|
423 | n/a | self.assertEqual(p.parent.parent.parent, P()) |
---|
424 | n/a | self.assertEqual(p.parent.parent.parent.parent, P()) |
---|
425 | n/a | # Anchored |
---|
426 | n/a | p = P('/a/b/c') |
---|
427 | n/a | self.assertEqual(p.parent, P('/a/b')) |
---|
428 | n/a | self.assertEqual(p.parent.parent, P('/a')) |
---|
429 | n/a | self.assertEqual(p.parent.parent.parent, P('/')) |
---|
430 | n/a | self.assertEqual(p.parent.parent.parent.parent, P('/')) |
---|
431 | n/a | |
---|
432 | n/a | def test_parents_common(self): |
---|
433 | n/a | # Relative |
---|
434 | n/a | P = self.cls |
---|
435 | n/a | p = P('a/b/c') |
---|
436 | n/a | par = p.parents |
---|
437 | n/a | self.assertEqual(len(par), 3) |
---|
438 | n/a | self.assertEqual(par[0], P('a/b')) |
---|
439 | n/a | self.assertEqual(par[1], P('a')) |
---|
440 | n/a | self.assertEqual(par[2], P('.')) |
---|
441 | n/a | self.assertEqual(list(par), [P('a/b'), P('a'), P('.')]) |
---|
442 | n/a | with self.assertRaises(IndexError): |
---|
443 | n/a | par[-1] |
---|
444 | n/a | with self.assertRaises(IndexError): |
---|
445 | n/a | par[3] |
---|
446 | n/a | with self.assertRaises(TypeError): |
---|
447 | n/a | par[0] = p |
---|
448 | n/a | # Anchored |
---|
449 | n/a | p = P('/a/b/c') |
---|
450 | n/a | par = p.parents |
---|
451 | n/a | self.assertEqual(len(par), 3) |
---|
452 | n/a | self.assertEqual(par[0], P('/a/b')) |
---|
453 | n/a | self.assertEqual(par[1], P('/a')) |
---|
454 | n/a | self.assertEqual(par[2], P('/')) |
---|
455 | n/a | self.assertEqual(list(par), [P('/a/b'), P('/a'), P('/')]) |
---|
456 | n/a | with self.assertRaises(IndexError): |
---|
457 | n/a | par[3] |
---|
458 | n/a | |
---|
459 | n/a | def test_drive_common(self): |
---|
460 | n/a | P = self.cls |
---|
461 | n/a | self.assertEqual(P('a/b').drive, '') |
---|
462 | n/a | self.assertEqual(P('/a/b').drive, '') |
---|
463 | n/a | self.assertEqual(P('').drive, '') |
---|
464 | n/a | |
---|
465 | n/a | def test_root_common(self): |
---|
466 | n/a | P = self.cls |
---|
467 | n/a | sep = self.sep |
---|
468 | n/a | self.assertEqual(P('').root, '') |
---|
469 | n/a | self.assertEqual(P('a/b').root, '') |
---|
470 | n/a | self.assertEqual(P('/').root, sep) |
---|
471 | n/a | self.assertEqual(P('/a/b').root, sep) |
---|
472 | n/a | |
---|
473 | n/a | def test_anchor_common(self): |
---|
474 | n/a | P = self.cls |
---|
475 | n/a | sep = self.sep |
---|
476 | n/a | self.assertEqual(P('').anchor, '') |
---|
477 | n/a | self.assertEqual(P('a/b').anchor, '') |
---|
478 | n/a | self.assertEqual(P('/').anchor, sep) |
---|
479 | n/a | self.assertEqual(P('/a/b').anchor, sep) |
---|
480 | n/a | |
---|
481 | n/a | def test_name_common(self): |
---|
482 | n/a | P = self.cls |
---|
483 | n/a | self.assertEqual(P('').name, '') |
---|
484 | n/a | self.assertEqual(P('.').name, '') |
---|
485 | n/a | self.assertEqual(P('/').name, '') |
---|
486 | n/a | self.assertEqual(P('a/b').name, 'b') |
---|
487 | n/a | self.assertEqual(P('/a/b').name, 'b') |
---|
488 | n/a | self.assertEqual(P('/a/b/.').name, 'b') |
---|
489 | n/a | self.assertEqual(P('a/b.py').name, 'b.py') |
---|
490 | n/a | self.assertEqual(P('/a/b.py').name, 'b.py') |
---|
491 | n/a | |
---|
492 | n/a | def test_suffix_common(self): |
---|
493 | n/a | P = self.cls |
---|
494 | n/a | self.assertEqual(P('').suffix, '') |
---|
495 | n/a | self.assertEqual(P('.').suffix, '') |
---|
496 | n/a | self.assertEqual(P('..').suffix, '') |
---|
497 | n/a | self.assertEqual(P('/').suffix, '') |
---|
498 | n/a | self.assertEqual(P('a/b').suffix, '') |
---|
499 | n/a | self.assertEqual(P('/a/b').suffix, '') |
---|
500 | n/a | self.assertEqual(P('/a/b/.').suffix, '') |
---|
501 | n/a | self.assertEqual(P('a/b.py').suffix, '.py') |
---|
502 | n/a | self.assertEqual(P('/a/b.py').suffix, '.py') |
---|
503 | n/a | self.assertEqual(P('a/.hgrc').suffix, '') |
---|
504 | n/a | self.assertEqual(P('/a/.hgrc').suffix, '') |
---|
505 | n/a | self.assertEqual(P('a/.hg.rc').suffix, '.rc') |
---|
506 | n/a | self.assertEqual(P('/a/.hg.rc').suffix, '.rc') |
---|
507 | n/a | self.assertEqual(P('a/b.tar.gz').suffix, '.gz') |
---|
508 | n/a | self.assertEqual(P('/a/b.tar.gz').suffix, '.gz') |
---|
509 | n/a | self.assertEqual(P('a/Some name. Ending with a dot.').suffix, '') |
---|
510 | n/a | self.assertEqual(P('/a/Some name. Ending with a dot.').suffix, '') |
---|
511 | n/a | |
---|
512 | n/a | def test_suffixes_common(self): |
---|
513 | n/a | P = self.cls |
---|
514 | n/a | self.assertEqual(P('').suffixes, []) |
---|
515 | n/a | self.assertEqual(P('.').suffixes, []) |
---|
516 | n/a | self.assertEqual(P('/').suffixes, []) |
---|
517 | n/a | self.assertEqual(P('a/b').suffixes, []) |
---|
518 | n/a | self.assertEqual(P('/a/b').suffixes, []) |
---|
519 | n/a | self.assertEqual(P('/a/b/.').suffixes, []) |
---|
520 | n/a | self.assertEqual(P('a/b.py').suffixes, ['.py']) |
---|
521 | n/a | self.assertEqual(P('/a/b.py').suffixes, ['.py']) |
---|
522 | n/a | self.assertEqual(P('a/.hgrc').suffixes, []) |
---|
523 | n/a | self.assertEqual(P('/a/.hgrc').suffixes, []) |
---|
524 | n/a | self.assertEqual(P('a/.hg.rc').suffixes, ['.rc']) |
---|
525 | n/a | self.assertEqual(P('/a/.hg.rc').suffixes, ['.rc']) |
---|
526 | n/a | self.assertEqual(P('a/b.tar.gz').suffixes, ['.tar', '.gz']) |
---|
527 | n/a | self.assertEqual(P('/a/b.tar.gz').suffixes, ['.tar', '.gz']) |
---|
528 | n/a | self.assertEqual(P('a/Some name. Ending with a dot.').suffixes, []) |
---|
529 | n/a | self.assertEqual(P('/a/Some name. Ending with a dot.').suffixes, []) |
---|
530 | n/a | |
---|
531 | n/a | def test_stem_common(self): |
---|
532 | n/a | P = self.cls |
---|
533 | n/a | self.assertEqual(P('').stem, '') |
---|
534 | n/a | self.assertEqual(P('.').stem, '') |
---|
535 | n/a | self.assertEqual(P('..').stem, '..') |
---|
536 | n/a | self.assertEqual(P('/').stem, '') |
---|
537 | n/a | self.assertEqual(P('a/b').stem, 'b') |
---|
538 | n/a | self.assertEqual(P('a/b.py').stem, 'b') |
---|
539 | n/a | self.assertEqual(P('a/.hgrc').stem, '.hgrc') |
---|
540 | n/a | self.assertEqual(P('a/.hg.rc').stem, '.hg') |
---|
541 | n/a | self.assertEqual(P('a/b.tar.gz').stem, 'b.tar') |
---|
542 | n/a | self.assertEqual(P('a/Some name. Ending with a dot.').stem, |
---|
543 | n/a | 'Some name. Ending with a dot.') |
---|
544 | n/a | |
---|
545 | n/a | def test_with_name_common(self): |
---|
546 | n/a | P = self.cls |
---|
547 | n/a | self.assertEqual(P('a/b').with_name('d.xml'), P('a/d.xml')) |
---|
548 | n/a | self.assertEqual(P('/a/b').with_name('d.xml'), P('/a/d.xml')) |
---|
549 | n/a | self.assertEqual(P('a/b.py').with_name('d.xml'), P('a/d.xml')) |
---|
550 | n/a | self.assertEqual(P('/a/b.py').with_name('d.xml'), P('/a/d.xml')) |
---|
551 | n/a | self.assertEqual(P('a/Dot ending.').with_name('d.xml'), P('a/d.xml')) |
---|
552 | n/a | self.assertEqual(P('/a/Dot ending.').with_name('d.xml'), P('/a/d.xml')) |
---|
553 | n/a | self.assertRaises(ValueError, P('').with_name, 'd.xml') |
---|
554 | n/a | self.assertRaises(ValueError, P('.').with_name, 'd.xml') |
---|
555 | n/a | self.assertRaises(ValueError, P('/').with_name, 'd.xml') |
---|
556 | n/a | self.assertRaises(ValueError, P('a/b').with_name, '') |
---|
557 | n/a | self.assertRaises(ValueError, P('a/b').with_name, '/c') |
---|
558 | n/a | self.assertRaises(ValueError, P('a/b').with_name, 'c/') |
---|
559 | n/a | self.assertRaises(ValueError, P('a/b').with_name, 'c/d') |
---|
560 | n/a | |
---|
561 | n/a | def test_with_suffix_common(self): |
---|
562 | n/a | P = self.cls |
---|
563 | n/a | self.assertEqual(P('a/b').with_suffix('.gz'), P('a/b.gz')) |
---|
564 | n/a | self.assertEqual(P('/a/b').with_suffix('.gz'), P('/a/b.gz')) |
---|
565 | n/a | self.assertEqual(P('a/b.py').with_suffix('.gz'), P('a/b.gz')) |
---|
566 | n/a | self.assertEqual(P('/a/b.py').with_suffix('.gz'), P('/a/b.gz')) |
---|
567 | n/a | # Stripping suffix |
---|
568 | n/a | self.assertEqual(P('a/b.py').with_suffix(''), P('a/b')) |
---|
569 | n/a | self.assertEqual(P('/a/b').with_suffix(''), P('/a/b')) |
---|
570 | n/a | # Path doesn't have a "filename" component |
---|
571 | n/a | self.assertRaises(ValueError, P('').with_suffix, '.gz') |
---|
572 | n/a | self.assertRaises(ValueError, P('.').with_suffix, '.gz') |
---|
573 | n/a | self.assertRaises(ValueError, P('/').with_suffix, '.gz') |
---|
574 | n/a | # Invalid suffix |
---|
575 | n/a | self.assertRaises(ValueError, P('a/b').with_suffix, 'gz') |
---|
576 | n/a | self.assertRaises(ValueError, P('a/b').with_suffix, '/') |
---|
577 | n/a | self.assertRaises(ValueError, P('a/b').with_suffix, '.') |
---|
578 | n/a | self.assertRaises(ValueError, P('a/b').with_suffix, '/.gz') |
---|
579 | n/a | self.assertRaises(ValueError, P('a/b').with_suffix, 'c/d') |
---|
580 | n/a | self.assertRaises(ValueError, P('a/b').with_suffix, '.c/.d') |
---|
581 | n/a | self.assertRaises(ValueError, P('a/b').with_suffix, './.d') |
---|
582 | n/a | self.assertRaises(ValueError, P('a/b').with_suffix, '.d/.') |
---|
583 | n/a | |
---|
584 | n/a | def test_relative_to_common(self): |
---|
585 | n/a | P = self.cls |
---|
586 | n/a | p = P('a/b') |
---|
587 | n/a | self.assertRaises(TypeError, p.relative_to) |
---|
588 | n/a | self.assertRaises(TypeError, p.relative_to, b'a') |
---|
589 | n/a | self.assertEqual(p.relative_to(P()), P('a/b')) |
---|
590 | n/a | self.assertEqual(p.relative_to(''), P('a/b')) |
---|
591 | n/a | self.assertEqual(p.relative_to(P('a')), P('b')) |
---|
592 | n/a | self.assertEqual(p.relative_to('a'), P('b')) |
---|
593 | n/a | self.assertEqual(p.relative_to('a/'), P('b')) |
---|
594 | n/a | self.assertEqual(p.relative_to(P('a/b')), P()) |
---|
595 | n/a | self.assertEqual(p.relative_to('a/b'), P()) |
---|
596 | n/a | # With several args |
---|
597 | n/a | self.assertEqual(p.relative_to('a', 'b'), P()) |
---|
598 | n/a | # Unrelated paths |
---|
599 | n/a | self.assertRaises(ValueError, p.relative_to, P('c')) |
---|
600 | n/a | self.assertRaises(ValueError, p.relative_to, P('a/b/c')) |
---|
601 | n/a | self.assertRaises(ValueError, p.relative_to, P('a/c')) |
---|
602 | n/a | self.assertRaises(ValueError, p.relative_to, P('/a')) |
---|
603 | n/a | p = P('/a/b') |
---|
604 | n/a | self.assertEqual(p.relative_to(P('/')), P('a/b')) |
---|
605 | n/a | self.assertEqual(p.relative_to('/'), P('a/b')) |
---|
606 | n/a | self.assertEqual(p.relative_to(P('/a')), P('b')) |
---|
607 | n/a | self.assertEqual(p.relative_to('/a'), P('b')) |
---|
608 | n/a | self.assertEqual(p.relative_to('/a/'), P('b')) |
---|
609 | n/a | self.assertEqual(p.relative_to(P('/a/b')), P()) |
---|
610 | n/a | self.assertEqual(p.relative_to('/a/b'), P()) |
---|
611 | n/a | # Unrelated paths |
---|
612 | n/a | self.assertRaises(ValueError, p.relative_to, P('/c')) |
---|
613 | n/a | self.assertRaises(ValueError, p.relative_to, P('/a/b/c')) |
---|
614 | n/a | self.assertRaises(ValueError, p.relative_to, P('/a/c')) |
---|
615 | n/a | self.assertRaises(ValueError, p.relative_to, P()) |
---|
616 | n/a | self.assertRaises(ValueError, p.relative_to, '') |
---|
617 | n/a | self.assertRaises(ValueError, p.relative_to, P('a')) |
---|
618 | n/a | |
---|
619 | n/a | def test_pickling_common(self): |
---|
620 | n/a | P = self.cls |
---|
621 | n/a | p = P('/a/b') |
---|
622 | n/a | for proto in range(0, pickle.HIGHEST_PROTOCOL + 1): |
---|
623 | n/a | dumped = pickle.dumps(p, proto) |
---|
624 | n/a | pp = pickle.loads(dumped) |
---|
625 | n/a | self.assertIs(pp.__class__, p.__class__) |
---|
626 | n/a | self.assertEqual(pp, p) |
---|
627 | n/a | self.assertEqual(hash(pp), hash(p)) |
---|
628 | n/a | self.assertEqual(str(pp), str(p)) |
---|
629 | n/a | |
---|
630 | n/a | |
---|
631 | n/a | class PurePosixPathTest(_BasePurePathTest, unittest.TestCase): |
---|
632 | n/a | cls = pathlib.PurePosixPath |
---|
633 | n/a | |
---|
634 | n/a | def test_root(self): |
---|
635 | n/a | P = self.cls |
---|
636 | n/a | self.assertEqual(P('/a/b').root, '/') |
---|
637 | n/a | self.assertEqual(P('///a/b').root, '/') |
---|
638 | n/a | # POSIX special case for two leading slashes |
---|
639 | n/a | self.assertEqual(P('//a/b').root, '//') |
---|
640 | n/a | |
---|
641 | n/a | def test_eq(self): |
---|
642 | n/a | P = self.cls |
---|
643 | n/a | self.assertNotEqual(P('a/b'), P('A/b')) |
---|
644 | n/a | self.assertEqual(P('/a'), P('///a')) |
---|
645 | n/a | self.assertNotEqual(P('/a'), P('//a')) |
---|
646 | n/a | |
---|
647 | n/a | def test_as_uri(self): |
---|
648 | n/a | P = self.cls |
---|
649 | n/a | self.assertEqual(P('/').as_uri(), 'file:///') |
---|
650 | n/a | self.assertEqual(P('/a/b.c').as_uri(), 'file:///a/b.c') |
---|
651 | n/a | self.assertEqual(P('/a/b%#c').as_uri(), 'file:///a/b%25%23c') |
---|
652 | n/a | |
---|
653 | n/a | def test_as_uri_non_ascii(self): |
---|
654 | n/a | from urllib.parse import quote_from_bytes |
---|
655 | n/a | P = self.cls |
---|
656 | n/a | try: |
---|
657 | n/a | os.fsencode('\xe9') |
---|
658 | n/a | except UnicodeEncodeError: |
---|
659 | n/a | self.skipTest("\\xe9 cannot be encoded to the filesystem encoding") |
---|
660 | n/a | self.assertEqual(P('/a/b\xe9').as_uri(), |
---|
661 | n/a | 'file:///a/b' + quote_from_bytes(os.fsencode('\xe9'))) |
---|
662 | n/a | |
---|
663 | n/a | def test_match(self): |
---|
664 | n/a | P = self.cls |
---|
665 | n/a | self.assertFalse(P('A.py').match('a.PY')) |
---|
666 | n/a | |
---|
667 | n/a | def test_is_absolute(self): |
---|
668 | n/a | P = self.cls |
---|
669 | n/a | self.assertFalse(P().is_absolute()) |
---|
670 | n/a | self.assertFalse(P('a').is_absolute()) |
---|
671 | n/a | self.assertFalse(P('a/b/').is_absolute()) |
---|
672 | n/a | self.assertTrue(P('/').is_absolute()) |
---|
673 | n/a | self.assertTrue(P('/a').is_absolute()) |
---|
674 | n/a | self.assertTrue(P('/a/b/').is_absolute()) |
---|
675 | n/a | self.assertTrue(P('//a').is_absolute()) |
---|
676 | n/a | self.assertTrue(P('//a/b').is_absolute()) |
---|
677 | n/a | |
---|
678 | n/a | def test_is_reserved(self): |
---|
679 | n/a | P = self.cls |
---|
680 | n/a | self.assertIs(False, P('').is_reserved()) |
---|
681 | n/a | self.assertIs(False, P('/').is_reserved()) |
---|
682 | n/a | self.assertIs(False, P('/foo/bar').is_reserved()) |
---|
683 | n/a | self.assertIs(False, P('/dev/con/PRN/NUL').is_reserved()) |
---|
684 | n/a | |
---|
685 | n/a | def test_join(self): |
---|
686 | n/a | P = self.cls |
---|
687 | n/a | p = P('//a') |
---|
688 | n/a | pp = p.joinpath('b') |
---|
689 | n/a | self.assertEqual(pp, P('//a/b')) |
---|
690 | n/a | pp = P('/a').joinpath('//c') |
---|
691 | n/a | self.assertEqual(pp, P('//c')) |
---|
692 | n/a | pp = P('//a').joinpath('/c') |
---|
693 | n/a | self.assertEqual(pp, P('/c')) |
---|
694 | n/a | |
---|
695 | n/a | def test_div(self): |
---|
696 | n/a | # Basically the same as joinpath() |
---|
697 | n/a | P = self.cls |
---|
698 | n/a | p = P('//a') |
---|
699 | n/a | pp = p / 'b' |
---|
700 | n/a | self.assertEqual(pp, P('//a/b')) |
---|
701 | n/a | pp = P('/a') / '//c' |
---|
702 | n/a | self.assertEqual(pp, P('//c')) |
---|
703 | n/a | pp = P('//a') / '/c' |
---|
704 | n/a | self.assertEqual(pp, P('/c')) |
---|
705 | n/a | |
---|
706 | n/a | |
---|
707 | n/a | class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase): |
---|
708 | n/a | cls = pathlib.PureWindowsPath |
---|
709 | n/a | |
---|
710 | n/a | equivalences = _BasePurePathTest.equivalences.copy() |
---|
711 | n/a | equivalences.update({ |
---|
712 | n/a | 'c:a': [ ('c:', 'a'), ('c:', 'a/'), ('/', 'c:', 'a') ], |
---|
713 | n/a | 'c:/a': [ |
---|
714 | n/a | ('c:/', 'a'), ('c:', '/', 'a'), ('c:', '/a'), |
---|
715 | n/a | ('/z', 'c:/', 'a'), ('//x/y', 'c:/', 'a'), |
---|
716 | n/a | ], |
---|
717 | n/a | '//a/b/': [ ('//a/b',) ], |
---|
718 | n/a | '//a/b/c': [ |
---|
719 | n/a | ('//a/b', 'c'), ('//a/b/', 'c'), |
---|
720 | n/a | ], |
---|
721 | n/a | }) |
---|
722 | n/a | |
---|
723 | n/a | def test_str(self): |
---|
724 | n/a | p = self.cls('a/b/c') |
---|
725 | n/a | self.assertEqual(str(p), 'a\\b\\c') |
---|
726 | n/a | p = self.cls('c:/a/b/c') |
---|
727 | n/a | self.assertEqual(str(p), 'c:\\a\\b\\c') |
---|
728 | n/a | p = self.cls('//a/b') |
---|
729 | n/a | self.assertEqual(str(p), '\\\\a\\b\\') |
---|
730 | n/a | p = self.cls('//a/b/c') |
---|
731 | n/a | self.assertEqual(str(p), '\\\\a\\b\\c') |
---|
732 | n/a | p = self.cls('//a/b/c/d') |
---|
733 | n/a | self.assertEqual(str(p), '\\\\a\\b\\c\\d') |
---|
734 | n/a | |
---|
735 | n/a | def test_str_subclass(self): |
---|
736 | n/a | self._check_str_subclass('c:') |
---|
737 | n/a | self._check_str_subclass('c:a') |
---|
738 | n/a | self._check_str_subclass('c:a\\b.txt') |
---|
739 | n/a | self._check_str_subclass('c:\\') |
---|
740 | n/a | self._check_str_subclass('c:\\a') |
---|
741 | n/a | self._check_str_subclass('c:\\a\\b.txt') |
---|
742 | n/a | self._check_str_subclass('\\\\some\\share') |
---|
743 | n/a | self._check_str_subclass('\\\\some\\share\\a') |
---|
744 | n/a | self._check_str_subclass('\\\\some\\share\\a\\b.txt') |
---|
745 | n/a | |
---|
746 | n/a | def test_eq(self): |
---|
747 | n/a | P = self.cls |
---|
748 | n/a | self.assertEqual(P('c:a/b'), P('c:a/b')) |
---|
749 | n/a | self.assertEqual(P('c:a/b'), P('c:', 'a', 'b')) |
---|
750 | n/a | self.assertNotEqual(P('c:a/b'), P('d:a/b')) |
---|
751 | n/a | self.assertNotEqual(P('c:a/b'), P('c:/a/b')) |
---|
752 | n/a | self.assertNotEqual(P('/a/b'), P('c:/a/b')) |
---|
753 | n/a | # Case-insensitivity |
---|
754 | n/a | self.assertEqual(P('a/B'), P('A/b')) |
---|
755 | n/a | self.assertEqual(P('C:a/B'), P('c:A/b')) |
---|
756 | n/a | self.assertEqual(P('//Some/SHARE/a/B'), P('//somE/share/A/b')) |
---|
757 | n/a | |
---|
758 | n/a | def test_as_uri(self): |
---|
759 | n/a | P = self.cls |
---|
760 | n/a | with self.assertRaises(ValueError): |
---|
761 | n/a | P('/a/b').as_uri() |
---|
762 | n/a | with self.assertRaises(ValueError): |
---|
763 | n/a | P('c:a/b').as_uri() |
---|
764 | n/a | self.assertEqual(P('c:/').as_uri(), 'file:///c:/') |
---|
765 | n/a | self.assertEqual(P('c:/a/b.c').as_uri(), 'file:///c:/a/b.c') |
---|
766 | n/a | self.assertEqual(P('c:/a/b%#c').as_uri(), 'file:///c:/a/b%25%23c') |
---|
767 | n/a | self.assertEqual(P('c:/a/b\xe9').as_uri(), 'file:///c:/a/b%C3%A9') |
---|
768 | n/a | self.assertEqual(P('//some/share/').as_uri(), 'file://some/share/') |
---|
769 | n/a | self.assertEqual(P('//some/share/a/b.c').as_uri(), |
---|
770 | n/a | 'file://some/share/a/b.c') |
---|
771 | n/a | self.assertEqual(P('//some/share/a/b%#c\xe9').as_uri(), |
---|
772 | n/a | 'file://some/share/a/b%25%23c%C3%A9') |
---|
773 | n/a | |
---|
774 | n/a | def test_match_common(self): |
---|
775 | n/a | P = self.cls |
---|
776 | n/a | # Absolute patterns |
---|
777 | n/a | self.assertTrue(P('c:/b.py').match('/*.py')) |
---|
778 | n/a | self.assertTrue(P('c:/b.py').match('c:*.py')) |
---|
779 | n/a | self.assertTrue(P('c:/b.py').match('c:/*.py')) |
---|
780 | n/a | self.assertFalse(P('d:/b.py').match('c:/*.py')) # wrong drive |
---|
781 | n/a | self.assertFalse(P('b.py').match('/*.py')) |
---|
782 | n/a | self.assertFalse(P('b.py').match('c:*.py')) |
---|
783 | n/a | self.assertFalse(P('b.py').match('c:/*.py')) |
---|
784 | n/a | self.assertFalse(P('c:b.py').match('/*.py')) |
---|
785 | n/a | self.assertFalse(P('c:b.py').match('c:/*.py')) |
---|
786 | n/a | self.assertFalse(P('/b.py').match('c:*.py')) |
---|
787 | n/a | self.assertFalse(P('/b.py').match('c:/*.py')) |
---|
788 | n/a | # UNC patterns |
---|
789 | n/a | self.assertTrue(P('//some/share/a.py').match('/*.py')) |
---|
790 | n/a | self.assertTrue(P('//some/share/a.py').match('//some/share/*.py')) |
---|
791 | n/a | self.assertFalse(P('//other/share/a.py').match('//some/share/*.py')) |
---|
792 | n/a | self.assertFalse(P('//some/share/a/b.py').match('//some/share/*.py')) |
---|
793 | n/a | # Case-insensitivity |
---|
794 | n/a | self.assertTrue(P('B.py').match('b.PY')) |
---|
795 | n/a | self.assertTrue(P('c:/a/B.Py').match('C:/A/*.pY')) |
---|
796 | n/a | self.assertTrue(P('//Some/Share/B.Py').match('//somE/sharE/*.pY')) |
---|
797 | n/a | |
---|
798 | n/a | def test_ordering_common(self): |
---|
799 | n/a | # Case-insensitivity |
---|
800 | n/a | def assertOrderedEqual(a, b): |
---|
801 | n/a | self.assertLessEqual(a, b) |
---|
802 | n/a | self.assertGreaterEqual(b, a) |
---|
803 | n/a | P = self.cls |
---|
804 | n/a | p = P('c:A/b') |
---|
805 | n/a | q = P('C:a/B') |
---|
806 | n/a | assertOrderedEqual(p, q) |
---|
807 | n/a | self.assertFalse(p < q) |
---|
808 | n/a | self.assertFalse(p > q) |
---|
809 | n/a | p = P('//some/Share/A/b') |
---|
810 | n/a | q = P('//Some/SHARE/a/B') |
---|
811 | n/a | assertOrderedEqual(p, q) |
---|
812 | n/a | self.assertFalse(p < q) |
---|
813 | n/a | self.assertFalse(p > q) |
---|
814 | n/a | |
---|
815 | n/a | def test_parts(self): |
---|
816 | n/a | P = self.cls |
---|
817 | n/a | p = P('c:a/b') |
---|
818 | n/a | parts = p.parts |
---|
819 | n/a | self.assertEqual(parts, ('c:', 'a', 'b')) |
---|
820 | n/a | p = P('c:/a/b') |
---|
821 | n/a | parts = p.parts |
---|
822 | n/a | self.assertEqual(parts, ('c:\\', 'a', 'b')) |
---|
823 | n/a | p = P('//a/b/c/d') |
---|
824 | n/a | parts = p.parts |
---|
825 | n/a | self.assertEqual(parts, ('\\\\a\\b\\', 'c', 'd')) |
---|
826 | n/a | |
---|
827 | n/a | def test_parent(self): |
---|
828 | n/a | # Anchored |
---|
829 | n/a | P = self.cls |
---|
830 | n/a | p = P('z:a/b/c') |
---|
831 | n/a | self.assertEqual(p.parent, P('z:a/b')) |
---|
832 | n/a | self.assertEqual(p.parent.parent, P('z:a')) |
---|
833 | n/a | self.assertEqual(p.parent.parent.parent, P('z:')) |
---|
834 | n/a | self.assertEqual(p.parent.parent.parent.parent, P('z:')) |
---|
835 | n/a | p = P('z:/a/b/c') |
---|
836 | n/a | self.assertEqual(p.parent, P('z:/a/b')) |
---|
837 | n/a | self.assertEqual(p.parent.parent, P('z:/a')) |
---|
838 | n/a | self.assertEqual(p.parent.parent.parent, P('z:/')) |
---|
839 | n/a | self.assertEqual(p.parent.parent.parent.parent, P('z:/')) |
---|
840 | n/a | p = P('//a/b/c/d') |
---|
841 | n/a | self.assertEqual(p.parent, P('//a/b/c')) |
---|
842 | n/a | self.assertEqual(p.parent.parent, P('//a/b')) |
---|
843 | n/a | self.assertEqual(p.parent.parent.parent, P('//a/b')) |
---|
844 | n/a | |
---|
845 | n/a | def test_parents(self): |
---|
846 | n/a | # Anchored |
---|
847 | n/a | P = self.cls |
---|
848 | n/a | p = P('z:a/b/') |
---|
849 | n/a | par = p.parents |
---|
850 | n/a | self.assertEqual(len(par), 2) |
---|
851 | n/a | self.assertEqual(par[0], P('z:a')) |
---|
852 | n/a | self.assertEqual(par[1], P('z:')) |
---|
853 | n/a | self.assertEqual(list(par), [P('z:a'), P('z:')]) |
---|
854 | n/a | with self.assertRaises(IndexError): |
---|
855 | n/a | par[2] |
---|
856 | n/a | p = P('z:/a/b/') |
---|
857 | n/a | par = p.parents |
---|
858 | n/a | self.assertEqual(len(par), 2) |
---|
859 | n/a | self.assertEqual(par[0], P('z:/a')) |
---|
860 | n/a | self.assertEqual(par[1], P('z:/')) |
---|
861 | n/a | self.assertEqual(list(par), [P('z:/a'), P('z:/')]) |
---|
862 | n/a | with self.assertRaises(IndexError): |
---|
863 | n/a | par[2] |
---|
864 | n/a | p = P('//a/b/c/d') |
---|
865 | n/a | par = p.parents |
---|
866 | n/a | self.assertEqual(len(par), 2) |
---|
867 | n/a | self.assertEqual(par[0], P('//a/b/c')) |
---|
868 | n/a | self.assertEqual(par[1], P('//a/b')) |
---|
869 | n/a | self.assertEqual(list(par), [P('//a/b/c'), P('//a/b')]) |
---|
870 | n/a | with self.assertRaises(IndexError): |
---|
871 | n/a | par[2] |
---|
872 | n/a | |
---|
873 | n/a | def test_drive(self): |
---|
874 | n/a | P = self.cls |
---|
875 | n/a | self.assertEqual(P('c:').drive, 'c:') |
---|
876 | n/a | self.assertEqual(P('c:a/b').drive, 'c:') |
---|
877 | n/a | self.assertEqual(P('c:/').drive, 'c:') |
---|
878 | n/a | self.assertEqual(P('c:/a/b/').drive, 'c:') |
---|
879 | n/a | self.assertEqual(P('//a/b').drive, '\\\\a\\b') |
---|
880 | n/a | self.assertEqual(P('//a/b/').drive, '\\\\a\\b') |
---|
881 | n/a | self.assertEqual(P('//a/b/c/d').drive, '\\\\a\\b') |
---|
882 | n/a | |
---|
883 | n/a | def test_root(self): |
---|
884 | n/a | P = self.cls |
---|
885 | n/a | self.assertEqual(P('c:').root, '') |
---|
886 | n/a | self.assertEqual(P('c:a/b').root, '') |
---|
887 | n/a | self.assertEqual(P('c:/').root, '\\') |
---|
888 | n/a | self.assertEqual(P('c:/a/b/').root, '\\') |
---|
889 | n/a | self.assertEqual(P('//a/b').root, '\\') |
---|
890 | n/a | self.assertEqual(P('//a/b/').root, '\\') |
---|
891 | n/a | self.assertEqual(P('//a/b/c/d').root, '\\') |
---|
892 | n/a | |
---|
893 | n/a | def test_anchor(self): |
---|
894 | n/a | P = self.cls |
---|
895 | n/a | self.assertEqual(P('c:').anchor, 'c:') |
---|
896 | n/a | self.assertEqual(P('c:a/b').anchor, 'c:') |
---|
897 | n/a | self.assertEqual(P('c:/').anchor, 'c:\\') |
---|
898 | n/a | self.assertEqual(P('c:/a/b/').anchor, 'c:\\') |
---|
899 | n/a | self.assertEqual(P('//a/b').anchor, '\\\\a\\b\\') |
---|
900 | n/a | self.assertEqual(P('//a/b/').anchor, '\\\\a\\b\\') |
---|
901 | n/a | self.assertEqual(P('//a/b/c/d').anchor, '\\\\a\\b\\') |
---|
902 | n/a | |
---|
903 | n/a | def test_name(self): |
---|
904 | n/a | P = self.cls |
---|
905 | n/a | self.assertEqual(P('c:').name, '') |
---|
906 | n/a | self.assertEqual(P('c:/').name, '') |
---|
907 | n/a | self.assertEqual(P('c:a/b').name, 'b') |
---|
908 | n/a | self.assertEqual(P('c:/a/b').name, 'b') |
---|
909 | n/a | self.assertEqual(P('c:a/b.py').name, 'b.py') |
---|
910 | n/a | self.assertEqual(P('c:/a/b.py').name, 'b.py') |
---|
911 | n/a | self.assertEqual(P('//My.py/Share.php').name, '') |
---|
912 | n/a | self.assertEqual(P('//My.py/Share.php/a/b').name, 'b') |
---|
913 | n/a | |
---|
914 | n/a | def test_suffix(self): |
---|
915 | n/a | P = self.cls |
---|
916 | n/a | self.assertEqual(P('c:').suffix, '') |
---|
917 | n/a | self.assertEqual(P('c:/').suffix, '') |
---|
918 | n/a | self.assertEqual(P('c:a/b').suffix, '') |
---|
919 | n/a | self.assertEqual(P('c:/a/b').suffix, '') |
---|
920 | n/a | self.assertEqual(P('c:a/b.py').suffix, '.py') |
---|
921 | n/a | self.assertEqual(P('c:/a/b.py').suffix, '.py') |
---|
922 | n/a | self.assertEqual(P('c:a/.hgrc').suffix, '') |
---|
923 | n/a | self.assertEqual(P('c:/a/.hgrc').suffix, '') |
---|
924 | n/a | self.assertEqual(P('c:a/.hg.rc').suffix, '.rc') |
---|
925 | n/a | self.assertEqual(P('c:/a/.hg.rc').suffix, '.rc') |
---|
926 | n/a | self.assertEqual(P('c:a/b.tar.gz').suffix, '.gz') |
---|
927 | n/a | self.assertEqual(P('c:/a/b.tar.gz').suffix, '.gz') |
---|
928 | n/a | self.assertEqual(P('c:a/Some name. Ending with a dot.').suffix, '') |
---|
929 | n/a | self.assertEqual(P('c:/a/Some name. Ending with a dot.').suffix, '') |
---|
930 | n/a | self.assertEqual(P('//My.py/Share.php').suffix, '') |
---|
931 | n/a | self.assertEqual(P('//My.py/Share.php/a/b').suffix, '') |
---|
932 | n/a | |
---|
933 | n/a | def test_suffixes(self): |
---|
934 | n/a | P = self.cls |
---|
935 | n/a | self.assertEqual(P('c:').suffixes, []) |
---|
936 | n/a | self.assertEqual(P('c:/').suffixes, []) |
---|
937 | n/a | self.assertEqual(P('c:a/b').suffixes, []) |
---|
938 | n/a | self.assertEqual(P('c:/a/b').suffixes, []) |
---|
939 | n/a | self.assertEqual(P('c:a/b.py').suffixes, ['.py']) |
---|
940 | n/a | self.assertEqual(P('c:/a/b.py').suffixes, ['.py']) |
---|
941 | n/a | self.assertEqual(P('c:a/.hgrc').suffixes, []) |
---|
942 | n/a | self.assertEqual(P('c:/a/.hgrc').suffixes, []) |
---|
943 | n/a | self.assertEqual(P('c:a/.hg.rc').suffixes, ['.rc']) |
---|
944 | n/a | self.assertEqual(P('c:/a/.hg.rc').suffixes, ['.rc']) |
---|
945 | n/a | self.assertEqual(P('c:a/b.tar.gz').suffixes, ['.tar', '.gz']) |
---|
946 | n/a | self.assertEqual(P('c:/a/b.tar.gz').suffixes, ['.tar', '.gz']) |
---|
947 | n/a | self.assertEqual(P('//My.py/Share.php').suffixes, []) |
---|
948 | n/a | self.assertEqual(P('//My.py/Share.php/a/b').suffixes, []) |
---|
949 | n/a | self.assertEqual(P('c:a/Some name. Ending with a dot.').suffixes, []) |
---|
950 | n/a | self.assertEqual(P('c:/a/Some name. Ending with a dot.').suffixes, []) |
---|
951 | n/a | |
---|
952 | n/a | def test_stem(self): |
---|
953 | n/a | P = self.cls |
---|
954 | n/a | self.assertEqual(P('c:').stem, '') |
---|
955 | n/a | self.assertEqual(P('c:.').stem, '') |
---|
956 | n/a | self.assertEqual(P('c:..').stem, '..') |
---|
957 | n/a | self.assertEqual(P('c:/').stem, '') |
---|
958 | n/a | self.assertEqual(P('c:a/b').stem, 'b') |
---|
959 | n/a | self.assertEqual(P('c:a/b.py').stem, 'b') |
---|
960 | n/a | self.assertEqual(P('c:a/.hgrc').stem, '.hgrc') |
---|
961 | n/a | self.assertEqual(P('c:a/.hg.rc').stem, '.hg') |
---|
962 | n/a | self.assertEqual(P('c:a/b.tar.gz').stem, 'b.tar') |
---|
963 | n/a | self.assertEqual(P('c:a/Some name. Ending with a dot.').stem, |
---|
964 | n/a | 'Some name. Ending with a dot.') |
---|
965 | n/a | |
---|
966 | n/a | def test_with_name(self): |
---|
967 | n/a | P = self.cls |
---|
968 | n/a | self.assertEqual(P('c:a/b').with_name('d.xml'), P('c:a/d.xml')) |
---|
969 | n/a | self.assertEqual(P('c:/a/b').with_name('d.xml'), P('c:/a/d.xml')) |
---|
970 | n/a | self.assertEqual(P('c:a/Dot ending.').with_name('d.xml'), P('c:a/d.xml')) |
---|
971 | n/a | self.assertEqual(P('c:/a/Dot ending.').with_name('d.xml'), P('c:/a/d.xml')) |
---|
972 | n/a | self.assertRaises(ValueError, P('c:').with_name, 'd.xml') |
---|
973 | n/a | self.assertRaises(ValueError, P('c:/').with_name, 'd.xml') |
---|
974 | n/a | self.assertRaises(ValueError, P('//My/Share').with_name, 'd.xml') |
---|
975 | n/a | self.assertRaises(ValueError, P('c:a/b').with_name, 'd:') |
---|
976 | n/a | self.assertRaises(ValueError, P('c:a/b').with_name, 'd:e') |
---|
977 | n/a | self.assertRaises(ValueError, P('c:a/b').with_name, 'd:/e') |
---|
978 | n/a | self.assertRaises(ValueError, P('c:a/b').with_name, '//My/Share') |
---|
979 | n/a | |
---|
980 | n/a | def test_with_suffix(self): |
---|
981 | n/a | P = self.cls |
---|
982 | n/a | self.assertEqual(P('c:a/b').with_suffix('.gz'), P('c:a/b.gz')) |
---|
983 | n/a | self.assertEqual(P('c:/a/b').with_suffix('.gz'), P('c:/a/b.gz')) |
---|
984 | n/a | self.assertEqual(P('c:a/b.py').with_suffix('.gz'), P('c:a/b.gz')) |
---|
985 | n/a | self.assertEqual(P('c:/a/b.py').with_suffix('.gz'), P('c:/a/b.gz')) |
---|
986 | n/a | # Path doesn't have a "filename" component |
---|
987 | n/a | self.assertRaises(ValueError, P('').with_suffix, '.gz') |
---|
988 | n/a | self.assertRaises(ValueError, P('.').with_suffix, '.gz') |
---|
989 | n/a | self.assertRaises(ValueError, P('/').with_suffix, '.gz') |
---|
990 | n/a | self.assertRaises(ValueError, P('//My/Share').with_suffix, '.gz') |
---|
991 | n/a | # Invalid suffix |
---|
992 | n/a | self.assertRaises(ValueError, P('c:a/b').with_suffix, 'gz') |
---|
993 | n/a | self.assertRaises(ValueError, P('c:a/b').with_suffix, '/') |
---|
994 | n/a | self.assertRaises(ValueError, P('c:a/b').with_suffix, '\\') |
---|
995 | n/a | self.assertRaises(ValueError, P('c:a/b').with_suffix, 'c:') |
---|
996 | n/a | self.assertRaises(ValueError, P('c:a/b').with_suffix, '/.gz') |
---|
997 | n/a | self.assertRaises(ValueError, P('c:a/b').with_suffix, '\\.gz') |
---|
998 | n/a | self.assertRaises(ValueError, P('c:a/b').with_suffix, 'c:.gz') |
---|
999 | n/a | self.assertRaises(ValueError, P('c:a/b').with_suffix, 'c/d') |
---|
1000 | n/a | self.assertRaises(ValueError, P('c:a/b').with_suffix, 'c\\d') |
---|
1001 | n/a | self.assertRaises(ValueError, P('c:a/b').with_suffix, '.c/d') |
---|
1002 | n/a | self.assertRaises(ValueError, P('c:a/b').with_suffix, '.c\\d') |
---|
1003 | n/a | |
---|
1004 | n/a | def test_relative_to(self): |
---|
1005 | n/a | P = self.cls |
---|
1006 | n/a | p = P('C:Foo/Bar') |
---|
1007 | n/a | self.assertEqual(p.relative_to(P('c:')), P('Foo/Bar')) |
---|
1008 | n/a | self.assertEqual(p.relative_to('c:'), P('Foo/Bar')) |
---|
1009 | n/a | self.assertEqual(p.relative_to(P('c:foO')), P('Bar')) |
---|
1010 | n/a | self.assertEqual(p.relative_to('c:foO'), P('Bar')) |
---|
1011 | n/a | self.assertEqual(p.relative_to('c:foO/'), P('Bar')) |
---|
1012 | n/a | self.assertEqual(p.relative_to(P('c:foO/baR')), P()) |
---|
1013 | n/a | self.assertEqual(p.relative_to('c:foO/baR'), P()) |
---|
1014 | n/a | # Unrelated paths |
---|
1015 | n/a | self.assertRaises(ValueError, p.relative_to, P()) |
---|
1016 | n/a | self.assertRaises(ValueError, p.relative_to, '') |
---|
1017 | n/a | self.assertRaises(ValueError, p.relative_to, P('d:')) |
---|
1018 | n/a | self.assertRaises(ValueError, p.relative_to, P('/')) |
---|
1019 | n/a | self.assertRaises(ValueError, p.relative_to, P('Foo')) |
---|
1020 | n/a | self.assertRaises(ValueError, p.relative_to, P('/Foo')) |
---|
1021 | n/a | self.assertRaises(ValueError, p.relative_to, P('C:/Foo')) |
---|
1022 | n/a | self.assertRaises(ValueError, p.relative_to, P('C:Foo/Bar/Baz')) |
---|
1023 | n/a | self.assertRaises(ValueError, p.relative_to, P('C:Foo/Baz')) |
---|
1024 | n/a | p = P('C:/Foo/Bar') |
---|
1025 | n/a | self.assertEqual(p.relative_to(P('c:')), P('/Foo/Bar')) |
---|
1026 | n/a | self.assertEqual(p.relative_to('c:'), P('/Foo/Bar')) |
---|
1027 | n/a | self.assertEqual(str(p.relative_to(P('c:'))), '\\Foo\\Bar') |
---|
1028 | n/a | self.assertEqual(str(p.relative_to('c:')), '\\Foo\\Bar') |
---|
1029 | n/a | self.assertEqual(p.relative_to(P('c:/')), P('Foo/Bar')) |
---|
1030 | n/a | self.assertEqual(p.relative_to('c:/'), P('Foo/Bar')) |
---|
1031 | n/a | self.assertEqual(p.relative_to(P('c:/foO')), P('Bar')) |
---|
1032 | n/a | self.assertEqual(p.relative_to('c:/foO'), P('Bar')) |
---|
1033 | n/a | self.assertEqual(p.relative_to('c:/foO/'), P('Bar')) |
---|
1034 | n/a | self.assertEqual(p.relative_to(P('c:/foO/baR')), P()) |
---|
1035 | n/a | self.assertEqual(p.relative_to('c:/foO/baR'), P()) |
---|
1036 | n/a | # Unrelated paths |
---|
1037 | n/a | self.assertRaises(ValueError, p.relative_to, P('C:/Baz')) |
---|
1038 | n/a | self.assertRaises(ValueError, p.relative_to, P('C:/Foo/Bar/Baz')) |
---|
1039 | n/a | self.assertRaises(ValueError, p.relative_to, P('C:/Foo/Baz')) |
---|
1040 | n/a | self.assertRaises(ValueError, p.relative_to, P('C:Foo')) |
---|
1041 | n/a | self.assertRaises(ValueError, p.relative_to, P('d:')) |
---|
1042 | n/a | self.assertRaises(ValueError, p.relative_to, P('d:/')) |
---|
1043 | n/a | self.assertRaises(ValueError, p.relative_to, P('/')) |
---|
1044 | n/a | self.assertRaises(ValueError, p.relative_to, P('/Foo')) |
---|
1045 | n/a | self.assertRaises(ValueError, p.relative_to, P('//C/Foo')) |
---|
1046 | n/a | # UNC paths |
---|
1047 | n/a | p = P('//Server/Share/Foo/Bar') |
---|
1048 | n/a | self.assertEqual(p.relative_to(P('//sErver/sHare')), P('Foo/Bar')) |
---|
1049 | n/a | self.assertEqual(p.relative_to('//sErver/sHare'), P('Foo/Bar')) |
---|
1050 | n/a | self.assertEqual(p.relative_to('//sErver/sHare/'), P('Foo/Bar')) |
---|
1051 | n/a | self.assertEqual(p.relative_to(P('//sErver/sHare/Foo')), P('Bar')) |
---|
1052 | n/a | self.assertEqual(p.relative_to('//sErver/sHare/Foo'), P('Bar')) |
---|
1053 | n/a | self.assertEqual(p.relative_to('//sErver/sHare/Foo/'), P('Bar')) |
---|
1054 | n/a | self.assertEqual(p.relative_to(P('//sErver/sHare/Foo/Bar')), P()) |
---|
1055 | n/a | self.assertEqual(p.relative_to('//sErver/sHare/Foo/Bar'), P()) |
---|
1056 | n/a | # Unrelated paths |
---|
1057 | n/a | self.assertRaises(ValueError, p.relative_to, P('/Server/Share/Foo')) |
---|
1058 | n/a | self.assertRaises(ValueError, p.relative_to, P('c:/Server/Share/Foo')) |
---|
1059 | n/a | self.assertRaises(ValueError, p.relative_to, P('//z/Share/Foo')) |
---|
1060 | n/a | self.assertRaises(ValueError, p.relative_to, P('//Server/z/Foo')) |
---|
1061 | n/a | |
---|
1062 | n/a | def test_is_absolute(self): |
---|
1063 | n/a | P = self.cls |
---|
1064 | n/a | # Under NT, only paths with both a drive and a root are absolute |
---|
1065 | n/a | self.assertFalse(P().is_absolute()) |
---|
1066 | n/a | self.assertFalse(P('a').is_absolute()) |
---|
1067 | n/a | self.assertFalse(P('a/b/').is_absolute()) |
---|
1068 | n/a | self.assertFalse(P('/').is_absolute()) |
---|
1069 | n/a | self.assertFalse(P('/a').is_absolute()) |
---|
1070 | n/a | self.assertFalse(P('/a/b/').is_absolute()) |
---|
1071 | n/a | self.assertFalse(P('c:').is_absolute()) |
---|
1072 | n/a | self.assertFalse(P('c:a').is_absolute()) |
---|
1073 | n/a | self.assertFalse(P('c:a/b/').is_absolute()) |
---|
1074 | n/a | self.assertTrue(P('c:/').is_absolute()) |
---|
1075 | n/a | self.assertTrue(P('c:/a').is_absolute()) |
---|
1076 | n/a | self.assertTrue(P('c:/a/b/').is_absolute()) |
---|
1077 | n/a | # UNC paths are absolute by definition |
---|
1078 | n/a | self.assertTrue(P('//a/b').is_absolute()) |
---|
1079 | n/a | self.assertTrue(P('//a/b/').is_absolute()) |
---|
1080 | n/a | self.assertTrue(P('//a/b/c').is_absolute()) |
---|
1081 | n/a | self.assertTrue(P('//a/b/c/d').is_absolute()) |
---|
1082 | n/a | |
---|
1083 | n/a | def test_join(self): |
---|
1084 | n/a | P = self.cls |
---|
1085 | n/a | p = P('C:/a/b') |
---|
1086 | n/a | pp = p.joinpath('x/y') |
---|
1087 | n/a | self.assertEqual(pp, P('C:/a/b/x/y')) |
---|
1088 | n/a | pp = p.joinpath('/x/y') |
---|
1089 | n/a | self.assertEqual(pp, P('C:/x/y')) |
---|
1090 | n/a | # Joining with a different drive => the first path is ignored, even |
---|
1091 | n/a | # if the second path is relative. |
---|
1092 | n/a | pp = p.joinpath('D:x/y') |
---|
1093 | n/a | self.assertEqual(pp, P('D:x/y')) |
---|
1094 | n/a | pp = p.joinpath('D:/x/y') |
---|
1095 | n/a | self.assertEqual(pp, P('D:/x/y')) |
---|
1096 | n/a | pp = p.joinpath('//host/share/x/y') |
---|
1097 | n/a | self.assertEqual(pp, P('//host/share/x/y')) |
---|
1098 | n/a | # Joining with the same drive => the first path is appended to if |
---|
1099 | n/a | # the second path is relative. |
---|
1100 | n/a | pp = p.joinpath('c:x/y') |
---|
1101 | n/a | self.assertEqual(pp, P('C:/a/b/x/y')) |
---|
1102 | n/a | pp = p.joinpath('c:/x/y') |
---|
1103 | n/a | self.assertEqual(pp, P('C:/x/y')) |
---|
1104 | n/a | |
---|
1105 | n/a | def test_div(self): |
---|
1106 | n/a | # Basically the same as joinpath() |
---|
1107 | n/a | P = self.cls |
---|
1108 | n/a | p = P('C:/a/b') |
---|
1109 | n/a | self.assertEqual(p / 'x/y', P('C:/a/b/x/y')) |
---|
1110 | n/a | self.assertEqual(p / 'x' / 'y', P('C:/a/b/x/y')) |
---|
1111 | n/a | self.assertEqual(p / '/x/y', P('C:/x/y')) |
---|
1112 | n/a | self.assertEqual(p / '/x' / 'y', P('C:/x/y')) |
---|
1113 | n/a | # Joining with a different drive => the first path is ignored, even |
---|
1114 | n/a | # if the second path is relative. |
---|
1115 | n/a | self.assertEqual(p / 'D:x/y', P('D:x/y')) |
---|
1116 | n/a | self.assertEqual(p / 'D:' / 'x/y', P('D:x/y')) |
---|
1117 | n/a | self.assertEqual(p / 'D:/x/y', P('D:/x/y')) |
---|
1118 | n/a | self.assertEqual(p / 'D:' / '/x/y', P('D:/x/y')) |
---|
1119 | n/a | self.assertEqual(p / '//host/share/x/y', P('//host/share/x/y')) |
---|
1120 | n/a | # Joining with the same drive => the first path is appended to if |
---|
1121 | n/a | # the second path is relative. |
---|
1122 | n/a | self.assertEqual(p / 'c:x/y', P('C:/a/b/x/y')) |
---|
1123 | n/a | self.assertEqual(p / 'c:/x/y', P('C:/x/y')) |
---|
1124 | n/a | |
---|
1125 | n/a | def test_is_reserved(self): |
---|
1126 | n/a | P = self.cls |
---|
1127 | n/a | self.assertIs(False, P('').is_reserved()) |
---|
1128 | n/a | self.assertIs(False, P('/').is_reserved()) |
---|
1129 | n/a | self.assertIs(False, P('/foo/bar').is_reserved()) |
---|
1130 | n/a | self.assertIs(True, P('con').is_reserved()) |
---|
1131 | n/a | self.assertIs(True, P('NUL').is_reserved()) |
---|
1132 | n/a | self.assertIs(True, P('NUL.txt').is_reserved()) |
---|
1133 | n/a | self.assertIs(True, P('com1').is_reserved()) |
---|
1134 | n/a | self.assertIs(True, P('com9.bar').is_reserved()) |
---|
1135 | n/a | self.assertIs(False, P('bar.com9').is_reserved()) |
---|
1136 | n/a | self.assertIs(True, P('lpt1').is_reserved()) |
---|
1137 | n/a | self.assertIs(True, P('lpt9.bar').is_reserved()) |
---|
1138 | n/a | self.assertIs(False, P('bar.lpt9').is_reserved()) |
---|
1139 | n/a | # Only the last component matters |
---|
1140 | n/a | self.assertIs(False, P('c:/NUL/con/baz').is_reserved()) |
---|
1141 | n/a | # UNC paths are never reserved |
---|
1142 | n/a | self.assertIs(False, P('//my/share/nul/con/aux').is_reserved()) |
---|
1143 | n/a | |
---|
1144 | n/a | class PurePathTest(_BasePurePathTest, unittest.TestCase): |
---|
1145 | n/a | cls = pathlib.PurePath |
---|
1146 | n/a | |
---|
1147 | n/a | def test_concrete_class(self): |
---|
1148 | n/a | p = self.cls('a') |
---|
1149 | n/a | self.assertIs(type(p), |
---|
1150 | n/a | pathlib.PureWindowsPath if os.name == 'nt' else pathlib.PurePosixPath) |
---|
1151 | n/a | |
---|
1152 | n/a | def test_different_flavours_unequal(self): |
---|
1153 | n/a | p = pathlib.PurePosixPath('a') |
---|
1154 | n/a | q = pathlib.PureWindowsPath('a') |
---|
1155 | n/a | self.assertNotEqual(p, q) |
---|
1156 | n/a | |
---|
1157 | n/a | def test_different_flavours_unordered(self): |
---|
1158 | n/a | p = pathlib.PurePosixPath('a') |
---|
1159 | n/a | q = pathlib.PureWindowsPath('a') |
---|
1160 | n/a | with self.assertRaises(TypeError): |
---|
1161 | n/a | p < q |
---|
1162 | n/a | with self.assertRaises(TypeError): |
---|
1163 | n/a | p <= q |
---|
1164 | n/a | with self.assertRaises(TypeError): |
---|
1165 | n/a | p > q |
---|
1166 | n/a | with self.assertRaises(TypeError): |
---|
1167 | n/a | p >= q |
---|
1168 | n/a | |
---|
1169 | n/a | |
---|
1170 | n/a | # |
---|
1171 | n/a | # Tests for the concrete classes |
---|
1172 | n/a | # |
---|
1173 | n/a | |
---|
1174 | n/a | # Make sure any symbolic links in the base test path are resolved |
---|
1175 | n/a | BASE = os.path.realpath(TESTFN) |
---|
1176 | n/a | join = lambda *x: os.path.join(BASE, *x) |
---|
1177 | n/a | rel_join = lambda *x: os.path.join(TESTFN, *x) |
---|
1178 | n/a | |
---|
1179 | n/a | def symlink_skip_reason(): |
---|
1180 | n/a | if not pathlib.supports_symlinks: |
---|
1181 | n/a | return "no system support for symlinks" |
---|
1182 | n/a | try: |
---|
1183 | n/a | os.symlink(__file__, BASE) |
---|
1184 | n/a | except OSError as e: |
---|
1185 | n/a | return str(e) |
---|
1186 | n/a | else: |
---|
1187 | n/a | support.unlink(BASE) |
---|
1188 | n/a | return None |
---|
1189 | n/a | |
---|
1190 | n/a | symlink_skip_reason = symlink_skip_reason() |
---|
1191 | n/a | |
---|
1192 | n/a | only_nt = unittest.skipIf(os.name != 'nt', |
---|
1193 | n/a | 'test requires a Windows-compatible system') |
---|
1194 | n/a | only_posix = unittest.skipIf(os.name == 'nt', |
---|
1195 | n/a | 'test requires a POSIX-compatible system') |
---|
1196 | n/a | with_symlinks = unittest.skipIf(symlink_skip_reason, symlink_skip_reason) |
---|
1197 | n/a | |
---|
1198 | n/a | |
---|
1199 | n/a | @only_posix |
---|
1200 | n/a | class PosixPathAsPureTest(PurePosixPathTest): |
---|
1201 | n/a | cls = pathlib.PosixPath |
---|
1202 | n/a | |
---|
1203 | n/a | @only_nt |
---|
1204 | n/a | class WindowsPathAsPureTest(PureWindowsPathTest): |
---|
1205 | n/a | cls = pathlib.WindowsPath |
---|
1206 | n/a | |
---|
1207 | n/a | def test_owner(self): |
---|
1208 | n/a | P = self.cls |
---|
1209 | n/a | with self.assertRaises(NotImplementedError): |
---|
1210 | n/a | P('c:/').owner() |
---|
1211 | n/a | |
---|
1212 | n/a | def test_group(self): |
---|
1213 | n/a | P = self.cls |
---|
1214 | n/a | with self.assertRaises(NotImplementedError): |
---|
1215 | n/a | P('c:/').group() |
---|
1216 | n/a | |
---|
1217 | n/a | |
---|
1218 | n/a | class _BasePathTest(object): |
---|
1219 | n/a | """Tests for the FS-accessing functionalities of the Path classes.""" |
---|
1220 | n/a | |
---|
1221 | n/a | # (BASE) |
---|
1222 | n/a | # | |
---|
1223 | n/a | # |-- brokenLink -> non-existing |
---|
1224 | n/a | # |-- dirA |
---|
1225 | n/a | # | `-- linkC -> ../dirB |
---|
1226 | n/a | # |-- dirB |
---|
1227 | n/a | # | |-- fileB |
---|
1228 | n/a | # | `-- linkD -> ../dirB |
---|
1229 | n/a | # |-- dirC |
---|
1230 | n/a | # | |-- dirD |
---|
1231 | n/a | # | | `-- fileD |
---|
1232 | n/a | # | `-- fileC |
---|
1233 | n/a | # |-- dirE # No permissions |
---|
1234 | n/a | # |-- fileA |
---|
1235 | n/a | # |-- linkA -> fileA |
---|
1236 | n/a | # `-- linkB -> dirB |
---|
1237 | n/a | # |
---|
1238 | n/a | |
---|
1239 | n/a | def setUp(self): |
---|
1240 | n/a | def cleanup(): |
---|
1241 | n/a | os.chmod(join('dirE'), 0o777) |
---|
1242 | n/a | support.rmtree(BASE) |
---|
1243 | n/a | self.addCleanup(cleanup) |
---|
1244 | n/a | os.mkdir(BASE) |
---|
1245 | n/a | os.mkdir(join('dirA')) |
---|
1246 | n/a | os.mkdir(join('dirB')) |
---|
1247 | n/a | os.mkdir(join('dirC')) |
---|
1248 | n/a | os.mkdir(join('dirC', 'dirD')) |
---|
1249 | n/a | os.mkdir(join('dirE')) |
---|
1250 | n/a | with open(join('fileA'), 'wb') as f: |
---|
1251 | n/a | f.write(b"this is file A\n") |
---|
1252 | n/a | with open(join('dirB', 'fileB'), 'wb') as f: |
---|
1253 | n/a | f.write(b"this is file B\n") |
---|
1254 | n/a | with open(join('dirC', 'fileC'), 'wb') as f: |
---|
1255 | n/a | f.write(b"this is file C\n") |
---|
1256 | n/a | with open(join('dirC', 'dirD', 'fileD'), 'wb') as f: |
---|
1257 | n/a | f.write(b"this is file D\n") |
---|
1258 | n/a | os.chmod(join('dirE'), 0) |
---|
1259 | n/a | if not symlink_skip_reason: |
---|
1260 | n/a | # Relative symlinks |
---|
1261 | n/a | os.symlink('fileA', join('linkA')) |
---|
1262 | n/a | os.symlink('non-existing', join('brokenLink')) |
---|
1263 | n/a | self.dirlink('dirB', join('linkB')) |
---|
1264 | n/a | self.dirlink(os.path.join('..', 'dirB'), join('dirA', 'linkC')) |
---|
1265 | n/a | # This one goes upwards, creating a loop |
---|
1266 | n/a | self.dirlink(os.path.join('..', 'dirB'), join('dirB', 'linkD')) |
---|
1267 | n/a | |
---|
1268 | n/a | if os.name == 'nt': |
---|
1269 | n/a | # Workaround for http://bugs.python.org/issue13772 |
---|
1270 | n/a | def dirlink(self, src, dest): |
---|
1271 | n/a | os.symlink(src, dest, target_is_directory=True) |
---|
1272 | n/a | else: |
---|
1273 | n/a | def dirlink(self, src, dest): |
---|
1274 | n/a | os.symlink(src, dest) |
---|
1275 | n/a | |
---|
1276 | n/a | def assertSame(self, path_a, path_b): |
---|
1277 | n/a | self.assertTrue(os.path.samefile(str(path_a), str(path_b)), |
---|
1278 | n/a | "%r and %r don't point to the same file" % |
---|
1279 | n/a | (path_a, path_b)) |
---|
1280 | n/a | |
---|
1281 | n/a | def assertFileNotFound(self, func, *args, **kwargs): |
---|
1282 | n/a | with self.assertRaises(FileNotFoundError) as cm: |
---|
1283 | n/a | func(*args, **kwargs) |
---|
1284 | n/a | self.assertEqual(cm.exception.errno, errno.ENOENT) |
---|
1285 | n/a | |
---|
1286 | n/a | def _test_cwd(self, p): |
---|
1287 | n/a | q = self.cls(os.getcwd()) |
---|
1288 | n/a | self.assertEqual(p, q) |
---|
1289 | n/a | self.assertEqual(str(p), str(q)) |
---|
1290 | n/a | self.assertIs(type(p), type(q)) |
---|
1291 | n/a | self.assertTrue(p.is_absolute()) |
---|
1292 | n/a | |
---|
1293 | n/a | def test_cwd(self): |
---|
1294 | n/a | p = self.cls.cwd() |
---|
1295 | n/a | self._test_cwd(p) |
---|
1296 | n/a | |
---|
1297 | n/a | def _test_home(self, p): |
---|
1298 | n/a | q = self.cls(os.path.expanduser('~')) |
---|
1299 | n/a | self.assertEqual(p, q) |
---|
1300 | n/a | self.assertEqual(str(p), str(q)) |
---|
1301 | n/a | self.assertIs(type(p), type(q)) |
---|
1302 | n/a | self.assertTrue(p.is_absolute()) |
---|
1303 | n/a | |
---|
1304 | n/a | def test_home(self): |
---|
1305 | n/a | p = self.cls.home() |
---|
1306 | n/a | self._test_home(p) |
---|
1307 | n/a | |
---|
1308 | n/a | def test_samefile(self): |
---|
1309 | n/a | fileA_path = os.path.join(BASE, 'fileA') |
---|
1310 | n/a | fileB_path = os.path.join(BASE, 'dirB', 'fileB') |
---|
1311 | n/a | p = self.cls(fileA_path) |
---|
1312 | n/a | pp = self.cls(fileA_path) |
---|
1313 | n/a | q = self.cls(fileB_path) |
---|
1314 | n/a | self.assertTrue(p.samefile(fileA_path)) |
---|
1315 | n/a | self.assertTrue(p.samefile(pp)) |
---|
1316 | n/a | self.assertFalse(p.samefile(fileB_path)) |
---|
1317 | n/a | self.assertFalse(p.samefile(q)) |
---|
1318 | n/a | # Test the non-existent file case |
---|
1319 | n/a | non_existent = os.path.join(BASE, 'foo') |
---|
1320 | n/a | r = self.cls(non_existent) |
---|
1321 | n/a | self.assertRaises(FileNotFoundError, p.samefile, r) |
---|
1322 | n/a | self.assertRaises(FileNotFoundError, p.samefile, non_existent) |
---|
1323 | n/a | self.assertRaises(FileNotFoundError, r.samefile, p) |
---|
1324 | n/a | self.assertRaises(FileNotFoundError, r.samefile, non_existent) |
---|
1325 | n/a | self.assertRaises(FileNotFoundError, r.samefile, r) |
---|
1326 | n/a | self.assertRaises(FileNotFoundError, r.samefile, non_existent) |
---|
1327 | n/a | |
---|
1328 | n/a | def test_empty_path(self): |
---|
1329 | n/a | # The empty path points to '.' |
---|
1330 | n/a | p = self.cls('') |
---|
1331 | n/a | self.assertEqual(p.stat(), os.stat('.')) |
---|
1332 | n/a | |
---|
1333 | n/a | def test_expanduser_common(self): |
---|
1334 | n/a | P = self.cls |
---|
1335 | n/a | p = P('~') |
---|
1336 | n/a | self.assertEqual(p.expanduser(), P(os.path.expanduser('~'))) |
---|
1337 | n/a | p = P('foo') |
---|
1338 | n/a | self.assertEqual(p.expanduser(), p) |
---|
1339 | n/a | p = P('/~') |
---|
1340 | n/a | self.assertEqual(p.expanduser(), p) |
---|
1341 | n/a | p = P('../~') |
---|
1342 | n/a | self.assertEqual(p.expanduser(), p) |
---|
1343 | n/a | p = P(P('').absolute().anchor) / '~' |
---|
1344 | n/a | self.assertEqual(p.expanduser(), p) |
---|
1345 | n/a | |
---|
1346 | n/a | def test_exists(self): |
---|
1347 | n/a | P = self.cls |
---|
1348 | n/a | p = P(BASE) |
---|
1349 | n/a | self.assertIs(True, p.exists()) |
---|
1350 | n/a | self.assertIs(True, (p / 'dirA').exists()) |
---|
1351 | n/a | self.assertIs(True, (p / 'fileA').exists()) |
---|
1352 | n/a | self.assertIs(False, (p / 'fileA' / 'bah').exists()) |
---|
1353 | n/a | if not symlink_skip_reason: |
---|
1354 | n/a | self.assertIs(True, (p / 'linkA').exists()) |
---|
1355 | n/a | self.assertIs(True, (p / 'linkB').exists()) |
---|
1356 | n/a | self.assertIs(True, (p / 'linkB' / 'fileB').exists()) |
---|
1357 | n/a | self.assertIs(False, (p / 'linkA' / 'bah').exists()) |
---|
1358 | n/a | self.assertIs(False, (p / 'foo').exists()) |
---|
1359 | n/a | self.assertIs(False, P('/xyzzy').exists()) |
---|
1360 | n/a | |
---|
1361 | n/a | def test_open_common(self): |
---|
1362 | n/a | p = self.cls(BASE) |
---|
1363 | n/a | with (p / 'fileA').open('r') as f: |
---|
1364 | n/a | self.assertIsInstance(f, io.TextIOBase) |
---|
1365 | n/a | self.assertEqual(f.read(), "this is file A\n") |
---|
1366 | n/a | with (p / 'fileA').open('rb') as f: |
---|
1367 | n/a | self.assertIsInstance(f, io.BufferedIOBase) |
---|
1368 | n/a | self.assertEqual(f.read().strip(), b"this is file A") |
---|
1369 | n/a | with (p / 'fileA').open('rb', buffering=0) as f: |
---|
1370 | n/a | self.assertIsInstance(f, io.RawIOBase) |
---|
1371 | n/a | self.assertEqual(f.read().strip(), b"this is file A") |
---|
1372 | n/a | |
---|
1373 | n/a | def test_read_write_bytes(self): |
---|
1374 | n/a | p = self.cls(BASE) |
---|
1375 | n/a | (p / 'fileA').write_bytes(b'abcdefg') |
---|
1376 | n/a | self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg') |
---|
1377 | n/a | # check that trying to write str does not truncate the file |
---|
1378 | n/a | self.assertRaises(TypeError, (p / 'fileA').write_bytes, 'somestr') |
---|
1379 | n/a | self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg') |
---|
1380 | n/a | |
---|
1381 | n/a | def test_read_write_text(self): |
---|
1382 | n/a | p = self.cls(BASE) |
---|
1383 | n/a | (p / 'fileA').write_text('รยคbcdefg', encoding='latin-1') |
---|
1384 | n/a | self.assertEqual((p / 'fileA').read_text( |
---|
1385 | n/a | encoding='utf-8', errors='ignore'), 'bcdefg') |
---|
1386 | n/a | # check that trying to write bytes does not truncate the file |
---|
1387 | n/a | self.assertRaises(TypeError, (p / 'fileA').write_text, b'somebytes') |
---|
1388 | n/a | self.assertEqual((p / 'fileA').read_text(encoding='latin-1'), 'รยคbcdefg') |
---|
1389 | n/a | |
---|
1390 | n/a | def test_iterdir(self): |
---|
1391 | n/a | P = self.cls |
---|
1392 | n/a | p = P(BASE) |
---|
1393 | n/a | it = p.iterdir() |
---|
1394 | n/a | paths = set(it) |
---|
1395 | n/a | expected = ['dirA', 'dirB', 'dirC', 'dirE', 'fileA'] |
---|
1396 | n/a | if not symlink_skip_reason: |
---|
1397 | n/a | expected += ['linkA', 'linkB', 'brokenLink'] |
---|
1398 | n/a | self.assertEqual(paths, { P(BASE, q) for q in expected }) |
---|
1399 | n/a | |
---|
1400 | n/a | @with_symlinks |
---|
1401 | n/a | def test_iterdir_symlink(self): |
---|
1402 | n/a | # __iter__ on a symlink to a directory |
---|
1403 | n/a | P = self.cls |
---|
1404 | n/a | p = P(BASE, 'linkB') |
---|
1405 | n/a | paths = set(p.iterdir()) |
---|
1406 | n/a | expected = { P(BASE, 'linkB', q) for q in ['fileB', 'linkD'] } |
---|
1407 | n/a | self.assertEqual(paths, expected) |
---|
1408 | n/a | |
---|
1409 | n/a | def test_iterdir_nodir(self): |
---|
1410 | n/a | # __iter__ on something that is not a directory |
---|
1411 | n/a | p = self.cls(BASE, 'fileA') |
---|
1412 | n/a | with self.assertRaises(OSError) as cm: |
---|
1413 | n/a | next(p.iterdir()) |
---|
1414 | n/a | # ENOENT or EINVAL under Windows, ENOTDIR otherwise |
---|
1415 | n/a | # (see issue #12802) |
---|
1416 | n/a | self.assertIn(cm.exception.errno, (errno.ENOTDIR, |
---|
1417 | n/a | errno.ENOENT, errno.EINVAL)) |
---|
1418 | n/a | |
---|
1419 | n/a | def test_glob_common(self): |
---|
1420 | n/a | def _check(glob, expected): |
---|
1421 | n/a | self.assertEqual(set(glob), { P(BASE, q) for q in expected }) |
---|
1422 | n/a | P = self.cls |
---|
1423 | n/a | p = P(BASE) |
---|
1424 | n/a | it = p.glob("fileA") |
---|
1425 | n/a | self.assertIsInstance(it, collections.Iterator) |
---|
1426 | n/a | _check(it, ["fileA"]) |
---|
1427 | n/a | _check(p.glob("fileB"), []) |
---|
1428 | n/a | _check(p.glob("dir*/file*"), ["dirB/fileB", "dirC/fileC"]) |
---|
1429 | n/a | if symlink_skip_reason: |
---|
1430 | n/a | _check(p.glob("*A"), ['dirA', 'fileA']) |
---|
1431 | n/a | else: |
---|
1432 | n/a | _check(p.glob("*A"), ['dirA', 'fileA', 'linkA']) |
---|
1433 | n/a | if symlink_skip_reason: |
---|
1434 | n/a | _check(p.glob("*B/*"), ['dirB/fileB']) |
---|
1435 | n/a | else: |
---|
1436 | n/a | _check(p.glob("*B/*"), ['dirB/fileB', 'dirB/linkD', |
---|
1437 | n/a | 'linkB/fileB', 'linkB/linkD']) |
---|
1438 | n/a | if symlink_skip_reason: |
---|
1439 | n/a | _check(p.glob("*/fileB"), ['dirB/fileB']) |
---|
1440 | n/a | else: |
---|
1441 | n/a | _check(p.glob("*/fileB"), ['dirB/fileB', 'linkB/fileB']) |
---|
1442 | n/a | |
---|
1443 | n/a | def test_rglob_common(self): |
---|
1444 | n/a | def _check(glob, expected): |
---|
1445 | n/a | self.assertEqual(set(glob), { P(BASE, q) for q in expected }) |
---|
1446 | n/a | P = self.cls |
---|
1447 | n/a | p = P(BASE) |
---|
1448 | n/a | it = p.rglob("fileA") |
---|
1449 | n/a | self.assertIsInstance(it, collections.Iterator) |
---|
1450 | n/a | _check(it, ["fileA"]) |
---|
1451 | n/a | _check(p.rglob("fileB"), ["dirB/fileB"]) |
---|
1452 | n/a | _check(p.rglob("*/fileA"), []) |
---|
1453 | n/a | if symlink_skip_reason: |
---|
1454 | n/a | _check(p.rglob("*/fileB"), ["dirB/fileB"]) |
---|
1455 | n/a | else: |
---|
1456 | n/a | _check(p.rglob("*/fileB"), ["dirB/fileB", "dirB/linkD/fileB", |
---|
1457 | n/a | "linkB/fileB", "dirA/linkC/fileB"]) |
---|
1458 | n/a | _check(p.rglob("file*"), ["fileA", "dirB/fileB", |
---|
1459 | n/a | "dirC/fileC", "dirC/dirD/fileD"]) |
---|
1460 | n/a | p = P(BASE, "dirC") |
---|
1461 | n/a | _check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"]) |
---|
1462 | n/a | _check(p.rglob("*/*"), ["dirC/dirD/fileD"]) |
---|
1463 | n/a | |
---|
1464 | n/a | @with_symlinks |
---|
1465 | n/a | def test_rglob_symlink_loop(self): |
---|
1466 | n/a | # Don't get fooled by symlink loops (Issue #26012) |
---|
1467 | n/a | P = self.cls |
---|
1468 | n/a | p = P(BASE) |
---|
1469 | n/a | given = set(p.rglob('*')) |
---|
1470 | n/a | expect = {'brokenLink', |
---|
1471 | n/a | 'dirA', 'dirA/linkC', |
---|
1472 | n/a | 'dirB', 'dirB/fileB', 'dirB/linkD', |
---|
1473 | n/a | 'dirC', 'dirC/dirD', 'dirC/dirD/fileD', 'dirC/fileC', |
---|
1474 | n/a | 'dirE', |
---|
1475 | n/a | 'fileA', |
---|
1476 | n/a | 'linkA', |
---|
1477 | n/a | 'linkB', |
---|
1478 | n/a | } |
---|
1479 | n/a | self.assertEqual(given, {p / x for x in expect}) |
---|
1480 | n/a | |
---|
1481 | n/a | def test_glob_dotdot(self): |
---|
1482 | n/a | # ".." is not special in globs |
---|
1483 | n/a | P = self.cls |
---|
1484 | n/a | p = P(BASE) |
---|
1485 | n/a | self.assertEqual(set(p.glob("..")), { P(BASE, "..") }) |
---|
1486 | n/a | self.assertEqual(set(p.glob("dirA/../file*")), { P(BASE, "dirA/../fileA") }) |
---|
1487 | n/a | self.assertEqual(set(p.glob("../xyzzy")), set()) |
---|
1488 | n/a | |
---|
1489 | n/a | |
---|
1490 | n/a | def _check_resolve(self, p, expected, strict=True): |
---|
1491 | n/a | q = p.resolve(strict) |
---|
1492 | n/a | self.assertEqual(q, expected) |
---|
1493 | n/a | |
---|
1494 | n/a | # this can be used to check both relative and absolute resolutions |
---|
1495 | n/a | _check_resolve_relative = _check_resolve_absolute = _check_resolve |
---|
1496 | n/a | |
---|
1497 | n/a | @with_symlinks |
---|
1498 | n/a | def test_resolve_common(self): |
---|
1499 | n/a | P = self.cls |
---|
1500 | n/a | p = P(BASE, 'foo') |
---|
1501 | n/a | with self.assertRaises(OSError) as cm: |
---|
1502 | n/a | p.resolve(strict=True) |
---|
1503 | n/a | self.assertEqual(cm.exception.errno, errno.ENOENT) |
---|
1504 | n/a | # Non-strict |
---|
1505 | n/a | self.assertEqual(str(p.resolve(strict=False)), |
---|
1506 | n/a | os.path.join(BASE, 'foo')) |
---|
1507 | n/a | p = P(BASE, 'foo', 'in', 'spam') |
---|
1508 | n/a | self.assertEqual(str(p.resolve(strict=False)), |
---|
1509 | n/a | os.path.join(BASE, 'foo')) |
---|
1510 | n/a | p = P(BASE, '..', 'foo', 'in', 'spam') |
---|
1511 | n/a | self.assertEqual(str(p.resolve(strict=False)), |
---|
1512 | n/a | os.path.abspath(os.path.join('foo'))) |
---|
1513 | n/a | # These are all relative symlinks |
---|
1514 | n/a | p = P(BASE, 'dirB', 'fileB') |
---|
1515 | n/a | self._check_resolve_relative(p, p) |
---|
1516 | n/a | p = P(BASE, 'linkA') |
---|
1517 | n/a | self._check_resolve_relative(p, P(BASE, 'fileA')) |
---|
1518 | n/a | p = P(BASE, 'dirA', 'linkC', 'fileB') |
---|
1519 | n/a | self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB')) |
---|
1520 | n/a | p = P(BASE, 'dirB', 'linkD', 'fileB') |
---|
1521 | n/a | self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB')) |
---|
1522 | n/a | # Non-strict |
---|
1523 | n/a | p = P(BASE, 'dirA', 'linkC', 'fileB', 'foo', 'in', 'spam') |
---|
1524 | n/a | self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB', 'foo'), False) |
---|
1525 | n/a | p = P(BASE, 'dirA', 'linkC', '..', 'foo', 'in', 'spam') |
---|
1526 | n/a | if os.name == 'nt': |
---|
1527 | n/a | # In Windows, if linkY points to dirB, 'dirA\linkY\..' |
---|
1528 | n/a | # resolves to 'dirA' without resolving linkY first. |
---|
1529 | n/a | self._check_resolve_relative(p, P(BASE, 'dirA', 'foo'), False) |
---|
1530 | n/a | else: |
---|
1531 | n/a | # In Posix, if linkY points to dirB, 'dirA/linkY/..' |
---|
1532 | n/a | # resolves to 'dirB/..' first before resolving to parent of dirB. |
---|
1533 | n/a | self._check_resolve_relative(p, P(BASE, 'foo'), False) |
---|
1534 | n/a | # Now create absolute symlinks |
---|
1535 | n/a | d = tempfile.mkdtemp(suffix='-dirD') |
---|
1536 | n/a | self.addCleanup(support.rmtree, d) |
---|
1537 | n/a | os.symlink(os.path.join(d), join('dirA', 'linkX')) |
---|
1538 | n/a | os.symlink(join('dirB'), os.path.join(d, 'linkY')) |
---|
1539 | n/a | p = P(BASE, 'dirA', 'linkX', 'linkY', 'fileB') |
---|
1540 | n/a | self._check_resolve_absolute(p, P(BASE, 'dirB', 'fileB')) |
---|
1541 | n/a | # Non-strict |
---|
1542 | n/a | p = P(BASE, 'dirA', 'linkX', 'linkY', 'foo', 'in', 'spam') |
---|
1543 | n/a | self._check_resolve_relative(p, P(BASE, 'dirB', 'foo'), False) |
---|
1544 | n/a | p = P(BASE, 'dirA', 'linkX', 'linkY', '..', 'foo', 'in', 'spam') |
---|
1545 | n/a | if os.name == 'nt': |
---|
1546 | n/a | # In Windows, if linkY points to dirB, 'dirA\linkY\..' |
---|
1547 | n/a | # resolves to 'dirA' without resolving linkY first. |
---|
1548 | n/a | self._check_resolve_relative(p, P(d, 'foo'), False) |
---|
1549 | n/a | else: |
---|
1550 | n/a | # In Posix, if linkY points to dirB, 'dirA/linkY/..' |
---|
1551 | n/a | # resolves to 'dirB/..' first before resolving to parent of dirB. |
---|
1552 | n/a | self._check_resolve_relative(p, P(BASE, 'foo'), False) |
---|
1553 | n/a | |
---|
1554 | n/a | @with_symlinks |
---|
1555 | n/a | def test_resolve_dot(self): |
---|
1556 | n/a | # See https://bitbucket.org/pitrou/pathlib/issue/9/pathresolve-fails-on-complex-symlinks |
---|
1557 | n/a | p = self.cls(BASE) |
---|
1558 | n/a | self.dirlink('.', join('0')) |
---|
1559 | n/a | self.dirlink(os.path.join('0', '0'), join('1')) |
---|
1560 | n/a | self.dirlink(os.path.join('1', '1'), join('2')) |
---|
1561 | n/a | q = p / '2' |
---|
1562 | n/a | self.assertEqual(q.resolve(strict=True), p) |
---|
1563 | n/a | r = q / '3' / '4' |
---|
1564 | n/a | self.assertRaises(FileNotFoundError, r.resolve, strict=True) |
---|
1565 | n/a | # Non-strict |
---|
1566 | n/a | self.assertEqual(r.resolve(strict=False), p / '3') |
---|
1567 | n/a | |
---|
1568 | n/a | def test_with(self): |
---|
1569 | n/a | p = self.cls(BASE) |
---|
1570 | n/a | it = p.iterdir() |
---|
1571 | n/a | it2 = p.iterdir() |
---|
1572 | n/a | next(it2) |
---|
1573 | n/a | with p: |
---|
1574 | n/a | pass |
---|
1575 | n/a | # I/O operation on closed path |
---|
1576 | n/a | self.assertRaises(ValueError, next, it) |
---|
1577 | n/a | self.assertRaises(ValueError, next, it2) |
---|
1578 | n/a | self.assertRaises(ValueError, p.open) |
---|
1579 | n/a | self.assertRaises(ValueError, p.resolve) |
---|
1580 | n/a | self.assertRaises(ValueError, p.absolute) |
---|
1581 | n/a | self.assertRaises(ValueError, p.__enter__) |
---|
1582 | n/a | |
---|
1583 | n/a | def test_chmod(self): |
---|
1584 | n/a | p = self.cls(BASE) / 'fileA' |
---|
1585 | n/a | mode = p.stat().st_mode |
---|
1586 | n/a | # Clear writable bit |
---|
1587 | n/a | new_mode = mode & ~0o222 |
---|
1588 | n/a | p.chmod(new_mode) |
---|
1589 | n/a | self.assertEqual(p.stat().st_mode, new_mode) |
---|
1590 | n/a | # Set writable bit |
---|
1591 | n/a | new_mode = mode | 0o222 |
---|
1592 | n/a | p.chmod(new_mode) |
---|
1593 | n/a | self.assertEqual(p.stat().st_mode, new_mode) |
---|
1594 | n/a | |
---|
1595 | n/a | # XXX also need a test for lchmod |
---|
1596 | n/a | |
---|
1597 | n/a | def test_stat(self): |
---|
1598 | n/a | p = self.cls(BASE) / 'fileA' |
---|
1599 | n/a | st = p.stat() |
---|
1600 | n/a | self.assertEqual(p.stat(), st) |
---|
1601 | n/a | # Change file mode by flipping write bit |
---|
1602 | n/a | p.chmod(st.st_mode ^ 0o222) |
---|
1603 | n/a | self.addCleanup(p.chmod, st.st_mode) |
---|
1604 | n/a | self.assertNotEqual(p.stat(), st) |
---|
1605 | n/a | |
---|
1606 | n/a | @with_symlinks |
---|
1607 | n/a | def test_lstat(self): |
---|
1608 | n/a | p = self.cls(BASE)/ 'linkA' |
---|
1609 | n/a | st = p.stat() |
---|
1610 | n/a | self.assertNotEqual(st, p.lstat()) |
---|
1611 | n/a | |
---|
1612 | n/a | def test_lstat_nosymlink(self): |
---|
1613 | n/a | p = self.cls(BASE) / 'fileA' |
---|
1614 | n/a | st = p.stat() |
---|
1615 | n/a | self.assertEqual(st, p.lstat()) |
---|
1616 | n/a | |
---|
1617 | n/a | @unittest.skipUnless(pwd, "the pwd module is needed for this test") |
---|
1618 | n/a | def test_owner(self): |
---|
1619 | n/a | p = self.cls(BASE) / 'fileA' |
---|
1620 | n/a | uid = p.stat().st_uid |
---|
1621 | n/a | try: |
---|
1622 | n/a | name = pwd.getpwuid(uid).pw_name |
---|
1623 | n/a | except KeyError: |
---|
1624 | n/a | self.skipTest( |
---|
1625 | n/a | "user %d doesn't have an entry in the system database" % uid) |
---|
1626 | n/a | self.assertEqual(name, p.owner()) |
---|
1627 | n/a | |
---|
1628 | n/a | @unittest.skipUnless(grp, "the grp module is needed for this test") |
---|
1629 | n/a | def test_group(self): |
---|
1630 | n/a | p = self.cls(BASE) / 'fileA' |
---|
1631 | n/a | gid = p.stat().st_gid |
---|
1632 | n/a | try: |
---|
1633 | n/a | name = grp.getgrgid(gid).gr_name |
---|
1634 | n/a | except KeyError: |
---|
1635 | n/a | self.skipTest( |
---|
1636 | n/a | "group %d doesn't have an entry in the system database" % gid) |
---|
1637 | n/a | self.assertEqual(name, p.group()) |
---|
1638 | n/a | |
---|
1639 | n/a | def test_unlink(self): |
---|
1640 | n/a | p = self.cls(BASE) / 'fileA' |
---|
1641 | n/a | p.unlink() |
---|
1642 | n/a | self.assertFileNotFound(p.stat) |
---|
1643 | n/a | self.assertFileNotFound(p.unlink) |
---|
1644 | n/a | |
---|
1645 | n/a | def test_rmdir(self): |
---|
1646 | n/a | p = self.cls(BASE) / 'dirA' |
---|
1647 | n/a | for q in p.iterdir(): |
---|
1648 | n/a | q.unlink() |
---|
1649 | n/a | p.rmdir() |
---|
1650 | n/a | self.assertFileNotFound(p.stat) |
---|
1651 | n/a | self.assertFileNotFound(p.unlink) |
---|
1652 | n/a | |
---|
1653 | n/a | def test_rename(self): |
---|
1654 | n/a | P = self.cls(BASE) |
---|
1655 | n/a | p = P / 'fileA' |
---|
1656 | n/a | size = p.stat().st_size |
---|
1657 | n/a | # Renaming to another path |
---|
1658 | n/a | q = P / 'dirA' / 'fileAA' |
---|
1659 | n/a | p.rename(q) |
---|
1660 | n/a | self.assertEqual(q.stat().st_size, size) |
---|
1661 | n/a | self.assertFileNotFound(p.stat) |
---|
1662 | n/a | # Renaming to a str of a relative path |
---|
1663 | n/a | r = rel_join('fileAAA') |
---|
1664 | n/a | q.rename(r) |
---|
1665 | n/a | self.assertEqual(os.stat(r).st_size, size) |
---|
1666 | n/a | self.assertFileNotFound(q.stat) |
---|
1667 | n/a | |
---|
1668 | n/a | def test_replace(self): |
---|
1669 | n/a | P = self.cls(BASE) |
---|
1670 | n/a | p = P / 'fileA' |
---|
1671 | n/a | size = p.stat().st_size |
---|
1672 | n/a | # Replacing a non-existing path |
---|
1673 | n/a | q = P / 'dirA' / 'fileAA' |
---|
1674 | n/a | p.replace(q) |
---|
1675 | n/a | self.assertEqual(q.stat().st_size, size) |
---|
1676 | n/a | self.assertFileNotFound(p.stat) |
---|
1677 | n/a | # Replacing another (existing) path |
---|
1678 | n/a | r = rel_join('dirB', 'fileB') |
---|
1679 | n/a | q.replace(r) |
---|
1680 | n/a | self.assertEqual(os.stat(r).st_size, size) |
---|
1681 | n/a | self.assertFileNotFound(q.stat) |
---|
1682 | n/a | |
---|
1683 | n/a | def test_touch_common(self): |
---|
1684 | n/a | P = self.cls(BASE) |
---|
1685 | n/a | p = P / 'newfileA' |
---|
1686 | n/a | self.assertFalse(p.exists()) |
---|
1687 | n/a | p.touch() |
---|
1688 | n/a | self.assertTrue(p.exists()) |
---|
1689 | n/a | st = p.stat() |
---|
1690 | n/a | old_mtime = st.st_mtime |
---|
1691 | n/a | old_mtime_ns = st.st_mtime_ns |
---|
1692 | n/a | # Rewind the mtime sufficiently far in the past to work around |
---|
1693 | n/a | # filesystem-specific timestamp granularity. |
---|
1694 | n/a | os.utime(str(p), (old_mtime - 10, old_mtime - 10)) |
---|
1695 | n/a | # The file mtime should be refreshed by calling touch() again |
---|
1696 | n/a | p.touch() |
---|
1697 | n/a | st = p.stat() |
---|
1698 | n/a | self.assertGreaterEqual(st.st_mtime_ns, old_mtime_ns) |
---|
1699 | n/a | self.assertGreaterEqual(st.st_mtime, old_mtime) |
---|
1700 | n/a | # Now with exist_ok=False |
---|
1701 | n/a | p = P / 'newfileB' |
---|
1702 | n/a | self.assertFalse(p.exists()) |
---|
1703 | n/a | p.touch(mode=0o700, exist_ok=False) |
---|
1704 | n/a | self.assertTrue(p.exists()) |
---|
1705 | n/a | self.assertRaises(OSError, p.touch, exist_ok=False) |
---|
1706 | n/a | |
---|
1707 | n/a | def test_touch_nochange(self): |
---|
1708 | n/a | P = self.cls(BASE) |
---|
1709 | n/a | p = P / 'fileA' |
---|
1710 | n/a | p.touch() |
---|
1711 | n/a | with p.open('rb') as f: |
---|
1712 | n/a | self.assertEqual(f.read().strip(), b"this is file A") |
---|
1713 | n/a | |
---|
1714 | n/a | def test_mkdir(self): |
---|
1715 | n/a | P = self.cls(BASE) |
---|
1716 | n/a | p = P / 'newdirA' |
---|
1717 | n/a | self.assertFalse(p.exists()) |
---|
1718 | n/a | p.mkdir() |
---|
1719 | n/a | self.assertTrue(p.exists()) |
---|
1720 | n/a | self.assertTrue(p.is_dir()) |
---|
1721 | n/a | with self.assertRaises(OSError) as cm: |
---|
1722 | n/a | p.mkdir() |
---|
1723 | n/a | self.assertEqual(cm.exception.errno, errno.EEXIST) |
---|
1724 | n/a | |
---|
1725 | n/a | def test_mkdir_parents(self): |
---|
1726 | n/a | # Creating a chain of directories |
---|
1727 | n/a | p = self.cls(BASE, 'newdirB', 'newdirC') |
---|
1728 | n/a | self.assertFalse(p.exists()) |
---|
1729 | n/a | with self.assertRaises(OSError) as cm: |
---|
1730 | n/a | p.mkdir() |
---|
1731 | n/a | self.assertEqual(cm.exception.errno, errno.ENOENT) |
---|
1732 | n/a | p.mkdir(parents=True) |
---|
1733 | n/a | self.assertTrue(p.exists()) |
---|
1734 | n/a | self.assertTrue(p.is_dir()) |
---|
1735 | n/a | with self.assertRaises(OSError) as cm: |
---|
1736 | n/a | p.mkdir(parents=True) |
---|
1737 | n/a | self.assertEqual(cm.exception.errno, errno.EEXIST) |
---|
1738 | n/a | # test `mode` arg |
---|
1739 | n/a | mode = stat.S_IMODE(p.stat().st_mode) # default mode |
---|
1740 | n/a | p = self.cls(BASE, 'newdirD', 'newdirE') |
---|
1741 | n/a | p.mkdir(0o555, parents=True) |
---|
1742 | n/a | self.assertTrue(p.exists()) |
---|
1743 | n/a | self.assertTrue(p.is_dir()) |
---|
1744 | n/a | if os.name != 'nt': |
---|
1745 | n/a | # the directory's permissions follow the mode argument |
---|
1746 | n/a | self.assertEqual(stat.S_IMODE(p.stat().st_mode), 0o7555 & mode) |
---|
1747 | n/a | # the parent's permissions follow the default process settings |
---|
1748 | n/a | self.assertEqual(stat.S_IMODE(p.parent.stat().st_mode), mode) |
---|
1749 | n/a | |
---|
1750 | n/a | def test_mkdir_exist_ok(self): |
---|
1751 | n/a | p = self.cls(BASE, 'dirB') |
---|
1752 | n/a | st_ctime_first = p.stat().st_ctime |
---|
1753 | n/a | self.assertTrue(p.exists()) |
---|
1754 | n/a | self.assertTrue(p.is_dir()) |
---|
1755 | n/a | with self.assertRaises(FileExistsError) as cm: |
---|
1756 | n/a | p.mkdir() |
---|
1757 | n/a | self.assertEqual(cm.exception.errno, errno.EEXIST) |
---|
1758 | n/a | p.mkdir(exist_ok=True) |
---|
1759 | n/a | self.assertTrue(p.exists()) |
---|
1760 | n/a | self.assertEqual(p.stat().st_ctime, st_ctime_first) |
---|
1761 | n/a | |
---|
1762 | n/a | def test_mkdir_exist_ok_with_parent(self): |
---|
1763 | n/a | p = self.cls(BASE, 'dirC') |
---|
1764 | n/a | self.assertTrue(p.exists()) |
---|
1765 | n/a | with self.assertRaises(FileExistsError) as cm: |
---|
1766 | n/a | p.mkdir() |
---|
1767 | n/a | self.assertEqual(cm.exception.errno, errno.EEXIST) |
---|
1768 | n/a | p = p / 'newdirC' |
---|
1769 | n/a | p.mkdir(parents=True) |
---|
1770 | n/a | st_ctime_first = p.stat().st_ctime |
---|
1771 | n/a | self.assertTrue(p.exists()) |
---|
1772 | n/a | with self.assertRaises(FileExistsError) as cm: |
---|
1773 | n/a | p.mkdir(parents=True) |
---|
1774 | n/a | self.assertEqual(cm.exception.errno, errno.EEXIST) |
---|
1775 | n/a | p.mkdir(parents=True, exist_ok=True) |
---|
1776 | n/a | self.assertTrue(p.exists()) |
---|
1777 | n/a | self.assertEqual(p.stat().st_ctime, st_ctime_first) |
---|
1778 | n/a | |
---|
1779 | n/a | @only_nt # XXX: not sure how to test this on POSIX |
---|
1780 | n/a | def test_mkdir_with_unknown_drive(self): |
---|
1781 | n/a | for d in 'ZYXWVUTSRQPONMLKJIHGFEDCBA': |
---|
1782 | n/a | p = self.cls(d + ':\\') |
---|
1783 | n/a | if not p.is_dir(): |
---|
1784 | n/a | break |
---|
1785 | n/a | else: |
---|
1786 | n/a | self.skipTest("cannot find a drive that doesn't exist") |
---|
1787 | n/a | with self.assertRaises(OSError): |
---|
1788 | n/a | (p / 'child' / 'path').mkdir(parents=True) |
---|
1789 | n/a | |
---|
1790 | n/a | def test_mkdir_with_child_file(self): |
---|
1791 | n/a | p = self.cls(BASE, 'dirB', 'fileB') |
---|
1792 | n/a | self.assertTrue(p.exists()) |
---|
1793 | n/a | # An exception is raised when the last path component is an existing |
---|
1794 | n/a | # regular file, regardless of whether exist_ok is true or not. |
---|
1795 | n/a | with self.assertRaises(FileExistsError) as cm: |
---|
1796 | n/a | p.mkdir(parents=True) |
---|
1797 | n/a | self.assertEqual(cm.exception.errno, errno.EEXIST) |
---|
1798 | n/a | with self.assertRaises(FileExistsError) as cm: |
---|
1799 | n/a | p.mkdir(parents=True, exist_ok=True) |
---|
1800 | n/a | self.assertEqual(cm.exception.errno, errno.EEXIST) |
---|
1801 | n/a | |
---|
1802 | n/a | def test_mkdir_no_parents_file(self): |
---|
1803 | n/a | p = self.cls(BASE, 'fileA') |
---|
1804 | n/a | self.assertTrue(p.exists()) |
---|
1805 | n/a | # An exception is raised when the last path component is an existing |
---|
1806 | n/a | # regular file, regardless of whether exist_ok is true or not. |
---|
1807 | n/a | with self.assertRaises(FileExistsError) as cm: |
---|
1808 | n/a | p.mkdir() |
---|
1809 | n/a | self.assertEqual(cm.exception.errno, errno.EEXIST) |
---|
1810 | n/a | with self.assertRaises(FileExistsError) as cm: |
---|
1811 | n/a | p.mkdir(exist_ok=True) |
---|
1812 | n/a | self.assertEqual(cm.exception.errno, errno.EEXIST) |
---|
1813 | n/a | |
---|
1814 | n/a | @with_symlinks |
---|
1815 | n/a | def test_symlink_to(self): |
---|
1816 | n/a | P = self.cls(BASE) |
---|
1817 | n/a | target = P / 'fileA' |
---|
1818 | n/a | # Symlinking a path target |
---|
1819 | n/a | link = P / 'dirA' / 'linkAA' |
---|
1820 | n/a | link.symlink_to(target) |
---|
1821 | n/a | self.assertEqual(link.stat(), target.stat()) |
---|
1822 | n/a | self.assertNotEqual(link.lstat(), target.stat()) |
---|
1823 | n/a | # Symlinking a str target |
---|
1824 | n/a | link = P / 'dirA' / 'linkAAA' |
---|
1825 | n/a | link.symlink_to(str(target)) |
---|
1826 | n/a | self.assertEqual(link.stat(), target.stat()) |
---|
1827 | n/a | self.assertNotEqual(link.lstat(), target.stat()) |
---|
1828 | n/a | self.assertFalse(link.is_dir()) |
---|
1829 | n/a | # Symlinking to a directory |
---|
1830 | n/a | target = P / 'dirB' |
---|
1831 | n/a | link = P / 'dirA' / 'linkAAAA' |
---|
1832 | n/a | link.symlink_to(target, target_is_directory=True) |
---|
1833 | n/a | self.assertEqual(link.stat(), target.stat()) |
---|
1834 | n/a | self.assertNotEqual(link.lstat(), target.stat()) |
---|
1835 | n/a | self.assertTrue(link.is_dir()) |
---|
1836 | n/a | self.assertTrue(list(link.iterdir())) |
---|
1837 | n/a | |
---|
1838 | n/a | def test_is_dir(self): |
---|
1839 | n/a | P = self.cls(BASE) |
---|
1840 | n/a | self.assertTrue((P / 'dirA').is_dir()) |
---|
1841 | n/a | self.assertFalse((P / 'fileA').is_dir()) |
---|
1842 | n/a | self.assertFalse((P / 'non-existing').is_dir()) |
---|
1843 | n/a | self.assertFalse((P / 'fileA' / 'bah').is_dir()) |
---|
1844 | n/a | if not symlink_skip_reason: |
---|
1845 | n/a | self.assertFalse((P / 'linkA').is_dir()) |
---|
1846 | n/a | self.assertTrue((P / 'linkB').is_dir()) |
---|
1847 | n/a | self.assertFalse((P/ 'brokenLink').is_dir()) |
---|
1848 | n/a | |
---|
1849 | n/a | def test_is_file(self): |
---|
1850 | n/a | P = self.cls(BASE) |
---|
1851 | n/a | self.assertTrue((P / 'fileA').is_file()) |
---|
1852 | n/a | self.assertFalse((P / 'dirA').is_file()) |
---|
1853 | n/a | self.assertFalse((P / 'non-existing').is_file()) |
---|
1854 | n/a | self.assertFalse((P / 'fileA' / 'bah').is_file()) |
---|
1855 | n/a | if not symlink_skip_reason: |
---|
1856 | n/a | self.assertTrue((P / 'linkA').is_file()) |
---|
1857 | n/a | self.assertFalse((P / 'linkB').is_file()) |
---|
1858 | n/a | self.assertFalse((P/ 'brokenLink').is_file()) |
---|
1859 | n/a | |
---|
1860 | n/a | def test_is_symlink(self): |
---|
1861 | n/a | P = self.cls(BASE) |
---|
1862 | n/a | self.assertFalse((P / 'fileA').is_symlink()) |
---|
1863 | n/a | self.assertFalse((P / 'dirA').is_symlink()) |
---|
1864 | n/a | self.assertFalse((P / 'non-existing').is_symlink()) |
---|
1865 | n/a | self.assertFalse((P / 'fileA' / 'bah').is_symlink()) |
---|
1866 | n/a | if not symlink_skip_reason: |
---|
1867 | n/a | self.assertTrue((P / 'linkA').is_symlink()) |
---|
1868 | n/a | self.assertTrue((P / 'linkB').is_symlink()) |
---|
1869 | n/a | self.assertTrue((P/ 'brokenLink').is_symlink()) |
---|
1870 | n/a | |
---|
1871 | n/a | def test_is_fifo_false(self): |
---|
1872 | n/a | P = self.cls(BASE) |
---|
1873 | n/a | self.assertFalse((P / 'fileA').is_fifo()) |
---|
1874 | n/a | self.assertFalse((P / 'dirA').is_fifo()) |
---|
1875 | n/a | self.assertFalse((P / 'non-existing').is_fifo()) |
---|
1876 | n/a | self.assertFalse((P / 'fileA' / 'bah').is_fifo()) |
---|
1877 | n/a | |
---|
1878 | n/a | @unittest.skipUnless(hasattr(os, "mkfifo"), "os.mkfifo() required") |
---|
1879 | n/a | @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user") |
---|
1880 | n/a | def test_is_fifo_true(self): |
---|
1881 | n/a | P = self.cls(BASE, 'myfifo') |
---|
1882 | n/a | os.mkfifo(str(P)) |
---|
1883 | n/a | self.assertTrue(P.is_fifo()) |
---|
1884 | n/a | self.assertFalse(P.is_socket()) |
---|
1885 | n/a | self.assertFalse(P.is_file()) |
---|
1886 | n/a | |
---|
1887 | n/a | def test_is_socket_false(self): |
---|
1888 | n/a | P = self.cls(BASE) |
---|
1889 | n/a | self.assertFalse((P / 'fileA').is_socket()) |
---|
1890 | n/a | self.assertFalse((P / 'dirA').is_socket()) |
---|
1891 | n/a | self.assertFalse((P / 'non-existing').is_socket()) |
---|
1892 | n/a | self.assertFalse((P / 'fileA' / 'bah').is_socket()) |
---|
1893 | n/a | |
---|
1894 | n/a | @unittest.skipUnless(hasattr(socket, "AF_UNIX"), "Unix sockets required") |
---|
1895 | n/a | def test_is_socket_true(self): |
---|
1896 | n/a | P = self.cls(BASE, 'mysock') |
---|
1897 | n/a | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
---|
1898 | n/a | self.addCleanup(sock.close) |
---|
1899 | n/a | try: |
---|
1900 | n/a | sock.bind(str(P)) |
---|
1901 | n/a | except OSError as e: |
---|
1902 | n/a | if (isinstance(e, PermissionError) or |
---|
1903 | n/a | "AF_UNIX path too long" in str(e)): |
---|
1904 | n/a | self.skipTest("cannot bind Unix socket: " + str(e)) |
---|
1905 | n/a | self.assertTrue(P.is_socket()) |
---|
1906 | n/a | self.assertFalse(P.is_fifo()) |
---|
1907 | n/a | self.assertFalse(P.is_file()) |
---|
1908 | n/a | |
---|
1909 | n/a | def test_is_block_device_false(self): |
---|
1910 | n/a | P = self.cls(BASE) |
---|
1911 | n/a | self.assertFalse((P / 'fileA').is_block_device()) |
---|
1912 | n/a | self.assertFalse((P / 'dirA').is_block_device()) |
---|
1913 | n/a | self.assertFalse((P / 'non-existing').is_block_device()) |
---|
1914 | n/a | self.assertFalse((P / 'fileA' / 'bah').is_block_device()) |
---|
1915 | n/a | |
---|
1916 | n/a | def test_is_char_device_false(self): |
---|
1917 | n/a | P = self.cls(BASE) |
---|
1918 | n/a | self.assertFalse((P / 'fileA').is_char_device()) |
---|
1919 | n/a | self.assertFalse((P / 'dirA').is_char_device()) |
---|
1920 | n/a | self.assertFalse((P / 'non-existing').is_char_device()) |
---|
1921 | n/a | self.assertFalse((P / 'fileA' / 'bah').is_char_device()) |
---|
1922 | n/a | |
---|
1923 | n/a | def test_is_char_device_true(self): |
---|
1924 | n/a | # Under Unix, /dev/null should generally be a char device |
---|
1925 | n/a | P = self.cls('/dev/null') |
---|
1926 | n/a | if not P.exists(): |
---|
1927 | n/a | self.skipTest("/dev/null required") |
---|
1928 | n/a | self.assertTrue(P.is_char_device()) |
---|
1929 | n/a | self.assertFalse(P.is_block_device()) |
---|
1930 | n/a | self.assertFalse(P.is_file()) |
---|
1931 | n/a | |
---|
1932 | n/a | def test_pickling_common(self): |
---|
1933 | n/a | p = self.cls(BASE, 'fileA') |
---|
1934 | n/a | for proto in range(0, pickle.HIGHEST_PROTOCOL + 1): |
---|
1935 | n/a | dumped = pickle.dumps(p, proto) |
---|
1936 | n/a | pp = pickle.loads(dumped) |
---|
1937 | n/a | self.assertEqual(pp.stat(), p.stat()) |
---|
1938 | n/a | |
---|
1939 | n/a | def test_parts_interning(self): |
---|
1940 | n/a | P = self.cls |
---|
1941 | n/a | p = P('/usr/bin/foo') |
---|
1942 | n/a | q = P('/usr/local/bin') |
---|
1943 | n/a | # 'usr' |
---|
1944 | n/a | self.assertIs(p.parts[1], q.parts[1]) |
---|
1945 | n/a | # 'bin' |
---|
1946 | n/a | self.assertIs(p.parts[2], q.parts[3]) |
---|
1947 | n/a | |
---|
1948 | n/a | def _check_complex_symlinks(self, link0_target): |
---|
1949 | n/a | # Test solving a non-looping chain of symlinks (issue #19887) |
---|
1950 | n/a | P = self.cls(BASE) |
---|
1951 | n/a | self.dirlink(os.path.join('link0', 'link0'), join('link1')) |
---|
1952 | n/a | self.dirlink(os.path.join('link1', 'link1'), join('link2')) |
---|
1953 | n/a | self.dirlink(os.path.join('link2', 'link2'), join('link3')) |
---|
1954 | n/a | self.dirlink(link0_target, join('link0')) |
---|
1955 | n/a | |
---|
1956 | n/a | # Resolve absolute paths |
---|
1957 | n/a | p = (P / 'link0').resolve() |
---|
1958 | n/a | self.assertEqual(p, P) |
---|
1959 | n/a | self.assertEqual(str(p), BASE) |
---|
1960 | n/a | p = (P / 'link1').resolve() |
---|
1961 | n/a | self.assertEqual(p, P) |
---|
1962 | n/a | self.assertEqual(str(p), BASE) |
---|
1963 | n/a | p = (P / 'link2').resolve() |
---|
1964 | n/a | self.assertEqual(p, P) |
---|
1965 | n/a | self.assertEqual(str(p), BASE) |
---|
1966 | n/a | p = (P / 'link3').resolve() |
---|
1967 | n/a | self.assertEqual(p, P) |
---|
1968 | n/a | self.assertEqual(str(p), BASE) |
---|
1969 | n/a | |
---|
1970 | n/a | # Resolve relative paths |
---|
1971 | n/a | old_path = os.getcwd() |
---|
1972 | n/a | os.chdir(BASE) |
---|
1973 | n/a | try: |
---|
1974 | n/a | p = self.cls('link0').resolve() |
---|
1975 | n/a | self.assertEqual(p, P) |
---|
1976 | n/a | self.assertEqual(str(p), BASE) |
---|
1977 | n/a | p = self.cls('link1').resolve() |
---|
1978 | n/a | self.assertEqual(p, P) |
---|
1979 | n/a | self.assertEqual(str(p), BASE) |
---|
1980 | n/a | p = self.cls('link2').resolve() |
---|
1981 | n/a | self.assertEqual(p, P) |
---|
1982 | n/a | self.assertEqual(str(p), BASE) |
---|
1983 | n/a | p = self.cls('link3').resolve() |
---|
1984 | n/a | self.assertEqual(p, P) |
---|
1985 | n/a | self.assertEqual(str(p), BASE) |
---|
1986 | n/a | finally: |
---|
1987 | n/a | os.chdir(old_path) |
---|
1988 | n/a | |
---|
1989 | n/a | @with_symlinks |
---|
1990 | n/a | def test_complex_symlinks_absolute(self): |
---|
1991 | n/a | self._check_complex_symlinks(BASE) |
---|
1992 | n/a | |
---|
1993 | n/a | @with_symlinks |
---|
1994 | n/a | def test_complex_symlinks_relative(self): |
---|
1995 | n/a | self._check_complex_symlinks('.') |
---|
1996 | n/a | |
---|
1997 | n/a | @with_symlinks |
---|
1998 | n/a | def test_complex_symlinks_relative_dot_dot(self): |
---|
1999 | n/a | self._check_complex_symlinks(os.path.join('dirA', '..')) |
---|
2000 | n/a | |
---|
2001 | n/a | |
---|
2002 | n/a | class PathTest(_BasePathTest, unittest.TestCase): |
---|
2003 | n/a | cls = pathlib.Path |
---|
2004 | n/a | |
---|
2005 | n/a | def test_concrete_class(self): |
---|
2006 | n/a | p = self.cls('a') |
---|
2007 | n/a | self.assertIs(type(p), |
---|
2008 | n/a | pathlib.WindowsPath if os.name == 'nt' else pathlib.PosixPath) |
---|
2009 | n/a | |
---|
2010 | n/a | def test_unsupported_flavour(self): |
---|
2011 | n/a | if os.name == 'nt': |
---|
2012 | n/a | self.assertRaises(NotImplementedError, pathlib.PosixPath) |
---|
2013 | n/a | else: |
---|
2014 | n/a | self.assertRaises(NotImplementedError, pathlib.WindowsPath) |
---|
2015 | n/a | |
---|
2016 | n/a | def test_glob_empty_pattern(self): |
---|
2017 | n/a | p = self.cls() |
---|
2018 | n/a | with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'): |
---|
2019 | n/a | list(p.glob('')) |
---|
2020 | n/a | |
---|
2021 | n/a | |
---|
2022 | n/a | @only_posix |
---|
2023 | n/a | class PosixPathTest(_BasePathTest, unittest.TestCase): |
---|
2024 | n/a | cls = pathlib.PosixPath |
---|
2025 | n/a | |
---|
2026 | n/a | def _check_symlink_loop(self, *args, strict=True): |
---|
2027 | n/a | path = self.cls(*args) |
---|
2028 | n/a | with self.assertRaises(RuntimeError): |
---|
2029 | n/a | print(path.resolve(strict)) |
---|
2030 | n/a | |
---|
2031 | n/a | def test_open_mode(self): |
---|
2032 | n/a | old_mask = os.umask(0) |
---|
2033 | n/a | self.addCleanup(os.umask, old_mask) |
---|
2034 | n/a | p = self.cls(BASE) |
---|
2035 | n/a | with (p / 'new_file').open('wb'): |
---|
2036 | n/a | pass |
---|
2037 | n/a | st = os.stat(join('new_file')) |
---|
2038 | n/a | self.assertEqual(stat.S_IMODE(st.st_mode), 0o666) |
---|
2039 | n/a | os.umask(0o022) |
---|
2040 | n/a | with (p / 'other_new_file').open('wb'): |
---|
2041 | n/a | pass |
---|
2042 | n/a | st = os.stat(join('other_new_file')) |
---|
2043 | n/a | self.assertEqual(stat.S_IMODE(st.st_mode), 0o644) |
---|
2044 | n/a | |
---|
2045 | n/a | def test_touch_mode(self): |
---|
2046 | n/a | old_mask = os.umask(0) |
---|
2047 | n/a | self.addCleanup(os.umask, old_mask) |
---|
2048 | n/a | p = self.cls(BASE) |
---|
2049 | n/a | (p / 'new_file').touch() |
---|
2050 | n/a | st = os.stat(join('new_file')) |
---|
2051 | n/a | self.assertEqual(stat.S_IMODE(st.st_mode), 0o666) |
---|
2052 | n/a | os.umask(0o022) |
---|
2053 | n/a | (p / 'other_new_file').touch() |
---|
2054 | n/a | st = os.stat(join('other_new_file')) |
---|
2055 | n/a | self.assertEqual(stat.S_IMODE(st.st_mode), 0o644) |
---|
2056 | n/a | (p / 'masked_new_file').touch(mode=0o750) |
---|
2057 | n/a | st = os.stat(join('masked_new_file')) |
---|
2058 | n/a | self.assertEqual(stat.S_IMODE(st.st_mode), 0o750) |
---|
2059 | n/a | |
---|
2060 | n/a | @with_symlinks |
---|
2061 | n/a | def test_resolve_loop(self): |
---|
2062 | n/a | # Loops with relative symlinks |
---|
2063 | n/a | os.symlink('linkX/inside', join('linkX')) |
---|
2064 | n/a | self._check_symlink_loop(BASE, 'linkX') |
---|
2065 | n/a | os.symlink('linkY', join('linkY')) |
---|
2066 | n/a | self._check_symlink_loop(BASE, 'linkY') |
---|
2067 | n/a | os.symlink('linkZ/../linkZ', join('linkZ')) |
---|
2068 | n/a | self._check_symlink_loop(BASE, 'linkZ') |
---|
2069 | n/a | # Non-strict |
---|
2070 | n/a | self._check_symlink_loop(BASE, 'linkZ', 'foo', strict=False) |
---|
2071 | n/a | # Loops with absolute symlinks |
---|
2072 | n/a | os.symlink(join('linkU/inside'), join('linkU')) |
---|
2073 | n/a | self._check_symlink_loop(BASE, 'linkU') |
---|
2074 | n/a | os.symlink(join('linkV'), join('linkV')) |
---|
2075 | n/a | self._check_symlink_loop(BASE, 'linkV') |
---|
2076 | n/a | os.symlink(join('linkW/../linkW'), join('linkW')) |
---|
2077 | n/a | self._check_symlink_loop(BASE, 'linkW') |
---|
2078 | n/a | # Non-strict |
---|
2079 | n/a | self._check_symlink_loop(BASE, 'linkW', 'foo', strict=False) |
---|
2080 | n/a | |
---|
2081 | n/a | def test_glob(self): |
---|
2082 | n/a | P = self.cls |
---|
2083 | n/a | p = P(BASE) |
---|
2084 | n/a | given = set(p.glob("FILEa")) |
---|
2085 | n/a | expect = set() if not support.fs_is_case_insensitive(BASE) else given |
---|
2086 | n/a | self.assertEqual(given, expect) |
---|
2087 | n/a | self.assertEqual(set(p.glob("FILEa*")), set()) |
---|
2088 | n/a | |
---|
2089 | n/a | def test_rglob(self): |
---|
2090 | n/a | P = self.cls |
---|
2091 | n/a | p = P(BASE, "dirC") |
---|
2092 | n/a | given = set(p.rglob("FILEd")) |
---|
2093 | n/a | expect = set() if not support.fs_is_case_insensitive(BASE) else given |
---|
2094 | n/a | self.assertEqual(given, expect) |
---|
2095 | n/a | self.assertEqual(set(p.rglob("FILEd*")), set()) |
---|
2096 | n/a | |
---|
2097 | n/a | @unittest.skipUnless(hasattr(pwd, 'getpwall'), |
---|
2098 | n/a | 'pwd module does not expose getpwall()') |
---|
2099 | n/a | def test_expanduser(self): |
---|
2100 | n/a | P = self.cls |
---|
2101 | n/a | support.import_module('pwd') |
---|
2102 | n/a | import pwd |
---|
2103 | n/a | pwdent = pwd.getpwuid(os.getuid()) |
---|
2104 | n/a | username = pwdent.pw_name |
---|
2105 | n/a | userhome = pwdent.pw_dir.rstrip('/') or '/' |
---|
2106 | n/a | # find arbitrary different user (if exists) |
---|
2107 | n/a | for pwdent in pwd.getpwall(): |
---|
2108 | n/a | othername = pwdent.pw_name |
---|
2109 | n/a | otherhome = pwdent.pw_dir.rstrip('/') |
---|
2110 | n/a | if othername != username and otherhome: |
---|
2111 | n/a | break |
---|
2112 | n/a | |
---|
2113 | n/a | p1 = P('~/Documents') |
---|
2114 | n/a | p2 = P('~' + username + '/Documents') |
---|
2115 | n/a | p3 = P('~' + othername + '/Documents') |
---|
2116 | n/a | p4 = P('../~' + username + '/Documents') |
---|
2117 | n/a | p5 = P('/~' + username + '/Documents') |
---|
2118 | n/a | p6 = P('') |
---|
2119 | n/a | p7 = P('~fakeuser/Documents') |
---|
2120 | n/a | |
---|
2121 | n/a | with support.EnvironmentVarGuard() as env: |
---|
2122 | n/a | env.pop('HOME', None) |
---|
2123 | n/a | |
---|
2124 | n/a | self.assertEqual(p1.expanduser(), P(userhome) / 'Documents') |
---|
2125 | n/a | self.assertEqual(p2.expanduser(), P(userhome) / 'Documents') |
---|
2126 | n/a | self.assertEqual(p3.expanduser(), P(otherhome) / 'Documents') |
---|
2127 | n/a | self.assertEqual(p4.expanduser(), p4) |
---|
2128 | n/a | self.assertEqual(p5.expanduser(), p5) |
---|
2129 | n/a | self.assertEqual(p6.expanduser(), p6) |
---|
2130 | n/a | self.assertRaises(RuntimeError, p7.expanduser) |
---|
2131 | n/a | |
---|
2132 | n/a | env['HOME'] = '/tmp' |
---|
2133 | n/a | self.assertEqual(p1.expanduser(), P('/tmp/Documents')) |
---|
2134 | n/a | self.assertEqual(p2.expanduser(), P(userhome) / 'Documents') |
---|
2135 | n/a | self.assertEqual(p3.expanduser(), P(otherhome) / 'Documents') |
---|
2136 | n/a | self.assertEqual(p4.expanduser(), p4) |
---|
2137 | n/a | self.assertEqual(p5.expanduser(), p5) |
---|
2138 | n/a | self.assertEqual(p6.expanduser(), p6) |
---|
2139 | n/a | self.assertRaises(RuntimeError, p7.expanduser) |
---|
2140 | n/a | |
---|
2141 | n/a | |
---|
2142 | n/a | @only_nt |
---|
2143 | n/a | class WindowsPathTest(_BasePathTest, unittest.TestCase): |
---|
2144 | n/a | cls = pathlib.WindowsPath |
---|
2145 | n/a | |
---|
2146 | n/a | def test_glob(self): |
---|
2147 | n/a | P = self.cls |
---|
2148 | n/a | p = P(BASE) |
---|
2149 | n/a | self.assertEqual(set(p.glob("FILEa")), { P(BASE, "fileA") }) |
---|
2150 | n/a | |
---|
2151 | n/a | def test_rglob(self): |
---|
2152 | n/a | P = self.cls |
---|
2153 | n/a | p = P(BASE, "dirC") |
---|
2154 | n/a | self.assertEqual(set(p.rglob("FILEd")), { P(BASE, "dirC/dirD/fileD") }) |
---|
2155 | n/a | |
---|
2156 | n/a | def test_expanduser(self): |
---|
2157 | n/a | P = self.cls |
---|
2158 | n/a | with support.EnvironmentVarGuard() as env: |
---|
2159 | n/a | env.pop('HOME', None) |
---|
2160 | n/a | env.pop('USERPROFILE', None) |
---|
2161 | n/a | env.pop('HOMEPATH', None) |
---|
2162 | n/a | env.pop('HOMEDRIVE', None) |
---|
2163 | n/a | env['USERNAME'] = 'alice' |
---|
2164 | n/a | |
---|
2165 | n/a | # test that the path returns unchanged |
---|
2166 | n/a | p1 = P('~/My Documents') |
---|
2167 | n/a | p2 = P('~alice/My Documents') |
---|
2168 | n/a | p3 = P('~bob/My Documents') |
---|
2169 | n/a | p4 = P('/~/My Documents') |
---|
2170 | n/a | p5 = P('d:~/My Documents') |
---|
2171 | n/a | p6 = P('') |
---|
2172 | n/a | self.assertRaises(RuntimeError, p1.expanduser) |
---|
2173 | n/a | self.assertRaises(RuntimeError, p2.expanduser) |
---|
2174 | n/a | self.assertRaises(RuntimeError, p3.expanduser) |
---|
2175 | n/a | self.assertEqual(p4.expanduser(), p4) |
---|
2176 | n/a | self.assertEqual(p5.expanduser(), p5) |
---|
2177 | n/a | self.assertEqual(p6.expanduser(), p6) |
---|
2178 | n/a | |
---|
2179 | n/a | def check(): |
---|
2180 | n/a | env.pop('USERNAME', None) |
---|
2181 | n/a | self.assertEqual(p1.expanduser(), |
---|
2182 | n/a | P('C:/Users/alice/My Documents')) |
---|
2183 | n/a | self.assertRaises(KeyError, p2.expanduser) |
---|
2184 | n/a | env['USERNAME'] = 'alice' |
---|
2185 | n/a | self.assertEqual(p2.expanduser(), |
---|
2186 | n/a | P('C:/Users/alice/My Documents')) |
---|
2187 | n/a | self.assertEqual(p3.expanduser(), |
---|
2188 | n/a | P('C:/Users/bob/My Documents')) |
---|
2189 | n/a | self.assertEqual(p4.expanduser(), p4) |
---|
2190 | n/a | self.assertEqual(p5.expanduser(), p5) |
---|
2191 | n/a | self.assertEqual(p6.expanduser(), p6) |
---|
2192 | n/a | |
---|
2193 | n/a | # test the first lookup key in the env vars |
---|
2194 | n/a | env['HOME'] = 'C:\\Users\\alice' |
---|
2195 | n/a | check() |
---|
2196 | n/a | |
---|
2197 | n/a | # test that HOMEPATH is available instead |
---|
2198 | n/a | env.pop('HOME', None) |
---|
2199 | n/a | env['HOMEPATH'] = 'C:\\Users\\alice' |
---|
2200 | n/a | check() |
---|
2201 | n/a | |
---|
2202 | n/a | env['HOMEDRIVE'] = 'C:\\' |
---|
2203 | n/a | env['HOMEPATH'] = 'Users\\alice' |
---|
2204 | n/a | check() |
---|
2205 | n/a | |
---|
2206 | n/a | env.pop('HOMEDRIVE', None) |
---|
2207 | n/a | env.pop('HOMEPATH', None) |
---|
2208 | n/a | env['USERPROFILE'] = 'C:\\Users\\alice' |
---|
2209 | n/a | check() |
---|
2210 | n/a | |
---|
2211 | n/a | |
---|
2212 | n/a | if __name__ == "__main__": |
---|
2213 | n/a | unittest.main() |
---|