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 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test.support import verbose, TestFailed |
| 4 | import test.support as support |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 5 | import unittest |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 7 | |
| 8 | def tester(fn, wantResult): |
Eric S. Raymond | fc170b1 | 2001-02-09 11:51:27 +0000 | [diff] [blame] | 9 | fn = fn.replace("\\", "\\\\") |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 10 | gotResult = eval(fn) |
| 11 | if wantResult != gotResult: |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 12 | raise TestFailed("%s should return: %s but returned: %s" \ |
| 13 | %(str(fn), str(wantResult), str(gotResult))) |
Tim Peters | 6578dc9 | 2002-12-24 18:31:27 +0000 | [diff] [blame] | 14 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 15 | # then with bytes |
| 16 | fn = fn.replace("('", "(b'") |
| 17 | fn = fn.replace('("', '(b"') |
| 18 | fn = fn.replace("['", "[b'") |
| 19 | fn = fn.replace('["', '[b"') |
| 20 | fn = fn.replace(", '", ", b'") |
| 21 | fn = fn.replace(', "', ', b"') |
| 22 | gotResult = eval(fn) |
| 23 | if isinstance(wantResult, str): |
| 24 | wantResult = wantResult.encode('ascii') |
| 25 | elif isinstance(wantResult, tuple): |
| 26 | wantResult = tuple(r.encode('ascii') for r in wantResult) |
| 27 | |
| 28 | gotResult = eval(fn) |
| 29 | if wantResult != gotResult: |
| 30 | raise TestFailed("%s should return: %s but returned: %s" \ |
| 31 | %(str(fn), str(wantResult), repr(gotResult))) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 32 | |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 33 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 34 | class TestNtpath(unittest.TestCase): |
| 35 | def test_splitext(self): |
| 36 | tester('ntpath.splitext("foo.ext")', ('foo', '.ext')) |
| 37 | tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext')) |
| 38 | tester('ntpath.splitext(".ext")', ('.ext', '')) |
| 39 | tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', '')) |
| 40 | tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', '')) |
| 41 | tester('ntpath.splitext("")', ('', '')) |
| 42 | tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext')) |
| 43 | tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext')) |
| 44 | tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext')) |
| 45 | 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] | 46 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 47 | def test_splitdrive(self): |
| 48 | tester('ntpath.splitdrive("c:\\foo\\bar")', |
| 49 | ('c:', '\\foo\\bar')) |
| 50 | tester('ntpath.splitdrive("c:/foo/bar")', |
| 51 | ('c:', '/foo/bar')) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 52 | tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")', |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 53 | ('\\\\conky\\mountpoint', '\\foo\\bar')) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 54 | tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")', |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 55 | ('//conky/mountpoint', '/foo/bar')) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 56 | tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")', |
| 57 | ('', '\\\\\\conky\\mountpoint\\foo\\bar')) |
| 58 | tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")', |
| 59 | ('', '///conky/mountpoint/foo/bar')) |
| 60 | tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")', |
| 61 | ('', '\\\\conky\\\\mountpoint\\foo\\bar')) |
| 62 | tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")', |
| 63 | ('', '//conky//mountpoint/foo/bar')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 64 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 65 | def test_split(self): |
| 66 | tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar')) |
| 67 | tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")', |
| 68 | ('\\\\conky\\mountpoint\\foo', 'bar')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 69 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 70 | tester('ntpath.split("c:\\")', ('c:\\', '')) |
| 71 | tester('ntpath.split("\\\\conky\\mountpoint\\")', |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 72 | ('\\\\conky\\mountpoint\\', '')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 73 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 74 | tester('ntpath.split("c:/")', ('c:/', '')) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 75 | tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', '')) |
Mark Hammond | 673c6cf | 2000-08-14 06:21:26 +0000 | [diff] [blame] | 76 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 77 | def test_isabs(self): |
| 78 | tester('ntpath.isabs("c:\\")', 1) |
| 79 | tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1) |
| 80 | tester('ntpath.isabs("\\foo")', 1) |
| 81 | tester('ntpath.isabs("\\foo\\bar")', 1) |
Tim Peters | d4f7f60 | 2001-07-19 19:11:41 +0000 | [diff] [blame] | 82 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 83 | def test_commonprefix(self): |
| 84 | tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])', |
| 85 | "/home/swen") |
| 86 | tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])', |
| 87 | "\\home\\swen\\") |
| 88 | tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])', |
| 89 | "/home/swen/spam") |
Tim Peters | 6a3e5f1 | 2001-11-05 21:25:02 +0000 | [diff] [blame] | 90 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 91 | def test_join(self): |
| 92 | tester('ntpath.join("")', '') |
| 93 | tester('ntpath.join("", "", "")', '') |
| 94 | tester('ntpath.join("a")', 'a') |
| 95 | tester('ntpath.join("/a")', '/a') |
| 96 | tester('ntpath.join("\\a")', '\\a') |
| 97 | tester('ntpath.join("a:")', 'a:') |
| 98 | tester('ntpath.join("a:", "b")', 'a:b') |
| 99 | tester('ntpath.join("a:", "/b")', 'a:/b') |
| 100 | tester('ntpath.join("a:", "\\b")', 'a:\\b') |
| 101 | tester('ntpath.join("a", "/b")', '/b') |
| 102 | tester('ntpath.join("a", "\\b")', '\\b') |
| 103 | tester('ntpath.join("a", "b", "c")', 'a\\b\\c') |
| 104 | tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c') |
| 105 | tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c') |
| 106 | tester('ntpath.join("a", "b", "\\c")', '\\c') |
| 107 | tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep') |
| 108 | tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b') |
| 109 | tester("ntpath.join('c:', '/a')", 'c:/a') |
| 110 | tester("ntpath.join('c:/', '/a')", 'c:/a') |
| 111 | tester("ntpath.join('c:/a', '/b')", '/b') |
| 112 | tester("ntpath.join('c:', 'd:/')", 'd:/') |
| 113 | tester("ntpath.join('c:/', 'd:/')", 'd:/') |
| 114 | tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b') |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 115 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 116 | tester("ntpath.join('')", '') |
| 117 | tester("ntpath.join('', '', '', '', '')", '') |
| 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\\') |
| 123 | tester("ntpath.join('a\\', '')", 'a\\') |
| 124 | tester("ntpath.join('a\\', '', '', '', '')", 'a\\') |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 125 | |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 126 | # from comment in ntpath.join |
| 127 | tester("ntpath.join('c:', '/a')", 'c:/a') |
| 128 | tester("ntpath.join('//computer/share', '/a')", '//computer/share/a') |
| 129 | tester("ntpath.join('c:/', '/a')", 'c:/a') |
| 130 | tester("ntpath.join('//computer/share/', '/a')", '//computer/share/a') |
| 131 | tester("ntpath.join('c:/a', '/b')", '/b') |
| 132 | tester("ntpath.join('//computer/share/a', '/b')", '/b') |
| 133 | tester("ntpath.join('c:', 'd:/')", 'd:/') |
| 134 | tester("ntpath.join('c:', '//computer/share/')", '//computer/share/') |
| 135 | tester("ntpath.join('//computer/share', 'd:/')", 'd:/') |
| 136 | tester("ntpath.join('//computer/share', '//computer/share/')", '//computer/share/') |
| 137 | tester("ntpath.join('c:/', 'd:/')", 'd:/') |
| 138 | tester("ntpath.join('c:/', '//computer/share/')", '//computer/share/') |
| 139 | tester("ntpath.join('//computer/share/', 'd:/')", 'd:/') |
| 140 | tester("ntpath.join('//computer/share/', '//computer/share/')", '//computer/share/') |
| 141 | |
| 142 | tester("ntpath.join('c:', '//computer/share/')", '//computer/share/') |
| 143 | tester("ntpath.join('c:/', '//computer/share/')", '//computer/share/') |
| 144 | tester("ntpath.join('c:/', '//computer/share/a/b')", '//computer/share/a/b') |
| 145 | |
| 146 | tester("ntpath.join('\\\\computer\\share\\', 'a', 'b')", '\\\\computer\\share\\a\\b') |
| 147 | tester("ntpath.join('\\\\computer\\share', 'a', 'b')", '\\\\computer\\share\\a\\b') |
| 148 | tester("ntpath.join('\\\\computer\\share', 'a\\b')", '\\\\computer\\share\\a\\b') |
| 149 | tester("ntpath.join('//computer/share/', 'a', 'b')", '//computer/share/a\\b') |
| 150 | tester("ntpath.join('//computer/share', 'a', 'b')", '//computer/share\\a\\b') |
| 151 | tester("ntpath.join('//computer/share', 'a/b')", '//computer/share\\a/b') |
| 152 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 153 | def test_normpath(self): |
| 154 | tester("ntpath.normpath('A//////././//.//B')", r'A\B') |
| 155 | tester("ntpath.normpath('A/./B')", r'A\B') |
| 156 | tester("ntpath.normpath('A/foo/../B')", r'A\B') |
| 157 | tester("ntpath.normpath('C:A//B')", r'C:A\B') |
| 158 | tester("ntpath.normpath('D:A/./B')", r'D:A\B') |
| 159 | tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B') |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 160 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 161 | tester("ntpath.normpath('C:///A//B')", r'C:\A\B') |
| 162 | tester("ntpath.normpath('D:///A/./B')", r'D:\A\B') |
| 163 | tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B') |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 164 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 165 | tester("ntpath.normpath('..')", r'..') |
| 166 | tester("ntpath.normpath('.')", r'.') |
| 167 | tester("ntpath.normpath('')", r'.') |
| 168 | tester("ntpath.normpath('/')", '\\') |
| 169 | tester("ntpath.normpath('c:/')", 'c:\\') |
| 170 | tester("ntpath.normpath('/../.././..')", '\\') |
| 171 | tester("ntpath.normpath('c:/../../..')", 'c:\\') |
| 172 | tester("ntpath.normpath('../.././..')", r'..\..\..') |
| 173 | tester("ntpath.normpath('K:../.././..')", r'K:..\..\..') |
| 174 | tester("ntpath.normpath('C:////a/b')", r'C:\a\b') |
| 175 | tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b') |
Fred Drake | 5e99731 | 2002-01-15 03:46:43 +0000 | [diff] [blame] | 176 | |
Georg Brandl | 611f8f5 | 2010-08-01 19:17:57 +0000 | [diff] [blame] | 177 | tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL') |
| 178 | tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z') |
| 179 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 180 | def test_expandvars(self): |
Walter Dörwald | 155374d | 2009-05-01 19:58:58 +0000 | [diff] [blame] | 181 | with support.EnvironmentVarGuard() as env: |
| 182 | env.clear() |
| 183 | env["foo"] = "bar" |
| 184 | env["{foo"] = "baz1" |
| 185 | env["{foo}"] = "baz2" |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 186 | tester('ntpath.expandvars("foo")', "foo") |
| 187 | tester('ntpath.expandvars("$foo bar")', "bar bar") |
| 188 | tester('ntpath.expandvars("${foo}bar")', "barbar") |
| 189 | tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar") |
| 190 | tester('ntpath.expandvars("$bar bar")', "$bar bar") |
| 191 | tester('ntpath.expandvars("$?bar")', "$?bar") |
| 192 | tester('ntpath.expandvars("${foo}bar")', "barbar") |
| 193 | tester('ntpath.expandvars("$foo}bar")', "bar}bar") |
| 194 | tester('ntpath.expandvars("${foo")', "${foo") |
| 195 | tester('ntpath.expandvars("${{foo}}")', "baz1}") |
| 196 | tester('ntpath.expandvars("$foo$foo")', "barbar") |
| 197 | tester('ntpath.expandvars("$bar$bar")', "$bar$bar") |
| 198 | tester('ntpath.expandvars("%foo% bar")', "bar bar") |
| 199 | tester('ntpath.expandvars("%foo%bar")', "barbar") |
| 200 | tester('ntpath.expandvars("%foo%%foo%")', "barbar") |
| 201 | tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar") |
| 202 | tester('ntpath.expandvars("%?bar%")', "%?bar%") |
| 203 | tester('ntpath.expandvars("%foo%%bar")', "bar%bar") |
| 204 | tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 205 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 206 | def test_abspath(self): |
| 207 | # ntpath.abspath() can only be used on a system with the "nt" module |
| 208 | # (reasonably), so we protect this test with "import nt". This allows |
| 209 | # the rest of the tests for the ntpath module to be run to completion |
| 210 | # on any platform, since most of the module is intended to be usable |
| 211 | # from any platform. |
| 212 | try: |
| 213 | import nt |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 214 | tester('ntpath.abspath("C:\\")', "C:\\") |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 215 | except ImportError: |
| 216 | pass |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 217 | |
| 218 | def test_relpath(self): |
| 219 | currentdir = os.path.split(os.getcwd())[-1] |
| 220 | tester('ntpath.relpath("a")', 'a') |
| 221 | tester('ntpath.relpath(os.path.abspath("a"))', 'a') |
| 222 | tester('ntpath.relpath("a/b")', 'a\\b') |
| 223 | tester('ntpath.relpath("../a/b")', '..\\a\\b') |
| 224 | tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a') |
| 225 | tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b') |
| 226 | tester('ntpath.relpath("a", "b/c")', '..\\..\\a') |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 227 | 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] | 228 | tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a') |
| 229 | tester('ntpath.relpath("a", "a")', '.') |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 230 | tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat') |
| 231 | tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat') |
| 232 | tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat') |
| 233 | tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..') |
| 234 | tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat') |
| 235 | tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x') |
| 236 | tester('ntpath.relpath("/", "/")', '.') |
| 237 | tester('ntpath.relpath("/a", "/a")', '.') |
| 238 | tester('ntpath.relpath("/a/b", "/a/b")', '.') |
Hirokazu Yamamoto | 089144e | 2010-10-18 13:49:09 +0000 | [diff] [blame] | 239 | tester('ntpath.relpath("c:/foo", "C:/FOO")', '.') |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 240 | |
| 241 | |
| 242 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 243 | support.run_unittest(TestNtpath) |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 244 | |
| 245 | |
| 246 | if __name__ == "__main__": |
| 247 | unittest.main() |