1 | n/a | import ntpath |
---|
2 | n/a | import os |
---|
3 | n/a | import sys |
---|
4 | n/a | import unittest |
---|
5 | n/a | import warnings |
---|
6 | n/a | from test.support import TestFailed |
---|
7 | n/a | from test import support, test_genericpath |
---|
8 | n/a | from tempfile import TemporaryFile |
---|
9 | n/a | |
---|
10 | n/a | |
---|
11 | n/a | def tester(fn, wantResult): |
---|
12 | n/a | fn = fn.replace("\\", "\\\\") |
---|
13 | n/a | gotResult = eval(fn) |
---|
14 | n/a | if wantResult != gotResult: |
---|
15 | n/a | raise TestFailed("%s should return: %s but returned: %s" \ |
---|
16 | n/a | %(str(fn), str(wantResult), str(gotResult))) |
---|
17 | n/a | |
---|
18 | n/a | # then with bytes |
---|
19 | n/a | fn = fn.replace("('", "(b'") |
---|
20 | n/a | fn = fn.replace('("', '(b"') |
---|
21 | n/a | fn = fn.replace("['", "[b'") |
---|
22 | n/a | fn = fn.replace('["', '[b"') |
---|
23 | n/a | fn = fn.replace(", '", ", b'") |
---|
24 | n/a | fn = fn.replace(', "', ', b"') |
---|
25 | n/a | fn = os.fsencode(fn).decode('latin1') |
---|
26 | n/a | fn = fn.encode('ascii', 'backslashreplace').decode('ascii') |
---|
27 | n/a | with warnings.catch_warnings(): |
---|
28 | n/a | warnings.simplefilter("ignore", DeprecationWarning) |
---|
29 | n/a | gotResult = eval(fn) |
---|
30 | n/a | if isinstance(wantResult, str): |
---|
31 | n/a | wantResult = os.fsencode(wantResult) |
---|
32 | n/a | elif isinstance(wantResult, tuple): |
---|
33 | n/a | wantResult = tuple(os.fsencode(r) for r in wantResult) |
---|
34 | n/a | |
---|
35 | n/a | gotResult = eval(fn) |
---|
36 | n/a | if wantResult != gotResult: |
---|
37 | n/a | raise TestFailed("%s should return: %s but returned: %s" \ |
---|
38 | n/a | %(str(fn), str(wantResult), repr(gotResult))) |
---|
39 | n/a | |
---|
40 | n/a | |
---|
41 | n/a | class TestNtpath(unittest.TestCase): |
---|
42 | n/a | def test_splitext(self): |
---|
43 | n/a | tester('ntpath.splitext("foo.ext")', ('foo', '.ext')) |
---|
44 | n/a | tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext')) |
---|
45 | n/a | tester('ntpath.splitext(".ext")', ('.ext', '')) |
---|
46 | n/a | tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', '')) |
---|
47 | n/a | tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', '')) |
---|
48 | n/a | tester('ntpath.splitext("")', ('', '')) |
---|
49 | n/a | tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext')) |
---|
50 | n/a | tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext')) |
---|
51 | n/a | tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext')) |
---|
52 | n/a | tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d')) |
---|
53 | n/a | |
---|
54 | n/a | def test_splitdrive(self): |
---|
55 | n/a | tester('ntpath.splitdrive("c:\\foo\\bar")', |
---|
56 | n/a | ('c:', '\\foo\\bar')) |
---|
57 | n/a | tester('ntpath.splitdrive("c:/foo/bar")', |
---|
58 | n/a | ('c:', '/foo/bar')) |
---|
59 | n/a | tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")', |
---|
60 | n/a | ('\\\\conky\\mountpoint', '\\foo\\bar')) |
---|
61 | n/a | tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")', |
---|
62 | n/a | ('//conky/mountpoint', '/foo/bar')) |
---|
63 | n/a | tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")', |
---|
64 | n/a | ('', '\\\\\\conky\\mountpoint\\foo\\bar')) |
---|
65 | n/a | tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")', |
---|
66 | n/a | ('', '///conky/mountpoint/foo/bar')) |
---|
67 | n/a | tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")', |
---|
68 | n/a | ('', '\\\\conky\\\\mountpoint\\foo\\bar')) |
---|
69 | n/a | tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")', |
---|
70 | n/a | ('', '//conky//mountpoint/foo/bar')) |
---|
71 | n/a | # Issue #19911: UNC part containing U+0130 |
---|
72 | n/a | self.assertEqual(ntpath.splitdrive('//conky/MOUNTPOÄ°NT/foo/bar'), |
---|
73 | n/a | ('//conky/MOUNTPOÄ°NT', '/foo/bar')) |
---|
74 | n/a | |
---|
75 | n/a | def test_split(self): |
---|
76 | n/a | tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar')) |
---|
77 | n/a | tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")', |
---|
78 | n/a | ('\\\\conky\\mountpoint\\foo', 'bar')) |
---|
79 | n/a | |
---|
80 | n/a | tester('ntpath.split("c:\\")', ('c:\\', '')) |
---|
81 | n/a | tester('ntpath.split("\\\\conky\\mountpoint\\")', |
---|
82 | n/a | ('\\\\conky\\mountpoint\\', '')) |
---|
83 | n/a | |
---|
84 | n/a | tester('ntpath.split("c:/")', ('c:/', '')) |
---|
85 | n/a | tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', '')) |
---|
86 | n/a | |
---|
87 | n/a | def test_isabs(self): |
---|
88 | n/a | tester('ntpath.isabs("c:\\")', 1) |
---|
89 | n/a | tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1) |
---|
90 | n/a | tester('ntpath.isabs("\\foo")', 1) |
---|
91 | n/a | tester('ntpath.isabs("\\foo\\bar")', 1) |
---|
92 | n/a | |
---|
93 | n/a | def test_commonprefix(self): |
---|
94 | n/a | tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])', |
---|
95 | n/a | "/home/swen") |
---|
96 | n/a | tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])', |
---|
97 | n/a | "\\home\\swen\\") |
---|
98 | n/a | tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])', |
---|
99 | n/a | "/home/swen/spam") |
---|
100 | n/a | |
---|
101 | n/a | def test_join(self): |
---|
102 | n/a | tester('ntpath.join("")', '') |
---|
103 | n/a | tester('ntpath.join("", "", "")', '') |
---|
104 | n/a | tester('ntpath.join("a")', 'a') |
---|
105 | n/a | tester('ntpath.join("/a")', '/a') |
---|
106 | n/a | tester('ntpath.join("\\a")', '\\a') |
---|
107 | n/a | tester('ntpath.join("a:")', 'a:') |
---|
108 | n/a | tester('ntpath.join("a:", "\\b")', 'a:\\b') |
---|
109 | n/a | tester('ntpath.join("a", "\\b")', '\\b') |
---|
110 | n/a | tester('ntpath.join("a", "b", "c")', 'a\\b\\c') |
---|
111 | n/a | tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c') |
---|
112 | n/a | tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c') |
---|
113 | n/a | tester('ntpath.join("a", "b", "\\c")', '\\c') |
---|
114 | n/a | tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep') |
---|
115 | n/a | tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b') |
---|
116 | n/a | |
---|
117 | n/a | tester("ntpath.join('', 'a')", 'a') |
---|
118 | n/a | tester("ntpath.join('', '', '', '', 'a')", 'a') |
---|
119 | n/a | tester("ntpath.join('a', '')", 'a\\') |
---|
120 | n/a | tester("ntpath.join('a', '', '', '', '')", 'a\\') |
---|
121 | n/a | tester("ntpath.join('a\\', '')", 'a\\') |
---|
122 | n/a | tester("ntpath.join('a\\', '', '', '', '')", 'a\\') |
---|
123 | n/a | tester("ntpath.join('a/', '')", 'a/') |
---|
124 | n/a | |
---|
125 | n/a | tester("ntpath.join('a/b', 'x/y')", 'a/b\\x/y') |
---|
126 | n/a | tester("ntpath.join('/a/b', 'x/y')", '/a/b\\x/y') |
---|
127 | n/a | tester("ntpath.join('/a/b/', 'x/y')", '/a/b/x/y') |
---|
128 | n/a | tester("ntpath.join('c:', 'x/y')", 'c:x/y') |
---|
129 | n/a | tester("ntpath.join('c:a/b', 'x/y')", 'c:a/b\\x/y') |
---|
130 | n/a | tester("ntpath.join('c:a/b/', 'x/y')", 'c:a/b/x/y') |
---|
131 | n/a | tester("ntpath.join('c:/', 'x/y')", 'c:/x/y') |
---|
132 | n/a | tester("ntpath.join('c:/a/b', 'x/y')", 'c:/a/b\\x/y') |
---|
133 | n/a | tester("ntpath.join('c:/a/b/', 'x/y')", 'c:/a/b/x/y') |
---|
134 | n/a | tester("ntpath.join('//computer/share', 'x/y')", '//computer/share\\x/y') |
---|
135 | n/a | tester("ntpath.join('//computer/share/', 'x/y')", '//computer/share/x/y') |
---|
136 | n/a | tester("ntpath.join('//computer/share/a/b', 'x/y')", '//computer/share/a/b\\x/y') |
---|
137 | n/a | |
---|
138 | n/a | tester("ntpath.join('a/b', '/x/y')", '/x/y') |
---|
139 | n/a | tester("ntpath.join('/a/b', '/x/y')", '/x/y') |
---|
140 | n/a | tester("ntpath.join('c:', '/x/y')", 'c:/x/y') |
---|
141 | n/a | tester("ntpath.join('c:a/b', '/x/y')", 'c:/x/y') |
---|
142 | n/a | tester("ntpath.join('c:/', '/x/y')", 'c:/x/y') |
---|
143 | n/a | tester("ntpath.join('c:/a/b', '/x/y')", 'c:/x/y') |
---|
144 | n/a | tester("ntpath.join('//computer/share', '/x/y')", '//computer/share/x/y') |
---|
145 | n/a | tester("ntpath.join('//computer/share/', '/x/y')", '//computer/share/x/y') |
---|
146 | n/a | tester("ntpath.join('//computer/share/a', '/x/y')", '//computer/share/x/y') |
---|
147 | n/a | |
---|
148 | n/a | tester("ntpath.join('c:', 'C:x/y')", 'C:x/y') |
---|
149 | n/a | tester("ntpath.join('c:a/b', 'C:x/y')", 'C:a/b\\x/y') |
---|
150 | n/a | tester("ntpath.join('c:/', 'C:x/y')", 'C:/x/y') |
---|
151 | n/a | tester("ntpath.join('c:/a/b', 'C:x/y')", 'C:/a/b\\x/y') |
---|
152 | n/a | |
---|
153 | n/a | for x in ('', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b', |
---|
154 | n/a | '//computer/share', '//computer/share/', '//computer/share/a/b'): |
---|
155 | n/a | for y in ('d:', 'd:x/y', 'd:/', 'd:/x/y', |
---|
156 | n/a | '//machine/common', '//machine/common/', '//machine/common/x/y'): |
---|
157 | n/a | tester("ntpath.join(%r, %r)" % (x, y), y) |
---|
158 | n/a | |
---|
159 | n/a | tester("ntpath.join('\\\\computer\\share\\', 'a', 'b')", '\\\\computer\\share\\a\\b') |
---|
160 | n/a | tester("ntpath.join('\\\\computer\\share', 'a', 'b')", '\\\\computer\\share\\a\\b') |
---|
161 | n/a | tester("ntpath.join('\\\\computer\\share', 'a\\b')", '\\\\computer\\share\\a\\b') |
---|
162 | n/a | tester("ntpath.join('//computer/share/', 'a', 'b')", '//computer/share/a\\b') |
---|
163 | n/a | tester("ntpath.join('//computer/share', 'a', 'b')", '//computer/share\\a\\b') |
---|
164 | n/a | tester("ntpath.join('//computer/share', 'a/b')", '//computer/share\\a/b') |
---|
165 | n/a | |
---|
166 | n/a | def test_normpath(self): |
---|
167 | n/a | tester("ntpath.normpath('A//////././//.//B')", r'A\B') |
---|
168 | n/a | tester("ntpath.normpath('A/./B')", r'A\B') |
---|
169 | n/a | tester("ntpath.normpath('A/foo/../B')", r'A\B') |
---|
170 | n/a | tester("ntpath.normpath('C:A//B')", r'C:A\B') |
---|
171 | n/a | tester("ntpath.normpath('D:A/./B')", r'D:A\B') |
---|
172 | n/a | tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B') |
---|
173 | n/a | |
---|
174 | n/a | tester("ntpath.normpath('C:///A//B')", r'C:\A\B') |
---|
175 | n/a | tester("ntpath.normpath('D:///A/./B')", r'D:\A\B') |
---|
176 | n/a | tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B') |
---|
177 | n/a | |
---|
178 | n/a | tester("ntpath.normpath('..')", r'..') |
---|
179 | n/a | tester("ntpath.normpath('.')", r'.') |
---|
180 | n/a | tester("ntpath.normpath('')", r'.') |
---|
181 | n/a | tester("ntpath.normpath('/')", '\\') |
---|
182 | n/a | tester("ntpath.normpath('c:/')", 'c:\\') |
---|
183 | n/a | tester("ntpath.normpath('/../.././..')", '\\') |
---|
184 | n/a | tester("ntpath.normpath('c:/../../..')", 'c:\\') |
---|
185 | n/a | tester("ntpath.normpath('../.././..')", r'..\..\..') |
---|
186 | n/a | tester("ntpath.normpath('K:../.././..')", r'K:..\..\..') |
---|
187 | n/a | tester("ntpath.normpath('C:////a/b')", r'C:\a\b') |
---|
188 | n/a | tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b') |
---|
189 | n/a | |
---|
190 | n/a | tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL') |
---|
191 | n/a | tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z') |
---|
192 | n/a | |
---|
193 | n/a | def test_expandvars(self): |
---|
194 | n/a | with support.EnvironmentVarGuard() as env: |
---|
195 | n/a | env.clear() |
---|
196 | n/a | env["foo"] = "bar" |
---|
197 | n/a | env["{foo"] = "baz1" |
---|
198 | n/a | env["{foo}"] = "baz2" |
---|
199 | n/a | tester('ntpath.expandvars("foo")', "foo") |
---|
200 | n/a | tester('ntpath.expandvars("$foo bar")', "bar bar") |
---|
201 | n/a | tester('ntpath.expandvars("${foo}bar")', "barbar") |
---|
202 | n/a | tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar") |
---|
203 | n/a | tester('ntpath.expandvars("$bar bar")', "$bar bar") |
---|
204 | n/a | tester('ntpath.expandvars("$?bar")', "$?bar") |
---|
205 | n/a | tester('ntpath.expandvars("$foo}bar")', "bar}bar") |
---|
206 | n/a | tester('ntpath.expandvars("${foo")', "${foo") |
---|
207 | n/a | tester('ntpath.expandvars("${{foo}}")', "baz1}") |
---|
208 | n/a | tester('ntpath.expandvars("$foo$foo")', "barbar") |
---|
209 | n/a | tester('ntpath.expandvars("$bar$bar")', "$bar$bar") |
---|
210 | n/a | tester('ntpath.expandvars("%foo% bar")', "bar bar") |
---|
211 | n/a | tester('ntpath.expandvars("%foo%bar")', "barbar") |
---|
212 | n/a | tester('ntpath.expandvars("%foo%%foo%")', "barbar") |
---|
213 | n/a | tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar") |
---|
214 | n/a | tester('ntpath.expandvars("%?bar%")', "%?bar%") |
---|
215 | n/a | tester('ntpath.expandvars("%foo%%bar")', "bar%bar") |
---|
216 | n/a | tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar") |
---|
217 | n/a | tester('ntpath.expandvars("bar\'%foo%")', "bar\'%foo%") |
---|
218 | n/a | |
---|
219 | n/a | @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII') |
---|
220 | n/a | def test_expandvars_nonascii(self): |
---|
221 | n/a | def check(value, expected): |
---|
222 | n/a | tester('ntpath.expandvars(%r)' % value, expected) |
---|
223 | n/a | with support.EnvironmentVarGuard() as env: |
---|
224 | n/a | env.clear() |
---|
225 | n/a | nonascii = support.FS_NONASCII |
---|
226 | n/a | env['spam'] = nonascii |
---|
227 | n/a | env[nonascii] = 'ham' + nonascii |
---|
228 | n/a | check('$spam bar', '%s bar' % nonascii) |
---|
229 | n/a | check('$%s bar' % nonascii, '$%s bar' % nonascii) |
---|
230 | n/a | check('${spam}bar', '%sbar' % nonascii) |
---|
231 | n/a | check('${%s}bar' % nonascii, 'ham%sbar' % nonascii) |
---|
232 | n/a | check('$spam}bar', '%s}bar' % nonascii) |
---|
233 | n/a | check('$%s}bar' % nonascii, '$%s}bar' % nonascii) |
---|
234 | n/a | check('%spam% bar', '%s bar' % nonascii) |
---|
235 | n/a | check('%{}% bar'.format(nonascii), 'ham%s bar' % nonascii) |
---|
236 | n/a | check('%spam%bar', '%sbar' % nonascii) |
---|
237 | n/a | check('%{}%bar'.format(nonascii), 'ham%sbar' % nonascii) |
---|
238 | n/a | |
---|
239 | n/a | def test_expanduser(self): |
---|
240 | n/a | tester('ntpath.expanduser("test")', 'test') |
---|
241 | n/a | |
---|
242 | n/a | with support.EnvironmentVarGuard() as env: |
---|
243 | n/a | env.clear() |
---|
244 | n/a | tester('ntpath.expanduser("~test")', '~test') |
---|
245 | n/a | |
---|
246 | n/a | env['HOMEPATH'] = 'eric\\idle' |
---|
247 | n/a | env['HOMEDRIVE'] = 'C:\\' |
---|
248 | n/a | tester('ntpath.expanduser("~test")', 'C:\\eric\\test') |
---|
249 | n/a | tester('ntpath.expanduser("~")', 'C:\\eric\\idle') |
---|
250 | n/a | |
---|
251 | n/a | del env['HOMEDRIVE'] |
---|
252 | n/a | tester('ntpath.expanduser("~test")', 'eric\\test') |
---|
253 | n/a | tester('ntpath.expanduser("~")', 'eric\\idle') |
---|
254 | n/a | |
---|
255 | n/a | env.clear() |
---|
256 | n/a | env['USERPROFILE'] = 'C:\\eric\\idle' |
---|
257 | n/a | tester('ntpath.expanduser("~test")', 'C:\\eric\\test') |
---|
258 | n/a | tester('ntpath.expanduser("~")', 'C:\\eric\\idle') |
---|
259 | n/a | |
---|
260 | n/a | env.clear() |
---|
261 | n/a | env['HOME'] = 'C:\\idle\\eric' |
---|
262 | n/a | tester('ntpath.expanduser("~test")', 'C:\\idle\\test') |
---|
263 | n/a | tester('ntpath.expanduser("~")', 'C:\\idle\\eric') |
---|
264 | n/a | |
---|
265 | n/a | tester('ntpath.expanduser("~test\\foo\\bar")', |
---|
266 | n/a | 'C:\\idle\\test\\foo\\bar') |
---|
267 | n/a | tester('ntpath.expanduser("~test/foo/bar")', |
---|
268 | n/a | 'C:\\idle\\test/foo/bar') |
---|
269 | n/a | tester('ntpath.expanduser("~\\foo\\bar")', |
---|
270 | n/a | 'C:\\idle\\eric\\foo\\bar') |
---|
271 | n/a | tester('ntpath.expanduser("~/foo/bar")', |
---|
272 | n/a | 'C:\\idle\\eric/foo/bar') |
---|
273 | n/a | |
---|
274 | n/a | def test_abspath(self): |
---|
275 | n/a | # ntpath.abspath() can only be used on a system with the "nt" module |
---|
276 | n/a | # (reasonably), so we protect this test with "import nt". This allows |
---|
277 | n/a | # the rest of the tests for the ntpath module to be run to completion |
---|
278 | n/a | # on any platform, since most of the module is intended to be usable |
---|
279 | n/a | # from any platform. |
---|
280 | n/a | try: |
---|
281 | n/a | import nt |
---|
282 | n/a | tester('ntpath.abspath("C:\\")', "C:\\") |
---|
283 | n/a | except ImportError: |
---|
284 | n/a | self.skipTest('nt module not available') |
---|
285 | n/a | |
---|
286 | n/a | def test_relpath(self): |
---|
287 | n/a | tester('ntpath.relpath("a")', 'a') |
---|
288 | n/a | tester('ntpath.relpath(os.path.abspath("a"))', 'a') |
---|
289 | n/a | tester('ntpath.relpath("a/b")', 'a\\b') |
---|
290 | n/a | tester('ntpath.relpath("../a/b")', '..\\a\\b') |
---|
291 | n/a | with support.temp_cwd(support.TESTFN) as cwd_dir: |
---|
292 | n/a | currentdir = os.path.basename(cwd_dir) |
---|
293 | n/a | tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a') |
---|
294 | n/a | tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b') |
---|
295 | n/a | tester('ntpath.relpath("a", "b/c")', '..\\..\\a') |
---|
296 | n/a | tester('ntpath.relpath("c:/foo/bar/bat", "c:/x/y")', '..\\..\\foo\\bar\\bat') |
---|
297 | n/a | tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a') |
---|
298 | n/a | tester('ntpath.relpath("a", "a")', '.') |
---|
299 | n/a | tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat') |
---|
300 | n/a | tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat') |
---|
301 | n/a | tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat') |
---|
302 | n/a | tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..') |
---|
303 | n/a | tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat') |
---|
304 | n/a | tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x') |
---|
305 | n/a | tester('ntpath.relpath("/", "/")', '.') |
---|
306 | n/a | tester('ntpath.relpath("/a", "/a")', '.') |
---|
307 | n/a | tester('ntpath.relpath("/a/b", "/a/b")', '.') |
---|
308 | n/a | tester('ntpath.relpath("c:/foo", "C:/FOO")', '.') |
---|
309 | n/a | |
---|
310 | n/a | def test_commonpath(self): |
---|
311 | n/a | def check(paths, expected): |
---|
312 | n/a | tester(('ntpath.commonpath(%r)' % paths).replace('\\\\', '\\'), |
---|
313 | n/a | expected) |
---|
314 | n/a | def check_error(exc, paths): |
---|
315 | n/a | self.assertRaises(exc, ntpath.commonpath, paths) |
---|
316 | n/a | self.assertRaises(exc, ntpath.commonpath, |
---|
317 | n/a | [os.fsencode(p) for p in paths]) |
---|
318 | n/a | |
---|
319 | n/a | self.assertRaises(ValueError, ntpath.commonpath, []) |
---|
320 | n/a | check_error(ValueError, ['C:\\Program Files', 'Program Files']) |
---|
321 | n/a | check_error(ValueError, ['C:\\Program Files', 'C:Program Files']) |
---|
322 | n/a | check_error(ValueError, ['\\Program Files', 'Program Files']) |
---|
323 | n/a | check_error(ValueError, ['Program Files', 'C:\\Program Files']) |
---|
324 | n/a | check(['C:\\Program Files'], 'C:\\Program Files') |
---|
325 | n/a | check(['C:\\Program Files', 'C:\\Program Files'], 'C:\\Program Files') |
---|
326 | n/a | check(['C:\\Program Files\\', 'C:\\Program Files'], |
---|
327 | n/a | 'C:\\Program Files') |
---|
328 | n/a | check(['C:\\Program Files\\', 'C:\\Program Files\\'], |
---|
329 | n/a | 'C:\\Program Files') |
---|
330 | n/a | check(['C:\\\\Program Files', 'C:\\Program Files\\\\'], |
---|
331 | n/a | 'C:\\Program Files') |
---|
332 | n/a | check(['C:\\.\\Program Files', 'C:\\Program Files\\.'], |
---|
333 | n/a | 'C:\\Program Files') |
---|
334 | n/a | check(['C:\\', 'C:\\bin'], 'C:\\') |
---|
335 | n/a | check(['C:\\Program Files', 'C:\\bin'], 'C:\\') |
---|
336 | n/a | check(['C:\\Program Files', 'C:\\Program Files\\Bar'], |
---|
337 | n/a | 'C:\\Program Files') |
---|
338 | n/a | check(['C:\\Program Files\\Foo', 'C:\\Program Files\\Bar'], |
---|
339 | n/a | 'C:\\Program Files') |
---|
340 | n/a | check(['C:\\Program Files', 'C:\\Projects'], 'C:\\') |
---|
341 | n/a | check(['C:\\Program Files\\', 'C:\\Projects'], 'C:\\') |
---|
342 | n/a | |
---|
343 | n/a | check(['C:\\Program Files\\Foo', 'C:/Program Files/Bar'], |
---|
344 | n/a | 'C:\\Program Files') |
---|
345 | n/a | check(['C:\\Program Files\\Foo', 'c:/program files/bar'], |
---|
346 | n/a | 'C:\\Program Files') |
---|
347 | n/a | check(['c:/program files/bar', 'C:\\Program Files\\Foo'], |
---|
348 | n/a | 'c:\\program files') |
---|
349 | n/a | |
---|
350 | n/a | check_error(ValueError, ['C:\\Program Files', 'D:\\Program Files']) |
---|
351 | n/a | |
---|
352 | n/a | check(['spam'], 'spam') |
---|
353 | n/a | check(['spam', 'spam'], 'spam') |
---|
354 | n/a | check(['spam', 'alot'], '') |
---|
355 | n/a | check(['and\\jam', 'and\\spam'], 'and') |
---|
356 | n/a | check(['and\\\\jam', 'and\\spam\\\\'], 'and') |
---|
357 | n/a | check(['and\\.\\jam', '.\\and\\spam'], 'and') |
---|
358 | n/a | check(['and\\jam', 'and\\spam', 'alot'], '') |
---|
359 | n/a | check(['and\\jam', 'and\\spam', 'and'], 'and') |
---|
360 | n/a | check(['C:and\\jam', 'C:and\\spam'], 'C:and') |
---|
361 | n/a | |
---|
362 | n/a | check([''], '') |
---|
363 | n/a | check(['', 'spam\\alot'], '') |
---|
364 | n/a | check_error(ValueError, ['', '\\spam\\alot']) |
---|
365 | n/a | |
---|
366 | n/a | self.assertRaises(TypeError, ntpath.commonpath, |
---|
367 | n/a | [b'C:\\Program Files', 'C:\\Program Files\\Foo']) |
---|
368 | n/a | self.assertRaises(TypeError, ntpath.commonpath, |
---|
369 | n/a | [b'C:\\Program Files', 'Program Files\\Foo']) |
---|
370 | n/a | self.assertRaises(TypeError, ntpath.commonpath, |
---|
371 | n/a | [b'Program Files', 'C:\\Program Files\\Foo']) |
---|
372 | n/a | self.assertRaises(TypeError, ntpath.commonpath, |
---|
373 | n/a | ['C:\\Program Files', b'C:\\Program Files\\Foo']) |
---|
374 | n/a | self.assertRaises(TypeError, ntpath.commonpath, |
---|
375 | n/a | ['C:\\Program Files', b'Program Files\\Foo']) |
---|
376 | n/a | self.assertRaises(TypeError, ntpath.commonpath, |
---|
377 | n/a | ['Program Files', b'C:\\Program Files\\Foo']) |
---|
378 | n/a | |
---|
379 | n/a | def test_sameopenfile(self): |
---|
380 | n/a | with TemporaryFile() as tf1, TemporaryFile() as tf2: |
---|
381 | n/a | # Make sure the same file is really the same |
---|
382 | n/a | self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno())) |
---|
383 | n/a | # Make sure different files are really different |
---|
384 | n/a | self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno())) |
---|
385 | n/a | # Make sure invalid values don't cause issues on win32 |
---|
386 | n/a | if sys.platform == "win32": |
---|
387 | n/a | with self.assertRaises(OSError): |
---|
388 | n/a | # Invalid file descriptors shouldn't display assert |
---|
389 | n/a | # dialogs (#4804) |
---|
390 | n/a | ntpath.sameopenfile(-1, -1) |
---|
391 | n/a | |
---|
392 | n/a | def test_ismount(self): |
---|
393 | n/a | self.assertTrue(ntpath.ismount("c:\\")) |
---|
394 | n/a | self.assertTrue(ntpath.ismount("C:\\")) |
---|
395 | n/a | self.assertTrue(ntpath.ismount("c:/")) |
---|
396 | n/a | self.assertTrue(ntpath.ismount("C:/")) |
---|
397 | n/a | self.assertTrue(ntpath.ismount("\\\\.\\c:\\")) |
---|
398 | n/a | self.assertTrue(ntpath.ismount("\\\\.\\C:\\")) |
---|
399 | n/a | |
---|
400 | n/a | self.assertTrue(ntpath.ismount(b"c:\\")) |
---|
401 | n/a | self.assertTrue(ntpath.ismount(b"C:\\")) |
---|
402 | n/a | self.assertTrue(ntpath.ismount(b"c:/")) |
---|
403 | n/a | self.assertTrue(ntpath.ismount(b"C:/")) |
---|
404 | n/a | self.assertTrue(ntpath.ismount(b"\\\\.\\c:\\")) |
---|
405 | n/a | self.assertTrue(ntpath.ismount(b"\\\\.\\C:\\")) |
---|
406 | n/a | |
---|
407 | n/a | with support.temp_dir() as d: |
---|
408 | n/a | self.assertFalse(ntpath.ismount(d)) |
---|
409 | n/a | |
---|
410 | n/a | if sys.platform == "win32": |
---|
411 | n/a | # |
---|
412 | n/a | # Make sure the current folder isn't the root folder |
---|
413 | n/a | # (or any other volume root). The drive-relative |
---|
414 | n/a | # locations below cannot then refer to mount points |
---|
415 | n/a | # |
---|
416 | n/a | drive, path = ntpath.splitdrive(sys.executable) |
---|
417 | n/a | with support.change_cwd(os.path.dirname(sys.executable)): |
---|
418 | n/a | self.assertFalse(ntpath.ismount(drive.lower())) |
---|
419 | n/a | self.assertFalse(ntpath.ismount(drive.upper())) |
---|
420 | n/a | |
---|
421 | n/a | self.assertTrue(ntpath.ismount("\\\\localhost\\c$")) |
---|
422 | n/a | self.assertTrue(ntpath.ismount("\\\\localhost\\c$\\")) |
---|
423 | n/a | |
---|
424 | n/a | self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$")) |
---|
425 | n/a | self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$\\")) |
---|
426 | n/a | |
---|
427 | n/a | class NtCommonTest(test_genericpath.CommonTest, unittest.TestCase): |
---|
428 | n/a | pathmodule = ntpath |
---|
429 | n/a | attributes = ['relpath'] |
---|
430 | n/a | |
---|
431 | n/a | |
---|
432 | n/a | class PathLikeTests(unittest.TestCase): |
---|
433 | n/a | |
---|
434 | n/a | path = ntpath |
---|
435 | n/a | |
---|
436 | n/a | class PathLike: |
---|
437 | n/a | def __init__(self, path=''): |
---|
438 | n/a | self.path = path |
---|
439 | n/a | def __fspath__(self): |
---|
440 | n/a | if isinstance(self.path, BaseException): |
---|
441 | n/a | raise self.path |
---|
442 | n/a | else: |
---|
443 | n/a | return self.path |
---|
444 | n/a | |
---|
445 | n/a | def setUp(self): |
---|
446 | n/a | self.file_name = support.TESTFN.lower() |
---|
447 | n/a | self.file_path = self.PathLike(support.TESTFN) |
---|
448 | n/a | self.addCleanup(support.unlink, self.file_name) |
---|
449 | n/a | with open(self.file_name, 'xb', 0) as file: |
---|
450 | n/a | file.write(b"test_ntpath.PathLikeTests") |
---|
451 | n/a | |
---|
452 | n/a | def assertPathEqual(self, func): |
---|
453 | n/a | self.assertEqual(func(self.file_path), func(self.file_name)) |
---|
454 | n/a | |
---|
455 | n/a | def test_path_normcase(self): |
---|
456 | n/a | self.assertPathEqual(self.path.normcase) |
---|
457 | n/a | |
---|
458 | n/a | def test_path_isabs(self): |
---|
459 | n/a | self.assertPathEqual(self.path.isabs) |
---|
460 | n/a | |
---|
461 | n/a | def test_path_join(self): |
---|
462 | n/a | self.assertEqual(self.path.join('a', self.PathLike('b'), 'c'), |
---|
463 | n/a | self.path.join('a', 'b', 'c')) |
---|
464 | n/a | |
---|
465 | n/a | def test_path_split(self): |
---|
466 | n/a | self.assertPathEqual(self.path.split) |
---|
467 | n/a | |
---|
468 | n/a | def test_path_splitext(self): |
---|
469 | n/a | self.assertPathEqual(self.path.splitext) |
---|
470 | n/a | |
---|
471 | n/a | def test_path_splitdrive(self): |
---|
472 | n/a | self.assertPathEqual(self.path.splitdrive) |
---|
473 | n/a | |
---|
474 | n/a | def test_path_basename(self): |
---|
475 | n/a | self.assertPathEqual(self.path.basename) |
---|
476 | n/a | |
---|
477 | n/a | def test_path_dirname(self): |
---|
478 | n/a | self.assertPathEqual(self.path.dirname) |
---|
479 | n/a | |
---|
480 | n/a | def test_path_islink(self): |
---|
481 | n/a | self.assertPathEqual(self.path.islink) |
---|
482 | n/a | |
---|
483 | n/a | def test_path_lexists(self): |
---|
484 | n/a | self.assertPathEqual(self.path.lexists) |
---|
485 | n/a | |
---|
486 | n/a | def test_path_ismount(self): |
---|
487 | n/a | self.assertPathEqual(self.path.ismount) |
---|
488 | n/a | |
---|
489 | n/a | def test_path_expanduser(self): |
---|
490 | n/a | self.assertPathEqual(self.path.expanduser) |
---|
491 | n/a | |
---|
492 | n/a | def test_path_expandvars(self): |
---|
493 | n/a | self.assertPathEqual(self.path.expandvars) |
---|
494 | n/a | |
---|
495 | n/a | def test_path_normpath(self): |
---|
496 | n/a | self.assertPathEqual(self.path.normpath) |
---|
497 | n/a | |
---|
498 | n/a | def test_path_abspath(self): |
---|
499 | n/a | self.assertPathEqual(self.path.abspath) |
---|
500 | n/a | |
---|
501 | n/a | def test_path_realpath(self): |
---|
502 | n/a | self.assertPathEqual(self.path.realpath) |
---|
503 | n/a | |
---|
504 | n/a | def test_path_relpath(self): |
---|
505 | n/a | self.assertPathEqual(self.path.relpath) |
---|
506 | n/a | |
---|
507 | n/a | def test_path_commonpath(self): |
---|
508 | n/a | common_path = self.path.commonpath([self.file_path, self.file_name]) |
---|
509 | n/a | self.assertEqual(common_path, self.file_name) |
---|
510 | n/a | |
---|
511 | n/a | def test_path_isdir(self): |
---|
512 | n/a | self.assertPathEqual(self.path.isdir) |
---|
513 | n/a | |
---|
514 | n/a | |
---|
515 | n/a | if __name__ == "__main__": |
---|
516 | n/a | unittest.main() |
---|