blob: 11e66fb0a7464c5bcf59a418b3f0251d54ad8e5f [file] [log] [blame]
Guido van Rossumead9d8d1999-02-03 17:21:21 +00001import ntpath
Mark Hammond673c6cf2000-08-14 06:21:26 +00002import os
Georg Brandla4f46e12010-02-07 17:03:15 +00003from test.test_support import TestFailed
Florent Xiclunadc1531c2010-03-06 18:07:18 +00004from test import test_support, test_genericpath
Jerry Seutterfc7b3e32008-03-26 05:58:14 +00005import unittest
Guido van Rossumead9d8d1999-02-03 17:21:21 +00006
Guido van Rossumead9d8d1999-02-03 17:21:21 +00007
8def tester(fn, wantResult):
Eric S. Raymondfc170b12001-02-09 11:51:27 +00009 fn = fn.replace("\\", "\\\\")
Fred Drake004d5e62000-10-23 17:22:08 +000010 gotResult = eval(fn)
11 if wantResult != gotResult:
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000012 raise TestFailed, "%s should return: %s but returned: %s" \
13 %(str(fn), str(wantResult), str(gotResult))
Tim Peters6578dc92002-12-24 18:31:27 +000014
Guido van Rossumead9d8d1999-02-03 17:21:21 +000015
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000016class 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'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000028
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000029 def test_splitdrive(self):
30 tester('ntpath.splitdrive("c:\\foo\\bar")',
31 ('c:', '\\foo\\bar'))
32 tester('ntpath.splitdrive("c:/foo/bar")',
33 ('c:', '/foo/bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000034
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000035 def test_splitunc(self):
Serhiy Storchakadd5a46c2013-12-16 15:15:29 +020036 tester('ntpath.splitunc("c:\\foo\\bar")',
37 ('', 'c:\\foo\\bar'))
38 tester('ntpath.splitunc("c:/foo/bar")',
39 ('', 'c:/foo/bar'))
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000040 tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")',
41 ('\\\\conky\\mountpoint', '\\foo\\bar'))
42 tester('ntpath.splitunc("//conky/mountpoint/foo/bar")',
43 ('//conky/mountpoint', '/foo/bar'))
Serhiy Storchakadd5a46c2013-12-16 15:15:29 +020044 tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")',
45 ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
46 tester('ntpath.splitunc("///conky/mountpoint/foo/bar")',
47 ('', '///conky/mountpoint/foo/bar'))
48 tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")',
49 ('', '\\\\conky\\\\mountpoint\\foo\\bar'))
50 tester('ntpath.splitunc("//conky//mountpoint/foo/bar")',
51 ('', '//conky//mountpoint/foo/bar'))
52 self.assertEqual(ntpath.splitunc(u'//conky/MOUNTPO\u0130NT/foo/bar'),
53 (u'//conky/MOUNTPO\u0130NT', u'/foo/bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000054
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000055 def test_split(self):
56 tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
57 tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
58 ('\\\\conky\\mountpoint\\foo', 'bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000059
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000060 tester('ntpath.split("c:\\")', ('c:\\', ''))
61 tester('ntpath.split("\\\\conky\\mountpoint\\")',
62 ('\\\\conky\\mountpoint', ''))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000063
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000064 tester('ntpath.split("c:/")', ('c:/', ''))
65 tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint', ''))
Mark Hammond673c6cf2000-08-14 06:21:26 +000066
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000067 def test_isabs(self):
68 tester('ntpath.isabs("c:\\")', 1)
69 tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
70 tester('ntpath.isabs("\\foo")', 1)
71 tester('ntpath.isabs("\\foo\\bar")', 1)
Tim Petersd4f7f602001-07-19 19:11:41 +000072
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000073 def test_commonprefix(self):
74 tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
75 "/home/swen")
76 tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
77 "\\home\\swen\\")
78 tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
79 "/home/swen/spam")
Tim Peters6a3e5f12001-11-05 21:25:02 +000080
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000081 def test_join(self):
82 tester('ntpath.join("")', '')
83 tester('ntpath.join("", "", "")', '')
84 tester('ntpath.join("a")', 'a')
85 tester('ntpath.join("/a")', '/a')
86 tester('ntpath.join("\\a")', '\\a')
87 tester('ntpath.join("a:")', 'a:')
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000088 tester('ntpath.join("a:", "\\b")', 'a:\\b')
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000089 tester('ntpath.join("a", "\\b")', '\\b')
90 tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
91 tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
92 tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
93 tester('ntpath.join("a", "b", "\\c")', '\\c')
94 tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
95 tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
Tim Peters54a14a32001-08-30 22:05:26 +000096
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000097 tester("ntpath.join('', 'a')", 'a')
98 tester("ntpath.join('', '', '', '', 'a')", 'a')
99 tester("ntpath.join('a', '')", 'a\\')
100 tester("ntpath.join('a', '', '', '', '')", 'a\\')
101 tester("ntpath.join('a\\', '')", 'a\\')
102 tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
Serhiy Storchaka31f51212014-01-27 23:14:51 +0200103 tester("ntpath.join('a/', '')", 'a/')
104
105 tester("ntpath.join('a/b', 'x/y')", 'a/b\\x/y')
106 tester("ntpath.join('/a/b', 'x/y')", '/a/b\\x/y')
107 tester("ntpath.join('/a/b/', 'x/y')", '/a/b/x/y')
108 tester("ntpath.join('c:', 'x/y')", 'c:x/y')
109 tester("ntpath.join('c:a/b', 'x/y')", 'c:a/b\\x/y')
110 tester("ntpath.join('c:a/b/', 'x/y')", 'c: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('//computer/share', 'x/y')", '//computer/share\\x/y')
115 #tester("ntpath.join('//computer/share/', 'x/y')", '//computer/share/x/y')
116 #tester("ntpath.join('//computer/share/a/b', 'x/y')", '//computer/share/a/b\\x/y')
117
118 tester("ntpath.join('a/b', '/x/y')", '/x/y')
119 tester("ntpath.join('/a/b', '/x/y')", '/x/y')
120 tester("ntpath.join('c:', '/x/y')", 'c:/x/y')
121 tester("ntpath.join('c:a/b', '/x/y')", 'c:/x/y')
122 tester("ntpath.join('c:/', '/x/y')", 'c:/x/y')
123 tester("ntpath.join('c:/a/b', '/x/y')", 'c:/x/y')
124 #tester("ntpath.join('//computer/share', '/x/y')", '//computer/share/x/y')
125 #tester("ntpath.join('//computer/share/', '/x/y')", '//computer/share/x/y')
126 #tester("ntpath.join('//computer/share/a', '/x/y')", '//computer/share/x/y')
127
128 tester("ntpath.join('c:', 'C:x/y')", 'C:x/y')
129 tester("ntpath.join('c:a/b', 'C:x/y')", 'C:a/b\\x/y')
130 tester("ntpath.join('c:/', 'C:x/y')", 'C:/x/y')
131 tester("ntpath.join('c:/a/b', 'C:x/y')", 'C:/a/b\\x/y')
132
133 for x in ('', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b'):
134 for y in ('d:', 'd:x/y', 'd:/', 'd:/x/y'):
135 tester("ntpath.join(%r, %r)" % (x, y), y)
Tim Peters54a14a32001-08-30 22:05:26 +0000136
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000137 def test_normpath(self):
138 tester("ntpath.normpath('A//////././//.//B')", r'A\B')
139 tester("ntpath.normpath('A/./B')", r'A\B')
140 tester("ntpath.normpath('A/foo/../B')", r'A\B')
141 tester("ntpath.normpath('C:A//B')", r'C:A\B')
142 tester("ntpath.normpath('D:A/./B')", r'D:A\B')
143 tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
Tim Peters54a14a32001-08-30 22:05:26 +0000144
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000145 tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
146 tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
147 tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
Sjoerd Mullender33a0a062007-01-16 16:42:38 +0000148
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000149 tester("ntpath.normpath('..')", r'..')
150 tester("ntpath.normpath('.')", r'.')
151 tester("ntpath.normpath('')", r'.')
152 tester("ntpath.normpath('/')", '\\')
153 tester("ntpath.normpath('c:/')", 'c:\\')
154 tester("ntpath.normpath('/../.././..')", '\\')
155 tester("ntpath.normpath('c:/../../..')", 'c:\\')
156 tester("ntpath.normpath('../.././..')", r'..\..\..')
157 tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
158 tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
159 tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
Fred Drake5e997312002-01-15 03:46:43 +0000160
Georg Brandle2773252010-08-01 19:14:56 +0000161 tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL')
162 tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z')
163
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000164 def test_expandvars(self):
Walter Dörwald6733bed2009-05-01 17:35:37 +0000165 with test_support.EnvironmentVarGuard() as env:
166 env.clear()
167 env["foo"] = "bar"
168 env["{foo"] = "baz1"
169 env["{foo}"] = "baz2"
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000170 tester('ntpath.expandvars("foo")', "foo")
171 tester('ntpath.expandvars("$foo bar")', "bar bar")
172 tester('ntpath.expandvars("${foo}bar")', "barbar")
173 tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
174 tester('ntpath.expandvars("$bar bar")', "$bar bar")
175 tester('ntpath.expandvars("$?bar")', "$?bar")
176 tester('ntpath.expandvars("${foo}bar")', "barbar")
177 tester('ntpath.expandvars("$foo}bar")', "bar}bar")
178 tester('ntpath.expandvars("${foo")', "${foo")
179 tester('ntpath.expandvars("${{foo}}")', "baz1}")
180 tester('ntpath.expandvars("$foo$foo")', "barbar")
181 tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
182 tester('ntpath.expandvars("%foo% bar")', "bar bar")
183 tester('ntpath.expandvars("%foo%bar")', "barbar")
184 tester('ntpath.expandvars("%foo%%foo%")', "barbar")
185 tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
186 tester('ntpath.expandvars("%?bar%")', "%?bar%")
187 tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
188 tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
Collin Winter6f187742007-03-16 22:16:08 +0000189
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000190 def test_abspath(self):
191 # ntpath.abspath() can only be used on a system with the "nt" module
192 # (reasonably), so we protect this test with "import nt". This allows
193 # the rest of the tests for the ntpath module to be run to completion
194 # on any platform, since most of the module is intended to be usable
195 # from any platform.
Ezio Melotti4cc80ca2010-02-20 08:09:39 +0000196 # XXX this needs more tests
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000197 try:
198 import nt
199 except ImportError:
Ezio Melotti4cc80ca2010-02-20 08:09:39 +0000200 # check that the function is there even if we are not on Windows
201 ntpath.abspath
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000202 else:
203 tester('ntpath.abspath("C:\\")', "C:\\")
204
205 def test_relpath(self):
206 currentdir = os.path.split(os.getcwd())[-1]
207 tester('ntpath.relpath("a")', 'a')
208 tester('ntpath.relpath(os.path.abspath("a"))', 'a')
209 tester('ntpath.relpath("a/b")', 'a\\b')
210 tester('ntpath.relpath("../a/b")', '..\\a\\b')
211 tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
212 tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
213 tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
214 tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
215 tester('ntpath.relpath("a", "a")', '.')
Hirokazu Yamamoto50f7d7e2010-10-18 13:55:29 +0000216 tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
217 tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
218 tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
219 tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
220 tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
221 tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
222 tester('ntpath.relpath("/", "/")', '.')
223 tester('ntpath.relpath("/a", "/a")', '.')
224 tester('ntpath.relpath("/a/b", "/a/b")', '.')
225 tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000226
227
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000228class NtCommonTest(test_genericpath.CommonTest):
Florent Xicluna985478d2010-03-06 18:52:52 +0000229 pathmodule = ntpath
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000230 attributes = ['relpath', 'splitunc']
231
232
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000233def test_main():
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000234 test_support.run_unittest(TestNtpath, NtCommonTest)
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000235
236
237if __name__ == "__main__":
238 unittest.main()