test_genericpath.py 22 KB

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