blob: 646495098230d8075df79a51e540fc1518b22688 [file] [log] [blame]
Guido van Rossumead9d8d1999-02-03 17:21:21 +00001import ntpath
Mark Hammond673c6cf2000-08-14 06:21:26 +00002import os
Brian Curtin13a0db52010-09-06 19:46:17 +00003import sys
Georg Brandl1b37e872010-03-14 10:45:50 +00004from test.support import TestFailed
Florent Xiclunac9c79782010-03-08 12:24:53 +00005from test import support, test_genericpath
Brian Curtin62857742010-09-06 17:07:27 +00006from tempfile import TemporaryFile
Christian Heimesf6cd9672008-03-26 13:45:42 +00007import unittest
Guido van Rossumead9d8d1999-02-03 17:21:21 +00008
Guido van Rossumead9d8d1999-02-03 17:21:21 +00009
10def tester(fn, wantResult):
Eric S. Raymondfc170b12001-02-09 11:51:27 +000011 fn = fn.replace("\\", "\\\\")
Fred Drake004d5e62000-10-23 17:22:08 +000012 gotResult = eval(fn)
13 if wantResult != gotResult:
Christian Heimesf6cd9672008-03-26 13:45:42 +000014 raise TestFailed("%s should return: %s but returned: %s" \
15 %(str(fn), str(wantResult), str(gotResult)))
Tim Peters6578dc92002-12-24 18:31:27 +000016
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +000017 # then with bytes
18 fn = fn.replace("('", "(b'")
19 fn = fn.replace('("', '(b"')
20 fn = fn.replace("['", "[b'")
21 fn = fn.replace('["', '[b"')
22 fn = fn.replace(", '", ", b'")
23 fn = fn.replace(', "', ', b"')
24 gotResult = eval(fn)
25 if isinstance(wantResult, str):
26 wantResult = wantResult.encode('ascii')
27 elif isinstance(wantResult, tuple):
28 wantResult = tuple(r.encode('ascii') for r in wantResult)
29
30 gotResult = eval(fn)
31 if wantResult != gotResult:
32 raise TestFailed("%s should return: %s but returned: %s" \
33 %(str(fn), str(wantResult), repr(gotResult)))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000034
Mark Hammond5a607a32009-05-06 08:04:54 +000035
Christian Heimesf6cd9672008-03-26 13:45:42 +000036class TestNtpath(unittest.TestCase):
37 def test_splitext(self):
38 tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
39 tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext'))
40 tester('ntpath.splitext(".ext")', ('.ext', ''))
41 tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', ''))
42 tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', ''))
43 tester('ntpath.splitext("")', ('', ''))
44 tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext'))
45 tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext'))
46 tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext'))
47 tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000048
Christian Heimesf6cd9672008-03-26 13:45:42 +000049 def test_splitdrive(self):
50 tester('ntpath.splitdrive("c:\\foo\\bar")',
51 ('c:', '\\foo\\bar'))
52 tester('ntpath.splitdrive("c:/foo/bar")',
53 ('c:', '/foo/bar'))
Mark Hammond5a607a32009-05-06 08:04:54 +000054 tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")',
Christian Heimesf6cd9672008-03-26 13:45:42 +000055 ('\\\\conky\\mountpoint', '\\foo\\bar'))
Mark Hammond5a607a32009-05-06 08:04:54 +000056 tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")',
Christian Heimesf6cd9672008-03-26 13:45:42 +000057 ('//conky/mountpoint', '/foo/bar'))
Mark Hammond5a607a32009-05-06 08:04:54 +000058 tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")',
59 ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
60 tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")',
61 ('', '///conky/mountpoint/foo/bar'))
62 tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")',
63 ('', '\\\\conky\\\\mountpoint\\foo\\bar'))
64 tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")',
65 ('', '//conky//mountpoint/foo/bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000066
Christian Heimesf6cd9672008-03-26 13:45:42 +000067 def test_split(self):
68 tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
69 tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
70 ('\\\\conky\\mountpoint\\foo', 'bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000071
Christian Heimesf6cd9672008-03-26 13:45:42 +000072 tester('ntpath.split("c:\\")', ('c:\\', ''))
73 tester('ntpath.split("\\\\conky\\mountpoint\\")',
Mark Hammond5a607a32009-05-06 08:04:54 +000074 ('\\\\conky\\mountpoint\\', ''))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000075
Christian Heimesf6cd9672008-03-26 13:45:42 +000076 tester('ntpath.split("c:/")', ('c:/', ''))
Mark Hammond5a607a32009-05-06 08:04:54 +000077 tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', ''))
Mark Hammond673c6cf2000-08-14 06:21:26 +000078
Christian Heimesf6cd9672008-03-26 13:45:42 +000079 def test_isabs(self):
80 tester('ntpath.isabs("c:\\")', 1)
81 tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
82 tester('ntpath.isabs("\\foo")', 1)
83 tester('ntpath.isabs("\\foo\\bar")', 1)
Tim Petersd4f7f602001-07-19 19:11:41 +000084
Christian Heimesf6cd9672008-03-26 13:45:42 +000085 def test_commonprefix(self):
86 tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
87 "/home/swen")
88 tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
89 "\\home\\swen\\")
90 tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
91 "/home/swen/spam")
Tim Peters6a3e5f12001-11-05 21:25:02 +000092
Christian Heimesf6cd9672008-03-26 13:45:42 +000093 def test_join(self):
94 tester('ntpath.join("")', '')
95 tester('ntpath.join("", "", "")', '')
96 tester('ntpath.join("a")', 'a')
97 tester('ntpath.join("/a")', '/a')
98 tester('ntpath.join("\\a")', '\\a')
99 tester('ntpath.join("a:")', 'a:')
100 tester('ntpath.join("a:", "b")', 'a:b')
101 tester('ntpath.join("a:", "/b")', 'a:/b')
102 tester('ntpath.join("a:", "\\b")', 'a:\\b')
103 tester('ntpath.join("a", "/b")', '/b')
104 tester('ntpath.join("a", "\\b")', '\\b')
105 tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
106 tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
107 tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
108 tester('ntpath.join("a", "b", "\\c")', '\\c')
109 tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
110 tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
111 tester("ntpath.join('c:', '/a')", 'c:/a')
112 tester("ntpath.join('c:/', '/a')", 'c:/a')
113 tester("ntpath.join('c:/a', '/b')", '/b')
114 tester("ntpath.join('c:', 'd:/')", 'd:/')
115 tester("ntpath.join('c:/', 'd:/')", 'd:/')
116 tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b')
Tim Peters54a14a32001-08-30 22:05:26 +0000117
Christian Heimesf6cd9672008-03-26 13:45:42 +0000118 tester("ntpath.join('')", '')
119 tester("ntpath.join('', '', '', '', '')", '')
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 tester("ntpath.join('a\\', '')", 'a\\')
126 tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
Tim Peters54a14a32001-08-30 22:05:26 +0000127
Mark Hammond5a607a32009-05-06 08:04:54 +0000128 # from comment in ntpath.join
129 tester("ntpath.join('c:', '/a')", 'c:/a')
130 tester("ntpath.join('//computer/share', '/a')", '//computer/share/a')
131 tester("ntpath.join('c:/', '/a')", 'c:/a')
132 tester("ntpath.join('//computer/share/', '/a')", '//computer/share/a')
133 tester("ntpath.join('c:/a', '/b')", '/b')
134 tester("ntpath.join('//computer/share/a', '/b')", '/b')
135 tester("ntpath.join('c:', 'd:/')", 'd:/')
136 tester("ntpath.join('c:', '//computer/share/')", '//computer/share/')
137 tester("ntpath.join('//computer/share', 'd:/')", 'd:/')
138 tester("ntpath.join('//computer/share', '//computer/share/')", '//computer/share/')
139 tester("ntpath.join('c:/', 'd:/')", 'd:/')
140 tester("ntpath.join('c:/', '//computer/share/')", '//computer/share/')
141 tester("ntpath.join('//computer/share/', 'd:/')", 'd:/')
142 tester("ntpath.join('//computer/share/', '//computer/share/')", '//computer/share/')
143
144 tester("ntpath.join('c:', '//computer/share/')", '//computer/share/')
145 tester("ntpath.join('c:/', '//computer/share/')", '//computer/share/')
146 tester("ntpath.join('c:/', '//computer/share/a/b')", '//computer/share/a/b')
147
148 tester("ntpath.join('\\\\computer\\share\\', 'a', 'b')", '\\\\computer\\share\\a\\b')
149 tester("ntpath.join('\\\\computer\\share', 'a', 'b')", '\\\\computer\\share\\a\\b')
150 tester("ntpath.join('\\\\computer\\share', 'a\\b')", '\\\\computer\\share\\a\\b')
151 tester("ntpath.join('//computer/share/', 'a', 'b')", '//computer/share/a\\b')
152 tester("ntpath.join('//computer/share', 'a', 'b')", '//computer/share\\a\\b')
153 tester("ntpath.join('//computer/share', 'a/b')", '//computer/share\\a/b')
154
Christian Heimesf6cd9672008-03-26 13:45:42 +0000155 def test_normpath(self):
156 tester("ntpath.normpath('A//////././//.//B')", r'A\B')
157 tester("ntpath.normpath('A/./B')", r'A\B')
158 tester("ntpath.normpath('A/foo/../B')", r'A\B')
159 tester("ntpath.normpath('C:A//B')", r'C:A\B')
160 tester("ntpath.normpath('D:A/./B')", r'D:A\B')
161 tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
Tim Peters54a14a32001-08-30 22:05:26 +0000162
Christian Heimesf6cd9672008-03-26 13:45:42 +0000163 tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
164 tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
165 tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
Thomas Woutersb2137042007-02-01 18:02:27 +0000166
Christian Heimesf6cd9672008-03-26 13:45:42 +0000167 tester("ntpath.normpath('..')", r'..')
168 tester("ntpath.normpath('.')", r'.')
169 tester("ntpath.normpath('')", r'.')
170 tester("ntpath.normpath('/')", '\\')
171 tester("ntpath.normpath('c:/')", 'c:\\')
172 tester("ntpath.normpath('/../.././..')", '\\')
173 tester("ntpath.normpath('c:/../../..')", 'c:\\')
174 tester("ntpath.normpath('../.././..')", r'..\..\..')
175 tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
176 tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
177 tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
Fred Drake5e997312002-01-15 03:46:43 +0000178
Georg Brandlcfb68212010-07-31 21:40:15 +0000179 tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL')
180 tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z')
181
Christian Heimesf6cd9672008-03-26 13:45:42 +0000182 def test_expandvars(self):
Walter Dörwald155374d2009-05-01 19:58:58 +0000183 with support.EnvironmentVarGuard() as env:
184 env.clear()
185 env["foo"] = "bar"
186 env["{foo"] = "baz1"
187 env["{foo}"] = "baz2"
Christian Heimesf6cd9672008-03-26 13:45:42 +0000188 tester('ntpath.expandvars("foo")', "foo")
189 tester('ntpath.expandvars("$foo bar")', "bar bar")
190 tester('ntpath.expandvars("${foo}bar")', "barbar")
191 tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
192 tester('ntpath.expandvars("$bar bar")', "$bar bar")
193 tester('ntpath.expandvars("$?bar")', "$?bar")
194 tester('ntpath.expandvars("${foo}bar")', "barbar")
195 tester('ntpath.expandvars("$foo}bar")', "bar}bar")
196 tester('ntpath.expandvars("${foo")', "${foo")
197 tester('ntpath.expandvars("${{foo}}")', "baz1}")
198 tester('ntpath.expandvars("$foo$foo")', "barbar")
199 tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
200 tester('ntpath.expandvars("%foo% bar")', "bar bar")
201 tester('ntpath.expandvars("%foo%bar")', "barbar")
202 tester('ntpath.expandvars("%foo%%foo%")', "barbar")
203 tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
204 tester('ntpath.expandvars("%?bar%")', "%?bar%")
205 tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
206 tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000207
Christian Heimesf6cd9672008-03-26 13:45:42 +0000208 def test_abspath(self):
209 # ntpath.abspath() can only be used on a system with the "nt" module
210 # (reasonably), so we protect this test with "import nt". This allows
211 # the rest of the tests for the ntpath module to be run to completion
212 # on any platform, since most of the module is intended to be usable
213 # from any platform.
214 try:
215 import nt
Mark Hammond5a607a32009-05-06 08:04:54 +0000216 tester('ntpath.abspath("C:\\")', "C:\\")
Christian Heimesf6cd9672008-03-26 13:45:42 +0000217 except ImportError:
218 pass
Christian Heimesf6cd9672008-03-26 13:45:42 +0000219
220 def test_relpath(self):
221 currentdir = os.path.split(os.getcwd())[-1]
222 tester('ntpath.relpath("a")', 'a')
223 tester('ntpath.relpath(os.path.abspath("a"))', 'a')
224 tester('ntpath.relpath("a/b")', 'a\\b')
225 tester('ntpath.relpath("../a/b")', '..\\a\\b')
226 tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
227 tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
228 tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
Mark Hammond5a607a32009-05-06 08:04:54 +0000229 tester('ntpath.relpath("c:/foo/bar/bat", "c:/x/y")', '..\\..\\foo\\bar\\bat')
Christian Heimesf6cd9672008-03-26 13:45:42 +0000230 tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
231 tester('ntpath.relpath("a", "a")', '.')
Mark Hammond5a607a32009-05-06 08:04:54 +0000232 tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
233 tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
234 tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
235 tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
236 tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
237 tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
238 tester('ntpath.relpath("/", "/")', '.')
239 tester('ntpath.relpath("/a", "/a")', '.')
240 tester('ntpath.relpath("/a/b", "/a/b")', '.')
Hirokazu Yamamotob08820a2010-10-18 12:13:18 +0000241 tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
Christian Heimesf6cd9672008-03-26 13:45:42 +0000242
Brian Curtin62857742010-09-06 17:07:27 +0000243 def test_sameopenfile(self):
244 with TemporaryFile() as tf1, TemporaryFile() as tf2:
245 # Make sure the same file is really the same
246 self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno()))
247 # Make sure different files are really different
248 self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno()))
Brian Curtin13a0db52010-09-06 19:46:17 +0000249 # Make sure invalid values don't cause issues on win32
250 if sys.platform == "win32":
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +0000251 with self.assertRaises(OSError):
Brian Curtin13a0db52010-09-06 19:46:17 +0000252 # Invalid file descriptors shouldn't display assert
253 # dialogs (#4804)
254 ntpath.sameopenfile(-1, -1)
Brian Curtin62857742010-09-06 17:07:27 +0000255
Christian Heimesf6cd9672008-03-26 13:45:42 +0000256
Florent Xiclunac9c79782010-03-08 12:24:53 +0000257class NtCommonTest(test_genericpath.CommonTest):
258 pathmodule = ntpath
259 attributes = ['relpath', 'splitunc']
260
261
Christian Heimesf6cd9672008-03-26 13:45:42 +0000262def test_main():
Florent Xiclunac9c79782010-03-08 12:24:53 +0000263 support.run_unittest(TestNtpath, NtCommonTest)
Christian Heimesf6cd9672008-03-26 13:45:42 +0000264
265
266if __name__ == "__main__":
267 unittest.main()