1 | n/a | """ |
---|
2 | n/a | Tests common to genericpath, macpath, ntpath and posixpath |
---|
3 | n/a | """ |
---|
4 | n/a | |
---|
5 | n/a | import genericpath |
---|
6 | n/a | import os |
---|
7 | n/a | import sys |
---|
8 | n/a | import unittest |
---|
9 | n/a | import warnings |
---|
10 | n/a | from test import support |
---|
11 | n/a | android_not_root = support.android_not_root |
---|
12 | n/a | |
---|
13 | n/a | |
---|
14 | n/a | def create_file(filename, data=b'foo'): |
---|
15 | n/a | with open(filename, 'xb', 0) as fp: |
---|
16 | n/a | fp.write(data) |
---|
17 | n/a | |
---|
18 | n/a | |
---|
19 | n/a | class GenericTest: |
---|
20 | n/a | common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime', |
---|
21 | n/a | 'getmtime', 'exists', 'isdir', 'isfile'] |
---|
22 | n/a | attributes = [] |
---|
23 | n/a | |
---|
24 | n/a | def test_no_argument(self): |
---|
25 | n/a | for attr in self.common_attributes + self.attributes: |
---|
26 | n/a | with self.assertRaises(TypeError): |
---|
27 | n/a | getattr(self.pathmodule, attr)() |
---|
28 | n/a | raise self.fail("{}.{}() did not raise a TypeError" |
---|
29 | n/a | .format(self.pathmodule.__name__, attr)) |
---|
30 | n/a | |
---|
31 | n/a | def test_commonprefix(self): |
---|
32 | n/a | commonprefix = self.pathmodule.commonprefix |
---|
33 | n/a | self.assertEqual( |
---|
34 | n/a | commonprefix([]), |
---|
35 | n/a | "" |
---|
36 | n/a | ) |
---|
37 | n/a | self.assertEqual( |
---|
38 | n/a | commonprefix(["/home/swenson/spam", "/home/swen/spam"]), |
---|
39 | n/a | "/home/swen" |
---|
40 | n/a | ) |
---|
41 | n/a | self.assertEqual( |
---|
42 | n/a | commonprefix(["/home/swen/spam", "/home/swen/eggs"]), |
---|
43 | n/a | "/home/swen/" |
---|
44 | n/a | ) |
---|
45 | n/a | self.assertEqual( |
---|
46 | n/a | commonprefix(["/home/swen/spam", "/home/swen/spam"]), |
---|
47 | n/a | "/home/swen/spam" |
---|
48 | n/a | ) |
---|
49 | n/a | self.assertEqual( |
---|
50 | n/a | commonprefix(["home:swenson:spam", "home:swen:spam"]), |
---|
51 | n/a | "home:swen" |
---|
52 | n/a | ) |
---|
53 | n/a | self.assertEqual( |
---|
54 | n/a | commonprefix([":home:swen:spam", ":home:swen:eggs"]), |
---|
55 | n/a | ":home:swen:" |
---|
56 | n/a | ) |
---|
57 | n/a | self.assertEqual( |
---|
58 | n/a | commonprefix([":home:swen:spam", ":home:swen:spam"]), |
---|
59 | n/a | ":home:swen:spam" |
---|
60 | n/a | ) |
---|
61 | n/a | |
---|
62 | n/a | self.assertEqual( |
---|
63 | n/a | commonprefix([b"/home/swenson/spam", b"/home/swen/spam"]), |
---|
64 | n/a | b"/home/swen" |
---|
65 | n/a | ) |
---|
66 | n/a | self.assertEqual( |
---|
67 | n/a | commonprefix([b"/home/swen/spam", b"/home/swen/eggs"]), |
---|
68 | n/a | b"/home/swen/" |
---|
69 | n/a | ) |
---|
70 | n/a | self.assertEqual( |
---|
71 | n/a | commonprefix([b"/home/swen/spam", b"/home/swen/spam"]), |
---|
72 | n/a | b"/home/swen/spam" |
---|
73 | n/a | ) |
---|
74 | n/a | self.assertEqual( |
---|
75 | n/a | commonprefix([b"home:swenson:spam", b"home:swen:spam"]), |
---|
76 | n/a | b"home:swen" |
---|
77 | n/a | ) |
---|
78 | n/a | self.assertEqual( |
---|
79 | n/a | commonprefix([b":home:swen:spam", b":home:swen:eggs"]), |
---|
80 | n/a | b":home:swen:" |
---|
81 | n/a | ) |
---|
82 | n/a | self.assertEqual( |
---|
83 | n/a | commonprefix([b":home:swen:spam", b":home:swen:spam"]), |
---|
84 | n/a | b":home:swen:spam" |
---|
85 | n/a | ) |
---|
86 | n/a | |
---|
87 | n/a | testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd', |
---|
88 | n/a | 'aXc', 'abd', 'ab', 'aX', 'abcX'] |
---|
89 | n/a | for s1 in testlist: |
---|
90 | n/a | for s2 in testlist: |
---|
91 | n/a | p = commonprefix([s1, s2]) |
---|
92 | n/a | self.assertTrue(s1.startswith(p)) |
---|
93 | n/a | self.assertTrue(s2.startswith(p)) |
---|
94 | n/a | if s1 != s2: |
---|
95 | n/a | n = len(p) |
---|
96 | n/a | self.assertNotEqual(s1[n:n+1], s2[n:n+1]) |
---|
97 | n/a | |
---|
98 | n/a | def test_getsize(self): |
---|
99 | n/a | filename = support.TESTFN |
---|
100 | n/a | self.addCleanup(support.unlink, filename) |
---|
101 | n/a | |
---|
102 | n/a | create_file(filename, b'Hello') |
---|
103 | n/a | self.assertEqual(self.pathmodule.getsize(filename), 5) |
---|
104 | n/a | os.remove(filename) |
---|
105 | n/a | |
---|
106 | n/a | create_file(filename, b'Hello World!') |
---|
107 | n/a | self.assertEqual(self.pathmodule.getsize(filename), 12) |
---|
108 | n/a | |
---|
109 | n/a | def test_filetime(self): |
---|
110 | n/a | filename = support.TESTFN |
---|
111 | n/a | self.addCleanup(support.unlink, filename) |
---|
112 | n/a | |
---|
113 | n/a | create_file(filename, b'foo') |
---|
114 | n/a | |
---|
115 | n/a | with open(filename, "ab", 0) as f: |
---|
116 | n/a | f.write(b"bar") |
---|
117 | n/a | |
---|
118 | n/a | with open(filename, "rb", 0) as f: |
---|
119 | n/a | data = f.read() |
---|
120 | n/a | self.assertEqual(data, b"foobar") |
---|
121 | n/a | |
---|
122 | n/a | self.assertLessEqual( |
---|
123 | n/a | self.pathmodule.getctime(filename), |
---|
124 | n/a | self.pathmodule.getmtime(filename) |
---|
125 | n/a | ) |
---|
126 | n/a | |
---|
127 | n/a | def test_exists(self): |
---|
128 | n/a | filename = support.TESTFN |
---|
129 | n/a | self.addCleanup(support.unlink, filename) |
---|
130 | n/a | |
---|
131 | n/a | self.assertIs(self.pathmodule.exists(filename), False) |
---|
132 | n/a | |
---|
133 | n/a | with open(filename, "xb") as f: |
---|
134 | n/a | f.write(b"foo") |
---|
135 | n/a | |
---|
136 | n/a | self.assertIs(self.pathmodule.exists(filename), True) |
---|
137 | n/a | |
---|
138 | n/a | if not self.pathmodule == genericpath: |
---|
139 | n/a | self.assertIs(self.pathmodule.lexists(filename), True) |
---|
140 | n/a | |
---|
141 | n/a | @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()") |
---|
142 | n/a | def test_exists_fd(self): |
---|
143 | n/a | r, w = os.pipe() |
---|
144 | n/a | try: |
---|
145 | n/a | self.assertTrue(self.pathmodule.exists(r)) |
---|
146 | n/a | finally: |
---|
147 | n/a | os.close(r) |
---|
148 | n/a | os.close(w) |
---|
149 | n/a | self.assertFalse(self.pathmodule.exists(r)) |
---|
150 | n/a | |
---|
151 | n/a | def test_isdir_file(self): |
---|
152 | n/a | filename = support.TESTFN |
---|
153 | n/a | self.addCleanup(support.unlink, filename) |
---|
154 | n/a | self.assertIs(self.pathmodule.isdir(filename), False) |
---|
155 | n/a | |
---|
156 | n/a | create_file(filename) |
---|
157 | n/a | self.assertIs(self.pathmodule.isdir(filename), False) |
---|
158 | n/a | |
---|
159 | n/a | def test_isdir_dir(self): |
---|
160 | n/a | filename = support.TESTFN |
---|
161 | n/a | self.addCleanup(support.rmdir, filename) |
---|
162 | n/a | self.assertIs(self.pathmodule.isdir(filename), False) |
---|
163 | n/a | |
---|
164 | n/a | os.mkdir(filename) |
---|
165 | n/a | self.assertIs(self.pathmodule.isdir(filename), True) |
---|
166 | n/a | |
---|
167 | n/a | def test_isfile_file(self): |
---|
168 | n/a | filename = support.TESTFN |
---|
169 | n/a | self.addCleanup(support.unlink, filename) |
---|
170 | n/a | self.assertIs(self.pathmodule.isfile(filename), False) |
---|
171 | n/a | |
---|
172 | n/a | create_file(filename) |
---|
173 | n/a | self.assertIs(self.pathmodule.isfile(filename), True) |
---|
174 | n/a | |
---|
175 | n/a | def test_isfile_dir(self): |
---|
176 | n/a | filename = support.TESTFN |
---|
177 | n/a | self.addCleanup(support.rmdir, filename) |
---|
178 | n/a | self.assertIs(self.pathmodule.isfile(filename), False) |
---|
179 | n/a | |
---|
180 | n/a | os.mkdir(filename) |
---|
181 | n/a | self.assertIs(self.pathmodule.isfile(filename), False) |
---|
182 | n/a | |
---|
183 | n/a | def test_samefile(self): |
---|
184 | n/a | file1 = support.TESTFN |
---|
185 | n/a | file2 = support.TESTFN + "2" |
---|
186 | n/a | self.addCleanup(support.unlink, file1) |
---|
187 | n/a | self.addCleanup(support.unlink, file2) |
---|
188 | n/a | |
---|
189 | n/a | create_file(file1) |
---|
190 | n/a | self.assertTrue(self.pathmodule.samefile(file1, file1)) |
---|
191 | n/a | |
---|
192 | n/a | create_file(file2) |
---|
193 | n/a | self.assertFalse(self.pathmodule.samefile(file1, file2)) |
---|
194 | n/a | |
---|
195 | n/a | self.assertRaises(TypeError, self.pathmodule.samefile) |
---|
196 | n/a | |
---|
197 | n/a | def _test_samefile_on_link_func(self, func): |
---|
198 | n/a | test_fn1 = support.TESTFN |
---|
199 | n/a | test_fn2 = support.TESTFN + "2" |
---|
200 | n/a | self.addCleanup(support.unlink, test_fn1) |
---|
201 | n/a | self.addCleanup(support.unlink, test_fn2) |
---|
202 | n/a | |
---|
203 | n/a | create_file(test_fn1) |
---|
204 | n/a | |
---|
205 | n/a | func(test_fn1, test_fn2) |
---|
206 | n/a | self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2)) |
---|
207 | n/a | os.remove(test_fn2) |
---|
208 | n/a | |
---|
209 | n/a | create_file(test_fn2) |
---|
210 | n/a | self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2)) |
---|
211 | n/a | |
---|
212 | n/a | @support.skip_unless_symlink |
---|
213 | n/a | def test_samefile_on_symlink(self): |
---|
214 | n/a | self._test_samefile_on_link_func(os.symlink) |
---|
215 | n/a | |
---|
216 | n/a | @unittest.skipIf(android_not_root, "hard links not allowed, non root user") |
---|
217 | n/a | def test_samefile_on_link(self): |
---|
218 | n/a | self._test_samefile_on_link_func(os.link) |
---|
219 | n/a | |
---|
220 | n/a | def test_samestat(self): |
---|
221 | n/a | test_fn1 = support.TESTFN |
---|
222 | n/a | test_fn2 = support.TESTFN + "2" |
---|
223 | n/a | self.addCleanup(support.unlink, test_fn1) |
---|
224 | n/a | self.addCleanup(support.unlink, test_fn2) |
---|
225 | n/a | |
---|
226 | n/a | create_file(test_fn1) |
---|
227 | n/a | stat1 = os.stat(test_fn1) |
---|
228 | n/a | self.assertTrue(self.pathmodule.samestat(stat1, os.stat(test_fn1))) |
---|
229 | n/a | |
---|
230 | n/a | create_file(test_fn2) |
---|
231 | n/a | stat2 = os.stat(test_fn2) |
---|
232 | n/a | self.assertFalse(self.pathmodule.samestat(stat1, stat2)) |
---|
233 | n/a | |
---|
234 | n/a | self.assertRaises(TypeError, self.pathmodule.samestat) |
---|
235 | n/a | |
---|
236 | n/a | def _test_samestat_on_link_func(self, func): |
---|
237 | n/a | test_fn1 = support.TESTFN + "1" |
---|
238 | n/a | test_fn2 = support.TESTFN + "2" |
---|
239 | n/a | self.addCleanup(support.unlink, test_fn1) |
---|
240 | n/a | self.addCleanup(support.unlink, test_fn2) |
---|
241 | n/a | |
---|
242 | n/a | create_file(test_fn1) |
---|
243 | n/a | func(test_fn1, test_fn2) |
---|
244 | n/a | self.assertTrue(self.pathmodule.samestat(os.stat(test_fn1), |
---|
245 | n/a | os.stat(test_fn2))) |
---|
246 | n/a | os.remove(test_fn2) |
---|
247 | n/a | |
---|
248 | n/a | create_file(test_fn2) |
---|
249 | n/a | self.assertFalse(self.pathmodule.samestat(os.stat(test_fn1), |
---|
250 | n/a | os.stat(test_fn2))) |
---|
251 | n/a | |
---|
252 | n/a | @support.skip_unless_symlink |
---|
253 | n/a | def test_samestat_on_symlink(self): |
---|
254 | n/a | self._test_samestat_on_link_func(os.symlink) |
---|
255 | n/a | |
---|
256 | n/a | @unittest.skipIf(android_not_root, "hard links not allowed, non root user") |
---|
257 | n/a | def test_samestat_on_link(self): |
---|
258 | n/a | self._test_samestat_on_link_func(os.link) |
---|
259 | n/a | |
---|
260 | n/a | def test_sameopenfile(self): |
---|
261 | n/a | filename = support.TESTFN |
---|
262 | n/a | self.addCleanup(support.unlink, filename) |
---|
263 | n/a | create_file(filename) |
---|
264 | n/a | |
---|
265 | n/a | with open(filename, "rb", 0) as fp1: |
---|
266 | n/a | fd1 = fp1.fileno() |
---|
267 | n/a | with open(filename, "rb", 0) as fp2: |
---|
268 | n/a | fd2 = fp2.fileno() |
---|
269 | n/a | self.assertTrue(self.pathmodule.sameopenfile(fd1, fd2)) |
---|
270 | n/a | |
---|
271 | n/a | |
---|
272 | n/a | class TestGenericTest(GenericTest, unittest.TestCase): |
---|
273 | n/a | # Issue 16852: GenericTest can't inherit from unittest.TestCase |
---|
274 | n/a | # for test discovery purposes; CommonTest inherits from GenericTest |
---|
275 | n/a | # and is only meant to be inherited by others. |
---|
276 | n/a | pathmodule = genericpath |
---|
277 | n/a | |
---|
278 | n/a | def test_null_bytes(self): |
---|
279 | n/a | for attr in GenericTest.common_attributes: |
---|
280 | n/a | # os.path.commonprefix doesn't raise ValueError |
---|
281 | n/a | if attr == 'commonprefix': |
---|
282 | n/a | continue |
---|
283 | n/a | with self.subTest(attr=attr): |
---|
284 | n/a | with self.assertRaises(ValueError) as cm: |
---|
285 | n/a | getattr(self.pathmodule, attr)('/tmp\x00abcds') |
---|
286 | n/a | self.assertIn('embedded null', str(cm.exception)) |
---|
287 | n/a | |
---|
288 | n/a | # Following TestCase is not supposed to be run from test_genericpath. |
---|
289 | n/a | # It is inherited by other test modules (macpath, ntpath, posixpath). |
---|
290 | n/a | |
---|
291 | n/a | class CommonTest(GenericTest): |
---|
292 | n/a | common_attributes = GenericTest.common_attributes + [ |
---|
293 | n/a | # Properties |
---|
294 | n/a | 'curdir', 'pardir', 'extsep', 'sep', |
---|
295 | n/a | 'pathsep', 'defpath', 'altsep', 'devnull', |
---|
296 | n/a | # Methods |
---|
297 | n/a | 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath', |
---|
298 | n/a | 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname', |
---|
299 | n/a | 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath', |
---|
300 | n/a | ] |
---|
301 | n/a | |
---|
302 | n/a | def test_normcase(self): |
---|
303 | n/a | normcase = self.pathmodule.normcase |
---|
304 | n/a | # check that normcase() is idempotent |
---|
305 | n/a | for p in ["FoO/./BaR", b"FoO/./BaR"]: |
---|
306 | n/a | p = normcase(p) |
---|
307 | n/a | self.assertEqual(p, normcase(p)) |
---|
308 | n/a | |
---|
309 | n/a | self.assertEqual(normcase(''), '') |
---|
310 | n/a | self.assertEqual(normcase(b''), b'') |
---|
311 | n/a | |
---|
312 | n/a | # check that normcase raises a TypeError for invalid types |
---|
313 | n/a | for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}): |
---|
314 | n/a | self.assertRaises(TypeError, normcase, path) |
---|
315 | n/a | |
---|
316 | n/a | def test_splitdrive(self): |
---|
317 | n/a | # splitdrive for non-NT paths |
---|
318 | n/a | splitdrive = self.pathmodule.splitdrive |
---|
319 | n/a | self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar")) |
---|
320 | n/a | self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar")) |
---|
321 | n/a | self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar")) |
---|
322 | n/a | |
---|
323 | n/a | self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar")) |
---|
324 | n/a | self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar")) |
---|
325 | n/a | self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar")) |
---|
326 | n/a | |
---|
327 | n/a | def test_expandvars(self): |
---|
328 | n/a | if self.pathmodule.__name__ == 'macpath': |
---|
329 | n/a | self.skipTest('macpath.expandvars is a stub') |
---|
330 | n/a | expandvars = self.pathmodule.expandvars |
---|
331 | n/a | with support.EnvironmentVarGuard() as env: |
---|
332 | n/a | env.clear() |
---|
333 | n/a | env["foo"] = "bar" |
---|
334 | n/a | env["{foo"] = "baz1" |
---|
335 | n/a | env["{foo}"] = "baz2" |
---|
336 | n/a | self.assertEqual(expandvars("foo"), "foo") |
---|
337 | n/a | self.assertEqual(expandvars("$foo bar"), "bar bar") |
---|
338 | n/a | self.assertEqual(expandvars("${foo}bar"), "barbar") |
---|
339 | n/a | self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar") |
---|
340 | n/a | self.assertEqual(expandvars("$bar bar"), "$bar bar") |
---|
341 | n/a | self.assertEqual(expandvars("$?bar"), "$?bar") |
---|
342 | n/a | self.assertEqual(expandvars("$foo}bar"), "bar}bar") |
---|
343 | n/a | self.assertEqual(expandvars("${foo"), "${foo") |
---|
344 | n/a | self.assertEqual(expandvars("${{foo}}"), "baz1}") |
---|
345 | n/a | self.assertEqual(expandvars("$foo$foo"), "barbar") |
---|
346 | n/a | self.assertEqual(expandvars("$bar$bar"), "$bar$bar") |
---|
347 | n/a | |
---|
348 | n/a | self.assertEqual(expandvars(b"foo"), b"foo") |
---|
349 | n/a | self.assertEqual(expandvars(b"$foo bar"), b"bar bar") |
---|
350 | n/a | self.assertEqual(expandvars(b"${foo}bar"), b"barbar") |
---|
351 | n/a | self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar") |
---|
352 | n/a | self.assertEqual(expandvars(b"$bar bar"), b"$bar bar") |
---|
353 | n/a | self.assertEqual(expandvars(b"$?bar"), b"$?bar") |
---|
354 | n/a | self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar") |
---|
355 | n/a | self.assertEqual(expandvars(b"${foo"), b"${foo") |
---|
356 | n/a | self.assertEqual(expandvars(b"${{foo}}"), b"baz1}") |
---|
357 | n/a | self.assertEqual(expandvars(b"$foo$foo"), b"barbar") |
---|
358 | n/a | self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar") |
---|
359 | n/a | |
---|
360 | n/a | @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII') |
---|
361 | n/a | def test_expandvars_nonascii(self): |
---|
362 | n/a | if self.pathmodule.__name__ == 'macpath': |
---|
363 | n/a | self.skipTest('macpath.expandvars is a stub') |
---|
364 | n/a | expandvars = self.pathmodule.expandvars |
---|
365 | n/a | def check(value, expected): |
---|
366 | n/a | self.assertEqual(expandvars(value), expected) |
---|
367 | n/a | with support.EnvironmentVarGuard() as env: |
---|
368 | n/a | env.clear() |
---|
369 | n/a | nonascii = support.FS_NONASCII |
---|
370 | n/a | env['spam'] = nonascii |
---|
371 | n/a | env[nonascii] = 'ham' + nonascii |
---|
372 | n/a | check(nonascii, nonascii) |
---|
373 | n/a | check('$spam bar', '%s bar' % nonascii) |
---|
374 | n/a | check('${spam}bar', '%sbar' % nonascii) |
---|
375 | n/a | check('${%s}bar' % nonascii, 'ham%sbar' % nonascii) |
---|
376 | n/a | check('$bar%s bar' % nonascii, '$bar%s bar' % nonascii) |
---|
377 | n/a | check('$spam}bar', '%s}bar' % nonascii) |
---|
378 | n/a | |
---|
379 | n/a | check(os.fsencode(nonascii), os.fsencode(nonascii)) |
---|
380 | n/a | check(b'$spam bar', os.fsencode('%s bar' % nonascii)) |
---|
381 | n/a | check(b'${spam}bar', os.fsencode('%sbar' % nonascii)) |
---|
382 | n/a | check(os.fsencode('${%s}bar' % nonascii), |
---|
383 | n/a | os.fsencode('ham%sbar' % nonascii)) |
---|
384 | n/a | check(os.fsencode('$bar%s bar' % nonascii), |
---|
385 | n/a | os.fsencode('$bar%s bar' % nonascii)) |
---|
386 | n/a | check(b'$spam}bar', os.fsencode('%s}bar' % nonascii)) |
---|
387 | n/a | |
---|
388 | n/a | def test_abspath(self): |
---|
389 | n/a | self.assertIn("foo", self.pathmodule.abspath("foo")) |
---|
390 | n/a | with warnings.catch_warnings(): |
---|
391 | n/a | warnings.simplefilter("ignore", DeprecationWarning) |
---|
392 | n/a | self.assertIn(b"foo", self.pathmodule.abspath(b"foo")) |
---|
393 | n/a | |
---|
394 | n/a | # avoid UnicodeDecodeError on Windows |
---|
395 | n/a | undecodable_path = b'' if sys.platform == 'win32' else b'f\xf2\xf2' |
---|
396 | n/a | |
---|
397 | n/a | # Abspath returns bytes when the arg is bytes |
---|
398 | n/a | with warnings.catch_warnings(): |
---|
399 | n/a | warnings.simplefilter("ignore", DeprecationWarning) |
---|
400 | n/a | for path in (b'', b'foo', undecodable_path, b'/foo', b'C:\\'): |
---|
401 | n/a | self.assertIsInstance(self.pathmodule.abspath(path), bytes) |
---|
402 | n/a | |
---|
403 | n/a | def test_realpath(self): |
---|
404 | n/a | self.assertIn("foo", self.pathmodule.realpath("foo")) |
---|
405 | n/a | with warnings.catch_warnings(): |
---|
406 | n/a | warnings.simplefilter("ignore", DeprecationWarning) |
---|
407 | n/a | self.assertIn(b"foo", self.pathmodule.realpath(b"foo")) |
---|
408 | n/a | |
---|
409 | n/a | def test_normpath_issue5827(self): |
---|
410 | n/a | # Make sure normpath preserves unicode |
---|
411 | n/a | for path in ('', '.', '/', '\\', '///foo/.//bar//'): |
---|
412 | n/a | self.assertIsInstance(self.pathmodule.normpath(path), str) |
---|
413 | n/a | |
---|
414 | n/a | def test_abspath_issue3426(self): |
---|
415 | n/a | # Check that abspath returns unicode when the arg is unicode |
---|
416 | n/a | # with both ASCII and non-ASCII cwds. |
---|
417 | n/a | abspath = self.pathmodule.abspath |
---|
418 | n/a | for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'): |
---|
419 | n/a | self.assertIsInstance(abspath(path), str) |
---|
420 | n/a | |
---|
421 | n/a | unicwd = '\xe7w\xf0' |
---|
422 | n/a | try: |
---|
423 | n/a | os.fsencode(unicwd) |
---|
424 | n/a | except (AttributeError, UnicodeEncodeError): |
---|
425 | n/a | # FS encoding is probably ASCII |
---|
426 | n/a | pass |
---|
427 | n/a | else: |
---|
428 | n/a | with support.temp_cwd(unicwd): |
---|
429 | n/a | for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'): |
---|
430 | n/a | self.assertIsInstance(abspath(path), str) |
---|
431 | n/a | |
---|
432 | n/a | def test_nonascii_abspath(self): |
---|
433 | n/a | if (support.TESTFN_UNDECODABLE |
---|
434 | n/a | # Mac OS X denies the creation of a directory with an invalid |
---|
435 | n/a | # UTF-8 name. Windows allows creating a directory with an |
---|
436 | n/a | # arbitrary bytes name, but fails to enter this directory |
---|
437 | n/a | # (when the bytes name is used). |
---|
438 | n/a | and sys.platform not in ('win32', 'darwin')): |
---|
439 | n/a | name = support.TESTFN_UNDECODABLE |
---|
440 | n/a | elif support.TESTFN_NONASCII: |
---|
441 | n/a | name = support.TESTFN_NONASCII |
---|
442 | n/a | else: |
---|
443 | n/a | self.skipTest("need support.TESTFN_NONASCII") |
---|
444 | n/a | |
---|
445 | n/a | with warnings.catch_warnings(): |
---|
446 | n/a | warnings.simplefilter("ignore", DeprecationWarning) |
---|
447 | n/a | with support.temp_cwd(name): |
---|
448 | n/a | self.test_abspath() |
---|
449 | n/a | |
---|
450 | n/a | def test_join_errors(self): |
---|
451 | n/a | # Check join() raises friendly TypeErrors. |
---|
452 | n/a | with support.check_warnings(('', BytesWarning), quiet=True): |
---|
453 | n/a | errmsg = "Can't mix strings and bytes in path components" |
---|
454 | n/a | with self.assertRaisesRegex(TypeError, errmsg): |
---|
455 | n/a | self.pathmodule.join(b'bytes', 'str') |
---|
456 | n/a | with self.assertRaisesRegex(TypeError, errmsg): |
---|
457 | n/a | self.pathmodule.join('str', b'bytes') |
---|
458 | n/a | # regression, see #15377 |
---|
459 | n/a | with self.assertRaisesRegex(TypeError, 'int'): |
---|
460 | n/a | self.pathmodule.join(42, 'str') |
---|
461 | n/a | with self.assertRaisesRegex(TypeError, 'int'): |
---|
462 | n/a | self.pathmodule.join('str', 42) |
---|
463 | n/a | with self.assertRaisesRegex(TypeError, 'int'): |
---|
464 | n/a | self.pathmodule.join(42) |
---|
465 | n/a | with self.assertRaisesRegex(TypeError, 'list'): |
---|
466 | n/a | self.pathmodule.join([]) |
---|
467 | n/a | with self.assertRaisesRegex(TypeError, 'bytearray'): |
---|
468 | n/a | self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar')) |
---|
469 | n/a | |
---|
470 | n/a | def test_relpath_errors(self): |
---|
471 | n/a | # Check relpath() raises friendly TypeErrors. |
---|
472 | n/a | with support.check_warnings(('', (BytesWarning, DeprecationWarning)), |
---|
473 | n/a | quiet=True): |
---|
474 | n/a | errmsg = "Can't mix strings and bytes in path components" |
---|
475 | n/a | with self.assertRaisesRegex(TypeError, errmsg): |
---|
476 | n/a | self.pathmodule.relpath(b'bytes', 'str') |
---|
477 | n/a | with self.assertRaisesRegex(TypeError, errmsg): |
---|
478 | n/a | self.pathmodule.relpath('str', b'bytes') |
---|
479 | n/a | with self.assertRaisesRegex(TypeError, 'int'): |
---|
480 | n/a | self.pathmodule.relpath(42, 'str') |
---|
481 | n/a | with self.assertRaisesRegex(TypeError, 'int'): |
---|
482 | n/a | self.pathmodule.relpath('str', 42) |
---|
483 | n/a | with self.assertRaisesRegex(TypeError, 'bytearray'): |
---|
484 | n/a | self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar')) |
---|
485 | n/a | |
---|
486 | n/a | |
---|
487 | n/a | class PathLikeTests(unittest.TestCase): |
---|
488 | n/a | |
---|
489 | n/a | class PathLike: |
---|
490 | n/a | def __init__(self, path=''): |
---|
491 | n/a | self.path = path |
---|
492 | n/a | def __fspath__(self): |
---|
493 | n/a | if isinstance(self.path, BaseException): |
---|
494 | n/a | raise self.path |
---|
495 | n/a | else: |
---|
496 | n/a | return self.path |
---|
497 | n/a | |
---|
498 | n/a | def setUp(self): |
---|
499 | n/a | self.file_name = support.TESTFN.lower() |
---|
500 | n/a | self.file_path = self.PathLike(support.TESTFN) |
---|
501 | n/a | self.addCleanup(support.unlink, self.file_name) |
---|
502 | n/a | create_file(self.file_name, b"test_genericpath.PathLikeTests") |
---|
503 | n/a | |
---|
504 | n/a | def assertPathEqual(self, func): |
---|
505 | n/a | self.assertEqual(func(self.file_path), func(self.file_name)) |
---|
506 | n/a | |
---|
507 | n/a | def test_path_exists(self): |
---|
508 | n/a | self.assertPathEqual(os.path.exists) |
---|
509 | n/a | |
---|
510 | n/a | def test_path_isfile(self): |
---|
511 | n/a | self.assertPathEqual(os.path.isfile) |
---|
512 | n/a | |
---|
513 | n/a | def test_path_isdir(self): |
---|
514 | n/a | self.assertPathEqual(os.path.isdir) |
---|
515 | n/a | |
---|
516 | n/a | def test_path_commonprefix(self): |
---|
517 | n/a | self.assertEqual(os.path.commonprefix([self.file_path, self.file_name]), |
---|
518 | n/a | self.file_name) |
---|
519 | n/a | |
---|
520 | n/a | def test_path_getsize(self): |
---|
521 | n/a | self.assertPathEqual(os.path.getsize) |
---|
522 | n/a | |
---|
523 | n/a | def test_path_getmtime(self): |
---|
524 | n/a | self.assertPathEqual(os.path.getatime) |
---|
525 | n/a | |
---|
526 | n/a | def test_path_getctime(self): |
---|
527 | n/a | self.assertPathEqual(os.path.getctime) |
---|
528 | n/a | |
---|
529 | n/a | def test_path_samefile(self): |
---|
530 | n/a | self.assertTrue(os.path.samefile(self.file_path, self.file_name)) |
---|
531 | n/a | |
---|
532 | n/a | |
---|
533 | n/a | if __name__=="__main__": |
---|
534 | n/a | unittest.main() |
---|