blob: 4a7a48b19c8be429c8df5e6597626a59be041694 [file] [log] [blame]
Guido van Rossumead9d8d1999-02-03 17:21:21 +00001import ntpath
Mark Hammond673c6cf2000-08-14 06:21:26 +00002import os
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test.support import verbose, TestFailed
4import test.support as support
Christian Heimesf6cd9672008-03-26 13:45:42 +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:
Christian Heimesf6cd9672008-03-26 13:45:42 +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
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +000015 # then with bytes
16 fn = fn.replace("('", "(b'")
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 gotResult = eval(fn)
23 if isinstance(wantResult, str):
24 wantResult = wantResult.encode('ascii')
25 elif isinstance(wantResult, tuple):
26 wantResult = tuple(r.encode('ascii') for r in wantResult)
27
28 gotResult = eval(fn)
29 if wantResult != gotResult:
30 raise TestFailed("%s should return: %s but returned: %s" \
31 %(str(fn), str(wantResult), repr(gotResult)))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000032
Mark Hammond5a607a32009-05-06 08:04:54 +000033
Christian Heimesf6cd9672008-03-26 13:45:42 +000034class TestNtpath(unittest.TestCase):
35 def test_splitext(self):
36 tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
37 tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext'))
38 tester('ntpath.splitext(".ext")', ('.ext', ''))
39 tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', ''))
40 tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', ''))
41 tester('ntpath.splitext("")', ('', ''))
42 tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext'))
43 tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext'))
44 tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext'))
45 tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000046
Christian Heimesf6cd9672008-03-26 13:45:42 +000047 def test_splitdrive(self):
48 tester('ntpath.splitdrive("c:\\foo\\bar")',
49 ('c:', '\\foo\\bar'))
50 tester('ntpath.splitdrive("c:/foo/bar")',
51 ('c:', '/foo/bar'))
Mark Hammond5a607a32009-05-06 08:04:54 +000052 tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")',
Christian Heimesf6cd9672008-03-26 13:45:42 +000053 ('\\\\conky\\mountpoint', '\\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")',
57 ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
58 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'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000064
Christian Heimesf6cd9672008-03-26 13:45:42 +000065 def test_split(self):
66 tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
67 tester('ntpath.split("\\\\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 tester('ntpath.split("c:\\")', ('c:\\', ''))
71 tester('ntpath.split("\\\\conky\\mountpoint\\")',
Mark Hammond5a607a32009-05-06 08:04:54 +000072 ('\\\\conky\\mountpoint\\', ''))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000073
Christian Heimesf6cd9672008-03-26 13:45:42 +000074 tester('ntpath.split("c:/")', ('c:/', ''))
Mark Hammond5a607a32009-05-06 08:04:54 +000075 tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', ''))
Mark Hammond673c6cf2000-08-14 06:21:26 +000076
Christian Heimesf6cd9672008-03-26 13:45:42 +000077 def test_isabs(self):
78 tester('ntpath.isabs("c:\\")', 1)
79 tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
80 tester('ntpath.isabs("\\foo")', 1)
81 tester('ntpath.isabs("\\foo\\bar")', 1)
Tim Petersd4f7f602001-07-19 19:11:41 +000082
Christian Heimesf6cd9672008-03-26 13:45:42 +000083 def test_commonprefix(self):
84 tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
85 "/home/swen")
86 tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
87 "\\home\\swen\\")
88 tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
89 "/home/swen/spam")
Tim Peters6a3e5f12001-11-05 21:25:02 +000090
Christian Heimesf6cd9672008-03-26 13:45:42 +000091 def test_join(self):
92 tester('ntpath.join("")', '')
93 tester('ntpath.join("", "", "")', '')
94 tester('ntpath.join("a")', 'a')
95 tester('ntpath.join("/a")', '/a')
96 tester('ntpath.join("\\a")', '\\a')
97 tester('ntpath.join("a:")', 'a:')
98 tester('ntpath.join("a:", "b")', 'a:b')
99 tester('ntpath.join("a:", "/b")', 'a:/b')
100 tester('ntpath.join("a:", "\\b")', 'a:\\b')
101 tester('ntpath.join("a", "/b")', '/b')
102 tester('ntpath.join("a", "\\b")', '\\b')
103 tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
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")', '\\c')
107 tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
108 tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
109 tester("ntpath.join('c:', '/a')", 'c:/a')
110 tester("ntpath.join('c:/', '/a')", 'c:/a')
111 tester("ntpath.join('c:/a', '/b')", '/b')
112 tester("ntpath.join('c:', 'd:/')", 'd:/')
113 tester("ntpath.join('c:/', 'd:/')", 'd:/')
114 tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b')
Tim Peters54a14a32001-08-30 22:05:26 +0000115
Christian Heimesf6cd9672008-03-26 13:45:42 +0000116 tester("ntpath.join('')", '')
117 tester("ntpath.join('', '', '', '', '')", '')
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\\')
Tim Peters54a14a32001-08-30 22:05:26 +0000125
Mark Hammond5a607a32009-05-06 08:04:54 +0000126 # from comment in ntpath.join
127 tester("ntpath.join('c:', '/a')", 'c:/a')
128 tester("ntpath.join('//computer/share', '/a')", '//computer/share/a')
129 tester("ntpath.join('c:/', '/a')", 'c:/a')
130 tester("ntpath.join('//computer/share/', '/a')", '//computer/share/a')
131 tester("ntpath.join('c:/a', '/b')", '/b')
132 tester("ntpath.join('//computer/share/a', '/b')", '/b')
133 tester("ntpath.join('c:', 'd:/')", 'd:/')
134 tester("ntpath.join('c:', '//computer/share/')", '//computer/share/')
135 tester("ntpath.join('//computer/share', 'd:/')", 'd:/')
136 tester("ntpath.join('//computer/share', '//computer/share/')", '//computer/share/')
137 tester("ntpath.join('c:/', 'd:/')", 'd:/')
138 tester("ntpath.join('c:/', '//computer/share/')", '//computer/share/')
139 tester("ntpath.join('//computer/share/', 'd:/')", 'd:/')
140 tester("ntpath.join('//computer/share/', '//computer/share/')", '//computer/share/')
141
142 tester("ntpath.join('c:', '//computer/share/')", '//computer/share/')
143 tester("ntpath.join('c:/', '//computer/share/')", '//computer/share/')
144 tester("ntpath.join('c:/', '//computer/share/a/b')", '//computer/share/a/b')
145
146 tester("ntpath.join('\\\\computer\\share\\', 'a', 'b')", '\\\\computer\\share\\a\\b')
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
Christian Heimesf6cd9672008-03-26 13:45:42 +0000153 def test_normpath(self):
154 tester("ntpath.normpath('A//////././//.//B')", r'A\B')
155 tester("ntpath.normpath('A/./B')", r'A\B')
156 tester("ntpath.normpath('A/foo/../B')", r'A\B')
157 tester("ntpath.normpath('C:A//B')", r'C:A\B')
158 tester("ntpath.normpath('D:A/./B')", r'D:A\B')
159 tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
Tim Peters54a14a32001-08-30 22:05:26 +0000160
Christian Heimesf6cd9672008-03-26 13:45:42 +0000161 tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
162 tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
163 tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
Thomas Woutersb2137042007-02-01 18:02:27 +0000164
Christian Heimesf6cd9672008-03-26 13:45:42 +0000165 tester("ntpath.normpath('..')", r'..')
166 tester("ntpath.normpath('.')", r'.')
167 tester("ntpath.normpath('')", r'.')
168 tester("ntpath.normpath('/')", '\\')
169 tester("ntpath.normpath('c:/')", 'c:\\')
170 tester("ntpath.normpath('/../.././..')", '\\')
171 tester("ntpath.normpath('c:/../../..')", 'c:\\')
172 tester("ntpath.normpath('../.././..')", r'..\..\..')
173 tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
174 tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
175 tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
Fred Drake5e997312002-01-15 03:46:43 +0000176
Christian Heimesf6cd9672008-03-26 13:45:42 +0000177 def test_expandvars(self):
Walter Dörwald155374d2009-05-01 19:58:58 +0000178 with support.EnvironmentVarGuard() as env:
179 env.clear()
180 env["foo"] = "bar"
181 env["{foo"] = "baz1"
182 env["{foo}"] = "baz2"
Christian Heimesf6cd9672008-03-26 13:45:42 +0000183 tester('ntpath.expandvars("foo")', "foo")
184 tester('ntpath.expandvars("$foo bar")', "bar bar")
185 tester('ntpath.expandvars("${foo}bar")', "barbar")
186 tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
187 tester('ntpath.expandvars("$bar bar")', "$bar bar")
188 tester('ntpath.expandvars("$?bar")', "$?bar")
189 tester('ntpath.expandvars("${foo}bar")', "barbar")
190 tester('ntpath.expandvars("$foo}bar")', "bar}bar")
191 tester('ntpath.expandvars("${foo")', "${foo")
192 tester('ntpath.expandvars("${{foo}}")', "baz1}")
193 tester('ntpath.expandvars("$foo$foo")', "barbar")
194 tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
195 tester('ntpath.expandvars("%foo% bar")', "bar bar")
196 tester('ntpath.expandvars("%foo%bar")', "barbar")
197 tester('ntpath.expandvars("%foo%%foo%")', "barbar")
198 tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
199 tester('ntpath.expandvars("%?bar%")', "%?bar%")
200 tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
201 tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000202
Christian Heimesf6cd9672008-03-26 13:45:42 +0000203 def test_abspath(self):
204 # ntpath.abspath() can only be used on a system with the "nt" module
205 # (reasonably), so we protect this test with "import nt". This allows
206 # the rest of the tests for the ntpath module to be run to completion
207 # on any platform, since most of the module is intended to be usable
208 # from any platform.
209 try:
210 import nt
Mark Hammond5a607a32009-05-06 08:04:54 +0000211 tester('ntpath.abspath("C:\\")', "C:\\")
Christian Heimesf6cd9672008-03-26 13:45:42 +0000212 except ImportError:
213 pass
Christian Heimesf6cd9672008-03-26 13:45:42 +0000214
215 def test_relpath(self):
216 currentdir = os.path.split(os.getcwd())[-1]
217 tester('ntpath.relpath("a")', 'a')
218 tester('ntpath.relpath(os.path.abspath("a"))', 'a')
219 tester('ntpath.relpath("a/b")', 'a\\b')
220 tester('ntpath.relpath("../a/b")', '..\\a\\b')
221 tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
222 tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
223 tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
Mark Hammond5a607a32009-05-06 08:04:54 +0000224 tester('ntpath.relpath("c:/foo/bar/bat", "c:/x/y")', '..\\..\\foo\\bar\\bat')
Christian Heimesf6cd9672008-03-26 13:45:42 +0000225 tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
226 tester('ntpath.relpath("a", "a")', '.')
Mark Hammond5a607a32009-05-06 08:04:54 +0000227 tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
228 tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
229 tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
230 tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
231 tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
232 tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
233 tester('ntpath.relpath("/", "/")', '.')
234 tester('ntpath.relpath("/a", "/a")', '.')
235 tester('ntpath.relpath("/a/b", "/a/b")', '.')
Christian Heimesf6cd9672008-03-26 13:45:42 +0000236
237
238def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000239 support.run_unittest(TestNtpath)
Christian Heimesf6cd9672008-03-26 13:45:42 +0000240
241
242if __name__ == "__main__":
243 unittest.main()