blob: a84b94c9badbeb44878f20dac62fbf05ba4291e2 [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
Serhiy Storchakab21d1552018-03-02 11:53:51 +02006from test.support import TestFailed, FakePath
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
Steve Dower75e06492019-08-21 13:43:06 -070010
Steve Dower23ad6d02018-02-22 10:39:10 -080011try:
12 import nt
13except ImportError:
14 # Most tests can complete without the nt module,
15 # but for those that require it we import here.
16 nt = None
Guido van Rossumead9d8d1999-02-03 17:21:21 +000017
Steve Dower75e06492019-08-21 13:43:06 -070018try:
19 ntpath._getfinalpathname
20except AttributeError:
21 HAVE_GETFINALPATHNAME = False
22else:
23 HAVE_GETFINALPATHNAME = True
24
25
Steve Dower97d79062019-09-10 14:52:48 +010026def _norm(path):
27 if isinstance(path, (bytes, str, os.PathLike)):
28 return ntpath.normcase(os.fsdecode(path))
29 elif hasattr(path, "__iter__"):
30 return tuple(ntpath.normcase(os.fsdecode(p)) for p in path)
31 return path
32
33
Guido van Rossumead9d8d1999-02-03 17:21:21 +000034def tester(fn, wantResult):
Eric S. Raymondfc170b12001-02-09 11:51:27 +000035 fn = fn.replace("\\", "\\\\")
Fred Drake004d5e62000-10-23 17:22:08 +000036 gotResult = eval(fn)
Steve Dower97d79062019-09-10 14:52:48 +010037 if wantResult != gotResult and _norm(wantResult) != _norm(gotResult):
Christian Heimesf6cd9672008-03-26 13:45:42 +000038 raise TestFailed("%s should return: %s but returned: %s" \
39 %(str(fn), str(wantResult), str(gotResult)))
Tim Peters6578dc92002-12-24 18:31:27 +000040
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +000041 # then with bytes
42 fn = fn.replace("('", "(b'")
43 fn = fn.replace('("', '(b"')
44 fn = fn.replace("['", "[b'")
45 fn = fn.replace('["', '[b"')
46 fn = fn.replace(", '", ", b'")
47 fn = fn.replace(', "', ', b"')
Serhiy Storchakadbb10192014-02-13 10:13:53 +020048 fn = os.fsencode(fn).decode('latin1')
49 fn = fn.encode('ascii', 'backslashreplace').decode('ascii')
Victor Stinner1ab6c2d2011-11-15 22:27:41 +010050 with warnings.catch_warnings():
51 warnings.simplefilter("ignore", DeprecationWarning)
52 gotResult = eval(fn)
Steve Dower97d79062019-09-10 14:52:48 +010053 if _norm(wantResult) != _norm(gotResult):
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +000054 raise TestFailed("%s should return: %s but returned: %s" \
55 %(str(fn), str(wantResult), repr(gotResult)))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000056
Mark Hammond5a607a32009-05-06 08:04:54 +000057
Steve Dower97d79062019-09-10 14:52:48 +010058class NtpathTestCase(unittest.TestCase):
59 def assertPathEqual(self, path1, path2):
60 if path1 == path2 or _norm(path1) == _norm(path2):
61 return
62 self.assertEqual(path1, path2)
63
64 def assertPathIn(self, path, pathset):
65 self.assertIn(_norm(path), _norm(pathset))
66
67
68class TestNtpath(NtpathTestCase):
Christian Heimesf6cd9672008-03-26 13:45:42 +000069 def test_splitext(self):
70 tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
71 tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext'))
72 tester('ntpath.splitext(".ext")', ('.ext', ''))
73 tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', ''))
74 tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', ''))
75 tester('ntpath.splitext("")', ('', ''))
76 tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext'))
77 tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext'))
78 tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext'))
79 tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000080
Christian Heimesf6cd9672008-03-26 13:45:42 +000081 def test_splitdrive(self):
82 tester('ntpath.splitdrive("c:\\foo\\bar")',
83 ('c:', '\\foo\\bar'))
84 tester('ntpath.splitdrive("c:/foo/bar")',
85 ('c:', '/foo/bar'))
Mark Hammond5a607a32009-05-06 08:04:54 +000086 tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")',
Christian Heimesf6cd9672008-03-26 13:45:42 +000087 ('\\\\conky\\mountpoint', '\\foo\\bar'))
Mark Hammond5a607a32009-05-06 08:04:54 +000088 tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")',
Christian Heimesf6cd9672008-03-26 13:45:42 +000089 ('//conky/mountpoint', '/foo/bar'))
Mark Hammond5a607a32009-05-06 08:04:54 +000090 tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")',
91 ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
92 tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")',
93 ('', '///conky/mountpoint/foo/bar'))
94 tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")',
95 ('', '\\\\conky\\\\mountpoint\\foo\\bar'))
96 tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")',
97 ('', '//conky//mountpoint/foo/bar'))
Serhiy Storchaka3d7e1152013-12-16 14:34:55 +020098 # Issue #19911: UNC part containing U+0130
99 self.assertEqual(ntpath.splitdrive('//conky/MOUNTPOİNT/foo/bar'),
100 ('//conky/MOUNTPOİNT', '/foo/bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +0000101
Christian Heimesf6cd9672008-03-26 13:45:42 +0000102 def test_split(self):
103 tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
104 tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
105 ('\\\\conky\\mountpoint\\foo', 'bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +0000106
Christian Heimesf6cd9672008-03-26 13:45:42 +0000107 tester('ntpath.split("c:\\")', ('c:\\', ''))
108 tester('ntpath.split("\\\\conky\\mountpoint\\")',
Mark Hammond5a607a32009-05-06 08:04:54 +0000109 ('\\\\conky\\mountpoint\\', ''))
Guido van Rossumead9d8d1999-02-03 17:21:21 +0000110
Christian Heimesf6cd9672008-03-26 13:45:42 +0000111 tester('ntpath.split("c:/")', ('c:/', ''))
Mark Hammond5a607a32009-05-06 08:04:54 +0000112 tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', ''))
Mark Hammond673c6cf2000-08-14 06:21:26 +0000113
Christian Heimesf6cd9672008-03-26 13:45:42 +0000114 def test_isabs(self):
115 tester('ntpath.isabs("c:\\")', 1)
116 tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
117 tester('ntpath.isabs("\\foo")', 1)
118 tester('ntpath.isabs("\\foo\\bar")', 1)
Tim Petersd4f7f602001-07-19 19:11:41 +0000119
Christian Heimesf6cd9672008-03-26 13:45:42 +0000120 def test_commonprefix(self):
121 tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
122 "/home/swen")
123 tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
124 "\\home\\swen\\")
125 tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
126 "/home/swen/spam")
Tim Peters6a3e5f12001-11-05 21:25:02 +0000127
Christian Heimesf6cd9672008-03-26 13:45:42 +0000128 def test_join(self):
129 tester('ntpath.join("")', '')
130 tester('ntpath.join("", "", "")', '')
131 tester('ntpath.join("a")', 'a')
132 tester('ntpath.join("/a")', '/a')
133 tester('ntpath.join("\\a")', '\\a')
134 tester('ntpath.join("a:")', 'a:')
Christian Heimesf6cd9672008-03-26 13:45:42 +0000135 tester('ntpath.join("a:", "\\b")', 'a:\\b')
Christian Heimesf6cd9672008-03-26 13:45:42 +0000136 tester('ntpath.join("a", "\\b")', '\\b')
137 tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
138 tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
139 tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
140 tester('ntpath.join("a", "b", "\\c")', '\\c')
141 tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
142 tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
Tim Peters54a14a32001-08-30 22:05:26 +0000143
Christian Heimesf6cd9672008-03-26 13:45:42 +0000144 tester("ntpath.join('', 'a')", 'a')
145 tester("ntpath.join('', '', '', '', 'a')", 'a')
146 tester("ntpath.join('a', '')", 'a\\')
147 tester("ntpath.join('a', '', '', '', '')", 'a\\')
148 tester("ntpath.join('a\\', '')", 'a\\')
149 tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
Serhiy Storchakac369c2c2014-01-27 23:15:14 +0200150 tester("ntpath.join('a/', '')", 'a/')
Tim Peters54a14a32001-08-30 22:05:26 +0000151
Serhiy Storchakac369c2c2014-01-27 23:15:14 +0200152 tester("ntpath.join('a/b', 'x/y')", 'a/b\\x/y')
153 tester("ntpath.join('/a/b', 'x/y')", '/a/b\\x/y')
154 tester("ntpath.join('/a/b/', 'x/y')", '/a/b/x/y')
155 tester("ntpath.join('c:', 'x/y')", 'c:x/y')
156 tester("ntpath.join('c:a/b', 'x/y')", 'c:a/b\\x/y')
157 tester("ntpath.join('c:a/b/', 'x/y')", 'c:a/b/x/y')
158 tester("ntpath.join('c:/', 'x/y')", 'c:/x/y')
159 tester("ntpath.join('c:/a/b', 'x/y')", 'c:/a/b\\x/y')
160 tester("ntpath.join('c:/a/b/', 'x/y')", 'c:/a/b/x/y')
161 tester("ntpath.join('//computer/share', 'x/y')", '//computer/share\\x/y')
162 tester("ntpath.join('//computer/share/', 'x/y')", '//computer/share/x/y')
163 tester("ntpath.join('//computer/share/a/b', 'x/y')", '//computer/share/a/b\\x/y')
Mark Hammond5a607a32009-05-06 08:04:54 +0000164
Serhiy Storchakac369c2c2014-01-27 23:15:14 +0200165 tester("ntpath.join('a/b', '/x/y')", '/x/y')
166 tester("ntpath.join('/a/b', '/x/y')", '/x/y')
167 tester("ntpath.join('c:', '/x/y')", 'c:/x/y')
168 tester("ntpath.join('c:a/b', '/x/y')", 'c:/x/y')
169 tester("ntpath.join('c:/', '/x/y')", 'c:/x/y')
170 tester("ntpath.join('c:/a/b', '/x/y')", 'c:/x/y')
171 tester("ntpath.join('//computer/share', '/x/y')", '//computer/share/x/y')
172 tester("ntpath.join('//computer/share/', '/x/y')", '//computer/share/x/y')
173 tester("ntpath.join('//computer/share/a', '/x/y')", '//computer/share/x/y')
174
175 tester("ntpath.join('c:', 'C:x/y')", 'C:x/y')
176 tester("ntpath.join('c:a/b', 'C:x/y')", 'C:a/b\\x/y')
177 tester("ntpath.join('c:/', 'C:x/y')", 'C:/x/y')
178 tester("ntpath.join('c:/a/b', 'C:x/y')", 'C:/a/b\\x/y')
179
180 for x in ('', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b',
181 '//computer/share', '//computer/share/', '//computer/share/a/b'):
182 for y in ('d:', 'd:x/y', 'd:/', 'd:/x/y',
183 '//machine/common', '//machine/common/', '//machine/common/x/y'):
184 tester("ntpath.join(%r, %r)" % (x, y), y)
Mark Hammond5a607a32009-05-06 08:04:54 +0000185
186 tester("ntpath.join('\\\\computer\\share\\', 'a', 'b')", '\\\\computer\\share\\a\\b')
187 tester("ntpath.join('\\\\computer\\share', 'a', 'b')", '\\\\computer\\share\\a\\b')
188 tester("ntpath.join('\\\\computer\\share', 'a\\b')", '\\\\computer\\share\\a\\b')
189 tester("ntpath.join('//computer/share/', 'a', 'b')", '//computer/share/a\\b')
190 tester("ntpath.join('//computer/share', 'a', 'b')", '//computer/share\\a\\b')
191 tester("ntpath.join('//computer/share', 'a/b')", '//computer/share\\a/b')
192
Christian Heimesf6cd9672008-03-26 13:45:42 +0000193 def test_normpath(self):
194 tester("ntpath.normpath('A//////././//.//B')", r'A\B')
195 tester("ntpath.normpath('A/./B')", r'A\B')
196 tester("ntpath.normpath('A/foo/../B')", r'A\B')
197 tester("ntpath.normpath('C:A//B')", r'C:A\B')
198 tester("ntpath.normpath('D:A/./B')", r'D:A\B')
199 tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
Tim Peters54a14a32001-08-30 22:05:26 +0000200
Christian Heimesf6cd9672008-03-26 13:45:42 +0000201 tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
202 tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
203 tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
Thomas Woutersb2137042007-02-01 18:02:27 +0000204
Christian Heimesf6cd9672008-03-26 13:45:42 +0000205 tester("ntpath.normpath('..')", r'..')
206 tester("ntpath.normpath('.')", r'.')
207 tester("ntpath.normpath('')", r'.')
208 tester("ntpath.normpath('/')", '\\')
209 tester("ntpath.normpath('c:/')", 'c:\\')
210 tester("ntpath.normpath('/../.././..')", '\\')
211 tester("ntpath.normpath('c:/../../..')", 'c:\\')
212 tester("ntpath.normpath('../.././..')", r'..\..\..')
213 tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
214 tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
215 tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
Fred Drake5e997312002-01-15 03:46:43 +0000216
Georg Brandlcfb68212010-07-31 21:40:15 +0000217 tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL')
218 tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z')
219
Steve Dower75e06492019-08-21 13:43:06 -0700220 def test_realpath_curdir(self):
221 expected = ntpath.normpath(os.getcwd())
222 tester("ntpath.realpath('.')", expected)
223 tester("ntpath.realpath('./.')", expected)
224 tester("ntpath.realpath('/'.join(['.'] * 100))", expected)
225 tester("ntpath.realpath('.\\.')", expected)
226 tester("ntpath.realpath('\\'.join(['.'] * 100))", expected)
227
228 def test_realpath_pardir(self):
229 expected = ntpath.normpath(os.getcwd())
230 tester("ntpath.realpath('..')", ntpath.dirname(expected))
231 tester("ntpath.realpath('../..')",
232 ntpath.dirname(ntpath.dirname(expected)))
233 tester("ntpath.realpath('/'.join(['..'] * 50))",
234 ntpath.splitdrive(expected)[0] + '\\')
235 tester("ntpath.realpath('..\\..')",
236 ntpath.dirname(ntpath.dirname(expected)))
237 tester("ntpath.realpath('\\'.join(['..'] * 50))",
238 ntpath.splitdrive(expected)[0] + '\\')
239
240 @support.skip_unless_symlink
241 @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
242 def test_realpath_basic(self):
243 ABSTFN = ntpath.abspath(support.TESTFN)
244 open(ABSTFN, "wb").close()
245 self.addCleanup(support.unlink, ABSTFN)
246 self.addCleanup(support.unlink, ABSTFN + "1")
247
248 os.symlink(ABSTFN, ABSTFN + "1")
Steve Dower97d79062019-09-10 14:52:48 +0100249 self.assertPathEqual(ntpath.realpath(ABSTFN + "1"), ABSTFN)
250 self.assertPathEqual(ntpath.realpath(os.fsencode(ABSTFN + "1")),
Steve Dower75e06492019-08-21 13:43:06 -0700251 os.fsencode(ABSTFN))
252
253 @support.skip_unless_symlink
254 @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
255 def test_realpath_relative(self):
256 ABSTFN = ntpath.abspath(support.TESTFN)
257 open(ABSTFN, "wb").close()
258 self.addCleanup(support.unlink, ABSTFN)
259 self.addCleanup(support.unlink, ABSTFN + "1")
260
261 os.symlink(ABSTFN, ntpath.relpath(ABSTFN + "1"))
Steve Dower97d79062019-09-10 14:52:48 +0100262 self.assertPathEqual(ntpath.realpath(ABSTFN + "1"), ABSTFN)
Steve Dower75e06492019-08-21 13:43:06 -0700263
264 @support.skip_unless_symlink
265 @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
266 def test_realpath_broken_symlinks(self):
267 ABSTFN = ntpath.abspath(support.TESTFN)
268 os.mkdir(ABSTFN)
269 self.addCleanup(support.rmtree, ABSTFN)
270
271 with support.change_cwd(ABSTFN):
272 os.mkdir("subdir")
273 os.chdir("subdir")
274 os.symlink(".", "recursive")
275 os.symlink("..", "parent")
276 os.chdir("..")
277 os.symlink(".", "self")
278 os.symlink("missing", "broken")
279 os.symlink(r"broken\bar", "broken1")
280 os.symlink(r"self\self\broken", "broken2")
281 os.symlink(r"subdir\parent\subdir\parent\broken", "broken3")
282 os.symlink(ABSTFN + r"\broken", "broken4")
283 os.symlink(r"recursive\..\broken", "broken5")
284
Steve Dower97d79062019-09-10 14:52:48 +0100285 self.assertPathEqual(ntpath.realpath("broken"),
286 ABSTFN + r"\missing")
287 self.assertPathEqual(ntpath.realpath(r"broken\foo"),
288 ABSTFN + r"\missing\foo")
Steve Dowerabde52c2019-11-15 09:49:21 -0800289 # bpo-38453: We no longer recursively resolve segments of relative
290 # symlinks that the OS cannot resolve.
Steve Dower97d79062019-09-10 14:52:48 +0100291 self.assertPathEqual(ntpath.realpath(r"broken1"),
Steve Dowerabde52c2019-11-15 09:49:21 -0800292 ABSTFN + r"\broken\bar")
Steve Dower97d79062019-09-10 14:52:48 +0100293 self.assertPathEqual(ntpath.realpath(r"broken1\baz"),
Steve Dowerabde52c2019-11-15 09:49:21 -0800294 ABSTFN + r"\broken\bar\baz")
Steve Dower97d79062019-09-10 14:52:48 +0100295 self.assertPathEqual(ntpath.realpath("broken2"),
Steve Dowerabde52c2019-11-15 09:49:21 -0800296 ABSTFN + r"\self\self\missing")
Steve Dower97d79062019-09-10 14:52:48 +0100297 self.assertPathEqual(ntpath.realpath("broken3"),
Steve Dowerabde52c2019-11-15 09:49:21 -0800298 ABSTFN + r"\subdir\parent\subdir\parent\missing")
Steve Dower97d79062019-09-10 14:52:48 +0100299 self.assertPathEqual(ntpath.realpath("broken4"),
300 ABSTFN + r"\missing")
301 self.assertPathEqual(ntpath.realpath("broken5"),
302 ABSTFN + r"\missing")
Steve Dower75e06492019-08-21 13:43:06 -0700303
Steve Dower97d79062019-09-10 14:52:48 +0100304 self.assertPathEqual(ntpath.realpath(b"broken"),
305 os.fsencode(ABSTFN + r"\missing"))
306 self.assertPathEqual(ntpath.realpath(rb"broken\foo"),
307 os.fsencode(ABSTFN + r"\missing\foo"))
308 self.assertPathEqual(ntpath.realpath(rb"broken1"),
Steve Dowerabde52c2019-11-15 09:49:21 -0800309 os.fsencode(ABSTFN + r"\broken\bar"))
Steve Dower97d79062019-09-10 14:52:48 +0100310 self.assertPathEqual(ntpath.realpath(rb"broken1\baz"),
Steve Dowerabde52c2019-11-15 09:49:21 -0800311 os.fsencode(ABSTFN + r"\broken\bar\baz"))
Steve Dower97d79062019-09-10 14:52:48 +0100312 self.assertPathEqual(ntpath.realpath(b"broken2"),
Steve Dowerabde52c2019-11-15 09:49:21 -0800313 os.fsencode(ABSTFN + r"\self\self\missing"))
Steve Dower97d79062019-09-10 14:52:48 +0100314 self.assertPathEqual(ntpath.realpath(rb"broken3"),
Steve Dowerabde52c2019-11-15 09:49:21 -0800315 os.fsencode(ABSTFN + r"\subdir\parent\subdir\parent\missing"))
Steve Dower97d79062019-09-10 14:52:48 +0100316 self.assertPathEqual(ntpath.realpath(b"broken4"),
317 os.fsencode(ABSTFN + r"\missing"))
318 self.assertPathEqual(ntpath.realpath(b"broken5"),
319 os.fsencode(ABSTFN + r"\missing"))
Steve Dower75e06492019-08-21 13:43:06 -0700320
321 @support.skip_unless_symlink
322 @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
323 def test_realpath_symlink_loops(self):
Steve Dowerabde52c2019-11-15 09:49:21 -0800324 # Symlink loops are non-deterministic as to which path is returned, but
325 # it will always be the fully resolved path of one member of the cycle
Steve Dower75e06492019-08-21 13:43:06 -0700326 ABSTFN = ntpath.abspath(support.TESTFN)
327 self.addCleanup(support.unlink, ABSTFN)
328 self.addCleanup(support.unlink, ABSTFN + "1")
329 self.addCleanup(support.unlink, ABSTFN + "2")
330 self.addCleanup(support.unlink, ABSTFN + "y")
331 self.addCleanup(support.unlink, ABSTFN + "c")
332 self.addCleanup(support.unlink, ABSTFN + "a")
333
Steve Dower75e06492019-08-21 13:43:06 -0700334 os.symlink(ABSTFN, ABSTFN)
Steve Dowera0e3d272019-10-03 08:31:03 -0700335 self.assertPathEqual(ntpath.realpath(ABSTFN), ABSTFN)
Steve Dower75e06492019-08-21 13:43:06 -0700336
Steve Dower75e06492019-08-21 13:43:06 -0700337 os.symlink(ABSTFN + "1", ABSTFN + "2")
338 os.symlink(ABSTFN + "2", ABSTFN + "1")
Steve Dowera0e3d272019-10-03 08:31:03 -0700339 expected = (ABSTFN + "1", ABSTFN + "2")
Steve Dower97d79062019-09-10 14:52:48 +0100340 self.assertPathIn(ntpath.realpath(ABSTFN + "1"), expected)
341 self.assertPathIn(ntpath.realpath(ABSTFN + "2"), expected)
Steve Dower75e06492019-08-21 13:43:06 -0700342
Steve Dower97d79062019-09-10 14:52:48 +0100343 self.assertPathIn(ntpath.realpath(ABSTFN + "1\\x"),
344 (ntpath.join(r, "x") for r in expected))
345 self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\.."),
346 ntpath.dirname(ABSTFN))
347 self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\..\\x"),
348 ntpath.dirname(ABSTFN) + "\\x")
Steve Dower75e06492019-08-21 13:43:06 -0700349 os.symlink(ABSTFN + "x", ABSTFN + "y")
Steve Dower97d79062019-09-10 14:52:48 +0100350 self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\..\\"
351 + ntpath.basename(ABSTFN) + "y"),
352 ABSTFN + "x")
353 self.assertPathIn(ntpath.realpath(ABSTFN + "1\\..\\"
354 + ntpath.basename(ABSTFN) + "1"),
355 expected)
Steve Dower75e06492019-08-21 13:43:06 -0700356
357 os.symlink(ntpath.basename(ABSTFN) + "a\\b", ABSTFN + "a")
Steve Dowera0e3d272019-10-03 08:31:03 -0700358 self.assertPathEqual(ntpath.realpath(ABSTFN + "a"), ABSTFN + "a")
Steve Dower75e06492019-08-21 13:43:06 -0700359
360 os.symlink("..\\" + ntpath.basename(ntpath.dirname(ABSTFN))
361 + "\\" + ntpath.basename(ABSTFN) + "c", ABSTFN + "c")
Steve Dowera0e3d272019-10-03 08:31:03 -0700362 self.assertPathEqual(ntpath.realpath(ABSTFN + "c"), ABSTFN + "c")
Steve Dower75e06492019-08-21 13:43:06 -0700363
364 # Test using relative path as well.
Steve Dowera0e3d272019-10-03 08:31:03 -0700365 self.assertPathEqual(ntpath.realpath(ntpath.basename(ABSTFN)), ABSTFN)
Steve Dower75e06492019-08-21 13:43:06 -0700366
367 @support.skip_unless_symlink
368 @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
369 def test_realpath_symlink_prefix(self):
370 ABSTFN = ntpath.abspath(support.TESTFN)
371 self.addCleanup(support.unlink, ABSTFN + "3")
372 self.addCleanup(support.unlink, "\\\\?\\" + ABSTFN + "3.")
373 self.addCleanup(support.unlink, ABSTFN + "3link")
374 self.addCleanup(support.unlink, ABSTFN + "3.link")
375
376 with open(ABSTFN + "3", "wb") as f:
377 f.write(b'0')
378 os.symlink(ABSTFN + "3", ABSTFN + "3link")
379
380 with open("\\\\?\\" + ABSTFN + "3.", "wb") as f:
381 f.write(b'1')
382 os.symlink("\\\\?\\" + ABSTFN + "3.", ABSTFN + "3.link")
383
Steve Dower97d79062019-09-10 14:52:48 +0100384 self.assertPathEqual(ntpath.realpath(ABSTFN + "3link"),
385 ABSTFN + "3")
386 self.assertPathEqual(ntpath.realpath(ABSTFN + "3.link"),
387 "\\\\?\\" + ABSTFN + "3.")
Steve Dower75e06492019-08-21 13:43:06 -0700388
389 # Resolved paths should be usable to open target files
390 with open(ntpath.realpath(ABSTFN + "3link"), "rb") as f:
391 self.assertEqual(f.read(), b'0')
392 with open(ntpath.realpath(ABSTFN + "3.link"), "rb") as f:
393 self.assertEqual(f.read(), b'1')
394
395 # When the prefix is included, it is not stripped
Steve Dower97d79062019-09-10 14:52:48 +0100396 self.assertPathEqual(ntpath.realpath("\\\\?\\" + ABSTFN + "3link"),
397 "\\\\?\\" + ABSTFN + "3")
398 self.assertPathEqual(ntpath.realpath("\\\\?\\" + ABSTFN + "3.link"),
399 "\\\\?\\" + ABSTFN + "3.")
Steve Dower75e06492019-08-21 13:43:06 -0700400
Steve Dower92521fe2019-09-11 10:48:36 +0100401 @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
402 def test_realpath_nul(self):
403 tester("ntpath.realpath('NUL')", r'\\.\NUL')
404
Steve Dowerabde52c2019-11-15 09:49:21 -0800405 @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
406 def test_realpath_cwd(self):
407 ABSTFN = ntpath.abspath(support.TESTFN)
408
409 support.unlink(ABSTFN)
410 support.rmtree(ABSTFN)
411 os.mkdir(ABSTFN)
412 self.addCleanup(support.rmtree, ABSTFN)
413
414 test_dir_long = ntpath.join(ABSTFN, "MyVeryLongDirectoryName")
415 test_dir_short = ntpath.join(ABSTFN, "MYVERY~1")
416 test_file_long = ntpath.join(test_dir_long, "file.txt")
417 test_file_short = ntpath.join(test_dir_short, "file.txt")
418
419 os.mkdir(test_dir_long)
420
421 with open(test_file_long, "wb") as f:
422 f.write(b"content")
423
424 self.assertPathEqual(test_file_long, ntpath.realpath(test_file_short))
425
426 with support.change_cwd(test_dir_long):
427 self.assertPathEqual(test_file_long, ntpath.realpath("file.txt"))
428 with support.change_cwd(test_dir_long.lower()):
429 self.assertPathEqual(test_file_long, ntpath.realpath("file.txt"))
430 with support.change_cwd(test_dir_short):
431 self.assertPathEqual(test_file_long, ntpath.realpath("file.txt"))
432
Christian Heimesf6cd9672008-03-26 13:45:42 +0000433 def test_expandvars(self):
Walter Dörwald155374d2009-05-01 19:58:58 +0000434 with support.EnvironmentVarGuard() as env:
435 env.clear()
436 env["foo"] = "bar"
437 env["{foo"] = "baz1"
438 env["{foo}"] = "baz2"
Christian Heimesf6cd9672008-03-26 13:45:42 +0000439 tester('ntpath.expandvars("foo")', "foo")
440 tester('ntpath.expandvars("$foo bar")', "bar bar")
441 tester('ntpath.expandvars("${foo}bar")', "barbar")
442 tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
443 tester('ntpath.expandvars("$bar bar")', "$bar bar")
444 tester('ntpath.expandvars("$?bar")', "$?bar")
Christian Heimesf6cd9672008-03-26 13:45:42 +0000445 tester('ntpath.expandvars("$foo}bar")', "bar}bar")
446 tester('ntpath.expandvars("${foo")', "${foo")
447 tester('ntpath.expandvars("${{foo}}")', "baz1}")
448 tester('ntpath.expandvars("$foo$foo")', "barbar")
449 tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
450 tester('ntpath.expandvars("%foo% bar")', "bar bar")
451 tester('ntpath.expandvars("%foo%bar")', "barbar")
452 tester('ntpath.expandvars("%foo%%foo%")', "barbar")
453 tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
454 tester('ntpath.expandvars("%?bar%")', "%?bar%")
455 tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
456 tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
Serhiy Storchaka1b87ae02015-03-25 16:40:15 +0200457 tester('ntpath.expandvars("bar\'%foo%")', "bar\'%foo%")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000458
Serhiy Storchakadbb10192014-02-13 10:13:53 +0200459 @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII')
460 def test_expandvars_nonascii(self):
461 def check(value, expected):
462 tester('ntpath.expandvars(%r)' % value, expected)
463 with support.EnvironmentVarGuard() as env:
464 env.clear()
465 nonascii = support.FS_NONASCII
466 env['spam'] = nonascii
467 env[nonascii] = 'ham' + nonascii
468 check('$spam bar', '%s bar' % nonascii)
469 check('$%s bar' % nonascii, '$%s bar' % nonascii)
470 check('${spam}bar', '%sbar' % nonascii)
471 check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
472 check('$spam}bar', '%s}bar' % nonascii)
473 check('$%s}bar' % nonascii, '$%s}bar' % nonascii)
474 check('%spam% bar', '%s bar' % nonascii)
475 check('%{}% bar'.format(nonascii), 'ham%s bar' % nonascii)
476 check('%spam%bar', '%sbar' % nonascii)
477 check('%{}%bar'.format(nonascii), 'ham%sbar' % nonascii)
478
Serhiy Storchakaffc1e6d2014-05-28 18:11:29 +0300479 def test_expanduser(self):
480 tester('ntpath.expanduser("test")', 'test')
481
482 with support.EnvironmentVarGuard() as env:
483 env.clear()
484 tester('ntpath.expanduser("~test")', '~test')
485
486 env['HOMEPATH'] = 'eric\\idle'
487 env['HOMEDRIVE'] = 'C:\\'
488 tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
489 tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
490
491 del env['HOMEDRIVE']
492 tester('ntpath.expanduser("~test")', 'eric\\test')
493 tester('ntpath.expanduser("~")', 'eric\\idle')
494
495 env.clear()
496 env['USERPROFILE'] = 'C:\\eric\\idle'
497 tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
498 tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
Serhiy Storchakaffc1e6d2014-05-28 18:11:29 +0300499 tester('ntpath.expanduser("~test\\foo\\bar")',
Anthony Sottile25ec4a42019-03-12 08:39:57 -0700500 'C:\\eric\\test\\foo\\bar')
Serhiy Storchakaffc1e6d2014-05-28 18:11:29 +0300501 tester('ntpath.expanduser("~test/foo/bar")',
Anthony Sottile25ec4a42019-03-12 08:39:57 -0700502 'C:\\eric\\test/foo/bar')
Serhiy Storchakaffc1e6d2014-05-28 18:11:29 +0300503 tester('ntpath.expanduser("~\\foo\\bar")',
Anthony Sottile25ec4a42019-03-12 08:39:57 -0700504 'C:\\eric\\idle\\foo\\bar')
Serhiy Storchakaffc1e6d2014-05-28 18:11:29 +0300505 tester('ntpath.expanduser("~/foo/bar")',
Anthony Sottile25ec4a42019-03-12 08:39:57 -0700506 'C:\\eric\\idle/foo/bar')
507
508 # bpo-36264: ignore `HOME` when set on windows
509 env.clear()
510 env['HOME'] = 'F:\\'
511 env['USERPROFILE'] = 'C:\\eric\\idle'
512 tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
513 tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
Serhiy Storchakaffc1e6d2014-05-28 18:11:29 +0300514
Steve Dower23ad6d02018-02-22 10:39:10 -0800515 @unittest.skipUnless(nt, "abspath requires 'nt' module")
Christian Heimesf6cd9672008-03-26 13:45:42 +0000516 def test_abspath(self):
Steve Dower23ad6d02018-02-22 10:39:10 -0800517 tester('ntpath.abspath("C:\\")', "C:\\")
Franz Wöllertd2e902e2018-07-29 14:47:09 +0200518 with support.temp_cwd(support.TESTFN) as cwd_dir: # bpo-31047
519 tester('ntpath.abspath("")', cwd_dir)
520 tester('ntpath.abspath(" ")', cwd_dir + "\\ ")
521 tester('ntpath.abspath("?")', cwd_dir + "\\?")
Tim Grahamd03b7752018-10-25 11:26:38 -0400522 drive, _ = ntpath.splitdrive(cwd_dir)
523 tester('ntpath.abspath("/abc/")', drive + "\\abc")
Christian Heimesf6cd9672008-03-26 13:45:42 +0000524
525 def test_relpath(self):
Christian Heimesf6cd9672008-03-26 13:45:42 +0000526 tester('ntpath.relpath("a")', 'a')
Steve Dower75e06492019-08-21 13:43:06 -0700527 tester('ntpath.relpath(ntpath.abspath("a"))', 'a')
Christian Heimesf6cd9672008-03-26 13:45:42 +0000528 tester('ntpath.relpath("a/b")', 'a\\b')
529 tester('ntpath.relpath("../a/b")', '..\\a\\b')
Serhiy Storchaka5106d042015-01-26 10:26:14 +0200530 with support.temp_cwd(support.TESTFN) as cwd_dir:
Steve Dower75e06492019-08-21 13:43:06 -0700531 currentdir = ntpath.basename(cwd_dir)
Serhiy Storchaka5106d042015-01-26 10:26:14 +0200532 tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
533 tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
Christian Heimesf6cd9672008-03-26 13:45:42 +0000534 tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
Mark Hammond5a607a32009-05-06 08:04:54 +0000535 tester('ntpath.relpath("c:/foo/bar/bat", "c:/x/y")', '..\\..\\foo\\bar\\bat')
Christian Heimesf6cd9672008-03-26 13:45:42 +0000536 tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
537 tester('ntpath.relpath("a", "a")', '.')
Mark Hammond5a607a32009-05-06 08:04:54 +0000538 tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
539 tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
540 tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
541 tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
542 tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
543 tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
544 tester('ntpath.relpath("/", "/")', '.')
545 tester('ntpath.relpath("/a", "/a")', '.')
546 tester('ntpath.relpath("/a/b", "/a/b")', '.')
Hirokazu Yamamotob08820a2010-10-18 12:13:18 +0000547 tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
Christian Heimesf6cd9672008-03-26 13:45:42 +0000548
Serhiy Storchaka38220932015-03-31 15:31:53 +0300549 def test_commonpath(self):
550 def check(paths, expected):
551 tester(('ntpath.commonpath(%r)' % paths).replace('\\\\', '\\'),
552 expected)
553 def check_error(exc, paths):
554 self.assertRaises(exc, ntpath.commonpath, paths)
555 self.assertRaises(exc, ntpath.commonpath,
556 [os.fsencode(p) for p in paths])
557
558 self.assertRaises(ValueError, ntpath.commonpath, [])
559 check_error(ValueError, ['C:\\Program Files', 'Program Files'])
560 check_error(ValueError, ['C:\\Program Files', 'C:Program Files'])
561 check_error(ValueError, ['\\Program Files', 'Program Files'])
562 check_error(ValueError, ['Program Files', 'C:\\Program Files'])
563 check(['C:\\Program Files'], 'C:\\Program Files')
564 check(['C:\\Program Files', 'C:\\Program Files'], 'C:\\Program Files')
565 check(['C:\\Program Files\\', 'C:\\Program Files'],
566 'C:\\Program Files')
567 check(['C:\\Program Files\\', 'C:\\Program Files\\'],
568 'C:\\Program Files')
569 check(['C:\\\\Program Files', 'C:\\Program Files\\\\'],
570 'C:\\Program Files')
571 check(['C:\\.\\Program Files', 'C:\\Program Files\\.'],
572 'C:\\Program Files')
573 check(['C:\\', 'C:\\bin'], 'C:\\')
574 check(['C:\\Program Files', 'C:\\bin'], 'C:\\')
575 check(['C:\\Program Files', 'C:\\Program Files\\Bar'],
576 'C:\\Program Files')
577 check(['C:\\Program Files\\Foo', 'C:\\Program Files\\Bar'],
578 'C:\\Program Files')
579 check(['C:\\Program Files', 'C:\\Projects'], 'C:\\')
580 check(['C:\\Program Files\\', 'C:\\Projects'], 'C:\\')
581
582 check(['C:\\Program Files\\Foo', 'C:/Program Files/Bar'],
583 'C:\\Program Files')
584 check(['C:\\Program Files\\Foo', 'c:/program files/bar'],
585 'C:\\Program Files')
586 check(['c:/program files/bar', 'C:\\Program Files\\Foo'],
587 'c:\\program files')
588
589 check_error(ValueError, ['C:\\Program Files', 'D:\\Program Files'])
590
591 check(['spam'], 'spam')
592 check(['spam', 'spam'], 'spam')
593 check(['spam', 'alot'], '')
594 check(['and\\jam', 'and\\spam'], 'and')
595 check(['and\\\\jam', 'and\\spam\\\\'], 'and')
596 check(['and\\.\\jam', '.\\and\\spam'], 'and')
597 check(['and\\jam', 'and\\spam', 'alot'], '')
598 check(['and\\jam', 'and\\spam', 'and'], 'and')
599 check(['C:and\\jam', 'C:and\\spam'], 'C:and')
600
601 check([''], '')
602 check(['', 'spam\\alot'], '')
603 check_error(ValueError, ['', '\\spam\\alot'])
604
605 self.assertRaises(TypeError, ntpath.commonpath,
606 [b'C:\\Program Files', 'C:\\Program Files\\Foo'])
607 self.assertRaises(TypeError, ntpath.commonpath,
608 [b'C:\\Program Files', 'Program Files\\Foo'])
609 self.assertRaises(TypeError, ntpath.commonpath,
610 [b'Program Files', 'C:\\Program Files\\Foo'])
611 self.assertRaises(TypeError, ntpath.commonpath,
612 ['C:\\Program Files', b'C:\\Program Files\\Foo'])
613 self.assertRaises(TypeError, ntpath.commonpath,
614 ['C:\\Program Files', b'Program Files\\Foo'])
615 self.assertRaises(TypeError, ntpath.commonpath,
616 ['Program Files', b'C:\\Program Files\\Foo'])
617
Brian Curtin62857742010-09-06 17:07:27 +0000618 def test_sameopenfile(self):
619 with TemporaryFile() as tf1, TemporaryFile() as tf2:
620 # Make sure the same file is really the same
621 self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno()))
622 # Make sure different files are really different
623 self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno()))
Brian Curtin13a0db52010-09-06 19:46:17 +0000624 # Make sure invalid values don't cause issues on win32
625 if sys.platform == "win32":
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +0000626 with self.assertRaises(OSError):
Brian Curtin13a0db52010-09-06 19:46:17 +0000627 # Invalid file descriptors shouldn't display assert
628 # dialogs (#4804)
629 ntpath.sameopenfile(-1, -1)
Brian Curtin62857742010-09-06 17:07:27 +0000630
Tim Golden6b528062013-08-01 12:44:00 +0100631 def test_ismount(self):
632 self.assertTrue(ntpath.ismount("c:\\"))
633 self.assertTrue(ntpath.ismount("C:\\"))
634 self.assertTrue(ntpath.ismount("c:/"))
635 self.assertTrue(ntpath.ismount("C:/"))
636 self.assertTrue(ntpath.ismount("\\\\.\\c:\\"))
637 self.assertTrue(ntpath.ismount("\\\\.\\C:\\"))
638
639 self.assertTrue(ntpath.ismount(b"c:\\"))
640 self.assertTrue(ntpath.ismount(b"C:\\"))
641 self.assertTrue(ntpath.ismount(b"c:/"))
642 self.assertTrue(ntpath.ismount(b"C:/"))
643 self.assertTrue(ntpath.ismount(b"\\\\.\\c:\\"))
644 self.assertTrue(ntpath.ismount(b"\\\\.\\C:\\"))
645
646 with support.temp_dir() as d:
647 self.assertFalse(ntpath.ismount(d))
648
Tim Goldenb2fcebb2013-08-01 13:58:58 +0100649 if sys.platform == "win32":
650 #
651 # Make sure the current folder isn't the root folder
652 # (or any other volume root). The drive-relative
653 # locations below cannot then refer to mount points
654 #
655 drive, path = ntpath.splitdrive(sys.executable)
Steve Dower75e06492019-08-21 13:43:06 -0700656 with support.change_cwd(ntpath.dirname(sys.executable)):
Tim Goldenb2fcebb2013-08-01 13:58:58 +0100657 self.assertFalse(ntpath.ismount(drive.lower()))
658 self.assertFalse(ntpath.ismount(drive.upper()))
Tim Golden6b528062013-08-01 12:44:00 +0100659
Tim Goldenb2fcebb2013-08-01 13:58:58 +0100660 self.assertTrue(ntpath.ismount("\\\\localhost\\c$"))
661 self.assertTrue(ntpath.ismount("\\\\localhost\\c$\\"))
Tim Golden6b528062013-08-01 12:44:00 +0100662
Tim Goldenb2fcebb2013-08-01 13:58:58 +0100663 self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$"))
664 self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$\\"))
Christian Heimesf6cd9672008-03-26 13:45:42 +0000665
Tim Goldenff64add2018-07-25 14:36:54 +0100666 def assertEqualCI(self, s1, s2):
667 """Assert that two strings are equal ignoring case differences."""
668 self.assertEqual(s1.lower(), s2.lower())
669
Steve Dower23ad6d02018-02-22 10:39:10 -0800670 @unittest.skipUnless(nt, "OS helpers require 'nt' module")
671 def test_nt_helpers(self):
672 # Trivial validation that the helpers do not break, and support both
673 # unicode and bytes (UTF-8) paths
674
Tim Goldenff64add2018-07-25 14:36:54 +0100675 executable = nt._getfinalpathname(sys.executable)
676
677 for path in executable, os.fsencode(executable):
678 volume_path = nt._getvolumepathname(path)
679 path_drive = ntpath.splitdrive(path)[0]
680 volume_path_drive = ntpath.splitdrive(volume_path)[0]
681 self.assertEqualCI(path_drive, volume_path_drive)
Steve Dower23ad6d02018-02-22 10:39:10 -0800682
683 cap, free = nt._getdiskusage(sys.exec_prefix)
684 self.assertGreater(cap, 0)
685 self.assertGreater(free, 0)
686 b_cap, b_free = nt._getdiskusage(sys.exec_prefix.encode())
687 # Free space may change, so only test the capacity is equal
688 self.assertEqual(b_cap, cap)
689 self.assertGreater(b_free, 0)
690
691 for path in [sys.prefix, sys.executable]:
692 final_path = nt._getfinalpathname(path)
693 self.assertIsInstance(final_path, str)
694 self.assertGreater(len(final_path), 0)
695
696 b_final_path = nt._getfinalpathname(path.encode())
697 self.assertIsInstance(b_final_path, bytes)
698 self.assertGreater(len(b_final_path), 0)
699
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200700class NtCommonTest(test_genericpath.CommonTest, unittest.TestCase):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000701 pathmodule = ntpath
Serhiy Storchaka9ed707e2017-01-13 20:55:05 +0200702 attributes = ['relpath']
Florent Xiclunac9c79782010-03-08 12:24:53 +0000703
704
Steve Dower97d79062019-09-10 14:52:48 +0100705class PathLikeTests(NtpathTestCase):
Brett Cannon3f9183b2016-08-26 14:44:48 -0700706
707 path = ntpath
708
Brett Cannon3f9183b2016-08-26 14:44:48 -0700709 def setUp(self):
710 self.file_name = support.TESTFN.lower()
Serhiy Storchakab21d1552018-03-02 11:53:51 +0200711 self.file_path = FakePath(support.TESTFN)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700712 self.addCleanup(support.unlink, self.file_name)
713 with open(self.file_name, 'xb', 0) as file:
714 file.write(b"test_ntpath.PathLikeTests")
715
Steve Dower97d79062019-09-10 14:52:48 +0100716 def _check_function(self, func):
717 self.assertPathEqual(func(self.file_path), func(self.file_name))
Brett Cannon3f9183b2016-08-26 14:44:48 -0700718
719 def test_path_normcase(self):
Steve Dower97d79062019-09-10 14:52:48 +0100720 self._check_function(self.path.normcase)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700721
722 def test_path_isabs(self):
Steve Dower97d79062019-09-10 14:52:48 +0100723 self._check_function(self.path.isabs)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700724
725 def test_path_join(self):
Serhiy Storchakab21d1552018-03-02 11:53:51 +0200726 self.assertEqual(self.path.join('a', FakePath('b'), 'c'),
Brett Cannon3f9183b2016-08-26 14:44:48 -0700727 self.path.join('a', 'b', 'c'))
728
729 def test_path_split(self):
Steve Dower97d79062019-09-10 14:52:48 +0100730 self._check_function(self.path.split)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700731
732 def test_path_splitext(self):
Steve Dower97d79062019-09-10 14:52:48 +0100733 self._check_function(self.path.splitext)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700734
735 def test_path_splitdrive(self):
Steve Dower97d79062019-09-10 14:52:48 +0100736 self._check_function(self.path.splitdrive)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700737
738 def test_path_basename(self):
Steve Dower97d79062019-09-10 14:52:48 +0100739 self._check_function(self.path.basename)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700740
741 def test_path_dirname(self):
Steve Dower97d79062019-09-10 14:52:48 +0100742 self._check_function(self.path.dirname)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700743
744 def test_path_islink(self):
Steve Dower97d79062019-09-10 14:52:48 +0100745 self._check_function(self.path.islink)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700746
747 def test_path_lexists(self):
Steve Dower97d79062019-09-10 14:52:48 +0100748 self._check_function(self.path.lexists)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700749
750 def test_path_ismount(self):
Steve Dower97d79062019-09-10 14:52:48 +0100751 self._check_function(self.path.ismount)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700752
753 def test_path_expanduser(self):
Steve Dower97d79062019-09-10 14:52:48 +0100754 self._check_function(self.path.expanduser)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700755
756 def test_path_expandvars(self):
Steve Dower97d79062019-09-10 14:52:48 +0100757 self._check_function(self.path.expandvars)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700758
759 def test_path_normpath(self):
Steve Dower97d79062019-09-10 14:52:48 +0100760 self._check_function(self.path.normpath)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700761
762 def test_path_abspath(self):
Steve Dower97d79062019-09-10 14:52:48 +0100763 self._check_function(self.path.abspath)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700764
765 def test_path_realpath(self):
Steve Dower97d79062019-09-10 14:52:48 +0100766 self._check_function(self.path.realpath)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700767
768 def test_path_relpath(self):
Steve Dower97d79062019-09-10 14:52:48 +0100769 self._check_function(self.path.relpath)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700770
771 def test_path_commonpath(self):
772 common_path = self.path.commonpath([self.file_path, self.file_name])
Steve Dower97d79062019-09-10 14:52:48 +0100773 self.assertPathEqual(common_path, self.file_name)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700774
775 def test_path_isdir(self):
Steve Dower97d79062019-09-10 14:52:48 +0100776 self._check_function(self.path.isdir)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700777
778
Christian Heimesf6cd9672008-03-26 13:45:42 +0000779if __name__ == "__main__":
780 unittest.main()