blob: 1f1a971442a9555c7535669db40dfe62dbd58be6 [file] [log] [blame]
Guido van Rossumead9d8d1999-02-03 17:21:21 +00001import ntpath
Mark Hammond673c6cf2000-08-14 06:21:26 +00002import os
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +02003import sys
Georg Brandla4f46e12010-02-07 17:03:15 +00004from test.test_support import TestFailed
Florent Xiclunadc1531c2010-03-06 18:07:18 +00005from test import test_support, test_genericpath
Jerry Seutterfc7b3e32008-03-26 05:58:14 +00006import unittest
Guido van Rossumead9d8d1999-02-03 17:21:21 +00007
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +02008def 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)
Guido van Rossumead9d8d1999-02-03 17:21:21 +000013
14def tester(fn, wantResult):
Eric S. Raymondfc170b12001-02-09 11:51:27 +000015 fn = fn.replace("\\", "\\\\")
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +020016 tester0(fn, wantResult)
Tim Peters6578dc92002-12-24 18:31:27 +000017
Guido van Rossumead9d8d1999-02-03 17:21:21 +000018
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000019class TestNtpath(unittest.TestCase):
20 def test_splitext(self):
21 tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
22 tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext'))
23 tester('ntpath.splitext(".ext")', ('.ext', ''))
24 tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', ''))
25 tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', ''))
26 tester('ntpath.splitext("")', ('', ''))
27 tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext'))
28 tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext'))
29 tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext'))
30 tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000031
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000032 def test_splitdrive(self):
33 tester('ntpath.splitdrive("c:\\foo\\bar")',
34 ('c:', '\\foo\\bar'))
35 tester('ntpath.splitdrive("c:/foo/bar")',
36 ('c:', '/foo/bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000037
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000038 def test_splitunc(self):
Serhiy Storchakadd5a46c2013-12-16 15:15:29 +020039 tester('ntpath.splitunc("c:\\foo\\bar")',
40 ('', 'c:\\foo\\bar'))
41 tester('ntpath.splitunc("c:/foo/bar")',
42 ('', 'c:/foo/bar'))
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000043 tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")',
44 ('\\\\conky\\mountpoint', '\\foo\\bar'))
45 tester('ntpath.splitunc("//conky/mountpoint/foo/bar")',
46 ('//conky/mountpoint', '/foo/bar'))
Serhiy Storchakadd5a46c2013-12-16 15:15:29 +020047 tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")',
48 ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
49 tester('ntpath.splitunc("///conky/mountpoint/foo/bar")',
50 ('', '///conky/mountpoint/foo/bar'))
51 tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")',
52 ('', '\\\\conky\\\\mountpoint\\foo\\bar'))
53 tester('ntpath.splitunc("//conky//mountpoint/foo/bar")',
54 ('', '//conky//mountpoint/foo/bar'))
55 self.assertEqual(ntpath.splitunc(u'//conky/MOUNTPO\u0130NT/foo/bar'),
56 (u'//conky/MOUNTPO\u0130NT', u'/foo/bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000057
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000058 def test_split(self):
59 tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
60 tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
61 ('\\\\conky\\mountpoint\\foo', 'bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000062
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000063 tester('ntpath.split("c:\\")', ('c:\\', ''))
64 tester('ntpath.split("\\\\conky\\mountpoint\\")',
65 ('\\\\conky\\mountpoint', ''))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000066
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000067 tester('ntpath.split("c:/")', ('c:/', ''))
68 tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint', ''))
Mark Hammond673c6cf2000-08-14 06:21:26 +000069
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000070 def test_isabs(self):
71 tester('ntpath.isabs("c:\\")', 1)
72 tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
73 tester('ntpath.isabs("\\foo")', 1)
74 tester('ntpath.isabs("\\foo\\bar")', 1)
Tim Petersd4f7f602001-07-19 19:11:41 +000075
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000076 def test_commonprefix(self):
77 tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
78 "/home/swen")
79 tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
80 "\\home\\swen\\")
81 tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
82 "/home/swen/spam")
Tim Peters6a3e5f12001-11-05 21:25:02 +000083
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000084 def test_join(self):
85 tester('ntpath.join("")', '')
86 tester('ntpath.join("", "", "")', '')
87 tester('ntpath.join("a")', 'a')
88 tester('ntpath.join("/a")', '/a')
89 tester('ntpath.join("\\a")', '\\a')
90 tester('ntpath.join("a:")', 'a:')
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000091 tester('ntpath.join("a:", "\\b")', 'a:\\b')
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000092 tester('ntpath.join("a", "\\b")', '\\b')
93 tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
94 tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
95 tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
96 tester('ntpath.join("a", "b", "\\c")', '\\c')
97 tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
98 tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
Tim Peters54a14a32001-08-30 22:05:26 +000099
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000100 tester("ntpath.join('', 'a')", 'a')
101 tester("ntpath.join('', '', '', '', 'a')", 'a')
102 tester("ntpath.join('a', '')", 'a\\')
103 tester("ntpath.join('a', '', '', '', '')", 'a\\')
104 tester("ntpath.join('a\\', '')", 'a\\')
105 tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
Serhiy Storchaka31f51212014-01-27 23:14:51 +0200106 tester("ntpath.join('a/', '')", 'a/')
107
108 tester("ntpath.join('a/b', 'x/y')", 'a/b\\x/y')
109 tester("ntpath.join('/a/b', 'x/y')", '/a/b\\x/y')
110 tester("ntpath.join('/a/b/', 'x/y')", '/a/b/x/y')
111 tester("ntpath.join('c:', 'x/y')", 'c:x/y')
112 tester("ntpath.join('c:a/b', 'x/y')", 'c:a/b\\x/y')
113 tester("ntpath.join('c:a/b/', 'x/y')", 'c:a/b/x/y')
114 tester("ntpath.join('c:/', 'x/y')", 'c:/x/y')
115 tester("ntpath.join('c:/a/b', 'x/y')", 'c:/a/b\\x/y')
116 tester("ntpath.join('c:/a/b/', 'x/y')", 'c:/a/b/x/y')
117 #tester("ntpath.join('//computer/share', 'x/y')", '//computer/share\\x/y')
118 #tester("ntpath.join('//computer/share/', 'x/y')", '//computer/share/x/y')
119 #tester("ntpath.join('//computer/share/a/b', 'x/y')", '//computer/share/a/b\\x/y')
120
121 tester("ntpath.join('a/b', '/x/y')", '/x/y')
122 tester("ntpath.join('/a/b', '/x/y')", '/x/y')
123 tester("ntpath.join('c:', '/x/y')", 'c:/x/y')
124 tester("ntpath.join('c:a/b', '/x/y')", 'c:/x/y')
125 tester("ntpath.join('c:/', '/x/y')", 'c:/x/y')
126 tester("ntpath.join('c:/a/b', '/x/y')", 'c:/x/y')
127 #tester("ntpath.join('//computer/share', '/x/y')", '//computer/share/x/y')
128 #tester("ntpath.join('//computer/share/', '/x/y')", '//computer/share/x/y')
129 #tester("ntpath.join('//computer/share/a', '/x/y')", '//computer/share/x/y')
130
131 tester("ntpath.join('c:', 'C:x/y')", 'C:x/y')
132 tester("ntpath.join('c:a/b', 'C:x/y')", 'C:a/b\\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
136 for x in ('', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b'):
137 for y in ('d:', 'd:x/y', 'd:/', 'd:/x/y'):
138 tester("ntpath.join(%r, %r)" % (x, y), y)
Tim Peters54a14a32001-08-30 22:05:26 +0000139
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000140 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')
Tim Peters54a14a32001-08-30 22:05:26 +0000147
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000148 tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
149 tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
150 tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
Sjoerd Mullender33a0a062007-01-16 16:42:38 +0000151
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000152 tester("ntpath.normpath('..')", r'..')
153 tester("ntpath.normpath('.')", r'.')
154 tester("ntpath.normpath('')", r'.')
155 tester("ntpath.normpath('/')", '\\')
156 tester("ntpath.normpath('c:/')", 'c:\\')
157 tester("ntpath.normpath('/../.././..')", '\\')
158 tester("ntpath.normpath('c:/../../..')", 'c:\\')
159 tester("ntpath.normpath('../.././..')", r'..\..\..')
160 tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
161 tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
162 tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
Fred Drake5e997312002-01-15 03:46:43 +0000163
Georg Brandle2773252010-08-01 19:14:56 +0000164 tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL')
165 tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z')
166
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000167 def test_expandvars(self):
Walter Dörwald6733bed2009-05-01 17:35:37 +0000168 with test_support.EnvironmentVarGuard() as env:
169 env.clear()
170 env["foo"] = "bar"
171 env["{foo"] = "baz1"
172 env["{foo}"] = "baz2"
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000173 tester('ntpath.expandvars("foo")', "foo")
174 tester('ntpath.expandvars("$foo bar")', "bar bar")
175 tester('ntpath.expandvars("${foo}bar")', "barbar")
176 tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
177 tester('ntpath.expandvars("$bar bar")', "$bar bar")
178 tester('ntpath.expandvars("$?bar")', "$?bar")
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000179 tester('ntpath.expandvars("$foo}bar")', "bar}bar")
180 tester('ntpath.expandvars("${foo")', "${foo")
181 tester('ntpath.expandvars("${{foo}}")', "baz1}")
182 tester('ntpath.expandvars("$foo$foo")', "barbar")
183 tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
184 tester('ntpath.expandvars("%foo% bar")', "bar bar")
185 tester('ntpath.expandvars("%foo%bar")', "barbar")
186 tester('ntpath.expandvars("%foo%%foo%")', "barbar")
187 tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
188 tester('ntpath.expandvars("%?bar%")', "%?bar%")
189 tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
190 tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
Collin Winter6f187742007-03-16 22:16:08 +0000191
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200192 @unittest.skipUnless(test_support.FS_NONASCII, 'need test_support.FS_NONASCII')
193 def test_expandvars_nonascii(self):
194 encoding = sys.getfilesystemencoding()
195 def check(value, expected):
196 tester0("ntpath.expandvars(%r)" % value, expected)
197 tester0("ntpath.expandvars(%r)" % value.decode(encoding),
198 expected.decode(encoding))
199 with test_support.EnvironmentVarGuard() as env:
200 env.clear()
201 unonascii = test_support.FS_NONASCII
202 snonascii = unonascii.encode(encoding)
203 env['spam'] = snonascii
204 env[snonascii] = 'ham' + snonascii
205 check('$spam bar', '%s bar' % snonascii)
206 check('$%s bar' % snonascii, '$%s bar' % snonascii)
207 check('${spam}bar', '%sbar' % snonascii)
208 check('${%s}bar' % snonascii, 'ham%sbar' % snonascii)
209 check('$spam}bar', '%s}bar' % snonascii)
210 check('$%s}bar' % snonascii, '$%s}bar' % snonascii)
211 check('%spam% bar', '%s bar' % snonascii)
212 check('%{}% bar'.format(snonascii), 'ham%s bar' % snonascii)
213 check('%spam%bar', '%sbar' % snonascii)
214 check('%{}%bar'.format(snonascii), 'ham%sbar' % snonascii)
215
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000216 def test_abspath(self):
217 # ntpath.abspath() can only be used on a system with the "nt" module
218 # (reasonably), so we protect this test with "import nt". This allows
219 # the rest of the tests for the ntpath module to be run to completion
220 # on any platform, since most of the module is intended to be usable
221 # from any platform.
Ezio Melotti4cc80ca2010-02-20 08:09:39 +0000222 # XXX this needs more tests
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000223 try:
224 import nt
225 except ImportError:
Ezio Melotti4cc80ca2010-02-20 08:09:39 +0000226 # check that the function is there even if we are not on Windows
227 ntpath.abspath
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000228 else:
229 tester('ntpath.abspath("C:\\")', "C:\\")
230
231 def test_relpath(self):
232 currentdir = os.path.split(os.getcwd())[-1]
233 tester('ntpath.relpath("a")', 'a')
234 tester('ntpath.relpath(os.path.abspath("a"))', 'a')
235 tester('ntpath.relpath("a/b")', 'a\\b')
236 tester('ntpath.relpath("../a/b")', '..\\a\\b')
237 tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
238 tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
239 tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
240 tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
241 tester('ntpath.relpath("a", "a")', '.')
Hirokazu Yamamoto50f7d7e2010-10-18 13:55:29 +0000242 tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
243 tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
244 tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
245 tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
246 tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
247 tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
248 tester('ntpath.relpath("/", "/")', '.')
249 tester('ntpath.relpath("/a", "/a")', '.')
250 tester('ntpath.relpath("/a/b", "/a/b")', '.')
251 tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000252
253
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000254class NtCommonTest(test_genericpath.CommonTest):
Florent Xicluna985478d2010-03-06 18:52:52 +0000255 pathmodule = ntpath
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000256 attributes = ['relpath', 'splitunc']
257
258
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000259def test_main():
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000260 test_support.run_unittest(TestNtpath, NtCommonTest)
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000261
262
263if __name__ == "__main__":
264 unittest.main()