• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# coding: utf-8
2import ntpath
3import os
4import sys
5from test.test_support import TestFailed
6from test import test_support, test_genericpath
7import unittest
8
9def 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)
14
15def tester(fn, wantResult):
16    fn = fn.replace("\\", "\\\\")
17    tester0(fn, wantResult)
18
19
20class 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'))
32
33    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'))
38        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'))
53        self.assertEqual(ntpath.splitdrive("//"), ("", "//"))
54
55    def test_splitunc(self):
56        tester('ntpath.splitunc("c:\\foo\\bar")',
57               ('', 'c:\\foo\\bar'))
58        tester('ntpath.splitunc("c:/foo/bar")',
59               ('', 'c:/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        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        if test_support.have_unicode:
73            self.assertEqual(ntpath.splitunc(u'//conky/MOUNTPO%cNT/foo/bar' % 0x0130),
74                             (u'//conky/MOUNTPO%cNT' % 0x0130, u'/foo/bar'))
75
76    def test_split(self):
77        tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
78        tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
79               ('\\\\conky\\mountpoint\\foo', 'bar'))
80
81        tester('ntpath.split("c:\\")', ('c:\\', ''))
82        tester('ntpath.split("\\\\conky\\mountpoint\\")',
83               ('\\\\conky\\mountpoint\\', ''))
84
85        tester('ntpath.split("c:/")', ('c:/', ''))
86        tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', ''))
87
88    def test_isabs(self):
89        tester('ntpath.isabs("c:\\")', 1)
90        tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
91        tester('ntpath.isabs("\\foo")', 1)
92        tester('ntpath.isabs("\\foo\\bar")', 1)
93
94    def test_commonprefix(self):
95        tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
96               "/home/swen")
97        tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
98               "\\home\\swen\\")
99        tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
100               "/home/swen/spam")
101
102    def test_join(self):
103        tester('ntpath.join("")', '')
104        tester('ntpath.join("", "", "")', '')
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:", "\\b")', 'a:\\b')
110        tester('ntpath.join("a", "\\b")', '\\b')
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")', 'a\\b\\c')
114        tester('ntpath.join("a", "b", "\\c")', '\\c')
115        tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
116        tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
117
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\\')
123        tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
124        tester("ntpath.join('a/', '')", 'a/')
125
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('/a/b/', 'x/y')", '/a/b/x/y')
129        tester("ntpath.join('c:', 'x/y')", 'c:x/y')
130        tester("ntpath.join('c:a/b', 'x/y')", 'c:a/b\\x/y')
131        tester("ntpath.join('c:a/b/', 'x/y')", 'c:a/b/x/y')
132        tester("ntpath.join('c:/', 'x/y')", 'c:/x/y')
133        tester("ntpath.join('c:/a/b', 'x/y')", 'c:/a/b\\x/y')
134        tester("ntpath.join('c:/a/b/', 'x/y')", 'c:/a/b/x/y')
135        tester("ntpath.join('//computer/share', 'x/y')", '//computer/share\\x/y')
136        tester("ntpath.join('//computer/share/', 'x/y')", '//computer/share/x/y')
137        tester("ntpath.join('//computer/share/a/b', 'x/y')", '//computer/share/a/b\\x/y')
138
139        tester("ntpath.join('a/b', '/x/y')", '/x/y')
140        tester("ntpath.join('/a/b', '/x/y')", '/x/y')
141        tester("ntpath.join('c:', '/x/y')", 'c:/x/y')
142        tester("ntpath.join('c:a/b', '/x/y')", 'c:/x/y')
143        tester("ntpath.join('c:/', '/x/y')", 'c:/x/y')
144        tester("ntpath.join('c:/a/b', '/x/y')", 'c:/x/y')
145        tester("ntpath.join('//computer/share', '/x/y')", '//computer/share/x/y')
146        tester("ntpath.join('//computer/share/', '/x/y')", '//computer/share/x/y')
147        tester("ntpath.join('//computer/share/a', '/x/y')", '//computer/share/x/y')
148
149        tester("ntpath.join('c:', 'C:x/y')", 'C:x/y')
150        tester("ntpath.join('c:a/b', 'C:x/y')", 'C:a/b\\x/y')
151        tester("ntpath.join('c:/', 'C:x/y')", 'C:/x/y')
152        tester("ntpath.join('c:/a/b', 'C:x/y')", 'C:/a/b\\x/y')
153
154        for x in ('', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b'):
155            for y in ('d:', 'd:x/y', 'd:/', 'd:/x/y'):
156                tester("ntpath.join(%r, %r)" % (x, y), y)
157
158    def test_normpath(self):
159        tester("ntpath.normpath('A//////././//.//B')", r'A\B')
160        tester("ntpath.normpath('A/./B')", r'A\B')
161        tester("ntpath.normpath('A/foo/../B')", r'A\B')
162        tester("ntpath.normpath('C:A//B')", r'C:A\B')
163        tester("ntpath.normpath('D:A/./B')", r'D:A\B')
164        tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
165
166        tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
167        tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
168        tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
169
170        tester("ntpath.normpath('..')", r'..')
171        tester("ntpath.normpath('.')", r'.')
172        tester("ntpath.normpath('')", r'.')
173        tester("ntpath.normpath('/')", '\\')
174        tester("ntpath.normpath('c:/')", 'c:\\')
175        tester("ntpath.normpath('/../.././..')", '\\')
176        tester("ntpath.normpath('c:/../../..')", 'c:\\')
177        tester("ntpath.normpath('../.././..')", r'..\..\..')
178        tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
179        tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
180        tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
181
182        tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL')
183        tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z')
184
185    def test_expandvars(self):
186        with test_support.EnvironmentVarGuard() as env:
187            env.clear()
188            env["foo"] = "bar"
189            env["{foo"] = "baz1"
190            env["{foo}"] = "baz2"
191            tester('ntpath.expandvars("foo")', "foo")
192            tester('ntpath.expandvars("$foo bar")', "bar bar")
193            tester('ntpath.expandvars("${foo}bar")', "barbar")
194            tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
195            tester('ntpath.expandvars("$bar bar")', "$bar bar")
196            tester('ntpath.expandvars("$?bar")', "$?bar")
197            tester('ntpath.expandvars("$foo}bar")', "bar}bar")
198            tester('ntpath.expandvars("${foo")', "${foo")
199            tester('ntpath.expandvars("${{foo}}")', "baz1}")
200            tester('ntpath.expandvars("$foo$foo")', "barbar")
201            tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
202            tester('ntpath.expandvars("%foo% bar")', "bar bar")
203            tester('ntpath.expandvars("%foo%bar")', "barbar")
204            tester('ntpath.expandvars("%foo%%foo%")', "barbar")
205            tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
206            tester('ntpath.expandvars("%?bar%")', "%?bar%")
207            tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
208            tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
209            tester('ntpath.expandvars("bar\'%foo%")', "bar\'%foo%")
210
211    @unittest.skipUnless(test_support.FS_NONASCII, 'need test_support.FS_NONASCII')
212    def test_expandvars_nonascii(self):
213        encoding = sys.getfilesystemencoding()
214        def check(value, expected):
215            tester0("ntpath.expandvars(%r)" % value, expected)
216            tester0("ntpath.expandvars(%r)" % value.decode(encoding),
217                    expected.decode(encoding))
218        with test_support.EnvironmentVarGuard() as env:
219            env.clear()
220            unonascii = test_support.FS_NONASCII
221            snonascii = unonascii.encode(encoding)
222            env['spam'] = snonascii
223            env[snonascii] = 'ham' + snonascii
224            check('$spam bar', '%s bar' % snonascii)
225            check('$%s bar' % snonascii, '$%s bar' % snonascii)
226            check('${spam}bar', '%sbar' % snonascii)
227            check('${%s}bar' % snonascii, 'ham%sbar' % snonascii)
228            check('$spam}bar', '%s}bar' % snonascii)
229            check('$%s}bar' % snonascii, '$%s}bar' % snonascii)
230            check('%spam% bar', '%s bar' % snonascii)
231            check('%{}% bar'.format(snonascii), 'ham%s bar' % snonascii)
232            check('%spam%bar', '%sbar' % snonascii)
233            check('%{}%bar'.format(snonascii), 'ham%sbar' % snonascii)
234
235    def test_expanduser(self):
236        tester('ntpath.expanduser("test")', 'test')
237
238        with test_support.EnvironmentVarGuard() as env:
239            env.clear()
240            tester('ntpath.expanduser("~test")', '~test')
241
242            env['HOMEPATH'] = 'eric\\idle'
243            env['HOMEDRIVE'] = 'C:\\'
244            tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
245            tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
246
247            del env['HOMEDRIVE']
248            tester('ntpath.expanduser("~test")', 'eric\\test')
249            tester('ntpath.expanduser("~")', 'eric\\idle')
250
251            env.clear()
252            env['USERPROFILE'] = 'C:\\eric\\idle'
253            tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
254            tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
255
256            env.clear()
257            env['HOME'] = 'C:\\idle\\eric'
258            tester('ntpath.expanduser("~test")', 'C:\\idle\\test')
259            tester('ntpath.expanduser("~")', 'C:\\idle\\eric')
260
261            tester('ntpath.expanduser("~test\\foo\\bar")',
262                   'C:\\idle\\test\\foo\\bar')
263            tester('ntpath.expanduser("~test/foo/bar")',
264                   'C:\\idle\\test/foo/bar')
265            tester('ntpath.expanduser("~\\foo\\bar")',
266                   'C:\\idle\\eric\\foo\\bar')
267            tester('ntpath.expanduser("~/foo/bar")',
268                   'C:\\idle\\eric/foo/bar')
269
270    def test_abspath(self):
271        # ntpath.abspath() can only be used on a system with the "nt" module
272        # (reasonably), so we protect this test with "import nt".  This allows
273        # the rest of the tests for the ntpath module to be run to completion
274        # on any platform, since most of the module is intended to be usable
275        # from any platform.
276        # XXX this needs more tests
277        try:
278            import nt
279        except ImportError:
280            # check that the function is there even if we are not on Windows
281            ntpath.abspath
282        else:
283            tester('ntpath.abspath("C:\\")', "C:\\")
284
285    def test_relpath(self):
286        tester('ntpath.relpath("a")', 'a')
287        tester('ntpath.relpath(os.path.abspath("a"))', 'a')
288        tester('ntpath.relpath("a/b")', 'a\\b')
289        tester('ntpath.relpath("../a/b")', '..\\a\\b')
290        with test_support.temp_cwd(test_support.TESTFN) as cwd_dir:
291            currentdir = os.path.basename(cwd_dir)
292            tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
293            tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
294        tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
295        tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
296        tester('ntpath.relpath("a", "a")', '.')
297        tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
298        tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
299        tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
300        tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
301        tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
302        tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
303        tester('ntpath.relpath("/", "/")', '.')
304        tester('ntpath.relpath("/a", "/a")', '.')
305        tester('ntpath.relpath("/a/b", "/a/b")', '.')
306        tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
307
308
309class NtCommonTest(test_genericpath.CommonTest):
310    pathmodule = ntpath
311    attributes = ['relpath', 'splitunc']
312
313
314def test_main():
315    test_support.run_unittest(TestNtpath, NtCommonTest)
316
317
318if __name__ == "__main__":
319    unittest.main()
320