test_ntpath.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # coding: utf-8
  2. import ntpath
  3. import os
  4. import sys
  5. from test.test_support import TestFailed
  6. from test import test_support, test_genericpath
  7. import unittest
  8. def tester0(fn, wantResult):
  9. gotResult = eval(fn)
  10. if wantResult != gotResult:
  11. raise TestFailed, "%s should return: %r but returned: %r" \
  12. %(fn, wantResult, gotResult)
  13. def tester(fn, wantResult):
  14. fn = fn.replace("\\", "\\\\")
  15. tester0(fn, wantResult)
  16. class TestNtpath(unittest.TestCase):
  17. def test_splitext(self):
  18. tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
  19. tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext'))
  20. tester('ntpath.splitext(".ext")', ('.ext', ''))
  21. tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', ''))
  22. tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', ''))
  23. tester('ntpath.splitext("")', ('', ''))
  24. tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext'))
  25. tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext'))
  26. tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext'))
  27. tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d'))
  28. def test_splitdrive(self):
  29. tester('ntpath.splitdrive("c:\\foo\\bar")',
  30. ('c:', '\\foo\\bar'))
  31. tester('ntpath.splitdrive("c:/foo/bar")',
  32. ('c:', '/foo/bar'))
  33. tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")',
  34. ('\\\\conky\\mountpoint', '\\foo\\bar'))
  35. tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")',
  36. ('//conky/mountpoint', '/foo/bar'))
  37. tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")',
  38. ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
  39. tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")',
  40. ('', '///conky/mountpoint/foo/bar'))
  41. tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")',
  42. ('', '\\\\conky\\\\mountpoint\\foo\\bar'))
  43. tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")',
  44. ('', '//conky//mountpoint/foo/bar'))
  45. # Issue #19911: UNC part containing U+0130
  46. self.assertEqual(ntpath.splitdrive(u'//conky/MOUNTPOİNT/foo/bar'),
  47. (u'//conky/MOUNTPOİNT', '/foo/bar'))
  48. self.assertEqual(ntpath.splitdrive("//"), ("", "//"))
  49. def test_splitunc(self):
  50. tester('ntpath.splitunc("c:\\foo\\bar")',
  51. ('', 'c:\\foo\\bar'))
  52. tester('ntpath.splitunc("c:/foo/bar")',
  53. ('', 'c:/foo/bar'))
  54. tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")',
  55. ('\\\\conky\\mountpoint', '\\foo\\bar'))
  56. tester('ntpath.splitunc("//conky/mountpoint/foo/bar")',
  57. ('//conky/mountpoint', '/foo/bar'))
  58. tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")',
  59. ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
  60. tester('ntpath.splitunc("///conky/mountpoint/foo/bar")',
  61. ('', '///conky/mountpoint/foo/bar'))
  62. tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")',
  63. ('', '\\\\conky\\\\mountpoint\\foo\\bar'))
  64. tester('ntpath.splitunc("//conky//mountpoint/foo/bar")',
  65. ('', '//conky//mountpoint/foo/bar'))
  66. if test_support.have_unicode:
  67. self.assertEqual(ntpath.splitunc(u'//conky/MOUNTPO%cNT/foo/bar' % 0x0130),
  68. (u'//conky/MOUNTPO%cNT' % 0x0130, u'/foo/bar'))
  69. def test_split(self):
  70. tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
  71. tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
  72. ('\\\\conky\\mountpoint\\foo', 'bar'))
  73. tester('ntpath.split("c:\\")', ('c:\\', ''))
  74. tester('ntpath.split("\\\\conky\\mountpoint\\")',
  75. ('\\\\conky\\mountpoint\\', ''))
  76. tester('ntpath.split("c:/")', ('c:/', ''))
  77. tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', ''))
  78. def test_isabs(self):
  79. tester('ntpath.isabs("c:\\")', 1)
  80. tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
  81. tester('ntpath.isabs("\\foo")', 1)
  82. tester('ntpath.isabs("\\foo\\bar")', 1)
  83. def test_commonprefix(self):
  84. tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
  85. "/home/swen")
  86. tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
  87. "\\home\\swen\\")
  88. tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
  89. "/home/swen/spam")
  90. def test_join(self):
  91. tester('ntpath.join("")', '')
  92. tester('ntpath.join("", "", "")', '')
  93. tester('ntpath.join("a")', 'a')
  94. tester('ntpath.join("/a")', '/a')
  95. tester('ntpath.join("\\a")', '\\a')
  96. tester('ntpath.join("a:")', 'a:')
  97. tester('ntpath.join("a:", "\\b")', 'a:\\b')
  98. tester('ntpath.join("a", "\\b")', '\\b')
  99. tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
  100. tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
  101. tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
  102. tester('ntpath.join("a", "b", "\\c")', '\\c')
  103. tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
  104. tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
  105. tester("ntpath.join('', 'a')", 'a')
  106. tester("ntpath.join('', '', '', '', 'a')", 'a')
  107. tester("ntpath.join('a', '')", 'a\\')
  108. tester("ntpath.join('a', '', '', '', '')", 'a\\')
  109. tester("ntpath.join('a\\', '')", 'a\\')
  110. tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
  111. tester("ntpath.join('a/', '')", 'a/')
  112. tester("ntpath.join('a/b', 'x/y')", 'a/b\\x/y')
  113. tester("ntpath.join('/a/b', 'x/y')", '/a/b\\x/y')
  114. tester("ntpath.join('/a/b/', 'x/y')", '/a/b/x/y')
  115. tester("ntpath.join('c:', 'x/y')", 'c:x/y')
  116. tester("ntpath.join('c:a/b', 'x/y')", 'c:a/b\\x/y')
  117. tester("ntpath.join('c:a/b/', 'x/y')", 'c:a/b/x/y')
  118. tester("ntpath.join('c:/', 'x/y')", 'c:/x/y')
  119. tester("ntpath.join('c:/a/b', 'x/y')", 'c:/a/b\\x/y')
  120. tester("ntpath.join('c:/a/b/', 'x/y')", 'c:/a/b/x/y')
  121. tester("ntpath.join('//computer/share', 'x/y')", '//computer/share\\x/y')
  122. tester("ntpath.join('//computer/share/', 'x/y')", '//computer/share/x/y')
  123. tester("ntpath.join('//computer/share/a/b', 'x/y')", '//computer/share/a/b\\x/y')
  124. tester("ntpath.join('a/b', '/x/y')", '/x/y')
  125. tester("ntpath.join('/a/b', '/x/y')", '/x/y')
  126. tester("ntpath.join('c:', '/x/y')", 'c:/x/y')
  127. tester("ntpath.join('c:a/b', '/x/y')", 'c:/x/y')
  128. tester("ntpath.join('c:/', '/x/y')", 'c:/x/y')
  129. tester("ntpath.join('c:/a/b', '/x/y')", 'c:/x/y')
  130. tester("ntpath.join('//computer/share', '/x/y')", '//computer/share/x/y')
  131. tester("ntpath.join('//computer/share/', '/x/y')", '//computer/share/x/y')
  132. tester("ntpath.join('//computer/share/a', '/x/y')", '//computer/share/x/y')
  133. tester("ntpath.join('c:', 'C:x/y')", 'C:x/y')
  134. tester("ntpath.join('c:a/b', 'C:x/y')", 'C:a/b\\x/y')
  135. tester("ntpath.join('c:/', 'C:x/y')", 'C:/x/y')
  136. tester("ntpath.join('c:/a/b', 'C:x/y')", 'C:/a/b\\x/y')
  137. for x in ('', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b'):
  138. for y in ('d:', 'd:x/y', 'd:/', 'd:/x/y'):
  139. tester("ntpath.join(%r, %r)" % (x, y), y)
  140. def test_normpath(self):
  141. tester("ntpath.normpath('A//////././//.//B')", r'A\B')
  142. tester("ntpath.normpath('A/./B')", r'A\B')
  143. tester("ntpath.normpath('A/foo/../B')", r'A\B')
  144. tester("ntpath.normpath('C:A//B')", r'C:A\B')
  145. tester("ntpath.normpath('D:A/./B')", r'D:A\B')
  146. tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
  147. tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
  148. tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
  149. tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
  150. tester("ntpath.normpath('..')", r'..')
  151. tester("ntpath.normpath('.')", r'.')
  152. tester("ntpath.normpath('')", r'.')
  153. tester("ntpath.normpath('/')", '\\')
  154. tester("ntpath.normpath('c:/')", 'c:\\')
  155. tester("ntpath.normpath('/../.././..')", '\\')
  156. tester("ntpath.normpath('c:/../../..')", 'c:\\')
  157. tester("ntpath.normpath('../.././..')", r'..\..\..')
  158. tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
  159. tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
  160. tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
  161. tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL')
  162. tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z')
  163. def test_expandvars(self):
  164. with test_support.EnvironmentVarGuard() as env:
  165. env.clear()
  166. env["foo"] = "bar"
  167. env["{foo"] = "baz1"
  168. env["{foo}"] = "baz2"
  169. tester('ntpath.expandvars("foo")', "foo")
  170. tester('ntpath.expandvars("$foo bar")', "bar bar")
  171. tester('ntpath.expandvars("${foo}bar")', "barbar")
  172. tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
  173. tester('ntpath.expandvars("$bar bar")', "$bar bar")
  174. tester('ntpath.expandvars("$?bar")', "$?bar")
  175. tester('ntpath.expandvars("$foo}bar")', "bar}bar")
  176. tester('ntpath.expandvars("${foo")', "${foo")
  177. tester('ntpath.expandvars("${{foo}}")', "baz1}")
  178. tester('ntpath.expandvars("$foo$foo")', "barbar")
  179. tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
  180. tester('ntpath.expandvars("%foo% bar")', "bar bar")
  181. tester('ntpath.expandvars("%foo%bar")', "barbar")
  182. tester('ntpath.expandvars("%foo%%foo%")', "barbar")
  183. tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
  184. tester('ntpath.expandvars("%?bar%")', "%?bar%")
  185. tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
  186. tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
  187. tester('ntpath.expandvars("bar\'%foo%")', "bar\'%foo%")
  188. @unittest.skipUnless(test_support.FS_NONASCII, 'need test_support.FS_NONASCII')
  189. def test_expandvars_nonascii(self):
  190. encoding = sys.getfilesystemencoding()
  191. def check(value, expected):
  192. tester0("ntpath.expandvars(%r)" % value, expected)
  193. tester0("ntpath.expandvars(%r)" % value.decode(encoding),
  194. expected.decode(encoding))
  195. with test_support.EnvironmentVarGuard() as env:
  196. env.clear()
  197. unonascii = test_support.FS_NONASCII
  198. snonascii = unonascii.encode(encoding)
  199. env['spam'] = snonascii
  200. env[snonascii] = 'ham' + snonascii
  201. check('$spam bar', '%s bar' % snonascii)
  202. check('$%s bar' % snonascii, '$%s bar' % snonascii)
  203. check('${spam}bar', '%sbar' % snonascii)
  204. check('${%s}bar' % snonascii, 'ham%sbar' % snonascii)
  205. check('$spam}bar', '%s}bar' % snonascii)
  206. check('$%s}bar' % snonascii, '$%s}bar' % snonascii)
  207. check('%spam% bar', '%s bar' % snonascii)
  208. check('%{}% bar'.format(snonascii), 'ham%s bar' % snonascii)
  209. check('%spam%bar', '%sbar' % snonascii)
  210. check('%{}%bar'.format(snonascii), 'ham%sbar' % snonascii)
  211. def test_expanduser(self):
  212. tester('ntpath.expanduser("test")', 'test')
  213. with test_support.EnvironmentVarGuard() as env:
  214. env.clear()
  215. tester('ntpath.expanduser("~test")', '~test')
  216. env['HOMEPATH'] = 'eric\\idle'
  217. env['HOMEDRIVE'] = 'C:\\'
  218. tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
  219. tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
  220. del env['HOMEDRIVE']
  221. tester('ntpath.expanduser("~test")', 'eric\\test')
  222. tester('ntpath.expanduser("~")', 'eric\\idle')
  223. env.clear()
  224. env['USERPROFILE'] = 'C:\\eric\\idle'
  225. tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
  226. tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
  227. env.clear()
  228. env['HOME'] = 'C:\\idle\\eric'
  229. tester('ntpath.expanduser("~test")', 'C:\\idle\\test')
  230. tester('ntpath.expanduser("~")', 'C:\\idle\\eric')
  231. tester('ntpath.expanduser("~test\\foo\\bar")',
  232. 'C:\\idle\\test\\foo\\bar')
  233. tester('ntpath.expanduser("~test/foo/bar")',
  234. 'C:\\idle\\test/foo/bar')
  235. tester('ntpath.expanduser("~\\foo\\bar")',
  236. 'C:\\idle\\eric\\foo\\bar')
  237. tester('ntpath.expanduser("~/foo/bar")',
  238. 'C:\\idle\\eric/foo/bar')
  239. def test_abspath(self):
  240. # ntpath.abspath() can only be used on a system with the "nt" module
  241. # (reasonably), so we protect this test with "import nt". This allows
  242. # the rest of the tests for the ntpath module to be run to completion
  243. # on any platform, since most of the module is intended to be usable
  244. # from any platform.
  245. # XXX this needs more tests
  246. try:
  247. import nt
  248. except ImportError:
  249. # check that the function is there even if we are not on Windows
  250. ntpath.abspath
  251. else:
  252. tester('ntpath.abspath("C:\\")', "C:\\")
  253. def test_relpath(self):
  254. tester('ntpath.relpath("a")', 'a')
  255. tester('ntpath.relpath(os.path.abspath("a"))', 'a')
  256. tester('ntpath.relpath("a/b")', 'a\\b')
  257. tester('ntpath.relpath("../a/b")', '..\\a\\b')
  258. with test_support.temp_cwd(test_support.TESTFN) as cwd_dir:
  259. currentdir = os.path.basename(cwd_dir)
  260. tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
  261. tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
  262. tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
  263. tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
  264. tester('ntpath.relpath("a", "a")', '.')
  265. tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
  266. tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
  267. tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
  268. tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
  269. tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
  270. tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
  271. tester('ntpath.relpath("/", "/")', '.')
  272. tester('ntpath.relpath("/a", "/a")', '.')
  273. tester('ntpath.relpath("/a/b", "/a/b")', '.')
  274. tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
  275. class NtCommonTest(test_genericpath.CommonTest):
  276. pathmodule = ntpath
  277. attributes = ['relpath', 'splitunc']
  278. def test_main():
  279. test_support.run_unittest(TestNtpath, NtCommonTest)
  280. if __name__ == "__main__":
  281. unittest.main()