Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(53921)

Delta Between Two Patch Sets: Lib/test/test_ntpath.py

Issue 10395: new os.path function to extract common prefix based on path components
Left Patch Set: Created 6 years, 10 months ago
Right Patch Set: Created 5 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/posixpath.py ('k') | Lib/test/test_posixpath.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import ntpath 1 import ntpath
2 import os 2 import os
3 import sys 3 import sys
4 import unittest 4 import unittest
5 import warnings 5 import warnings
6 from test.support import TestFailed 6 from test.support import TestFailed
7 from test import support, test_genericpath 7 from test import support, test_genericpath
8 from tempfile import TemporaryFile 8 from tempfile import TemporaryFile
9 9
10 10
11 def tester(fn, wantResult): 11 def tester(fn, wantResult):
12 fn = fn.replace("\\", "\\\\") 12 fn = fn.replace("\\", "\\\\")
13 gotResult = eval(fn) 13 gotResult = eval(fn)
14 if wantResult != gotResult: 14 if wantResult != gotResult:
15 raise TestFailed("%s should return: %s but returned: %s" \ 15 raise TestFailed("%s should return: %s but returned: %s" \
16 %(str(fn), str(wantResult), str(gotResult))) 16 %(str(fn), str(wantResult), str(gotResult)))
17 17
18 # then with bytes 18 # then with bytes
19 fn = fn.replace("('", "(b'") 19 fn = fn.replace("('", "(b'")
20 fn = fn.replace('("', '(b"') 20 fn = fn.replace('("', '(b"')
21 fn = fn.replace("['", "[b'") 21 fn = fn.replace("['", "[b'")
22 fn = fn.replace('["', '[b"') 22 fn = fn.replace('["', '[b"')
23 fn = fn.replace(", '", ", b'") 23 fn = fn.replace(", '", ", b'")
24 fn = fn.replace(', "', ', b"') 24 fn = fn.replace(', "', ', b"')
25 fn = os.fsencode(fn).decode('latin1')
26 fn = fn.encode('ascii', 'backslashreplace').decode('ascii')
25 with warnings.catch_warnings(): 27 with warnings.catch_warnings():
26 warnings.simplefilter("ignore", DeprecationWarning) 28 warnings.simplefilter("ignore", DeprecationWarning)
27 gotResult = eval(fn) 29 gotResult = eval(fn)
28 if isinstance(wantResult, str): 30 if isinstance(wantResult, str):
29 wantResult = wantResult.encode('ascii') 31 wantResult = os.fsencode(wantResult)
30 elif isinstance(wantResult, tuple): 32 elif isinstance(wantResult, tuple):
31 wantResult = tuple(r.encode('ascii') for r in wantResult) 33 wantResult = tuple(os.fsencode(r) for r in wantResult)
32 34
33 gotResult = eval(fn) 35 gotResult = eval(fn)
34 if wantResult != gotResult: 36 if wantResult != gotResult:
35 raise TestFailed("%s should return: %s but returned: %s" \ 37 raise TestFailed("%s should return: %s but returned: %s" \
36 %(str(fn), str(wantResult), repr(gotResult))) 38 %(str(fn), str(wantResult), repr(gotResult)))
37 39
38 40
39 class TestNtpath(unittest.TestCase): 41 class TestNtpath(unittest.TestCase):
40 def test_splitext(self): 42 def test_splitext(self):
41 tester('ntpath.splitext("foo.ext")', ('foo', '.ext')) 43 tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
(...skipping 17 matching lines...) Expand all
59 tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")', 61 tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")',
60 ('//conky/mountpoint', '/foo/bar')) 62 ('//conky/mountpoint', '/foo/bar'))
61 tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")', 63 tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")',
62 ('', '\\\\\\conky\\mountpoint\\foo\\bar')) 64 ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
63 tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")', 65 tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")',
64 ('', '///conky/mountpoint/foo/bar')) 66 ('', '///conky/mountpoint/foo/bar'))
65 tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")', 67 tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")',
66 ('', '\\\\conky\\\\mountpoint\\foo\\bar')) 68 ('', '\\\\conky\\\\mountpoint\\foo\\bar'))
67 tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")', 69 tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")',
68 ('', '//conky//mountpoint/foo/bar')) 70 ('', '//conky//mountpoint/foo/bar'))
71 # Issue #19911: UNC part containing U+0130
72 self.assertEqual(ntpath.splitdrive('//conky/MOUNTPOİNT/foo/bar'),
73 ('//conky/MOUNTPOİNT', '/foo/bar'))
74
75 def test_splitunc(self):
76 with self.assertWarns(DeprecationWarning):
77 ntpath.splitunc('')
78 with support.check_warnings(('', DeprecationWarning)):
79 tester('ntpath.splitunc("c:\\foo\\bar")',
80 ('', 'c:\\foo\\bar'))
81 tester('ntpath.splitunc("c:/foo/bar")',
82 ('', 'c:/foo/bar'))
83 tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")',
84 ('\\\\conky\\mountpoint', '\\foo\\bar'))
85 tester('ntpath.splitunc("//conky/mountpoint/foo/bar")',
86 ('//conky/mountpoint', '/foo/bar'))
87 tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")',
88 ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
89 tester('ntpath.splitunc("///conky/mountpoint/foo/bar")',
90 ('', '///conky/mountpoint/foo/bar'))
91 tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")',
92 ('', '\\\\conky\\\\mountpoint\\foo\\bar'))
93 tester('ntpath.splitunc("//conky//mountpoint/foo/bar")',
94 ('', '//conky//mountpoint/foo/bar'))
95 self.assertEqual(ntpath.splitunc('//conky/MOUNTPOİNT/foo/bar'),
96 ('//conky/MOUNTPOİNT', '/foo/bar'))
69 97
70 def test_split(self): 98 def test_split(self):
71 tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar')) 99 tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
72 tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")', 100 tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
73 ('\\\\conky\\mountpoint\\foo', 'bar')) 101 ('\\\\conky\\mountpoint\\foo', 'bar'))
74 102
75 tester('ntpath.split("c:\\")', ('c:\\', '')) 103 tester('ntpath.split("c:\\")', ('c:\\', ''))
76 tester('ntpath.split("\\\\conky\\mountpoint\\")', 104 tester('ntpath.split("\\\\conky\\mountpoint\\")',
77 ('\\\\conky\\mountpoint\\', '')) 105 ('\\\\conky\\mountpoint\\', ''))
78 106
(...skipping 14 matching lines...) Expand all
93 tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])', 121 tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
94 "/home/swen/spam") 122 "/home/swen/spam")
95 123
96 def test_join(self): 124 def test_join(self):
97 tester('ntpath.join("")', '') 125 tester('ntpath.join("")', '')
98 tester('ntpath.join("", "", "")', '') 126 tester('ntpath.join("", "", "")', '')
99 tester('ntpath.join("a")', 'a') 127 tester('ntpath.join("a")', 'a')
100 tester('ntpath.join("/a")', '/a') 128 tester('ntpath.join("/a")', '/a')
101 tester('ntpath.join("\\a")', '\\a') 129 tester('ntpath.join("\\a")', '\\a')
102 tester('ntpath.join("a:")', 'a:') 130 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') 131 tester('ntpath.join("a:", "\\b")', 'a:\\b')
106 tester('ntpath.join("a", "/b")', '/b')
107 tester('ntpath.join("a", "\\b")', '\\b') 132 tester('ntpath.join("a", "\\b")', '\\b')
108 tester('ntpath.join("a", "b", "c")', 'a\\b\\c') 133 tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
109 tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c') 134 tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
110 tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c') 135 tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
111 tester('ntpath.join("a", "b", "\\c")', '\\c') 136 tester('ntpath.join("a", "b", "\\c")', '\\c')
112 tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep') 137 tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
113 tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b') 138 tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
114 tester("ntpath.join('c:', '/a')", 'c:/a') 139
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')
120
121 tester("ntpath.join('')", '')
122 tester("ntpath.join('', '', '', '', '')", '')
123 tester("ntpath.join('a')", 'a')
124 tester("ntpath.join('', 'a')", 'a') 140 tester("ntpath.join('', 'a')", 'a')
125 tester("ntpath.join('', '', '', '', 'a')", 'a') 141 tester("ntpath.join('', '', '', '', 'a')", 'a')
126 tester("ntpath.join('a', '')", 'a\\') 142 tester("ntpath.join('a', '')", 'a\\')
127 tester("ntpath.join('a', '', '', '', '')", 'a\\') 143 tester("ntpath.join('a', '', '', '', '')", 'a\\')
128 tester("ntpath.join('a\\', '')", 'a\\') 144 tester("ntpath.join('a\\', '')", 'a\\')
129 tester("ntpath.join('a\\', '', '', '', '')", 'a\\') 145 tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
130 146 tester("ntpath.join('a/', '')", 'a/')
131 # from comment in ntpath.join 147
132 tester("ntpath.join('c:', '/a')", 'c:/a') 148 tester("ntpath.join('a/b', 'x/y')", 'a/b\\x/y')
133 tester("ntpath.join('//computer/share', '/a')", '//computer/share/a') 149 tester("ntpath.join('/a/b', 'x/y')", '/a/b\\x/y')
134 tester("ntpath.join('c:/', '/a')", 'c:/a') 150 tester("ntpath.join('/a/b/', 'x/y')", '/a/b/x/y')
135 tester("ntpath.join('//computer/share/', '/a')", '//computer/share/a') 151 tester("ntpath.join('c:', 'x/y')", 'c:x/y')
136 tester("ntpath.join('c:/a', '/b')", '/b') 152 tester("ntpath.join('c:a/b', 'x/y')", 'c:a/b\\x/y')
137 tester("ntpath.join('//computer/share/a', '/b')", '/b') 153 tester("ntpath.join('c:a/b/', 'x/y')", 'c:a/b/x/y')
138 tester("ntpath.join('c:', 'd:/')", 'd:/') 154 tester("ntpath.join('c:/', 'x/y')", 'c:/x/y')
139 tester("ntpath.join('c:', '//computer/share/')", '//computer/share/') 155 tester("ntpath.join('c:/a/b', 'x/y')", 'c:/a/b\\x/y')
140 tester("ntpath.join('//computer/share', 'd:/')", 'd:/') 156 tester("ntpath.join('c:/a/b/', 'x/y')", 'c:/a/b/x/y')
141 tester("ntpath.join('//computer/share', '//computer/share/')", '//comput er/share/') 157 tester("ntpath.join('//computer/share', 'x/y')", '//computer/share\\x/y' )
142 tester("ntpath.join('c:/', 'd:/')", 'd:/') 158 tester("ntpath.join('//computer/share/', 'x/y')", '//computer/share/x/y' )
143 tester("ntpath.join('c:/', '//computer/share/')", '//computer/share/') 159 tester("ntpath.join('//computer/share/a/b', 'x/y')", '//computer/share/a /b\\x/y')
144 tester("ntpath.join('//computer/share/', 'd:/')", 'd:/') 160
145 tester("ntpath.join('//computer/share/', '//computer/share/')", '//compu ter/share/') 161 tester("ntpath.join('a/b', '/x/y')", '/x/y')
146 162 tester("ntpath.join('/a/b', '/x/y')", '/x/y')
147 tester("ntpath.join('c:', '//computer/share/')", '//computer/share/') 163 tester("ntpath.join('c:', '/x/y')", 'c:/x/y')
148 tester("ntpath.join('c:/', '//computer/share/')", '//computer/share/') 164 tester("ntpath.join('c:a/b', '/x/y')", 'c:/x/y')
149 tester("ntpath.join('c:/', '//computer/share/a/b')", '//computer/share/a /b') 165 tester("ntpath.join('c:/', '/x/y')", 'c:/x/y')
166 tester("ntpath.join('c:/a/b', '/x/y')", 'c:/x/y')
167 tester("ntpath.join('//computer/share', '/x/y')", '//computer/share/x/y' )
168 tester("ntpath.join('//computer/share/', '/x/y')", '//computer/share/x/y ')
169 tester("ntpath.join('//computer/share/a', '/x/y')", '//computer/share/x/ y')
170
171 tester("ntpath.join('c:', 'C:x/y')", 'C:x/y')
172 tester("ntpath.join('c:a/b', 'C:x/y')", 'C:a/b\\x/y')
173 tester("ntpath.join('c:/', 'C:x/y')", 'C:/x/y')
174 tester("ntpath.join('c:/a/b', 'C:x/y')", 'C:/a/b\\x/y')
175
176 for x in ('', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b',
177 '//computer/share', '//computer/share/', '//computer/share/a/b '):
178 for y in ('d:', 'd:x/y', 'd:/', 'd:/x/y',
179 '//machine/common', '//machine/common/', '//machine/common /x/y'):
180 tester("ntpath.join(%r, %r)" % (x, y), y)
150 181
151 tester("ntpath.join('\\\\computer\\share\\', 'a', 'b')", '\\\\computer\\ share\\a\\b') 182 tester("ntpath.join('\\\\computer\\share\\', 'a', 'b')", '\\\\computer\\ share\\a\\b')
152 tester("ntpath.join('\\\\computer\\share', 'a', 'b')", '\\\\computer\\sh are\\a\\b') 183 tester("ntpath.join('\\\\computer\\share', 'a', 'b')", '\\\\computer\\sh are\\a\\b')
153 tester("ntpath.join('\\\\computer\\share', 'a\\b')", '\\\\computer\\shar e\\a\\b') 184 tester("ntpath.join('\\\\computer\\share', 'a\\b')", '\\\\computer\\shar e\\a\\b')
154 tester("ntpath.join('//computer/share/', 'a', 'b')", '//computer/share/a \\b') 185 tester("ntpath.join('//computer/share/', 'a', 'b')", '//computer/share/a \\b')
155 tester("ntpath.join('//computer/share', 'a', 'b')", '//computer/share\\a \\b') 186 tester("ntpath.join('//computer/share', 'a', 'b')", '//computer/share\\a \\b')
156 tester("ntpath.join('//computer/share', 'a/b')", '//computer/share\\a/b' ) 187 tester("ntpath.join('//computer/share', 'a/b')", '//computer/share\\a/b' )
157 188
158 def test_normpath(self): 189 def test_normpath(self):
159 tester("ntpath.normpath('A//////././//.//B')", r'A\B') 190 tester("ntpath.normpath('A//////././//.//B')", r'A\B')
(...skipping 27 matching lines...) Expand all
187 env.clear() 218 env.clear()
188 env["foo"] = "bar" 219 env["foo"] = "bar"
189 env["{foo"] = "baz1" 220 env["{foo"] = "baz1"
190 env["{foo}"] = "baz2" 221 env["{foo}"] = "baz2"
191 tester('ntpath.expandvars("foo")', "foo") 222 tester('ntpath.expandvars("foo")', "foo")
192 tester('ntpath.expandvars("$foo bar")', "bar bar") 223 tester('ntpath.expandvars("$foo bar")', "bar bar")
193 tester('ntpath.expandvars("${foo}bar")', "barbar") 224 tester('ntpath.expandvars("${foo}bar")', "barbar")
194 tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar") 225 tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
195 tester('ntpath.expandvars("$bar bar")', "$bar bar") 226 tester('ntpath.expandvars("$bar bar")', "$bar bar")
196 tester('ntpath.expandvars("$?bar")', "$?bar") 227 tester('ntpath.expandvars("$?bar")', "$?bar")
197 tester('ntpath.expandvars("${foo}bar")', "barbar")
198 tester('ntpath.expandvars("$foo}bar")', "bar}bar") 228 tester('ntpath.expandvars("$foo}bar")', "bar}bar")
199 tester('ntpath.expandvars("${foo")', "${foo") 229 tester('ntpath.expandvars("${foo")', "${foo")
200 tester('ntpath.expandvars("${{foo}}")', "baz1}") 230 tester('ntpath.expandvars("${{foo}}")', "baz1}")
201 tester('ntpath.expandvars("$foo$foo")', "barbar") 231 tester('ntpath.expandvars("$foo$foo")', "barbar")
202 tester('ntpath.expandvars("$bar$bar")', "$bar$bar") 232 tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
203 tester('ntpath.expandvars("%foo% bar")', "bar bar") 233 tester('ntpath.expandvars("%foo% bar")', "bar bar")
204 tester('ntpath.expandvars("%foo%bar")', "barbar") 234 tester('ntpath.expandvars("%foo%bar")', "barbar")
205 tester('ntpath.expandvars("%foo%%foo%")', "barbar") 235 tester('ntpath.expandvars("%foo%%foo%")', "barbar")
206 tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar") 236 tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
207 tester('ntpath.expandvars("%?bar%")', "%?bar%") 237 tester('ntpath.expandvars("%?bar%")', "%?bar%")
208 tester('ntpath.expandvars("%foo%%bar")', "bar%bar") 238 tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
209 tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar") 239 tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
210 240
241 @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII')
242 def test_expandvars_nonascii(self):
243 def check(value, expected):
244 tester('ntpath.expandvars(%r)' % value, expected)
245 with support.EnvironmentVarGuard() as env:
246 env.clear()
247 nonascii = support.FS_NONASCII
248 env['spam'] = nonascii
249 env[nonascii] = 'ham' + nonascii
250 check('$spam bar', '%s bar' % nonascii)
251 check('$%s bar' % nonascii, '$%s bar' % nonascii)
252 check('${spam}bar', '%sbar' % nonascii)
253 check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
254 check('$spam}bar', '%s}bar' % nonascii)
255 check('$%s}bar' % nonascii, '$%s}bar' % nonascii)
256 check('%spam% bar', '%s bar' % nonascii)
257 check('%{}% bar'.format(nonascii), 'ham%s bar' % nonascii)
258 check('%spam%bar', '%sbar' % nonascii)
259 check('%{}%bar'.format(nonascii), 'ham%sbar' % nonascii)
260
261 def test_expanduser(self):
262 tester('ntpath.expanduser("test")', 'test')
263
264 with support.EnvironmentVarGuard() as env:
265 env.clear()
266 tester('ntpath.expanduser("~test")', '~test')
267
268 env['HOMEPATH'] = 'eric\\idle'
269 env['HOMEDRIVE'] = 'C:\\'
270 tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
271 tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
272
273 del env['HOMEDRIVE']
274 tester('ntpath.expanduser("~test")', 'eric\\test')
275 tester('ntpath.expanduser("~")', 'eric\\idle')
276
277 env.clear()
278 env['USERPROFILE'] = 'C:\\eric\\idle'
279 tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
280 tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
281
282 env.clear()
283 env['HOME'] = 'C:\\idle\\eric'
284 tester('ntpath.expanduser("~test")', 'C:\\idle\\test')
285 tester('ntpath.expanduser("~")', 'C:\\idle\\eric')
286
287 tester('ntpath.expanduser("~test\\foo\\bar")',
288 'C:\\idle\\test\\foo\\bar')
289 tester('ntpath.expanduser("~test/foo/bar")',
290 'C:\\idle\\test/foo/bar')
291 tester('ntpath.expanduser("~\\foo\\bar")',
292 'C:\\idle\\eric\\foo\\bar')
293 tester('ntpath.expanduser("~/foo/bar")',
294 'C:\\idle\\eric/foo/bar')
295
211 def test_abspath(self): 296 def test_abspath(self):
212 # ntpath.abspath() can only be used on a system with the "nt" module 297 # 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 298 # (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 299 # 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 300 # on any platform, since most of the module is intended to be usable
216 # from any platform. 301 # from any platform.
217 try: 302 try:
218 import nt 303 import nt
219 tester('ntpath.abspath("C:\\")', "C:\\") 304 tester('ntpath.abspath("C:\\")', "C:\\")
220 except ImportError: 305 except ImportError:
221 pass 306 self.skipTest('nt module not available')
222 307
223 def test_relpath(self): 308 def test_relpath(self):
224 currentdir = os.path.split(os.getcwd())[-1] 309 currentdir = os.path.split(os.getcwd())[-1]
225 tester('ntpath.relpath("a")', 'a') 310 tester('ntpath.relpath("a")', 'a')
226 tester('ntpath.relpath(os.path.abspath("a"))', 'a') 311 tester('ntpath.relpath(os.path.abspath("a"))', 'a')
227 tester('ntpath.relpath("a/b")', 'a\\b') 312 tester('ntpath.relpath("a/b")', 'a\\b')
228 tester('ntpath.relpath("../a/b")', '..\\a\\b') 313 tester('ntpath.relpath("../a/b")', '..\\a\\b')
229 tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a') 314 tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
230 tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b') 315 tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
231 tester('ntpath.relpath("a", "b/c")', '..\\..\\a') 316 tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
232 tester('ntpath.relpath("c:/foo/bar/bat", "c:/x/y")', '..\\..\\foo\\bar\\ bat') 317 tester('ntpath.relpath("c:/foo/bar/bat", "c:/x/y")', '..\\..\\foo\\bar\\ bat')
233 tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c") ', '..\\..\\a') 318 tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c") ', '..\\..\\a')
234 tester('ntpath.relpath("a", "a")', '.') 319 tester('ntpath.relpath("a", "a")', '.')
235 tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar \\bat') 320 tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar \\bat')
236 tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat') 321 tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
237 tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat') 322 tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
238 tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..') 323 tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
239 tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat') 324 tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
240 tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x') 325 tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
241 tester('ntpath.relpath("/", "/")', '.') 326 tester('ntpath.relpath("/", "/")', '.')
242 tester('ntpath.relpath("/a", "/a")', '.') 327 tester('ntpath.relpath("/a", "/a")', '.')
243 tester('ntpath.relpath("/a/b", "/a/b")', '.') 328 tester('ntpath.relpath("/a/b", "/a/b")', '.')
244 tester('ntpath.relpath("c:/foo", "C:/FOO")', '.') 329 tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
245 330
246 def test_commonpath(self): 331 def test_commonpath(self):
247 332 def check(paths, expected):
248 tester('ntpath.commonpath([])', None) 333 tester(('ntpath.commonpath(%r)' % paths).replace('\\\\', '\\'),
249 tester('ntpath.commonpath(["C:\\Program Files", "Documents/"])', None) 334 expected)
250 tester('ntpath.commonpath(["C:\\Program Files"])', 'C:\\Program Files') 335 def check_error(exc, paths):
251 tester('ntpath.commonpath(["C:\\Program Files", "C:\\Program Files"])', 'C:\\Program Files') 336 self.assertRaises(exc, ntpath.commonpath, paths)
252 tester('ntpath.commonpath(["C:\\Program Files\\", "C:\\Program Files"])' , 'C:\\Program Files') 337 self.assertRaises(exc, ntpath.commonpath,
253 tester('ntpath.commonpath(["C:\\Program Files\\", "C:\\Program Files\\"] )', 'C:\\Program Files') 338 [os.fsencode(p) for p in paths])
254 tester('ntpath.commonpath(["C:\\", "C:\\bin"])', 'C:\\') 339
255 tester('ntpath.commonpath(["C:\\Program Files", "C:\\bin"])', 'C:\\') 340 self.assertRaises(ValueError, ntpath.commonpath, [])
256 tester('ntpath.commonpath(["C:\\Program Files", "C:\\Program Files\\Bar" ])', 'C:\\Program Files') 341 check_error(ValueError, ['C:\\Program Files', 'Program Files'])
257 tester('ntpath.commonpath(["C:\\Program Files\\Foo", "C:\\Program Files\ \Bar"])', 'C:\\Program Files') 342 check_error(ValueError, ['C:\\Program Files', 'C:Program Files'])
258 tester('ntpath.commonpath([b"C:\\Program Files\\Foo", b"C:\\Program File s\\Bar"])', b'C:\\Program Files') 343 check_error(ValueError, ['\\Program Files', 'Program Files'])
259 tester('ntpath.commonpath(["C:\\Program Files", "C:\\Projects"])', 'C:\\ ') 344 check_error(ValueError, ['Program Files', 'C:\\Program Files'])
260 tester('ntpath.commonpath(["C:\\Program Files\\", "C:\\Projects"])', 'C: \\') 345 check(['C:\\Program Files'], 'C:\\Program Files')
261 346 check(['C:\\Program Files', 'C:\\Program Files'], 'C:\\Program Files')
262 tester('ntpath.commonpath(["C:\\Program Files\\Foo", "C:/Program Files/B ar"])', 'C:\\Program Files') 347 check(['C:\\Program Files\\', 'C:\\Program Files'],
263 tester('ntpath.commonpath(["C:\\Program Files\\Foo", "c:/program files/b ar"])', 'C:\\Program Files') 348 'C:\\Program Files')
264 tester('ntpath.commonpath(["c:/program files/bar", "C:\\Program Files\\F oo"])', 'c:\\program files') 349 check(['C:\\Program Files\\', 'C:\\Program Files\\'],
265 350 'C:\\Program Files')
266 tester('ntpath.commonpath(["C:\\Program Files\\Foo", "D:/Program Files/B ar"])', None) 351 check(['C:\\\\Program Files', 'C:\\Program Files\\\\'],
267 352 'C:\\Program Files')
268 # tester function won't work with the following tests 353 check(['C:\\.\\Program Files', 'C:\\Program Files\\.'],
269 self.assertEquals(ntpath.commonpath(['spam']), 'spam') 354 'C:\\Program Files')
storchaka 2012/11/13 10:24:46 self.assertEqual
270 self.assertEqual(ntpath.commonpath(['spam']), 'spam') 355 check(['C:\\', 'C:\\bin'], 'C:\\')
271 self.assertEqual(ntpath.commonpath(['spam', 'spam']), 'spam') 356 check(['C:\\Program Files', 'C:\\bin'], 'C:\\')
272 self.assertEqual(ntpath.commonpath(['spam', 'alot']), '') 357 check(['C:\\Program Files', 'C:\\Program Files\\Bar'],
273 self.assertEqual(ntpath.commonpath(['and\\jam', 'and\\spam']), 'and') 358 'C:\\Program Files')
274 self.assertEqual( 359 check(['C:\\Program Files\\Foo', 'C:\\Program Files\\Bar'],
275 ntpath.commonpath(['and\\jam', 'and\\spam', 'alot']), '') 360 'C:\\Program Files')
276 self.assertEqual( 361 check(['C:\\Program Files', 'C:\\Projects'], 'C:\\')
277 ntpath.commonpath(['and\\jam', 'and\\spam', 'and']), 'and') 362 check(['C:\\Program Files\\', 'C:\\Projects'], 'C:\\')
278 363
279 self.assertEqual(ntpath.commonpath(['']), '') 364 check(['C:\\Program Files\\Foo', 'C:/Program Files/Bar'],
280 self.assertEqual(ntpath.commonpath(['', 'spam\\alot']), '') 365 'C:\\Program Files')
281 self.assertIsNone(ntpath.commonpath(['', '\\spam\\alot'])) 366 check(['C:\\Program Files\\Foo', 'c:/program files/bar'],
367 'C:\\Program Files')
368 check(['c:/program files/bar', 'C:\\Program Files\\Foo'],
369 'c:\\program files')
370
371 check_error(ValueError, ['C:\\Program Files', 'D:\\Program Files'])
372
373 check(['spam'], 'spam')
374 check(['spam', 'spam'], 'spam')
375 check(['spam', 'alot'], '')
376 check(['and\\jam', 'and\\spam'], 'and')
377 check(['and\\\\jam', 'and\\spam\\\\'], 'and')
378 check(['and\\.\\jam', '.\\and\\spam'], 'and')
379 check(['and\\jam', 'and\\spam', 'alot'], '')
380 check(['and\\jam', 'and\\spam', 'and'], 'and')
381 check(['C:and\\jam', 'C:and\\spam'], 'C:and')
382
383 check([''], '')
384 check(['', 'spam\\alot'], '')
385 check_error(ValueError, ['', '\\spam\\alot'])
282 386
283 self.assertRaises(TypeError, ntpath.commonpath, 387 self.assertRaises(TypeError, ntpath.commonpath,
284 [b'C:\\Program Files', 'C:\\Program Files\\Foo']) 388 [b'C:\\Program Files', 'C:\\Program Files\\Foo'])
389 self.assertRaises(TypeError, ntpath.commonpath,
390 [b'C:\\Program Files', 'Program Files\\Foo'])
391 self.assertRaises(TypeError, ntpath.commonpath,
392 [b'Program Files', 'C:\\Program Files\\Foo'])
393 self.assertRaises(TypeError, ntpath.commonpath,
394 ['C:\\Program Files', b'C:\\Program Files\\Foo'])
395 self.assertRaises(TypeError, ntpath.commonpath,
396 ['C:\\Program Files', b'Program Files\\Foo'])
397 self.assertRaises(TypeError, ntpath.commonpath,
398 ['Program Files', b'C:\\Program Files\\Foo'])
285 399
286 def test_sameopenfile(self): 400 def test_sameopenfile(self):
287 with TemporaryFile() as tf1, TemporaryFile() as tf2: 401 with TemporaryFile() as tf1, TemporaryFile() as tf2:
288 # Make sure the same file is really the same 402 # Make sure the same file is really the same
289 self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno())) 403 self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno()))
290 # Make sure different files are really different 404 # Make sure different files are really different
291 self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno())) 405 self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno()))
292 # Make sure invalid values don't cause issues on win32 406 # Make sure invalid values don't cause issues on win32
293 if sys.platform == "win32": 407 if sys.platform == "win32":
294 with self.assertRaises(OSError): 408 with self.assertRaises(OSError):
295 # Invalid file descriptors shouldn't display assert 409 # Invalid file descriptors shouldn't display assert
296 # dialogs (#4804) 410 # dialogs (#4804)
297 ntpath.sameopenfile(-1, -1) 411 ntpath.sameopenfile(-1, -1)
298 412
299 413 def test_ismount(self):
300 class NtCommonTest(test_genericpath.CommonTest): 414 self.assertTrue(ntpath.ismount("c:\\"))
415 self.assertTrue(ntpath.ismount("C:\\"))
416 self.assertTrue(ntpath.ismount("c:/"))
417 self.assertTrue(ntpath.ismount("C:/"))
418 self.assertTrue(ntpath.ismount("\\\\.\\c:\\"))
419 self.assertTrue(ntpath.ismount("\\\\.\\C:\\"))
420
421 self.assertTrue(ntpath.ismount(b"c:\\"))
422 self.assertTrue(ntpath.ismount(b"C:\\"))
423 self.assertTrue(ntpath.ismount(b"c:/"))
424 self.assertTrue(ntpath.ismount(b"C:/"))
425 self.assertTrue(ntpath.ismount(b"\\\\.\\c:\\"))
426 self.assertTrue(ntpath.ismount(b"\\\\.\\C:\\"))
427
428 with support.temp_dir() as d:
429 self.assertFalse(ntpath.ismount(d))
430
431 if sys.platform == "win32":
432 #
433 # Make sure the current folder isn't the root folder
434 # (or any other volume root). The drive-relative
435 # locations below cannot then refer to mount points
436 #
437 drive, path = ntpath.splitdrive(sys.executable)
438 with support.change_cwd(os.path.dirname(sys.executable)):
439 self.assertFalse(ntpath.ismount(drive.lower()))
440 self.assertFalse(ntpath.ismount(drive.upper()))
441
442 self.assertTrue(ntpath.ismount("\\\\localhost\\c$"))
443 self.assertTrue(ntpath.ismount("\\\\localhost\\c$\\"))
444
445 self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$"))
446 self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$\\"))
447
448 class NtCommonTest(test_genericpath.CommonTest, unittest.TestCase):
301 pathmodule = ntpath 449 pathmodule = ntpath
302 attributes = ['relpath', 'splitunc'] 450 attributes = ['relpath', 'splitunc']
303 451
304 452
305 def test_main():
306 support.run_unittest(TestNtpath, NtCommonTest)
307
308
309 if __name__ == "__main__": 453 if __name__ == "__main__":
310 unittest.main() 454 unittest.main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+