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