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 | |
Serhiy Storchaka | 593568b | 2013-12-16 15:13:28 +0200 | [diff] [blame] | 75 | def test_splitunc(self): |
| 76 | with self.assertWarns(DeprecationWarning): |
| 77 | ntpath.splitunc('') |
| 78 | with support.check_warnings(('', DeprecationWarning)): |
| 79 | tester('ntpath.splitunc("c:\\foo\\bar")', |
| 80 | ('', 'c:\\foo\\bar')) |
| 81 | tester('ntpath.splitunc("c:/foo/bar")', |
| 82 | ('', 'c:/foo/bar')) |
| 83 | tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")', |
| 84 | ('\\\\conky\\mountpoint', '\\foo\\bar')) |
| 85 | tester('ntpath.splitunc("//conky/mountpoint/foo/bar")', |
| 86 | ('//conky/mountpoint', '/foo/bar')) |
| 87 | tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")', |
| 88 | ('', '\\\\\\conky\\mountpoint\\foo\\bar')) |
| 89 | tester('ntpath.splitunc("///conky/mountpoint/foo/bar")', |
| 90 | ('', '///conky/mountpoint/foo/bar')) |
| 91 | tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")', |
| 92 | ('', '\\\\conky\\\\mountpoint\\foo\\bar')) |
| 93 | tester('ntpath.splitunc("//conky//mountpoint/foo/bar")', |
| 94 | ('', '//conky//mountpoint/foo/bar')) |
| 95 | self.assertEqual(ntpath.splitunc('//conky/MOUNTPOİNT/foo/bar'), |
| 96 | ('//conky/MOUNTPOİNT', '/foo/bar')) |
| 97 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 98 | def test_split(self): |
| 99 | tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar')) |
| 100 | tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")', |
| 101 | ('\\\\conky\\mountpoint\\foo', 'bar')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 102 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 103 | tester('ntpath.split("c:\\")', ('c:\\', '')) |
| 104 | tester('ntpath.split("\\\\conky\\mountpoint\\")', |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 105 | ('\\\\conky\\mountpoint\\', '')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 106 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 107 | tester('ntpath.split("c:/")', ('c:/', '')) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 108 | tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', '')) |
Mark Hammond | 673c6cf | 2000-08-14 06:21:26 +0000 | [diff] [blame] | 109 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 110 | def test_isabs(self): |
| 111 | tester('ntpath.isabs("c:\\")', 1) |
| 112 | tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1) |
| 113 | tester('ntpath.isabs("\\foo")', 1) |
| 114 | tester('ntpath.isabs("\\foo\\bar")', 1) |
Tim Peters | d4f7f60 | 2001-07-19 19:11:41 +0000 | [diff] [blame] | 115 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 116 | def test_commonprefix(self): |
| 117 | tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])', |
| 118 | "/home/swen") |
| 119 | tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])', |
| 120 | "\\home\\swen\\") |
| 121 | tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])', |
| 122 | "/home/swen/spam") |
Tim Peters | 6a3e5f1 | 2001-11-05 21:25:02 +0000 | [diff] [blame] | 123 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 124 | def test_join(self): |
| 125 | tester('ntpath.join("")', '') |
| 126 | tester('ntpath.join("", "", "")', '') |
| 127 | tester('ntpath.join("a")', 'a') |
| 128 | tester('ntpath.join("/a")', '/a') |
| 129 | tester('ntpath.join("\\a")', '\\a') |
| 130 | tester('ntpath.join("a:")', 'a:') |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 131 | tester('ntpath.join("a:", "\\b")', 'a:\\b') |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 132 | tester('ntpath.join("a", "\\b")', '\\b') |
| 133 | tester('ntpath.join("a", "b", "c")', 'a\\b\\c') |
| 134 | tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c') |
| 135 | tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c') |
| 136 | tester('ntpath.join("a", "b", "\\c")', '\\c') |
| 137 | tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep') |
| 138 | tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b') |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 139 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 140 | tester("ntpath.join('', 'a')", 'a') |
| 141 | tester("ntpath.join('', '', '', '', 'a')", 'a') |
| 142 | tester("ntpath.join('a', '')", 'a\\') |
| 143 | tester("ntpath.join('a', '', '', '', '')", 'a\\') |
| 144 | tester("ntpath.join('a\\', '')", 'a\\') |
| 145 | tester("ntpath.join('a\\', '', '', '', '')", 'a\\') |
Serhiy Storchaka | c369c2c | 2014-01-27 23:15:14 +0200 | [diff] [blame] | 146 | tester("ntpath.join('a/', '')", 'a/') |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 147 | |
Serhiy Storchaka | c369c2c | 2014-01-27 23:15:14 +0200 | [diff] [blame] | 148 | tester("ntpath.join('a/b', 'x/y')", 'a/b\\x/y') |
| 149 | tester("ntpath.join('/a/b', 'x/y')", '/a/b\\x/y') |
| 150 | tester("ntpath.join('/a/b/', 'x/y')", '/a/b/x/y') |
| 151 | tester("ntpath.join('c:', 'x/y')", 'c:x/y') |
| 152 | tester("ntpath.join('c:a/b', 'x/y')", 'c:a/b\\x/y') |
| 153 | tester("ntpath.join('c:a/b/', 'x/y')", 'c:a/b/x/y') |
| 154 | tester("ntpath.join('c:/', 'x/y')", 'c:/x/y') |
| 155 | tester("ntpath.join('c:/a/b', 'x/y')", 'c:/a/b\\x/y') |
| 156 | tester("ntpath.join('c:/a/b/', 'x/y')", 'c:/a/b/x/y') |
| 157 | tester("ntpath.join('//computer/share', 'x/y')", '//computer/share\\x/y') |
| 158 | tester("ntpath.join('//computer/share/', 'x/y')", '//computer/share/x/y') |
| 159 | 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] | 160 | |
Serhiy Storchaka | c369c2c | 2014-01-27 23:15:14 +0200 | [diff] [blame] | 161 | tester("ntpath.join('a/b', '/x/y')", '/x/y') |
| 162 | tester("ntpath.join('/a/b', '/x/y')", '/x/y') |
| 163 | tester("ntpath.join('c:', '/x/y')", 'c:/x/y') |
| 164 | tester("ntpath.join('c:a/b', '/x/y')", 'c:/x/y') |
| 165 | tester("ntpath.join('c:/', '/x/y')", 'c:/x/y') |
| 166 | tester("ntpath.join('c:/a/b', '/x/y')", 'c:/x/y') |
| 167 | tester("ntpath.join('//computer/share', '/x/y')", '//computer/share/x/y') |
| 168 | tester("ntpath.join('//computer/share/', '/x/y')", '//computer/share/x/y') |
| 169 | tester("ntpath.join('//computer/share/a', '/x/y')", '//computer/share/x/y') |
| 170 | |
| 171 | tester("ntpath.join('c:', 'C:x/y')", 'C:x/y') |
| 172 | tester("ntpath.join('c:a/b', 'C:x/y')", 'C:a/b\\x/y') |
| 173 | tester("ntpath.join('c:/', 'C:x/y')", 'C:/x/y') |
| 174 | tester("ntpath.join('c:/a/b', 'C:x/y')", 'C:/a/b\\x/y') |
| 175 | |
| 176 | for x in ('', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b', |
| 177 | '//computer/share', '//computer/share/', '//computer/share/a/b'): |
| 178 | for y in ('d:', 'd:x/y', 'd:/', 'd:/x/y', |
| 179 | '//machine/common', '//machine/common/', '//machine/common/x/y'): |
| 180 | tester("ntpath.join(%r, %r)" % (x, y), y) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 181 | |
| 182 | tester("ntpath.join('\\\\computer\\share\\', 'a', 'b')", '\\\\computer\\share\\a\\b') |
| 183 | tester("ntpath.join('\\\\computer\\share', 'a', 'b')", '\\\\computer\\share\\a\\b') |
| 184 | tester("ntpath.join('\\\\computer\\share', 'a\\b')", '\\\\computer\\share\\a\\b') |
| 185 | tester("ntpath.join('//computer/share/', 'a', 'b')", '//computer/share/a\\b') |
| 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 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 189 | def test_normpath(self): |
| 190 | tester("ntpath.normpath('A//////././//.//B')", r'A\B') |
| 191 | tester("ntpath.normpath('A/./B')", r'A\B') |
| 192 | tester("ntpath.normpath('A/foo/../B')", r'A\B') |
| 193 | tester("ntpath.normpath('C:A//B')", r'C:A\B') |
| 194 | tester("ntpath.normpath('D:A/./B')", r'D:A\B') |
| 195 | tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B') |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 196 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 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') |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 200 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 201 | tester("ntpath.normpath('..')", r'..') |
| 202 | tester("ntpath.normpath('.')", r'.') |
| 203 | tester("ntpath.normpath('')", r'.') |
| 204 | tester("ntpath.normpath('/')", '\\') |
| 205 | tester("ntpath.normpath('c:/')", 'c:\\') |
| 206 | tester("ntpath.normpath('/../.././..')", '\\') |
| 207 | tester("ntpath.normpath('c:/../../..')", 'c:\\') |
| 208 | tester("ntpath.normpath('../.././..')", r'..\..\..') |
| 209 | tester("ntpath.normpath('K:../.././..')", r'K:..\..\..') |
| 210 | tester("ntpath.normpath('C:////a/b')", r'C:\a\b') |
| 211 | tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b') |
Fred Drake | 5e99731 | 2002-01-15 03:46:43 +0000 | [diff] [blame] | 212 | |
Georg Brandl | cfb6821 | 2010-07-31 21:40:15 +0000 | [diff] [blame] | 213 | tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL') |
| 214 | tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z') |
| 215 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 216 | def test_expandvars(self): |
Walter Dörwald | 155374d | 2009-05-01 19:58:58 +0000 | [diff] [blame] | 217 | with support.EnvironmentVarGuard() as env: |
| 218 | env.clear() |
| 219 | env["foo"] = "bar" |
| 220 | env["{foo"] = "baz1" |
| 221 | env["{foo}"] = "baz2" |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 222 | tester('ntpath.expandvars("foo")', "foo") |
| 223 | tester('ntpath.expandvars("$foo bar")', "bar bar") |
| 224 | tester('ntpath.expandvars("${foo}bar")', "barbar") |
| 225 | tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar") |
| 226 | tester('ntpath.expandvars("$bar bar")', "$bar bar") |
| 227 | tester('ntpath.expandvars("$?bar")', "$?bar") |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 228 | tester('ntpath.expandvars("$foo}bar")', "bar}bar") |
| 229 | tester('ntpath.expandvars("${foo")', "${foo") |
| 230 | tester('ntpath.expandvars("${{foo}}")', "baz1}") |
| 231 | tester('ntpath.expandvars("$foo$foo")', "barbar") |
| 232 | tester('ntpath.expandvars("$bar$bar")', "$bar$bar") |
| 233 | tester('ntpath.expandvars("%foo% bar")', "bar bar") |
| 234 | tester('ntpath.expandvars("%foo%bar")', "barbar") |
| 235 | tester('ntpath.expandvars("%foo%%foo%")', "barbar") |
| 236 | tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar") |
| 237 | tester('ntpath.expandvars("%?bar%")', "%?bar%") |
| 238 | tester('ntpath.expandvars("%foo%%bar")', "bar%bar") |
| 239 | tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar") |
Serhiy Storchaka | 1b87ae0 | 2015-03-25 16:40:15 +0200 | [diff] [blame] | 240 | tester('ntpath.expandvars("bar\'%foo%")', "bar\'%foo%") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 241 | |
Serhiy Storchaka | dbb1019 | 2014-02-13 10:13:53 +0200 | [diff] [blame] | 242 | @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII') |
| 243 | def test_expandvars_nonascii(self): |
| 244 | def check(value, expected): |
| 245 | tester('ntpath.expandvars(%r)' % value, expected) |
| 246 | with support.EnvironmentVarGuard() as env: |
| 247 | env.clear() |
| 248 | nonascii = support.FS_NONASCII |
| 249 | env['spam'] = nonascii |
| 250 | env[nonascii] = 'ham' + nonascii |
| 251 | check('$spam bar', '%s bar' % nonascii) |
| 252 | check('$%s bar' % nonascii, '$%s bar' % nonascii) |
| 253 | check('${spam}bar', '%sbar' % nonascii) |
| 254 | check('${%s}bar' % nonascii, 'ham%sbar' % nonascii) |
| 255 | check('$spam}bar', '%s}bar' % nonascii) |
| 256 | check('$%s}bar' % nonascii, '$%s}bar' % nonascii) |
| 257 | check('%spam% bar', '%s bar' % nonascii) |
| 258 | check('%{}% bar'.format(nonascii), 'ham%s bar' % nonascii) |
| 259 | check('%spam%bar', '%sbar' % nonascii) |
| 260 | check('%{}%bar'.format(nonascii), 'ham%sbar' % nonascii) |
| 261 | |
Serhiy Storchaka | ffc1e6d | 2014-05-28 18:11:29 +0300 | [diff] [blame] | 262 | def test_expanduser(self): |
| 263 | tester('ntpath.expanduser("test")', 'test') |
| 264 | |
| 265 | with support.EnvironmentVarGuard() as env: |
| 266 | env.clear() |
| 267 | tester('ntpath.expanduser("~test")', '~test') |
| 268 | |
| 269 | env['HOMEPATH'] = 'eric\\idle' |
| 270 | env['HOMEDRIVE'] = 'C:\\' |
| 271 | tester('ntpath.expanduser("~test")', 'C:\\eric\\test') |
| 272 | tester('ntpath.expanduser("~")', 'C:\\eric\\idle') |
| 273 | |
| 274 | del env['HOMEDRIVE'] |
| 275 | tester('ntpath.expanduser("~test")', 'eric\\test') |
| 276 | tester('ntpath.expanduser("~")', 'eric\\idle') |
| 277 | |
| 278 | env.clear() |
| 279 | env['USERPROFILE'] = 'C:\\eric\\idle' |
| 280 | tester('ntpath.expanduser("~test")', 'C:\\eric\\test') |
| 281 | tester('ntpath.expanduser("~")', 'C:\\eric\\idle') |
| 282 | |
| 283 | env.clear() |
| 284 | env['HOME'] = 'C:\\idle\\eric' |
| 285 | tester('ntpath.expanduser("~test")', 'C:\\idle\\test') |
| 286 | tester('ntpath.expanduser("~")', 'C:\\idle\\eric') |
| 287 | |
| 288 | tester('ntpath.expanduser("~test\\foo\\bar")', |
| 289 | 'C:\\idle\\test\\foo\\bar') |
| 290 | tester('ntpath.expanduser("~test/foo/bar")', |
| 291 | 'C:\\idle\\test/foo/bar') |
| 292 | tester('ntpath.expanduser("~\\foo\\bar")', |
| 293 | 'C:\\idle\\eric\\foo\\bar') |
| 294 | tester('ntpath.expanduser("~/foo/bar")', |
| 295 | 'C:\\idle\\eric/foo/bar') |
| 296 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 297 | def test_abspath(self): |
| 298 | # ntpath.abspath() can only be used on a system with the "nt" module |
| 299 | # (reasonably), so we protect this test with "import nt". This allows |
| 300 | # the rest of the tests for the ntpath module to be run to completion |
| 301 | # on any platform, since most of the module is intended to be usable |
| 302 | # from any platform. |
| 303 | try: |
| 304 | import nt |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 305 | tester('ntpath.abspath("C:\\")', "C:\\") |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 306 | except ImportError: |
Zachary Ware | 9fe6d86 | 2013-12-08 00:20:35 -0600 | [diff] [blame] | 307 | self.skipTest('nt module not available') |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 308 | |
| 309 | def test_relpath(self): |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 310 | tester('ntpath.relpath("a")', 'a') |
| 311 | tester('ntpath.relpath(os.path.abspath("a"))', 'a') |
| 312 | tester('ntpath.relpath("a/b")', 'a\\b') |
| 313 | tester('ntpath.relpath("../a/b")', '..\\a\\b') |
Serhiy Storchaka | 5106d04 | 2015-01-26 10:26:14 +0200 | [diff] [blame] | 314 | with support.temp_cwd(support.TESTFN) as cwd_dir: |
| 315 | currentdir = os.path.basename(cwd_dir) |
| 316 | tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a') |
| 317 | tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b') |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 318 | tester('ntpath.relpath("a", "b/c")', '..\\..\\a') |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 319 | 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] | 320 | tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a') |
| 321 | tester('ntpath.relpath("a", "a")', '.') |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 322 | tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat') |
| 323 | tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat') |
| 324 | tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat') |
| 325 | tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..') |
| 326 | tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat') |
| 327 | tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x') |
| 328 | tester('ntpath.relpath("/", "/")', '.') |
| 329 | tester('ntpath.relpath("/a", "/a")', '.') |
| 330 | tester('ntpath.relpath("/a/b", "/a/b")', '.') |
Hirokazu Yamamoto | b08820a | 2010-10-18 12:13:18 +0000 | [diff] [blame] | 331 | tester('ntpath.relpath("c:/foo", "C:/FOO")', '.') |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 332 | |
Serhiy Storchaka | 3822093 | 2015-03-31 15:31:53 +0300 | [diff] [blame] | 333 | def test_commonpath(self): |
| 334 | def check(paths, expected): |
| 335 | tester(('ntpath.commonpath(%r)' % paths).replace('\\\\', '\\'), |
| 336 | expected) |
| 337 | def check_error(exc, paths): |
| 338 | self.assertRaises(exc, ntpath.commonpath, paths) |
| 339 | self.assertRaises(exc, ntpath.commonpath, |
| 340 | [os.fsencode(p) for p in paths]) |
| 341 | |
| 342 | self.assertRaises(ValueError, ntpath.commonpath, []) |
| 343 | check_error(ValueError, ['C:\\Program Files', 'Program Files']) |
| 344 | check_error(ValueError, ['C:\\Program Files', 'C:Program Files']) |
| 345 | check_error(ValueError, ['\\Program Files', 'Program Files']) |
| 346 | check_error(ValueError, ['Program Files', 'C:\\Program Files']) |
| 347 | check(['C:\\Program Files'], 'C:\\Program Files') |
| 348 | check(['C:\\Program Files', 'C:\\Program Files'], 'C:\\Program Files') |
| 349 | check(['C:\\Program Files\\', 'C:\\Program Files'], |
| 350 | 'C:\\Program Files') |
| 351 | check(['C:\\Program Files\\', 'C:\\Program Files\\'], |
| 352 | 'C:\\Program Files') |
| 353 | check(['C:\\\\Program Files', 'C:\\Program Files\\\\'], |
| 354 | 'C:\\Program Files') |
| 355 | check(['C:\\.\\Program Files', 'C:\\Program Files\\.'], |
| 356 | 'C:\\Program Files') |
| 357 | check(['C:\\', 'C:\\bin'], 'C:\\') |
| 358 | check(['C:\\Program Files', 'C:\\bin'], 'C:\\') |
| 359 | check(['C:\\Program Files', 'C:\\Program Files\\Bar'], |
| 360 | 'C:\\Program Files') |
| 361 | check(['C:\\Program Files\\Foo', 'C:\\Program Files\\Bar'], |
| 362 | 'C:\\Program Files') |
| 363 | check(['C:\\Program Files', 'C:\\Projects'], 'C:\\') |
| 364 | check(['C:\\Program Files\\', 'C:\\Projects'], 'C:\\') |
| 365 | |
| 366 | check(['C:\\Program Files\\Foo', 'C:/Program Files/Bar'], |
| 367 | 'C:\\Program Files') |
| 368 | check(['C:\\Program Files\\Foo', 'c:/program files/bar'], |
| 369 | 'C:\\Program Files') |
| 370 | check(['c:/program files/bar', 'C:\\Program Files\\Foo'], |
| 371 | 'c:\\program files') |
| 372 | |
| 373 | check_error(ValueError, ['C:\\Program Files', 'D:\\Program Files']) |
| 374 | |
| 375 | check(['spam'], 'spam') |
| 376 | check(['spam', 'spam'], 'spam') |
| 377 | check(['spam', 'alot'], '') |
| 378 | check(['and\\jam', 'and\\spam'], 'and') |
| 379 | check(['and\\\\jam', 'and\\spam\\\\'], 'and') |
| 380 | check(['and\\.\\jam', '.\\and\\spam'], 'and') |
| 381 | check(['and\\jam', 'and\\spam', 'alot'], '') |
| 382 | check(['and\\jam', 'and\\spam', 'and'], 'and') |
| 383 | check(['C:and\\jam', 'C:and\\spam'], 'C:and') |
| 384 | |
| 385 | check([''], '') |
| 386 | check(['', 'spam\\alot'], '') |
| 387 | check_error(ValueError, ['', '\\spam\\alot']) |
| 388 | |
| 389 | self.assertRaises(TypeError, ntpath.commonpath, |
| 390 | [b'C:\\Program Files', 'C:\\Program Files\\Foo']) |
| 391 | self.assertRaises(TypeError, ntpath.commonpath, |
| 392 | [b'C:\\Program Files', 'Program Files\\Foo']) |
| 393 | self.assertRaises(TypeError, ntpath.commonpath, |
| 394 | [b'Program Files', 'C:\\Program Files\\Foo']) |
| 395 | self.assertRaises(TypeError, ntpath.commonpath, |
| 396 | ['C:\\Program Files', b'C:\\Program Files\\Foo']) |
| 397 | self.assertRaises(TypeError, ntpath.commonpath, |
| 398 | ['C:\\Program Files', b'Program Files\\Foo']) |
| 399 | self.assertRaises(TypeError, ntpath.commonpath, |
| 400 | ['Program Files', b'C:\\Program Files\\Foo']) |
| 401 | |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 402 | def test_sameopenfile(self): |
| 403 | with TemporaryFile() as tf1, TemporaryFile() as tf2: |
| 404 | # Make sure the same file is really the same |
| 405 | self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno())) |
| 406 | # Make sure different files are really different |
| 407 | self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno())) |
Brian Curtin | 13a0db5 | 2010-09-06 19:46:17 +0000 | [diff] [blame] | 408 | # Make sure invalid values don't cause issues on win32 |
| 409 | if sys.platform == "win32": |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 410 | with self.assertRaises(OSError): |
Brian Curtin | 13a0db5 | 2010-09-06 19:46:17 +0000 | [diff] [blame] | 411 | # Invalid file descriptors shouldn't display assert |
| 412 | # dialogs (#4804) |
| 413 | ntpath.sameopenfile(-1, -1) |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 414 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 415 | def test_ismount(self): |
| 416 | self.assertTrue(ntpath.ismount("c:\\")) |
| 417 | self.assertTrue(ntpath.ismount("C:\\")) |
| 418 | self.assertTrue(ntpath.ismount("c:/")) |
| 419 | self.assertTrue(ntpath.ismount("C:/")) |
| 420 | self.assertTrue(ntpath.ismount("\\\\.\\c:\\")) |
| 421 | self.assertTrue(ntpath.ismount("\\\\.\\C:\\")) |
| 422 | |
| 423 | self.assertTrue(ntpath.ismount(b"c:\\")) |
| 424 | self.assertTrue(ntpath.ismount(b"C:\\")) |
| 425 | self.assertTrue(ntpath.ismount(b"c:/")) |
| 426 | self.assertTrue(ntpath.ismount(b"C:/")) |
| 427 | self.assertTrue(ntpath.ismount(b"\\\\.\\c:\\")) |
| 428 | self.assertTrue(ntpath.ismount(b"\\\\.\\C:\\")) |
| 429 | |
| 430 | with support.temp_dir() as d: |
| 431 | self.assertFalse(ntpath.ismount(d)) |
| 432 | |
Tim Golden | b2fcebb | 2013-08-01 13:58:58 +0100 | [diff] [blame] | 433 | if sys.platform == "win32": |
| 434 | # |
| 435 | # Make sure the current folder isn't the root folder |
| 436 | # (or any other volume root). The drive-relative |
| 437 | # locations below cannot then refer to mount points |
| 438 | # |
| 439 | drive, path = ntpath.splitdrive(sys.executable) |
| 440 | with support.change_cwd(os.path.dirname(sys.executable)): |
| 441 | self.assertFalse(ntpath.ismount(drive.lower())) |
| 442 | self.assertFalse(ntpath.ismount(drive.upper())) |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 443 | |
Tim Golden | b2fcebb | 2013-08-01 13:58:58 +0100 | [diff] [blame] | 444 | self.assertTrue(ntpath.ismount("\\\\localhost\\c$")) |
| 445 | self.assertTrue(ntpath.ismount("\\\\localhost\\c$\\")) |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 446 | |
Tim Golden | b2fcebb | 2013-08-01 13:58:58 +0100 | [diff] [blame] | 447 | self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$")) |
| 448 | self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$\\")) |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 449 | |
Ezio Melotti | d0dfe9a | 2013-01-10 03:12:50 +0200 | [diff] [blame] | 450 | class NtCommonTest(test_genericpath.CommonTest, unittest.TestCase): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 451 | pathmodule = ntpath |
| 452 | attributes = ['relpath', 'splitunc'] |
| 453 | |
| 454 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 455 | if __name__ == "__main__": |
| 456 | unittest.main() |