Debian Codegraph @

Lib/test/test_genericpath.py

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