blob: 9f39abaec33e698374d61dec66af199d397efad5 [file] [log] [blame]
Guido van Rossumead9d8d1999-02-03 17:21:21 +00001import ntpath
Mark Hammond673c6cf2000-08-14 06:21:26 +00002import os
Georg Brandl1b37e872010-03-14 10:45:50 +00003from test.support import TestFailed
Florent Xiclunac9c79782010-03-08 12:24:53 +00004from test import support, test_genericpath
Brian Curtin62857742010-09-06 17:07:27 +00005from tempfile import TemporaryFile
Christian Heimesf6cd9672008-03-26 13:45:42 +00006import unittest
Guido van Rossumead9d8d1999-02-03 17:21:21 +00007
Guido van Rossumead9d8d1999-02-03 17:21:21 +00008
9def tester(fn, wantResult):
Eric S. Raymondfc170b12001-02-09 11:51:27 +000010 fn = fn.replace("\\", "\\\\")
Fred Drake004d5e62000-10-23 17:22:08 +000011 gotResult = eval(fn)
12 if wantResult != gotResult:
Christian Heimesf6cd9672008-03-26 13:45:42 +000013 raise TestFailed("%s should return: %s but returned: %s" \
14 %(str(fn), str(wantResult), str(gotResult)))
Tim Peters6578dc92002-12-24 18:31:27 +000015
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +000016 # then with bytes
17 fn = fn.replace("('", "(b'")
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 gotResult = eval(fn)
24 if isinstance(wantResult, str):
25 wantResult = wantResult.encode('ascii')
26 elif isinstance(wantResult, tuple):
27 wantResult = tuple(r.encode('ascii') for r in wantResult)
28
29 gotResult = eval(fn)
30 if wantResult != gotResult:
31 raise TestFailed("%s should return: %s but returned: %s" \
32 %(str(fn), str(wantResult), repr(gotResult)))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000033
Mark Hammond5a607a32009-05-06 08:04:54 +000034
Christian Heimesf6cd9672008-03-26 13:45:42 +000035class TestNtpath(unittest.TestCase):
36 def test_splitext(self):
37 tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
38 tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext'))
39 tester('ntpath.splitext(".ext")', ('.ext', ''))
40 tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', ''))
41 tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', ''))
42 tester('ntpath.splitext("")', ('', ''))
43 tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext'))
44 tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext'))
45 tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext'))
46 tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000047
Christian Heimesf6cd9672008-03-26 13:45:42 +000048 def test_splitdrive(self):
49 tester('ntpath.splitdrive("c:\\foo\\bar")',
50 ('c:', '\\foo\\bar'))
51 tester('ntpath.splitdrive("c:/foo/bar")',
52 ('c:', '/foo/bar'))
Mark Hammond5a607a32009-05-06 08:04:54 +000053 tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")',
Christian Heimesf6cd9672008-03-26 13:45:42 +000054 ('\\\\conky\\mountpoint', '\\foo\\bar'))
Mark Hammond5a607a32009-05-06 08:04:54 +000055 tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")',
Christian Heimesf6cd9672008-03-26 13:45:42 +000056 ('//conky/mountpoint', '/foo/bar'))
Mark Hammond5a607a32009-05-06 08:04:54 +000057 tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")',
58 ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
59 tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")',
60 ('', '///conky/mountpoint/foo/bar'))
61 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'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000065
Christian Heimesf6cd9672008-03-26 13:45:42 +000066 def test_split(self):
67 tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
68 tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
69 ('\\\\conky\\mountpoint\\foo', 'bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000070
Christian Heimesf6cd9672008-03-26 13:45:42 +000071 tester('ntpath.split("c:\\")', ('c:\\', ''))
72 tester('ntpath.split("\\\\conky\\mountpoint\\")',
Mark Hammond5a607a32009-05-06 08:04:54 +000073 ('\\\\conky\\mountpoint\\', ''))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000074
Christian Heimesf6cd9672008-03-26 13:45:42 +000075 tester('ntpath.split("c:/")', ('c:/', ''))
Mark Hammond5a607a32009-05-06 08:04:54 +000076 tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', ''))
Mark Hammond673c6cf2000-08-14 06:21:26 +000077
Christian Heimesf6cd9672008-03-26 13:45:42 +000078 def test_isabs(self):
79 tester('ntpath.isabs("c:\\")', 1)
80 tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
81 tester('ntpath.isabs("\\foo")', 1)
82 tester('ntpath.isabs("\\foo\\bar")', 1)
Tim Petersd4f7f602001-07-19 19:11:41 +000083
Christian Heimesf6cd9672008-03-26 13:45:42 +000084 def test_commonprefix(self):
85 tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
86 "/home/swen")
87 tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
88 "\\home\\swen\\")
89 tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
90 "/home/swen/spam")
Tim Peters6a3e5f12001-11-05 21:25:02 +000091
Christian Heimesf6cd9672008-03-26 13:45:42 +000092 def test_join(self):
93 tester('ntpath.join("")', '')
94 tester('ntpath.join("", "", "")', '')
95 tester('ntpath.join("a")', 'a')
96 tester('ntpath.join("/a")', '/a')
97 tester('ntpath.join("\\a")', '\\a')
98 tester('ntpath.join("a:")', 'a:')
99 tester('ntpath.join("a:", "b")', 'a:b')
100 tester('ntpath.join("a:", "/b")', 'a:/b')
101 tester('ntpath.join("a:", "\\b")', 'a:\\b')
102 tester('ntpath.join("a", "/b")', '/b')
103 tester('ntpath.join("a", "\\b")', '\\b')
104 tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
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")', '\\c')
108 tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
109 tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
110 tester("ntpath.join('c:', '/a')", 'c:/a')
111 tester("ntpath.join('c:/', '/a')", 'c:/a')
112 tester("ntpath.join('c:/a', '/b')", '/b')
113 tester("ntpath.join('c:', 'd:/')", 'd:/')
114 tester("ntpath.join('c:/', 'd:/')", 'd:/')
115 tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b')
Tim Peters54a14a32001-08-30 22:05:26 +0000116
Christian Heimesf6cd9672008-03-26 13:45:42 +0000117 tester("ntpath.join('')", '')
118 tester("ntpath.join('', '', '', '', '')", '')
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 tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
Tim Peters54a14a32001-08-30 22:05:26 +0000126
Mark Hammond5a607a32009-05-06 08:04:54 +0000127 # from comment in ntpath.join
128 tester("ntpath.join('c:', '/a')", 'c:/a')
129 tester("ntpath.join('//computer/share', '/a')", '//computer/share/a')
130 tester("ntpath.join('c:/', '/a')", 'c:/a')
131 tester("ntpath.join('//computer/share/', '/a')", '//computer/share/a')
132 tester("ntpath.join('c:/a', '/b')", '/b')
133 tester("ntpath.join('//computer/share/a', '/b')", '/b')
134 tester("ntpath.join('c:', 'd:/')", 'd:/')
135 tester("ntpath.join('c:', '//computer/share/')", '//computer/share/')
136 tester("ntpath.join('//computer/share', 'd:/')", 'd:/')
137 tester("ntpath.join('//computer/share', '//computer/share/')", '//computer/share/')
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
143 tester("ntpath.join('c:', '//computer/share/')", '//computer/share/')
144 tester("ntpath.join('c:/', '//computer/share/')", '//computer/share/')
145 tester("ntpath.join('c:/', '//computer/share/a/b')", '//computer/share/a/b')
146
147 tester("ntpath.join('\\\\computer\\share\\', 'a', 'b')", '\\\\computer\\share\\a\\b')
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
Christian Heimesf6cd9672008-03-26 13:45:42 +0000154 def test_normpath(self):
155 tester("ntpath.normpath('A//////././//.//B')", r'A\B')
156 tester("ntpath.normpath('A/./B')", r'A\B')
157 tester("ntpath.normpath('A/foo/../B')", r'A\B')
158 tester("ntpath.normpath('C:A//B')", r'C:A\B')
159 tester("ntpath.normpath('D:A/./B')", r'D:A\B')
160 tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
Tim Peters54a14a32001-08-30 22:05:26 +0000161
Christian Heimesf6cd9672008-03-26 13:45:42 +0000162 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')
Thomas Woutersb2137042007-02-01 18:02:27 +0000165
Christian Heimesf6cd9672008-03-26 13:45:42 +0000166 tester("ntpath.normpath('..')", r'..')
167 tester("ntpath.normpath('.')", r'.')
168 tester("ntpath.normpath('')", r'.')
169 tester("ntpath.normpath('/')", '\\')
170 tester("ntpath.normpath('c:/')", 'c:\\')
171 tester("ntpath.normpath('/../.././..')", '\\')
172 tester("ntpath.normpath('c:/../../..')", 'c:\\')
173 tester("ntpath.normpath('../.././..')", r'..\..\..')
174 tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
175 tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
176 tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
Fred Drake5e997312002-01-15 03:46:43 +0000177
Georg Brandlcfb68212010-07-31 21:40:15 +0000178 tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL')
179 tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z')
180
Christian Heimesf6cd9672008-03-26 13:45:42 +0000181 def test_expandvars(self):
Walter Dörwald155374d2009-05-01 19:58:58 +0000182 with support.EnvironmentVarGuard() as env:
183 env.clear()
184 env["foo"] = "bar"
185 env["{foo"] = "baz1"
186 env["{foo}"] = "baz2"
Christian Heimesf6cd9672008-03-26 13:45:42 +0000187 tester('ntpath.expandvars("foo")', "foo")
188 tester('ntpath.expandvars("$foo bar")', "bar bar")
189 tester('ntpath.expandvars("${foo}bar")', "barbar")
190 tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
191 tester('ntpath.expandvars("$bar bar")', "$bar bar")
192 tester('ntpath.expandvars("$?bar")', "$?bar")
193 tester('ntpath.expandvars("${foo}bar")', "barbar")
194 tester('ntpath.expandvars("$foo}bar")', "bar}bar")
195 tester('ntpath.expandvars("${foo")', "${foo")
196 tester('ntpath.expandvars("${{foo}}")', "baz1}")
197 tester('ntpath.expandvars("$foo$foo")', "barbar")
198 tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
199 tester('ntpath.expandvars("%foo% bar")', "bar bar")
200 tester('ntpath.expandvars("%foo%bar")', "barbar")
201 tester('ntpath.expandvars("%foo%%foo%")', "barbar")
202 tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
203 tester('ntpath.expandvars("%?bar%")', "%?bar%")
204 tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
205 tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000206
Christian Heimesf6cd9672008-03-26 13:45:42 +0000207 def test_abspath(self):
208 # ntpath.abspath() can only be used on a system with the "nt" module
209 # (reasonably), so we protect this test with "import nt". This allows
210 # the rest of the tests for the ntpath module to be run to completion
211 # on any platform, since most of the module is intended to be usable
212 # from any platform.
213 try:
214 import nt
Mark Hammond5a607a32009-05-06 08:04:54 +0000215 tester('ntpath.abspath("C:\\")', "C:\\")
Christian Heimesf6cd9672008-03-26 13:45:42 +0000216 except ImportError:
217 pass
Christian Heimesf6cd9672008-03-26 13:45:42 +0000218
219 def test_relpath(self):
220 currentdir = os.path.split(os.getcwd())[-1]
221 tester('ntpath.relpath("a")', 'a')
222 tester('ntpath.relpath(os.path.abspath("a"))', 'a')
223 tester('ntpath.relpath("a/b")', 'a\\b')
224 tester('ntpath.relpath("../a/b")', '..\\a\\b')
225 tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
226 tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
227 tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
Mark Hammond5a607a32009-05-06 08:04:54 +0000228 tester('ntpath.relpath("c:/foo/bar/bat", "c:/x/y")', '..\\..\\foo\\bar\\bat')
Christian Heimesf6cd9672008-03-26 13:45:42 +0000229 tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
230 tester('ntpath.relpath("a", "a")', '.')
Mark Hammond5a607a32009-05-06 08:04:54 +0000231 tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
232 tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
233 tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
234 tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
235 tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
236 tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
237 tester('ntpath.relpath("/", "/")', '.')
238 tester('ntpath.relpath("/a", "/a")', '.')
239 tester('ntpath.relpath("/a/b", "/a/b")', '.')
Christian Heimesf6cd9672008-03-26 13:45:42 +0000240
Brian Curtin62857742010-09-06 17:07:27 +0000241 def test_sameopenfile(self):
242 with TemporaryFile() as tf1, TemporaryFile() as tf2:
243 # Make sure the same file is really the same
244 self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno()))
245 # Make sure different files are really different
246 self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno()))
247 # Make sure invalid values don't cause issues
248 with self.assertRaises(ValueError):
249 # Invalid file descriptors shouldn't display assert
250 # dialogs (#4804)
251 ntpath.sameopenfile(-1, -1)
252
Christian Heimesf6cd9672008-03-26 13:45:42 +0000253
Florent Xiclunac9c79782010-03-08 12:24:53 +0000254class NtCommonTest(test_genericpath.CommonTest):
255 pathmodule = ntpath
256 attributes = ['relpath', 'splitunc']
257
258
Christian Heimesf6cd9672008-03-26 13:45:42 +0000259def test_main():
Florent Xiclunac9c79782010-03-08 12:24:53 +0000260 support.run_unittest(TestNtpath, NtCommonTest)
Christian Heimesf6cd9672008-03-26 13:45:42 +0000261
262
263if __name__ == "__main__":
264 unittest.main()