blob: 2c9dab9bb053e552e50a80b1c1eafad0d9ac012d [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
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01004import unittest
5import warnings
Georg Brandl1b37e872010-03-14 10:45:50 +00006from test.support import TestFailed
Florent Xiclunac9c79782010-03-08 12:24:53 +00007from test import support, test_genericpath
Brian Curtin62857742010-09-06 17:07:27 +00008from tempfile import TemporaryFile
Guido van Rossumead9d8d1999-02-03 17:21:21 +00009
Guido van Rossumead9d8d1999-02-03 17:21:21 +000010
11def tester(fn, wantResult):
Eric S. Raymondfc170b12001-02-09 11:51:27 +000012 fn = fn.replace("\\", "\\\\")
Fred Drake004d5e62000-10-23 17:22:08 +000013 gotResult = eval(fn)
14 if wantResult != gotResult:
Christian Heimesf6cd9672008-03-26 13:45:42 +000015 raise TestFailed("%s should return: %s but returned: %s" \
16 %(str(fn), str(wantResult), str(gotResult)))
Tim Peters6578dc92002-12-24 18:31:27 +000017
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +000018 # then with bytes
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 fn = fn.replace(', "', ', b"')
Victor Stinner1ab6c2d2011-11-15 22:27:41 +010025 with warnings.catch_warnings():
26 warnings.simplefilter("ignore", DeprecationWarning)
27 gotResult = eval(fn)
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +000028 if isinstance(wantResult, str):
29 wantResult = wantResult.encode('ascii')
30 elif isinstance(wantResult, tuple):
31 wantResult = tuple(r.encode('ascii') for r in wantResult)
32
33 gotResult = eval(fn)
34 if wantResult != gotResult:
35 raise TestFailed("%s should return: %s but returned: %s" \
36 %(str(fn), str(wantResult), repr(gotResult)))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000037
Mark Hammond5a607a32009-05-06 08:04:54 +000038
Christian Heimesf6cd9672008-03-26 13:45:42 +000039class TestNtpath(unittest.TestCase):
40 def test_splitext(self):
41 tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
42 tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext'))
43 tester('ntpath.splitext(".ext")', ('.ext', ''))
44 tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', ''))
45 tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', ''))
46 tester('ntpath.splitext("")', ('', ''))
47 tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext'))
48 tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext'))
49 tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext'))
50 tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000051
Christian Heimesf6cd9672008-03-26 13:45:42 +000052 def test_splitdrive(self):
53 tester('ntpath.splitdrive("c:\\foo\\bar")',
54 ('c:', '\\foo\\bar'))
55 tester('ntpath.splitdrive("c:/foo/bar")',
56 ('c:', '/foo/bar'))
Mark Hammond5a607a32009-05-06 08:04:54 +000057 tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")',
Christian Heimesf6cd9672008-03-26 13:45:42 +000058 ('\\\\conky\\mountpoint', '\\foo\\bar'))
Mark Hammond5a607a32009-05-06 08:04:54 +000059 tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")',
Christian Heimesf6cd9672008-03-26 13:45:42 +000060 ('//conky/mountpoint', '/foo/bar'))
Mark Hammond5a607a32009-05-06 08:04:54 +000061 tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")',
62 ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
63 tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")',
64 ('', '///conky/mountpoint/foo/bar'))
65 tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")',
66 ('', '\\\\conky\\\\mountpoint\\foo\\bar'))
67 tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")',
68 ('', '//conky//mountpoint/foo/bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000069
Christian Heimesf6cd9672008-03-26 13:45:42 +000070 def test_split(self):
71 tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
72 tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
73 ('\\\\conky\\mountpoint\\foo', 'bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000074
Christian Heimesf6cd9672008-03-26 13:45:42 +000075 tester('ntpath.split("c:\\")', ('c:\\', ''))
76 tester('ntpath.split("\\\\conky\\mountpoint\\")',
Mark Hammond5a607a32009-05-06 08:04:54 +000077 ('\\\\conky\\mountpoint\\', ''))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000078
Christian Heimesf6cd9672008-03-26 13:45:42 +000079 tester('ntpath.split("c:/")', ('c:/', ''))
Mark Hammond5a607a32009-05-06 08:04:54 +000080 tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', ''))
Mark Hammond673c6cf2000-08-14 06:21:26 +000081
Christian Heimesf6cd9672008-03-26 13:45:42 +000082 def test_isabs(self):
83 tester('ntpath.isabs("c:\\")', 1)
84 tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
85 tester('ntpath.isabs("\\foo")', 1)
86 tester('ntpath.isabs("\\foo\\bar")', 1)
Tim Petersd4f7f602001-07-19 19:11:41 +000087
Christian Heimesf6cd9672008-03-26 13:45:42 +000088 def test_commonprefix(self):
89 tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
90 "/home/swen")
91 tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
92 "\\home\\swen\\")
93 tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
94 "/home/swen/spam")
Tim Peters6a3e5f12001-11-05 21:25:02 +000095
Christian Heimesf6cd9672008-03-26 13:45:42 +000096 def test_join(self):
97 tester('ntpath.join("")', '')
98 tester('ntpath.join("", "", "")', '')
99 tester('ntpath.join("a")', 'a')
100 tester('ntpath.join("/a")', '/a')
101 tester('ntpath.join("\\a")', '\\a')
102 tester('ntpath.join("a:")', 'a:')
103 tester('ntpath.join("a:", "b")', 'a:b')
104 tester('ntpath.join("a:", "/b")', 'a:/b')
105 tester('ntpath.join("a:", "\\b")', 'a:\\b')
106 tester('ntpath.join("a", "/b")', '/b')
107 tester('ntpath.join("a", "\\b")', '\\b')
108 tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
109 tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
110 tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
111 tester('ntpath.join("a", "b", "\\c")', '\\c')
112 tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
113 tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
114 tester("ntpath.join('c:', '/a')", 'c:/a')
115 tester("ntpath.join('c:/', '/a')", 'c:/a')
116 tester("ntpath.join('c:/a', '/b')", '/b')
117 tester("ntpath.join('c:', 'd:/')", 'd:/')
118 tester("ntpath.join('c:/', 'd:/')", 'd:/')
119 tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b')
Tim Peters54a14a32001-08-30 22:05:26 +0000120
Christian Heimesf6cd9672008-03-26 13:45:42 +0000121 tester("ntpath.join('')", '')
122 tester("ntpath.join('', '', '', '', '')", '')
123 tester("ntpath.join('a')", 'a')
124 tester("ntpath.join('', 'a')", 'a')
125 tester("ntpath.join('', '', '', '', 'a')", 'a')
126 tester("ntpath.join('a', '')", 'a\\')
127 tester("ntpath.join('a', '', '', '', '')", 'a\\')
128 tester("ntpath.join('a\\', '')", 'a\\')
129 tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
Tim Peters54a14a32001-08-30 22:05:26 +0000130
Mark Hammond5a607a32009-05-06 08:04:54 +0000131 # from comment in ntpath.join
132 tester("ntpath.join('c:', '/a')", 'c:/a')
133 tester("ntpath.join('//computer/share', '/a')", '//computer/share/a')
134 tester("ntpath.join('c:/', '/a')", 'c:/a')
135 tester("ntpath.join('//computer/share/', '/a')", '//computer/share/a')
136 tester("ntpath.join('c:/a', '/b')", '/b')
137 tester("ntpath.join('//computer/share/a', '/b')", '/b')
138 tester("ntpath.join('c:', 'd:/')", 'd:/')
139 tester("ntpath.join('c:', '//computer/share/')", '//computer/share/')
140 tester("ntpath.join('//computer/share', 'd:/')", 'd:/')
141 tester("ntpath.join('//computer/share', '//computer/share/')", '//computer/share/')
142 tester("ntpath.join('c:/', 'd:/')", 'd:/')
143 tester("ntpath.join('c:/', '//computer/share/')", '//computer/share/')
144 tester("ntpath.join('//computer/share/', 'd:/')", 'd:/')
145 tester("ntpath.join('//computer/share/', '//computer/share/')", '//computer/share/')
146
147 tester("ntpath.join('c:', '//computer/share/')", '//computer/share/')
148 tester("ntpath.join('c:/', '//computer/share/')", '//computer/share/')
149 tester("ntpath.join('c:/', '//computer/share/a/b')", '//computer/share/a/b')
150
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 tester("ntpath.join('//computer/share/', 'a', 'b')", '//computer/share/a\\b')
155 tester("ntpath.join('//computer/share', 'a', 'b')", '//computer/share\\a\\b')
156 tester("ntpath.join('//computer/share', 'a/b')", '//computer/share\\a/b')
157
Christian Heimesf6cd9672008-03-26 13:45:42 +0000158 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')
Tim Peters54a14a32001-08-30 22:05:26 +0000165
Christian Heimesf6cd9672008-03-26 13:45:42 +0000166 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')
Thomas Woutersb2137042007-02-01 18:02:27 +0000169
Christian Heimesf6cd9672008-03-26 13:45:42 +0000170 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')
Fred Drake5e997312002-01-15 03:46:43 +0000181
Georg Brandlcfb68212010-07-31 21:40:15 +0000182 tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL')
183 tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z')
184
Christian Heimesf6cd9672008-03-26 13:45:42 +0000185 def test_expandvars(self):
Walter Dörwald155374d2009-05-01 19:58:58 +0000186 with support.EnvironmentVarGuard() as env:
187 env.clear()
188 env["foo"] = "bar"
189 env["{foo"] = "baz1"
190 env["{foo}"] = "baz2"
Christian Heimesf6cd9672008-03-26 13:45:42 +0000191 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")', "barbar")
198 tester('ntpath.expandvars("$foo}bar")', "bar}bar")
199 tester('ntpath.expandvars("${foo")', "${foo")
200 tester('ntpath.expandvars("${{foo}}")', "baz1}")
201 tester('ntpath.expandvars("$foo$foo")', "barbar")
202 tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
203 tester('ntpath.expandvars("%foo% bar")', "bar bar")
204 tester('ntpath.expandvars("%foo%bar")', "barbar")
205 tester('ntpath.expandvars("%foo%%foo%")', "barbar")
206 tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
207 tester('ntpath.expandvars("%?bar%")', "%?bar%")
208 tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
209 tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000210
Christian Heimesf6cd9672008-03-26 13:45:42 +0000211 def test_abspath(self):
212 # ntpath.abspath() can only be used on a system with the "nt" module
213 # (reasonably), so we protect this test with "import nt". This allows
214 # the rest of the tests for the ntpath module to be run to completion
215 # on any platform, since most of the module is intended to be usable
216 # from any platform.
217 try:
218 import nt
Mark Hammond5a607a32009-05-06 08:04:54 +0000219 tester('ntpath.abspath("C:\\")', "C:\\")
Christian Heimesf6cd9672008-03-26 13:45:42 +0000220 except ImportError:
221 pass
Christian Heimesf6cd9672008-03-26 13:45:42 +0000222
223 def test_relpath(self):
224 currentdir = os.path.split(os.getcwd())[-1]
225 tester('ntpath.relpath("a")', 'a')
226 tester('ntpath.relpath(os.path.abspath("a"))', 'a')
227 tester('ntpath.relpath("a/b")', 'a\\b')
228 tester('ntpath.relpath("../a/b")', '..\\a\\b')
229 tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
230 tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
231 tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
Mark Hammond5a607a32009-05-06 08:04:54 +0000232 tester('ntpath.relpath("c:/foo/bar/bat", "c:/x/y")', '..\\..\\foo\\bar\\bat')
Christian Heimesf6cd9672008-03-26 13:45:42 +0000233 tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
234 tester('ntpath.relpath("a", "a")', '.')
Mark Hammond5a607a32009-05-06 08:04:54 +0000235 tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
236 tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
237 tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
238 tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
239 tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
240 tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
241 tester('ntpath.relpath("/", "/")', '.')
242 tester('ntpath.relpath("/a", "/a")', '.')
243 tester('ntpath.relpath("/a/b", "/a/b")', '.')
Hirokazu Yamamotob08820a2010-10-18 12:13:18 +0000244 tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
Christian Heimesf6cd9672008-03-26 13:45:42 +0000245
Brian Curtin62857742010-09-06 17:07:27 +0000246 def test_sameopenfile(self):
247 with TemporaryFile() as tf1, TemporaryFile() as tf2:
248 # Make sure the same file is really the same
249 self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno()))
250 # Make sure different files are really different
251 self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno()))
Brian Curtin13a0db52010-09-06 19:46:17 +0000252 # Make sure invalid values don't cause issues on win32
253 if sys.platform == "win32":
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +0000254 with self.assertRaises(OSError):
Brian Curtin13a0db52010-09-06 19:46:17 +0000255 # Invalid file descriptors shouldn't display assert
256 # dialogs (#4804)
257 ntpath.sameopenfile(-1, -1)
Brian Curtin62857742010-09-06 17:07:27 +0000258
Christian Heimesf6cd9672008-03-26 13:45:42 +0000259
Florent Xiclunac9c79782010-03-08 12:24:53 +0000260class NtCommonTest(test_genericpath.CommonTest):
261 pathmodule = ntpath
262 attributes = ['relpath', 'splitunc']
263
264
Christian Heimesf6cd9672008-03-26 13:45:42 +0000265def test_main():
Florent Xiclunac9c79782010-03-08 12:24:53 +0000266 support.run_unittest(TestNtpath, NtCommonTest)
Christian Heimesf6cd9672008-03-26 13:45:42 +0000267
268
269if __name__ == "__main__":
270 unittest.main()