test_ntpath.py 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. import ntpath
  2. import os
  3. import sys
  4. import unittest
  5. import warnings
  6. from test.support import os_helper
  7. from test.support import TestFailed, is_emscripten
  8. from test.support.os_helper import FakePath
  9. from test import test_genericpath
  10. from tempfile import TemporaryFile
  11. try:
  12. import nt
  13. except ImportError:
  14. # Most tests can complete without the nt module,
  15. # but for those that require it we import here.
  16. nt = None
  17. try:
  18. ntpath._getfinalpathname
  19. except AttributeError:
  20. HAVE_GETFINALPATHNAME = False
  21. else:
  22. HAVE_GETFINALPATHNAME = True
  23. try:
  24. import ctypes
  25. except ImportError:
  26. HAVE_GETSHORTPATHNAME = False
  27. else:
  28. HAVE_GETSHORTPATHNAME = True
  29. def _getshortpathname(path):
  30. GSPN = ctypes.WinDLL("kernel32", use_last_error=True).GetShortPathNameW
  31. GSPN.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32]
  32. GSPN.restype = ctypes.c_uint32
  33. result_len = GSPN(path, None, 0)
  34. if not result_len:
  35. raise OSError("failed to get short path name 0x{:08X}"
  36. .format(ctypes.get_last_error()))
  37. result = ctypes.create_unicode_buffer(result_len)
  38. result_len = GSPN(path, result, result_len)
  39. return result[:result_len]
  40. def _norm(path):
  41. if isinstance(path, (bytes, str, os.PathLike)):
  42. return ntpath.normcase(os.fsdecode(path))
  43. elif hasattr(path, "__iter__"):
  44. return tuple(ntpath.normcase(os.fsdecode(p)) for p in path)
  45. return path
  46. def tester(fn, wantResult):
  47. fn = fn.replace("\\", "\\\\")
  48. gotResult = eval(fn)
  49. if wantResult != gotResult and _norm(wantResult) != _norm(gotResult):
  50. raise TestFailed("%s should return: %s but returned: %s" \
  51. %(str(fn), str(wantResult), str(gotResult)))
  52. # then with bytes
  53. fn = fn.replace("('", "(b'")
  54. fn = fn.replace('("', '(b"')
  55. fn = fn.replace("['", "[b'")
  56. fn = fn.replace('["', '[b"')
  57. fn = fn.replace(", '", ", b'")
  58. fn = fn.replace(', "', ', b"')
  59. fn = os.fsencode(fn).decode('latin1')
  60. fn = fn.encode('ascii', 'backslashreplace').decode('ascii')
  61. with warnings.catch_warnings():
  62. warnings.simplefilter("ignore", DeprecationWarning)
  63. gotResult = eval(fn)
  64. if _norm(wantResult) != _norm(gotResult):
  65. raise TestFailed("%s should return: %s but returned: %s" \
  66. %(str(fn), str(wantResult), repr(gotResult)))
  67. class NtpathTestCase(unittest.TestCase):
  68. def assertPathEqual(self, path1, path2):
  69. if path1 == path2 or _norm(path1) == _norm(path2):
  70. return
  71. self.assertEqual(path1, path2)
  72. def assertPathIn(self, path, pathset):
  73. self.assertIn(_norm(path), _norm(pathset))
  74. class TestNtpath(NtpathTestCase):
  75. def test_splitext(self):
  76. tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
  77. tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext'))
  78. tester('ntpath.splitext(".ext")', ('.ext', ''))
  79. tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', ''))
  80. tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', ''))
  81. tester('ntpath.splitext("")', ('', ''))
  82. tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext'))
  83. tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext'))
  84. tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext'))
  85. tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d'))
  86. def test_splitdrive(self):
  87. tester('ntpath.splitdrive("c:\\foo\\bar")',
  88. ('c:', '\\foo\\bar'))
  89. tester('ntpath.splitdrive("c:/foo/bar")',
  90. ('c:', '/foo/bar'))
  91. tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")',
  92. ('\\\\conky\\mountpoint', '\\foo\\bar'))
  93. tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")',
  94. ('//conky/mountpoint', '/foo/bar'))
  95. tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")',
  96. ('\\\\\\conky', '\\mountpoint\\foo\\bar'))
  97. tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")',
  98. ('///conky', '/mountpoint/foo/bar'))
  99. tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")',
  100. ('\\\\conky\\', '\\mountpoint\\foo\\bar'))
  101. tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")',
  102. ('//conky/', '/mountpoint/foo/bar'))
  103. # Issue #19911: UNC part containing U+0130
  104. self.assertEqual(ntpath.splitdrive('//conky/MOUNTPOİNT/foo/bar'),
  105. ('//conky/MOUNTPOİNT', '/foo/bar'))
  106. # gh-81790: support device namespace, including UNC drives.
  107. tester('ntpath.splitdrive("//?/c:")', ("//?/c:", ""))
  108. tester('ntpath.splitdrive("//?/c:/")', ("//?/c:", "/"))
  109. tester('ntpath.splitdrive("//?/c:/dir")', ("//?/c:", "/dir"))
  110. tester('ntpath.splitdrive("//?/UNC")', ("//?/UNC", ""))
  111. tester('ntpath.splitdrive("//?/UNC/")', ("//?/UNC/", ""))
  112. tester('ntpath.splitdrive("//?/UNC/server/")', ("//?/UNC/server/", ""))
  113. tester('ntpath.splitdrive("//?/UNC/server/share")', ("//?/UNC/server/share", ""))
  114. tester('ntpath.splitdrive("//?/UNC/server/share/dir")', ("//?/UNC/server/share", "/dir"))
  115. tester('ntpath.splitdrive("//?/VOLUME{00000000-0000-0000-0000-000000000000}/spam")',
  116. ('//?/VOLUME{00000000-0000-0000-0000-000000000000}', '/spam'))
  117. tester('ntpath.splitdrive("//?/BootPartition/")', ("//?/BootPartition", "/"))
  118. tester('ntpath.splitdrive("\\\\?\\c:")', ("\\\\?\\c:", ""))
  119. tester('ntpath.splitdrive("\\\\?\\c:\\")', ("\\\\?\\c:", "\\"))
  120. tester('ntpath.splitdrive("\\\\?\\c:\\dir")', ("\\\\?\\c:", "\\dir"))
  121. tester('ntpath.splitdrive("\\\\?\\UNC")', ("\\\\?\\UNC", ""))
  122. tester('ntpath.splitdrive("\\\\?\\UNC\\")', ("\\\\?\\UNC\\", ""))
  123. tester('ntpath.splitdrive("\\\\?\\UNC\\server\\")', ("\\\\?\\UNC\\server\\", ""))
  124. tester('ntpath.splitdrive("\\\\?\\UNC\\server\\share")', ("\\\\?\\UNC\\server\\share", ""))
  125. tester('ntpath.splitdrive("\\\\?\\UNC\\server\\share\\dir")',
  126. ("\\\\?\\UNC\\server\\share", "\\dir"))
  127. tester('ntpath.splitdrive("\\\\?\\VOLUME{00000000-0000-0000-0000-000000000000}\\spam")',
  128. ('\\\\?\\VOLUME{00000000-0000-0000-0000-000000000000}', '\\spam'))
  129. tester('ntpath.splitdrive("\\\\?\\BootPartition\\")', ("\\\\?\\BootPartition", "\\"))
  130. # gh-96290: support partial/invalid UNC drives
  131. tester('ntpath.splitdrive("//")', ("//", "")) # empty server & missing share
  132. tester('ntpath.splitdrive("///")', ("///", "")) # empty server & empty share
  133. tester('ntpath.splitdrive("///y")', ("///y", "")) # empty server & non-empty share
  134. tester('ntpath.splitdrive("//x")', ("//x", "")) # non-empty server & missing share
  135. tester('ntpath.splitdrive("//x/")', ("//x/", "")) # non-empty server & empty share
  136. def test_split(self):
  137. tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
  138. tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
  139. ('\\\\conky\\mountpoint\\foo', 'bar'))
  140. tester('ntpath.split("c:\\")', ('c:\\', ''))
  141. tester('ntpath.split("\\\\conky\\mountpoint\\")',
  142. ('\\\\conky\\mountpoint\\', ''))
  143. tester('ntpath.split("c:/")', ('c:/', ''))
  144. tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', ''))
  145. def test_isabs(self):
  146. tester('ntpath.isabs("c:\\")', 1)
  147. tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
  148. tester('ntpath.isabs("\\foo")', 1)
  149. tester('ntpath.isabs("\\foo\\bar")', 1)
  150. # gh-96290: normal UNC paths and device paths without trailing backslashes
  151. tester('ntpath.isabs("\\\\conky\\mountpoint")', 1)
  152. tester('ntpath.isabs("\\\\.\\C:")', 1)
  153. def test_commonprefix(self):
  154. tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
  155. "/home/swen")
  156. tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
  157. "\\home\\swen\\")
  158. tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
  159. "/home/swen/spam")
  160. def test_join(self):
  161. tester('ntpath.join("")', '')
  162. tester('ntpath.join("", "", "")', '')
  163. tester('ntpath.join("a")', 'a')
  164. tester('ntpath.join("/a")', '/a')
  165. tester('ntpath.join("\\a")', '\\a')
  166. tester('ntpath.join("a:")', 'a:')
  167. tester('ntpath.join("a:", "\\b")', 'a:\\b')
  168. tester('ntpath.join("a", "\\b")', '\\b')
  169. tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
  170. tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
  171. tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
  172. tester('ntpath.join("a", "b", "\\c")', '\\c')
  173. tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
  174. tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
  175. tester("ntpath.join('', 'a')", 'a')
  176. tester("ntpath.join('', '', '', '', 'a')", 'a')
  177. tester("ntpath.join('a', '')", 'a\\')
  178. tester("ntpath.join('a', '', '', '', '')", 'a\\')
  179. tester("ntpath.join('a\\', '')", 'a\\')
  180. tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
  181. tester("ntpath.join('a/', '')", 'a/')
  182. tester("ntpath.join('a/b', 'x/y')", 'a/b\\x/y')
  183. tester("ntpath.join('/a/b', 'x/y')", '/a/b\\x/y')
  184. tester("ntpath.join('/a/b/', 'x/y')", '/a/b/x/y')
  185. tester("ntpath.join('c:', 'x/y')", 'c:x/y')
  186. tester("ntpath.join('c:a/b', 'x/y')", 'c:a/b\\x/y')
  187. tester("ntpath.join('c:a/b/', 'x/y')", 'c:a/b/x/y')
  188. tester("ntpath.join('c:/', 'x/y')", 'c:/x/y')
  189. tester("ntpath.join('c:/a/b', 'x/y')", 'c:/a/b\\x/y')
  190. tester("ntpath.join('c:/a/b/', 'x/y')", 'c:/a/b/x/y')
  191. tester("ntpath.join('//computer/share', 'x/y')", '//computer/share\\x/y')
  192. tester("ntpath.join('//computer/share/', 'x/y')", '//computer/share/x/y')
  193. tester("ntpath.join('//computer/share/a/b', 'x/y')", '//computer/share/a/b\\x/y')
  194. tester("ntpath.join('a/b', '/x/y')", '/x/y')
  195. tester("ntpath.join('/a/b', '/x/y')", '/x/y')
  196. tester("ntpath.join('c:', '/x/y')", 'c:/x/y')
  197. tester("ntpath.join('c:a/b', '/x/y')", 'c:/x/y')
  198. tester("ntpath.join('c:/', '/x/y')", 'c:/x/y')
  199. tester("ntpath.join('c:/a/b', '/x/y')", 'c:/x/y')
  200. tester("ntpath.join('//computer/share', '/x/y')", '//computer/share/x/y')
  201. tester("ntpath.join('//computer/share/', '/x/y')", '//computer/share/x/y')
  202. tester("ntpath.join('//computer/share/a', '/x/y')", '//computer/share/x/y')
  203. tester("ntpath.join('c:', 'C:x/y')", 'C:x/y')
  204. tester("ntpath.join('c:a/b', 'C:x/y')", 'C:a/b\\x/y')
  205. tester("ntpath.join('c:/', 'C:x/y')", 'C:/x/y')
  206. tester("ntpath.join('c:/a/b', 'C:x/y')", 'C:/a/b\\x/y')
  207. for x in ('', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b',
  208. '//computer/share', '//computer/share/', '//computer/share/a/b'):
  209. for y in ('d:', 'd:x/y', 'd:/', 'd:/x/y',
  210. '//machine/common', '//machine/common/', '//machine/common/x/y'):
  211. tester("ntpath.join(%r, %r)" % (x, y), y)
  212. tester("ntpath.join('\\\\computer\\share\\', 'a', 'b')", '\\\\computer\\share\\a\\b')
  213. tester("ntpath.join('\\\\computer\\share', 'a', 'b')", '\\\\computer\\share\\a\\b')
  214. tester("ntpath.join('\\\\computer\\share', 'a\\b')", '\\\\computer\\share\\a\\b')
  215. tester("ntpath.join('//computer/share/', 'a', 'b')", '//computer/share/a\\b')
  216. tester("ntpath.join('//computer/share', 'a', 'b')", '//computer/share\\a\\b')
  217. tester("ntpath.join('//computer/share', 'a/b')", '//computer/share\\a/b')
  218. def test_normpath(self):
  219. tester("ntpath.normpath('A//////././//.//B')", r'A\B')
  220. tester("ntpath.normpath('A/./B')", r'A\B')
  221. tester("ntpath.normpath('A/foo/../B')", r'A\B')
  222. tester("ntpath.normpath('C:A//B')", r'C:A\B')
  223. tester("ntpath.normpath('D:A/./B')", r'D:A\B')
  224. tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
  225. tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
  226. tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
  227. tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
  228. tester("ntpath.normpath('..')", r'..')
  229. tester("ntpath.normpath('.')", r'.')
  230. tester("ntpath.normpath('')", r'.')
  231. tester("ntpath.normpath('/')", '\\')
  232. tester("ntpath.normpath('c:/')", 'c:\\')
  233. tester("ntpath.normpath('/../.././..')", '\\')
  234. tester("ntpath.normpath('c:/../../..')", 'c:\\')
  235. tester("ntpath.normpath('../.././..')", r'..\..\..')
  236. tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
  237. tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
  238. tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
  239. tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL')
  240. tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z')
  241. tester("ntpath.normpath('handbook/../../Tests/image.png')", r'..\Tests\image.png')
  242. tester("ntpath.normpath('handbook/../../../Tests/image.png')", r'..\..\Tests\image.png')
  243. tester("ntpath.normpath('handbook///../a/.././../b/c')", r'..\b\c')
  244. tester("ntpath.normpath('handbook/a/../..///../../b/c')", r'..\..\b\c')
  245. tester("ntpath.normpath('//server/share/..')" , '\\\\server\\share\\')
  246. tester("ntpath.normpath('//server/share/../')" , '\\\\server\\share\\')
  247. tester("ntpath.normpath('//server/share/../..')", '\\\\server\\share\\')
  248. tester("ntpath.normpath('//server/share/../../')", '\\\\server\\share\\')
  249. # gh-96290: don't normalize partial/invalid UNC drives as rooted paths.
  250. tester("ntpath.normpath('\\\\foo\\\\')", '\\\\foo\\\\')
  251. tester("ntpath.normpath('\\\\foo\\')", '\\\\foo\\')
  252. tester("ntpath.normpath('\\\\foo')", '\\\\foo')
  253. tester("ntpath.normpath('\\\\')", '\\\\')
  254. def test_realpath_curdir(self):
  255. expected = ntpath.normpath(os.getcwd())
  256. tester("ntpath.realpath('.')", expected)
  257. tester("ntpath.realpath('./.')", expected)
  258. tester("ntpath.realpath('/'.join(['.'] * 100))", expected)
  259. tester("ntpath.realpath('.\\.')", expected)
  260. tester("ntpath.realpath('\\'.join(['.'] * 100))", expected)
  261. def test_realpath_pardir(self):
  262. expected = ntpath.normpath(os.getcwd())
  263. tester("ntpath.realpath('..')", ntpath.dirname(expected))
  264. tester("ntpath.realpath('../..')",
  265. ntpath.dirname(ntpath.dirname(expected)))
  266. tester("ntpath.realpath('/'.join(['..'] * 50))",
  267. ntpath.splitdrive(expected)[0] + '\\')
  268. tester("ntpath.realpath('..\\..')",
  269. ntpath.dirname(ntpath.dirname(expected)))
  270. tester("ntpath.realpath('\\'.join(['..'] * 50))",
  271. ntpath.splitdrive(expected)[0] + '\\')
  272. @os_helper.skip_unless_symlink
  273. @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
  274. def test_realpath_basic(self):
  275. ABSTFN = ntpath.abspath(os_helper.TESTFN)
  276. open(ABSTFN, "wb").close()
  277. self.addCleanup(os_helper.unlink, ABSTFN)
  278. self.addCleanup(os_helper.unlink, ABSTFN + "1")
  279. os.symlink(ABSTFN, ABSTFN + "1")
  280. self.assertPathEqual(ntpath.realpath(ABSTFN + "1"), ABSTFN)
  281. self.assertPathEqual(ntpath.realpath(os.fsencode(ABSTFN + "1")),
  282. os.fsencode(ABSTFN))
  283. @os_helper.skip_unless_symlink
  284. @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
  285. def test_realpath_strict(self):
  286. # Bug #43757: raise FileNotFoundError in strict mode if we encounter
  287. # a path that does not exist.
  288. ABSTFN = ntpath.abspath(os_helper.TESTFN)
  289. os.symlink(ABSTFN + "1", ABSTFN)
  290. self.addCleanup(os_helper.unlink, ABSTFN)
  291. self.assertRaises(FileNotFoundError, ntpath.realpath, ABSTFN, strict=True)
  292. self.assertRaises(FileNotFoundError, ntpath.realpath, ABSTFN + "2", strict=True)
  293. @os_helper.skip_unless_symlink
  294. @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
  295. def test_realpath_relative(self):
  296. ABSTFN = ntpath.abspath(os_helper.TESTFN)
  297. open(ABSTFN, "wb").close()
  298. self.addCleanup(os_helper.unlink, ABSTFN)
  299. self.addCleanup(os_helper.unlink, ABSTFN + "1")
  300. os.symlink(ABSTFN, ntpath.relpath(ABSTFN + "1"))
  301. self.assertPathEqual(ntpath.realpath(ABSTFN + "1"), ABSTFN)
  302. @os_helper.skip_unless_symlink
  303. @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
  304. def test_realpath_broken_symlinks(self):
  305. ABSTFN = ntpath.abspath(os_helper.TESTFN)
  306. os.mkdir(ABSTFN)
  307. self.addCleanup(os_helper.rmtree, ABSTFN)
  308. with os_helper.change_cwd(ABSTFN):
  309. os.mkdir("subdir")
  310. os.chdir("subdir")
  311. os.symlink(".", "recursive")
  312. os.symlink("..", "parent")
  313. os.chdir("..")
  314. os.symlink(".", "self")
  315. os.symlink("missing", "broken")
  316. os.symlink(r"broken\bar", "broken1")
  317. os.symlink(r"self\self\broken", "broken2")
  318. os.symlink(r"subdir\parent\subdir\parent\broken", "broken3")
  319. os.symlink(ABSTFN + r"\broken", "broken4")
  320. os.symlink(r"recursive\..\broken", "broken5")
  321. self.assertPathEqual(ntpath.realpath("broken"),
  322. ABSTFN + r"\missing")
  323. self.assertPathEqual(ntpath.realpath(r"broken\foo"),
  324. ABSTFN + r"\missing\foo")
  325. # bpo-38453: We no longer recursively resolve segments of relative
  326. # symlinks that the OS cannot resolve.
  327. self.assertPathEqual(ntpath.realpath(r"broken1"),
  328. ABSTFN + r"\broken\bar")
  329. self.assertPathEqual(ntpath.realpath(r"broken1\baz"),
  330. ABSTFN + r"\broken\bar\baz")
  331. self.assertPathEqual(ntpath.realpath("broken2"),
  332. ABSTFN + r"\self\self\missing")
  333. self.assertPathEqual(ntpath.realpath("broken3"),
  334. ABSTFN + r"\subdir\parent\subdir\parent\missing")
  335. self.assertPathEqual(ntpath.realpath("broken4"),
  336. ABSTFN + r"\missing")
  337. self.assertPathEqual(ntpath.realpath("broken5"),
  338. ABSTFN + r"\missing")
  339. self.assertPathEqual(ntpath.realpath(b"broken"),
  340. os.fsencode(ABSTFN + r"\missing"))
  341. self.assertPathEqual(ntpath.realpath(rb"broken\foo"),
  342. os.fsencode(ABSTFN + r"\missing\foo"))
  343. self.assertPathEqual(ntpath.realpath(rb"broken1"),
  344. os.fsencode(ABSTFN + r"\broken\bar"))
  345. self.assertPathEqual(ntpath.realpath(rb"broken1\baz"),
  346. os.fsencode(ABSTFN + r"\broken\bar\baz"))
  347. self.assertPathEqual(ntpath.realpath(b"broken2"),
  348. os.fsencode(ABSTFN + r"\self\self\missing"))
  349. self.assertPathEqual(ntpath.realpath(rb"broken3"),
  350. os.fsencode(ABSTFN + r"\subdir\parent\subdir\parent\missing"))
  351. self.assertPathEqual(ntpath.realpath(b"broken4"),
  352. os.fsencode(ABSTFN + r"\missing"))
  353. self.assertPathEqual(ntpath.realpath(b"broken5"),
  354. os.fsencode(ABSTFN + r"\missing"))
  355. @os_helper.skip_unless_symlink
  356. @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
  357. def test_realpath_symlink_loops(self):
  358. # Symlink loops in non-strict mode are non-deterministic as to which
  359. # path is returned, but it will always be the fully resolved path of
  360. # one member of the cycle
  361. ABSTFN = ntpath.abspath(os_helper.TESTFN)
  362. self.addCleanup(os_helper.unlink, ABSTFN)
  363. self.addCleanup(os_helper.unlink, ABSTFN + "1")
  364. self.addCleanup(os_helper.unlink, ABSTFN + "2")
  365. self.addCleanup(os_helper.unlink, ABSTFN + "y")
  366. self.addCleanup(os_helper.unlink, ABSTFN + "c")
  367. self.addCleanup(os_helper.unlink, ABSTFN + "a")
  368. os.symlink(ABSTFN, ABSTFN)
  369. self.assertPathEqual(ntpath.realpath(ABSTFN), ABSTFN)
  370. os.symlink(ABSTFN + "1", ABSTFN + "2")
  371. os.symlink(ABSTFN + "2", ABSTFN + "1")
  372. expected = (ABSTFN + "1", ABSTFN + "2")
  373. self.assertPathIn(ntpath.realpath(ABSTFN + "1"), expected)
  374. self.assertPathIn(ntpath.realpath(ABSTFN + "2"), expected)
  375. self.assertPathIn(ntpath.realpath(ABSTFN + "1\\x"),
  376. (ntpath.join(r, "x") for r in expected))
  377. self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\.."),
  378. ntpath.dirname(ABSTFN))
  379. self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\..\\x"),
  380. ntpath.dirname(ABSTFN) + "\\x")
  381. os.symlink(ABSTFN + "x", ABSTFN + "y")
  382. self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\..\\"
  383. + ntpath.basename(ABSTFN) + "y"),
  384. ABSTFN + "x")
  385. self.assertPathIn(ntpath.realpath(ABSTFN + "1\\..\\"
  386. + ntpath.basename(ABSTFN) + "1"),
  387. expected)
  388. os.symlink(ntpath.basename(ABSTFN) + "a\\b", ABSTFN + "a")
  389. self.assertPathEqual(ntpath.realpath(ABSTFN + "a"), ABSTFN + "a")
  390. os.symlink("..\\" + ntpath.basename(ntpath.dirname(ABSTFN))
  391. + "\\" + ntpath.basename(ABSTFN) + "c", ABSTFN + "c")
  392. self.assertPathEqual(ntpath.realpath(ABSTFN + "c"), ABSTFN + "c")
  393. # Test using relative path as well.
  394. self.assertPathEqual(ntpath.realpath(ntpath.basename(ABSTFN)), ABSTFN)
  395. @os_helper.skip_unless_symlink
  396. @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
  397. def test_realpath_symlink_loops_strict(self):
  398. # Symlink loops raise OSError in strict mode
  399. ABSTFN = ntpath.abspath(os_helper.TESTFN)
  400. self.addCleanup(os_helper.unlink, ABSTFN)
  401. self.addCleanup(os_helper.unlink, ABSTFN + "1")
  402. self.addCleanup(os_helper.unlink, ABSTFN + "2")
  403. self.addCleanup(os_helper.unlink, ABSTFN + "y")
  404. self.addCleanup(os_helper.unlink, ABSTFN + "c")
  405. self.addCleanup(os_helper.unlink, ABSTFN + "a")
  406. os.symlink(ABSTFN, ABSTFN)
  407. self.assertRaises(OSError, ntpath.realpath, ABSTFN, strict=True)
  408. os.symlink(ABSTFN + "1", ABSTFN + "2")
  409. os.symlink(ABSTFN + "2", ABSTFN + "1")
  410. self.assertRaises(OSError, ntpath.realpath, ABSTFN + "1", strict=True)
  411. self.assertRaises(OSError, ntpath.realpath, ABSTFN + "2", strict=True)
  412. self.assertRaises(OSError, ntpath.realpath, ABSTFN + "1\\x", strict=True)
  413. # Windows eliminates '..' components before resolving links, so the
  414. # following call is not expected to raise.
  415. self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\..", strict=True),
  416. ntpath.dirname(ABSTFN))
  417. self.assertRaises(OSError, ntpath.realpath, ABSTFN + "1\\..\\x", strict=True)
  418. os.symlink(ABSTFN + "x", ABSTFN + "y")
  419. self.assertRaises(OSError, ntpath.realpath, ABSTFN + "1\\..\\"
  420. + ntpath.basename(ABSTFN) + "y",
  421. strict=True)
  422. self.assertRaises(OSError, ntpath.realpath,
  423. ABSTFN + "1\\..\\" + ntpath.basename(ABSTFN) + "1",
  424. strict=True)
  425. os.symlink(ntpath.basename(ABSTFN) + "a\\b", ABSTFN + "a")
  426. self.assertRaises(OSError, ntpath.realpath, ABSTFN + "a", strict=True)
  427. os.symlink("..\\" + ntpath.basename(ntpath.dirname(ABSTFN))
  428. + "\\" + ntpath.basename(ABSTFN) + "c", ABSTFN + "c")
  429. self.assertRaises(OSError, ntpath.realpath, ABSTFN + "c", strict=True)
  430. # Test using relative path as well.
  431. self.assertRaises(OSError, ntpath.realpath, ntpath.basename(ABSTFN),
  432. strict=True)
  433. @os_helper.skip_unless_symlink
  434. @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
  435. def test_realpath_symlink_prefix(self):
  436. ABSTFN = ntpath.abspath(os_helper.TESTFN)
  437. self.addCleanup(os_helper.unlink, ABSTFN + "3")
  438. self.addCleanup(os_helper.unlink, "\\\\?\\" + ABSTFN + "3.")
  439. self.addCleanup(os_helper.unlink, ABSTFN + "3link")
  440. self.addCleanup(os_helper.unlink, ABSTFN + "3.link")
  441. with open(ABSTFN + "3", "wb") as f:
  442. f.write(b'0')
  443. os.symlink(ABSTFN + "3", ABSTFN + "3link")
  444. with open("\\\\?\\" + ABSTFN + "3.", "wb") as f:
  445. f.write(b'1')
  446. os.symlink("\\\\?\\" + ABSTFN + "3.", ABSTFN + "3.link")
  447. self.assertPathEqual(ntpath.realpath(ABSTFN + "3link"),
  448. ABSTFN + "3")
  449. self.assertPathEqual(ntpath.realpath(ABSTFN + "3.link"),
  450. "\\\\?\\" + ABSTFN + "3.")
  451. # Resolved paths should be usable to open target files
  452. with open(ntpath.realpath(ABSTFN + "3link"), "rb") as f:
  453. self.assertEqual(f.read(), b'0')
  454. with open(ntpath.realpath(ABSTFN + "3.link"), "rb") as f:
  455. self.assertEqual(f.read(), b'1')
  456. # When the prefix is included, it is not stripped
  457. self.assertPathEqual(ntpath.realpath("\\\\?\\" + ABSTFN + "3link"),
  458. "\\\\?\\" + ABSTFN + "3")
  459. self.assertPathEqual(ntpath.realpath("\\\\?\\" + ABSTFN + "3.link"),
  460. "\\\\?\\" + ABSTFN + "3.")
  461. @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
  462. def test_realpath_nul(self):
  463. tester("ntpath.realpath('NUL')", r'\\.\NUL')
  464. @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
  465. @unittest.skipUnless(HAVE_GETSHORTPATHNAME, 'need _getshortpathname')
  466. def test_realpath_cwd(self):
  467. ABSTFN = ntpath.abspath(os_helper.TESTFN)
  468. os_helper.unlink(ABSTFN)
  469. os_helper.rmtree(ABSTFN)
  470. os.mkdir(ABSTFN)
  471. self.addCleanup(os_helper.rmtree, ABSTFN)
  472. test_dir_long = ntpath.join(ABSTFN, "MyVeryLongDirectoryName")
  473. os.mkdir(test_dir_long)
  474. test_dir_short = _getshortpathname(test_dir_long)
  475. test_file_long = ntpath.join(test_dir_long, "file.txt")
  476. test_file_short = ntpath.join(test_dir_short, "file.txt")
  477. with open(test_file_long, "wb") as f:
  478. f.write(b"content")
  479. self.assertPathEqual(test_file_long, ntpath.realpath(test_file_short))
  480. with os_helper.change_cwd(test_dir_long):
  481. self.assertPathEqual(test_file_long, ntpath.realpath("file.txt"))
  482. with os_helper.change_cwd(test_dir_long.lower()):
  483. self.assertPathEqual(test_file_long, ntpath.realpath("file.txt"))
  484. with os_helper.change_cwd(test_dir_short):
  485. self.assertPathEqual(test_file_long, ntpath.realpath("file.txt"))
  486. def test_expandvars(self):
  487. with os_helper.EnvironmentVarGuard() as env:
  488. env.clear()
  489. env["foo"] = "bar"
  490. env["{foo"] = "baz1"
  491. env["{foo}"] = "baz2"
  492. tester('ntpath.expandvars("foo")', "foo")
  493. tester('ntpath.expandvars("$foo bar")', "bar bar")
  494. tester('ntpath.expandvars("${foo}bar")', "barbar")
  495. tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
  496. tester('ntpath.expandvars("$bar bar")', "$bar bar")
  497. tester('ntpath.expandvars("$?bar")', "$?bar")
  498. tester('ntpath.expandvars("$foo}bar")', "bar}bar")
  499. tester('ntpath.expandvars("${foo")', "${foo")
  500. tester('ntpath.expandvars("${{foo}}")', "baz1}")
  501. tester('ntpath.expandvars("$foo$foo")', "barbar")
  502. tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
  503. tester('ntpath.expandvars("%foo% bar")', "bar bar")
  504. tester('ntpath.expandvars("%foo%bar")', "barbar")
  505. tester('ntpath.expandvars("%foo%%foo%")', "barbar")
  506. tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
  507. tester('ntpath.expandvars("%?bar%")', "%?bar%")
  508. tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
  509. tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
  510. tester('ntpath.expandvars("bar\'%foo%")', "bar\'%foo%")
  511. @unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
  512. def test_expandvars_nonascii(self):
  513. def check(value, expected):
  514. tester('ntpath.expandvars(%r)' % value, expected)
  515. with os_helper.EnvironmentVarGuard() as env:
  516. env.clear()
  517. nonascii = os_helper.FS_NONASCII
  518. env['spam'] = nonascii
  519. env[nonascii] = 'ham' + nonascii
  520. check('$spam bar', '%s bar' % nonascii)
  521. check('$%s bar' % nonascii, '$%s bar' % nonascii)
  522. check('${spam}bar', '%sbar' % nonascii)
  523. check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
  524. check('$spam}bar', '%s}bar' % nonascii)
  525. check('$%s}bar' % nonascii, '$%s}bar' % nonascii)
  526. check('%spam% bar', '%s bar' % nonascii)
  527. check('%{}% bar'.format(nonascii), 'ham%s bar' % nonascii)
  528. check('%spam%bar', '%sbar' % nonascii)
  529. check('%{}%bar'.format(nonascii), 'ham%sbar' % nonascii)
  530. def test_expanduser(self):
  531. tester('ntpath.expanduser("test")', 'test')
  532. with os_helper.EnvironmentVarGuard() as env:
  533. env.clear()
  534. tester('ntpath.expanduser("~test")', '~test')
  535. env['HOMEDRIVE'] = 'C:\\'
  536. env['HOMEPATH'] = 'Users\\eric'
  537. env['USERNAME'] = 'eric'
  538. tester('ntpath.expanduser("~test")', 'C:\\Users\\test')
  539. tester('ntpath.expanduser("~")', 'C:\\Users\\eric')
  540. del env['HOMEDRIVE']
  541. tester('ntpath.expanduser("~test")', 'Users\\test')
  542. tester('ntpath.expanduser("~")', 'Users\\eric')
  543. env.clear()
  544. env['USERPROFILE'] = 'C:\\Users\\eric'
  545. env['USERNAME'] = 'eric'
  546. tester('ntpath.expanduser("~test")', 'C:\\Users\\test')
  547. tester('ntpath.expanduser("~")', 'C:\\Users\\eric')
  548. tester('ntpath.expanduser("~test\\foo\\bar")',
  549. 'C:\\Users\\test\\foo\\bar')
  550. tester('ntpath.expanduser("~test/foo/bar")',
  551. 'C:\\Users\\test/foo/bar')
  552. tester('ntpath.expanduser("~\\foo\\bar")',
  553. 'C:\\Users\\eric\\foo\\bar')
  554. tester('ntpath.expanduser("~/foo/bar")',
  555. 'C:\\Users\\eric/foo/bar')
  556. # bpo-36264: ignore `HOME` when set on windows
  557. env.clear()
  558. env['HOME'] = 'F:\\'
  559. env['USERPROFILE'] = 'C:\\Users\\eric'
  560. env['USERNAME'] = 'eric'
  561. tester('ntpath.expanduser("~test")', 'C:\\Users\\test')
  562. tester('ntpath.expanduser("~")', 'C:\\Users\\eric')
  563. # bpo-39899: don't guess another user's home directory if
  564. # `%USERNAME% != basename(%USERPROFILE%)`
  565. env.clear()
  566. env['USERPROFILE'] = 'C:\\Users\\eric'
  567. env['USERNAME'] = 'idle'
  568. tester('ntpath.expanduser("~test")', '~test')
  569. tester('ntpath.expanduser("~")', 'C:\\Users\\eric')
  570. @unittest.skipUnless(nt, "abspath requires 'nt' module")
  571. def test_abspath(self):
  572. tester('ntpath.abspath("C:\\")', "C:\\")
  573. tester('ntpath.abspath("\\\\?\\C:////spam////eggs. . .")', "\\\\?\\C:\\spam\\eggs")
  574. tester('ntpath.abspath("\\\\.\\C:////spam////eggs. . .")', "\\\\.\\C:\\spam\\eggs")
  575. tester('ntpath.abspath("//spam//eggs. . .")', "\\\\spam\\eggs")
  576. tester('ntpath.abspath("\\\\spam\\\\eggs. . .")', "\\\\spam\\eggs")
  577. tester('ntpath.abspath("C:/spam. . .")', "C:\\spam")
  578. tester('ntpath.abspath("C:\\spam. . .")', "C:\\spam")
  579. tester('ntpath.abspath("C:/nul")', "\\\\.\\nul")
  580. tester('ntpath.abspath("C:\\nul")', "\\\\.\\nul")
  581. tester('ntpath.abspath("//..")', "\\\\")
  582. tester('ntpath.abspath("//../")', "\\\\..\\")
  583. tester('ntpath.abspath("//../..")', "\\\\..\\")
  584. tester('ntpath.abspath("//../../")', "\\\\..\\..\\")
  585. tester('ntpath.abspath("//../../../")', "\\\\..\\..\\")
  586. tester('ntpath.abspath("//../../../..")', "\\\\..\\..\\")
  587. tester('ntpath.abspath("//../../../../")', "\\\\..\\..\\")
  588. tester('ntpath.abspath("//server")', "\\\\server")
  589. tester('ntpath.abspath("//server/")', "\\\\server\\")
  590. tester('ntpath.abspath("//server/..")', "\\\\server\\")
  591. tester('ntpath.abspath("//server/../")', "\\\\server\\..\\")
  592. tester('ntpath.abspath("//server/../..")', "\\\\server\\..\\")
  593. tester('ntpath.abspath("//server/../../")', "\\\\server\\..\\")
  594. tester('ntpath.abspath("//server/../../..")', "\\\\server\\..\\")
  595. tester('ntpath.abspath("//server/../../../")', "\\\\server\\..\\")
  596. tester('ntpath.abspath("//server/share")', "\\\\server\\share")
  597. tester('ntpath.abspath("//server/share/")', "\\\\server\\share\\")
  598. tester('ntpath.abspath("//server/share/..")', "\\\\server\\share\\")
  599. tester('ntpath.abspath("//server/share/../")', "\\\\server\\share\\")
  600. tester('ntpath.abspath("//server/share/../..")', "\\\\server\\share\\")
  601. tester('ntpath.abspath("//server/share/../../")', "\\\\server\\share\\")
  602. tester('ntpath.abspath("C:\\nul. . .")', "\\\\.\\nul")
  603. tester('ntpath.abspath("//... . .")', "\\\\")
  604. tester('ntpath.abspath("//.. . . .")', "\\\\")
  605. tester('ntpath.abspath("//../... . .")', "\\\\..\\")
  606. tester('ntpath.abspath("//../.. . . .")', "\\\\..\\")
  607. with os_helper.temp_cwd(os_helper.TESTFN) as cwd_dir: # bpo-31047
  608. tester('ntpath.abspath("")', cwd_dir)
  609. tester('ntpath.abspath(" ")', cwd_dir + "\\ ")
  610. tester('ntpath.abspath("?")', cwd_dir + "\\?")
  611. drive, _ = ntpath.splitdrive(cwd_dir)
  612. tester('ntpath.abspath("/abc/")', drive + "\\abc")
  613. def test_relpath(self):
  614. tester('ntpath.relpath("a")', 'a')
  615. tester('ntpath.relpath(ntpath.abspath("a"))', 'a')
  616. tester('ntpath.relpath("a/b")', 'a\\b')
  617. tester('ntpath.relpath("../a/b")', '..\\a\\b')
  618. with os_helper.temp_cwd(os_helper.TESTFN) as cwd_dir:
  619. currentdir = ntpath.basename(cwd_dir)
  620. tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
  621. tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
  622. tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
  623. tester('ntpath.relpath("c:/foo/bar/bat", "c:/x/y")', '..\\..\\foo\\bar\\bat')
  624. tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
  625. tester('ntpath.relpath("a", "a")', '.')
  626. tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
  627. tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
  628. tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
  629. tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
  630. tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
  631. tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
  632. tester('ntpath.relpath("/", "/")', '.')
  633. tester('ntpath.relpath("/a", "/a")', '.')
  634. tester('ntpath.relpath("/a/b", "/a/b")', '.')
  635. tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
  636. def test_commonpath(self):
  637. def check(paths, expected):
  638. tester(('ntpath.commonpath(%r)' % paths).replace('\\\\', '\\'),
  639. expected)
  640. def check_error(exc, paths):
  641. self.assertRaises(exc, ntpath.commonpath, paths)
  642. self.assertRaises(exc, ntpath.commonpath,
  643. [os.fsencode(p) for p in paths])
  644. self.assertRaises(ValueError, ntpath.commonpath, [])
  645. check_error(ValueError, ['C:\\Program Files', 'Program Files'])
  646. check_error(ValueError, ['C:\\Program Files', 'C:Program Files'])
  647. check_error(ValueError, ['\\Program Files', 'Program Files'])
  648. check_error(ValueError, ['Program Files', 'C:\\Program Files'])
  649. check(['C:\\Program Files'], 'C:\\Program Files')
  650. check(['C:\\Program Files', 'C:\\Program Files'], 'C:\\Program Files')
  651. check(['C:\\Program Files\\', 'C:\\Program Files'],
  652. 'C:\\Program Files')
  653. check(['C:\\Program Files\\', 'C:\\Program Files\\'],
  654. 'C:\\Program Files')
  655. check(['C:\\\\Program Files', 'C:\\Program Files\\\\'],
  656. 'C:\\Program Files')
  657. check(['C:\\.\\Program Files', 'C:\\Program Files\\.'],
  658. 'C:\\Program Files')
  659. check(['C:\\', 'C:\\bin'], 'C:\\')
  660. check(['C:\\Program Files', 'C:\\bin'], 'C:\\')
  661. check(['C:\\Program Files', 'C:\\Program Files\\Bar'],
  662. 'C:\\Program Files')
  663. check(['C:\\Program Files\\Foo', 'C:\\Program Files\\Bar'],
  664. 'C:\\Program Files')
  665. check(['C:\\Program Files', 'C:\\Projects'], 'C:\\')
  666. check(['C:\\Program Files\\', 'C:\\Projects'], 'C:\\')
  667. check(['C:\\Program Files\\Foo', 'C:/Program Files/Bar'],
  668. 'C:\\Program Files')
  669. check(['C:\\Program Files\\Foo', 'c:/program files/bar'],
  670. 'C:\\Program Files')
  671. check(['c:/program files/bar', 'C:\\Program Files\\Foo'],
  672. 'c:\\program files')
  673. check_error(ValueError, ['C:\\Program Files', 'D:\\Program Files'])
  674. check(['spam'], 'spam')
  675. check(['spam', 'spam'], 'spam')
  676. check(['spam', 'alot'], '')
  677. check(['and\\jam', 'and\\spam'], 'and')
  678. check(['and\\\\jam', 'and\\spam\\\\'], 'and')
  679. check(['and\\.\\jam', '.\\and\\spam'], 'and')
  680. check(['and\\jam', 'and\\spam', 'alot'], '')
  681. check(['and\\jam', 'and\\spam', 'and'], 'and')
  682. check(['C:and\\jam', 'C:and\\spam'], 'C:and')
  683. check([''], '')
  684. check(['', 'spam\\alot'], '')
  685. check_error(ValueError, ['', '\\spam\\alot'])
  686. self.assertRaises(TypeError, ntpath.commonpath,
  687. [b'C:\\Program Files', 'C:\\Program Files\\Foo'])
  688. self.assertRaises(TypeError, ntpath.commonpath,
  689. [b'C:\\Program Files', 'Program Files\\Foo'])
  690. self.assertRaises(TypeError, ntpath.commonpath,
  691. [b'Program Files', 'C:\\Program Files\\Foo'])
  692. self.assertRaises(TypeError, ntpath.commonpath,
  693. ['C:\\Program Files', b'C:\\Program Files\\Foo'])
  694. self.assertRaises(TypeError, ntpath.commonpath,
  695. ['C:\\Program Files', b'Program Files\\Foo'])
  696. self.assertRaises(TypeError, ntpath.commonpath,
  697. ['Program Files', b'C:\\Program Files\\Foo'])
  698. @unittest.skipIf(is_emscripten, "Emscripten cannot fstat unnamed files.")
  699. def test_sameopenfile(self):
  700. with TemporaryFile() as tf1, TemporaryFile() as tf2:
  701. # Make sure the same file is really the same
  702. self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno()))
  703. # Make sure different files are really different
  704. self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno()))
  705. # Make sure invalid values don't cause issues on win32
  706. if sys.platform == "win32":
  707. with self.assertRaises(OSError):
  708. # Invalid file descriptors shouldn't display assert
  709. # dialogs (#4804)
  710. ntpath.sameopenfile(-1, -1)
  711. def test_ismount(self):
  712. self.assertTrue(ntpath.ismount("c:\\"))
  713. self.assertTrue(ntpath.ismount("C:\\"))
  714. self.assertTrue(ntpath.ismount("c:/"))
  715. self.assertTrue(ntpath.ismount("C:/"))
  716. self.assertTrue(ntpath.ismount("\\\\.\\c:\\"))
  717. self.assertTrue(ntpath.ismount("\\\\.\\C:\\"))
  718. self.assertTrue(ntpath.ismount(b"c:\\"))
  719. self.assertTrue(ntpath.ismount(b"C:\\"))
  720. self.assertTrue(ntpath.ismount(b"c:/"))
  721. self.assertTrue(ntpath.ismount(b"C:/"))
  722. self.assertTrue(ntpath.ismount(b"\\\\.\\c:\\"))
  723. self.assertTrue(ntpath.ismount(b"\\\\.\\C:\\"))
  724. with os_helper.temp_dir() as d:
  725. self.assertFalse(ntpath.ismount(d))
  726. if sys.platform == "win32":
  727. #
  728. # Make sure the current folder isn't the root folder
  729. # (or any other volume root). The drive-relative
  730. # locations below cannot then refer to mount points
  731. #
  732. test_cwd = os.getenv("SystemRoot")
  733. drive, path = ntpath.splitdrive(test_cwd)
  734. with os_helper.change_cwd(test_cwd):
  735. self.assertFalse(ntpath.ismount(drive.lower()))
  736. self.assertFalse(ntpath.ismount(drive.upper()))
  737. self.assertTrue(ntpath.ismount("\\\\localhost\\c$"))
  738. self.assertTrue(ntpath.ismount("\\\\localhost\\c$\\"))
  739. self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$"))
  740. self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$\\"))
  741. def assertEqualCI(self, s1, s2):
  742. """Assert that two strings are equal ignoring case differences."""
  743. self.assertEqual(s1.lower(), s2.lower())
  744. @unittest.skipUnless(nt, "OS helpers require 'nt' module")
  745. def test_nt_helpers(self):
  746. # Trivial validation that the helpers do not break, and support both
  747. # unicode and bytes (UTF-8) paths
  748. executable = nt._getfinalpathname(sys.executable)
  749. for path in executable, os.fsencode(executable):
  750. volume_path = nt._getvolumepathname(path)
  751. path_drive = ntpath.splitdrive(path)[0]
  752. volume_path_drive = ntpath.splitdrive(volume_path)[0]
  753. self.assertEqualCI(path_drive, volume_path_drive)
  754. cap, free = nt._getdiskusage(sys.exec_prefix)
  755. self.assertGreater(cap, 0)
  756. self.assertGreater(free, 0)
  757. b_cap, b_free = nt._getdiskusage(sys.exec_prefix.encode())
  758. # Free space may change, so only test the capacity is equal
  759. self.assertEqual(b_cap, cap)
  760. self.assertGreater(b_free, 0)
  761. for path in [sys.prefix, sys.executable]:
  762. final_path = nt._getfinalpathname(path)
  763. self.assertIsInstance(final_path, str)
  764. self.assertGreater(len(final_path), 0)
  765. b_final_path = nt._getfinalpathname(path.encode())
  766. self.assertIsInstance(b_final_path, bytes)
  767. self.assertGreater(len(b_final_path), 0)
  768. class NtCommonTest(test_genericpath.CommonTest, unittest.TestCase):
  769. pathmodule = ntpath
  770. attributes = ['relpath']
  771. class PathLikeTests(NtpathTestCase):
  772. path = ntpath
  773. def setUp(self):
  774. self.file_name = os_helper.TESTFN
  775. self.file_path = FakePath(os_helper.TESTFN)
  776. self.addCleanup(os_helper.unlink, self.file_name)
  777. with open(self.file_name, 'xb', 0) as file:
  778. file.write(b"test_ntpath.PathLikeTests")
  779. def _check_function(self, func):
  780. self.assertPathEqual(func(self.file_path), func(self.file_name))
  781. def test_path_normcase(self):
  782. self._check_function(self.path.normcase)
  783. if sys.platform == 'win32':
  784. self.assertEqual(ntpath.normcase('\u03a9\u2126'), 'ωΩ')
  785. def test_path_isabs(self):
  786. self._check_function(self.path.isabs)
  787. def test_path_join(self):
  788. self.assertEqual(self.path.join('a', FakePath('b'), 'c'),
  789. self.path.join('a', 'b', 'c'))
  790. def test_path_split(self):
  791. self._check_function(self.path.split)
  792. def test_path_splitext(self):
  793. self._check_function(self.path.splitext)
  794. def test_path_splitdrive(self):
  795. self._check_function(self.path.splitdrive)
  796. def test_path_basename(self):
  797. self._check_function(self.path.basename)
  798. def test_path_dirname(self):
  799. self._check_function(self.path.dirname)
  800. def test_path_islink(self):
  801. self._check_function(self.path.islink)
  802. def test_path_lexists(self):
  803. self._check_function(self.path.lexists)
  804. def test_path_ismount(self):
  805. self._check_function(self.path.ismount)
  806. def test_path_expanduser(self):
  807. self._check_function(self.path.expanduser)
  808. def test_path_expandvars(self):
  809. self._check_function(self.path.expandvars)
  810. def test_path_normpath(self):
  811. self._check_function(self.path.normpath)
  812. def test_path_abspath(self):
  813. self._check_function(self.path.abspath)
  814. def test_path_realpath(self):
  815. self._check_function(self.path.realpath)
  816. def test_path_relpath(self):
  817. self._check_function(self.path.relpath)
  818. def test_path_commonpath(self):
  819. common_path = self.path.commonpath([self.file_path, self.file_name])
  820. self.assertPathEqual(common_path, self.file_name)
  821. def test_path_isdir(self):
  822. self._check_function(self.path.isdir)
  823. if __name__ == "__main__":
  824. unittest.main()