blob: 0fbe6a17786aafa84d7c6d6632374b4d8c5eaa53 [file] [log] [blame]
Benjamin Peterson3bef9352014-06-22 19:07:38 -07001# coding: utf-8
Guido van Rossumead9d8d1999-02-03 17:21:21 +00002import ntpath
Mark Hammond673c6cf2000-08-14 06:21:26 +00003import os
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +02004import sys
Georg Brandla4f46e12010-02-07 17:03:15 +00005from test.test_support import TestFailed
Florent Xiclunadc1531c2010-03-06 18:07:18 +00006from test import test_support, test_genericpath
Jerry Seutterfc7b3e32008-03-26 05:58:14 +00007import unittest
Guido van Rossumead9d8d1999-02-03 17:21:21 +00008
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +02009def tester0(fn, wantResult):
10 gotResult = eval(fn)
11 if wantResult != gotResult:
12 raise TestFailed, "%s should return: %r but returned: %r" \
13 %(fn, wantResult, gotResult)
Guido van Rossumead9d8d1999-02-03 17:21:21 +000014
15def tester(fn, wantResult):
Eric S. Raymondfc170b12001-02-09 11:51:27 +000016 fn = fn.replace("\\", "\\\\")
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +020017 tester0(fn, wantResult)
Tim Peters6578dc92002-12-24 18:31:27 +000018
Guido van Rossumead9d8d1999-02-03 17:21:21 +000019
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000020class TestNtpath(unittest.TestCase):
21 def test_splitext(self):
22 tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
23 tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext'))
24 tester('ntpath.splitext(".ext")', ('.ext', ''))
25 tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', ''))
26 tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', ''))
27 tester('ntpath.splitext("")', ('', ''))
28 tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext'))
29 tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext'))
30 tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext'))
31 tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000032
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000033 def test_splitdrive(self):
34 tester('ntpath.splitdrive("c:\\foo\\bar")',
35 ('c:', '\\foo\\bar'))
36 tester('ntpath.splitdrive("c:/foo/bar")',
37 ('c:', '/foo/bar'))
Benjamin Peterson3bef9352014-06-22 19:07:38 -070038 tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")',
39 ('\\\\conky\\mountpoint', '\\foo\\bar'))
40 tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")',
41 ('//conky/mountpoint', '/foo/bar'))
42 tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")',
43 ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
44 tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")',
45 ('', '///conky/mountpoint/foo/bar'))
46 tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")',
47 ('', '\\\\conky\\\\mountpoint\\foo\\bar'))
48 tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")',
49 ('', '//conky//mountpoint/foo/bar'))
50 # Issue #19911: UNC part containing U+0130
51 self.assertEqual(ntpath.splitdrive(u'//conky/MOUNTPOİNT/foo/bar'),
52 (u'//conky/MOUNTPOİNT', '/foo/bar'))
Benjamin Peterson2ad54212014-08-31 09:34:09 -040053 self.assertEqual(ntpath.splitdrive("//"), ("", "//"))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000054
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000055 def test_splitunc(self):
Serhiy Storchakadd5a46c2013-12-16 15:15:29 +020056 tester('ntpath.splitunc("c:\\foo\\bar")',
57 ('', 'c:\\foo\\bar'))
58 tester('ntpath.splitunc("c:/foo/bar")',
59 ('', 'c:/foo/bar'))
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000060 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'))
Serhiy Storchakadd5a46c2013-12-16 15:15:29 +020064 tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")',
65 ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
66 tester('ntpath.splitunc("///conky/mountpoint/foo/bar")',
67 ('', '///conky/mountpoint/foo/bar'))
68 tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")',
69 ('', '\\\\conky\\\\mountpoint\\foo\\bar'))
70 tester('ntpath.splitunc("//conky//mountpoint/foo/bar")',
71 ('', '//conky//mountpoint/foo/bar'))
72 self.assertEqual(ntpath.splitunc(u'//conky/MOUNTPO\u0130NT/foo/bar'),
73 (u'//conky/MOUNTPO\u0130NT', u'/foo/bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000074
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000075 def test_split(self):
76 tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
77 tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
78 ('\\\\conky\\mountpoint\\foo', 'bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000079
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000080 tester('ntpath.split("c:\\")', ('c:\\', ''))
81 tester('ntpath.split("\\\\conky\\mountpoint\\")',
Benjamin Peterson3bef9352014-06-22 19:07:38 -070082 ('\\\\conky\\mountpoint\\', ''))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000083
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000084 tester('ntpath.split("c:/")', ('c:/', ''))
Benjamin Peterson3bef9352014-06-22 19:07:38 -070085 tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', ''))
Mark Hammond673c6cf2000-08-14 06:21:26 +000086
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000087 def test_isabs(self):
88 tester('ntpath.isabs("c:\\")', 1)
89 tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
90 tester('ntpath.isabs("\\foo")', 1)
91 tester('ntpath.isabs("\\foo\\bar")', 1)
Tim Petersd4f7f602001-07-19 19:11:41 +000092
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000093 def test_commonprefix(self):
94 tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
95 "/home/swen")
96 tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
97 "\\home\\swen\\")
98 tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
99 "/home/swen/spam")
Tim Peters6a3e5f12001-11-05 21:25:02 +0000100
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000101 def test_join(self):
102 tester('ntpath.join("")', '')
103 tester('ntpath.join("", "", "")', '')
104 tester('ntpath.join("a")', 'a')
105 tester('ntpath.join("/a")', '/a')
106 tester('ntpath.join("\\a")', '\\a')
107 tester('ntpath.join("a:")', 'a:')
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000108 tester('ntpath.join("a:", "\\b")', 'a:\\b')
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000109 tester('ntpath.join("a", "\\b")', '\\b')
110 tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
111 tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
112 tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
113 tester('ntpath.join("a", "b", "\\c")', '\\c')
114 tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
115 tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
Tim Peters54a14a32001-08-30 22:05:26 +0000116
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000117 tester("ntpath.join('', 'a')", 'a')
118 tester("ntpath.join('', '', '', '', 'a')", 'a')
119 tester("ntpath.join('a', '')", 'a\\')
120 tester("ntpath.join('a', '', '', '', '')", 'a\\')
121 tester("ntpath.join('a\\', '')", 'a\\')
122 tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
Serhiy Storchaka31f51212014-01-27 23:14:51 +0200123 tester("ntpath.join('a/', '')", 'a/')
124
125 tester("ntpath.join('a/b', 'x/y')", 'a/b\\x/y')
126 tester("ntpath.join('/a/b', 'x/y')", '/a/b\\x/y')
127 tester("ntpath.join('/a/b/', 'x/y')", '/a/b/x/y')
128 tester("ntpath.join('c:', 'x/y')", 'c:x/y')
129 tester("ntpath.join('c:a/b', 'x/y')", 'c:a/b\\x/y')
130 tester("ntpath.join('c:a/b/', 'x/y')", 'c:a/b/x/y')
131 tester("ntpath.join('c:/', 'x/y')", 'c:/x/y')
132 tester("ntpath.join('c:/a/b', 'x/y')", 'c:/a/b\\x/y')
133 tester("ntpath.join('c:/a/b/', 'x/y')", 'c:/a/b/x/y')
Benjamin Peterson3bef9352014-06-22 19:07:38 -0700134 tester("ntpath.join('//computer/share', 'x/y')", '//computer/share\\x/y')
135 tester("ntpath.join('//computer/share/', 'x/y')", '//computer/share/x/y')
136 tester("ntpath.join('//computer/share/a/b', 'x/y')", '//computer/share/a/b\\x/y')
Serhiy Storchaka31f51212014-01-27 23:14:51 +0200137
138 tester("ntpath.join('a/b', '/x/y')", '/x/y')
139 tester("ntpath.join('/a/b', '/x/y')", '/x/y')
140 tester("ntpath.join('c:', '/x/y')", 'c:/x/y')
141 tester("ntpath.join('c:a/b', '/x/y')", 'c:/x/y')
142 tester("ntpath.join('c:/', '/x/y')", 'c:/x/y')
143 tester("ntpath.join('c:/a/b', '/x/y')", 'c:/x/y')
Benjamin Peterson3bef9352014-06-22 19:07:38 -0700144 tester("ntpath.join('//computer/share', '/x/y')", '//computer/share/x/y')
145 tester("ntpath.join('//computer/share/', '/x/y')", '//computer/share/x/y')
146 tester("ntpath.join('//computer/share/a', '/x/y')", '//computer/share/x/y')
Serhiy Storchaka31f51212014-01-27 23:14:51 +0200147
148 tester("ntpath.join('c:', 'C:x/y')", 'C:x/y')
149 tester("ntpath.join('c:a/b', 'C:x/y')", 'C:a/b\\x/y')
150 tester("ntpath.join('c:/', 'C:x/y')", 'C:/x/y')
151 tester("ntpath.join('c:/a/b', 'C:x/y')", 'C:/a/b\\x/y')
152
153 for x in ('', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b'):
154 for y in ('d:', 'd:x/y', 'd:/', 'd:/x/y'):
155 tester("ntpath.join(%r, %r)" % (x, y), y)
Tim Peters54a14a32001-08-30 22:05:26 +0000156
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000157 def test_normpath(self):
158 tester("ntpath.normpath('A//////././//.//B')", r'A\B')
159 tester("ntpath.normpath('A/./B')", r'A\B')
160 tester("ntpath.normpath('A/foo/../B')", r'A\B')
161 tester("ntpath.normpath('C:A//B')", r'C:A\B')
162 tester("ntpath.normpath('D:A/./B')", r'D:A\B')
163 tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
Tim Peters54a14a32001-08-30 22:05:26 +0000164
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000165 tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
166 tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
167 tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
Sjoerd Mullender33a0a062007-01-16 16:42:38 +0000168
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000169 tester("ntpath.normpath('..')", r'..')
170 tester("ntpath.normpath('.')", r'.')
171 tester("ntpath.normpath('')", r'.')
172 tester("ntpath.normpath('/')", '\\')
173 tester("ntpath.normpath('c:/')", 'c:\\')
174 tester("ntpath.normpath('/../.././..')", '\\')
175 tester("ntpath.normpath('c:/../../..')", 'c:\\')
176 tester("ntpath.normpath('../.././..')", r'..\..\..')
177 tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
178 tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
179 tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
Fred Drake5e997312002-01-15 03:46:43 +0000180
Georg Brandle2773252010-08-01 19:14:56 +0000181 tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL')
182 tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z')
183
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000184 def test_expandvars(self):
Walter Dörwald6733bed2009-05-01 17:35:37 +0000185 with test_support.EnvironmentVarGuard() as env:
186 env.clear()
187 env["foo"] = "bar"
188 env["{foo"] = "baz1"
189 env["{foo}"] = "baz2"
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000190 tester('ntpath.expandvars("foo")', "foo")
191 tester('ntpath.expandvars("$foo bar")', "bar bar")
192 tester('ntpath.expandvars("${foo}bar")', "barbar")
193 tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
194 tester('ntpath.expandvars("$bar bar")', "$bar bar")
195 tester('ntpath.expandvars("$?bar")', "$?bar")
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000196 tester('ntpath.expandvars("$foo}bar")', "bar}bar")
197 tester('ntpath.expandvars("${foo")', "${foo")
198 tester('ntpath.expandvars("${{foo}}")', "baz1}")
199 tester('ntpath.expandvars("$foo$foo")', "barbar")
200 tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
201 tester('ntpath.expandvars("%foo% bar")', "bar bar")
202 tester('ntpath.expandvars("%foo%bar")', "barbar")
203 tester('ntpath.expandvars("%foo%%foo%")', "barbar")
204 tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
205 tester('ntpath.expandvars("%?bar%")', "%?bar%")
206 tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
207 tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
Collin Winter6f187742007-03-16 22:16:08 +0000208
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200209 @unittest.skipUnless(test_support.FS_NONASCII, 'need test_support.FS_NONASCII')
210 def test_expandvars_nonascii(self):
211 encoding = sys.getfilesystemencoding()
212 def check(value, expected):
213 tester0("ntpath.expandvars(%r)" % value, expected)
214 tester0("ntpath.expandvars(%r)" % value.decode(encoding),
215 expected.decode(encoding))
216 with test_support.EnvironmentVarGuard() as env:
217 env.clear()
218 unonascii = test_support.FS_NONASCII
219 snonascii = unonascii.encode(encoding)
220 env['spam'] = snonascii
221 env[snonascii] = 'ham' + snonascii
222 check('$spam bar', '%s bar' % snonascii)
223 check('$%s bar' % snonascii, '$%s bar' % snonascii)
224 check('${spam}bar', '%sbar' % snonascii)
225 check('${%s}bar' % snonascii, 'ham%sbar' % snonascii)
226 check('$spam}bar', '%s}bar' % snonascii)
227 check('$%s}bar' % snonascii, '$%s}bar' % snonascii)
228 check('%spam% bar', '%s bar' % snonascii)
229 check('%{}% bar'.format(snonascii), 'ham%s bar' % snonascii)
230 check('%spam%bar', '%sbar' % snonascii)
231 check('%{}%bar'.format(snonascii), 'ham%sbar' % snonascii)
232
Serhiy Storchaka49b20862014-05-28 18:11:08 +0300233 def test_expanduser(self):
234 tester('ntpath.expanduser("test")', 'test')
235
236 with test_support.EnvironmentVarGuard() as env:
237 env.clear()
238 tester('ntpath.expanduser("~test")', '~test')
239
240 env['HOMEPATH'] = 'eric\\idle'
241 env['HOMEDRIVE'] = 'C:\\'
242 tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
243 tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
244
245 del env['HOMEDRIVE']
246 tester('ntpath.expanduser("~test")', 'eric\\test')
247 tester('ntpath.expanduser("~")', 'eric\\idle')
248
249 env.clear()
250 env['USERPROFILE'] = 'C:\\eric\\idle'
251 tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
252 tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
253
254 env.clear()
255 env['HOME'] = 'C:\\idle\\eric'
256 tester('ntpath.expanduser("~test")', 'C:\\idle\\test')
257 tester('ntpath.expanduser("~")', 'C:\\idle\\eric')
258
259 tester('ntpath.expanduser("~test\\foo\\bar")',
260 'C:\\idle\\test\\foo\\bar')
261 tester('ntpath.expanduser("~test/foo/bar")',
262 'C:\\idle\\test/foo/bar')
263 tester('ntpath.expanduser("~\\foo\\bar")',
264 'C:\\idle\\eric\\foo\\bar')
265 tester('ntpath.expanduser("~/foo/bar")',
266 'C:\\idle\\eric/foo/bar')
267
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000268 def test_abspath(self):
269 # ntpath.abspath() can only be used on a system with the "nt" module
270 # (reasonably), so we protect this test with "import nt". This allows
271 # the rest of the tests for the ntpath module to be run to completion
272 # on any platform, since most of the module is intended to be usable
273 # from any platform.
Ezio Melotti4cc80ca2010-02-20 08:09:39 +0000274 # XXX this needs more tests
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000275 try:
276 import nt
277 except ImportError:
Ezio Melotti4cc80ca2010-02-20 08:09:39 +0000278 # check that the function is there even if we are not on Windows
279 ntpath.abspath
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000280 else:
281 tester('ntpath.abspath("C:\\")', "C:\\")
282
283 def test_relpath(self):
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000284 tester('ntpath.relpath("a")', 'a')
285 tester('ntpath.relpath(os.path.abspath("a"))', 'a')
286 tester('ntpath.relpath("a/b")', 'a\\b')
287 tester('ntpath.relpath("../a/b")', '..\\a\\b')
Serhiy Storchaka38a33ec2015-01-26 10:26:00 +0200288 with test_support.temp_cwd(test_support.TESTFN) as cwd_dir:
289 currentdir = os.path.basename(cwd_dir)
290 tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
291 tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000292 tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
293 tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
294 tester('ntpath.relpath("a", "a")', '.')
Hirokazu Yamamoto50f7d7e2010-10-18 13:55:29 +0000295 tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
296 tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
297 tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
298 tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
299 tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
300 tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
301 tester('ntpath.relpath("/", "/")', '.')
302 tester('ntpath.relpath("/a", "/a")', '.')
303 tester('ntpath.relpath("/a/b", "/a/b")', '.')
304 tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000305
306
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000307class NtCommonTest(test_genericpath.CommonTest):
Florent Xicluna985478d2010-03-06 18:52:52 +0000308 pathmodule = ntpath
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000309 attributes = ['relpath', 'splitunc']
310
311
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000312def test_main():
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000313 test_support.run_unittest(TestNtpath, NtCommonTest)
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000314
315
316if __name__ == "__main__":
317 unittest.main()