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 | |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 15 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 16 | class TestNtpath(unittest.TestCase): |
| 17 | def test_splitext(self): |
| 18 | tester('ntpath.splitext("foo.ext")', ('foo', '.ext')) |
| 19 | tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext')) |
| 20 | tester('ntpath.splitext(".ext")', ('.ext', '')) |
| 21 | tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', '')) |
| 22 | tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', '')) |
| 23 | tester('ntpath.splitext("")', ('', '')) |
| 24 | tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext')) |
| 25 | tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext')) |
| 26 | tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext')) |
| 27 | 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] | 28 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 29 | def test_splitdrive(self): |
| 30 | tester('ntpath.splitdrive("c:\\foo\\bar")', |
| 31 | ('c:', '\\foo\\bar')) |
| 32 | tester('ntpath.splitdrive("c:/foo/bar")', |
| 33 | ('c:', '/foo/bar')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 34 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 35 | def test_splitunc(self): |
| 36 | tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")', |
| 37 | ('\\\\conky\\mountpoint', '\\foo\\bar')) |
| 38 | tester('ntpath.splitunc("//conky/mountpoint/foo/bar")', |
| 39 | ('//conky/mountpoint', '/foo/bar')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 40 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 41 | def test_split(self): |
| 42 | tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar')) |
| 43 | tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")', |
| 44 | ('\\\\conky\\mountpoint\\foo', 'bar')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 45 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 46 | tester('ntpath.split("c:\\")', ('c:\\', '')) |
| 47 | tester('ntpath.split("\\\\conky\\mountpoint\\")', |
| 48 | ('\\\\conky\\mountpoint', '')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 49 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 50 | tester('ntpath.split("c:/")', ('c:/', '')) |
| 51 | tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint', '')) |
Mark Hammond | 673c6cf | 2000-08-14 06:21:26 +0000 | [diff] [blame] | 52 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 53 | def test_isabs(self): |
| 54 | tester('ntpath.isabs("c:\\")', 1) |
| 55 | tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1) |
| 56 | tester('ntpath.isabs("\\foo")', 1) |
| 57 | tester('ntpath.isabs("\\foo\\bar")', 1) |
Tim Peters | d4f7f60 | 2001-07-19 19:11:41 +0000 | [diff] [blame] | 58 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 59 | def test_commonprefix(self): |
| 60 | tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])', |
| 61 | "/home/swen") |
| 62 | tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])', |
| 63 | "\\home\\swen\\") |
| 64 | tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])', |
| 65 | "/home/swen/spam") |
Tim Peters | 6a3e5f1 | 2001-11-05 21:25:02 +0000 | [diff] [blame] | 66 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 67 | def test_join(self): |
| 68 | tester('ntpath.join("")', '') |
| 69 | tester('ntpath.join("", "", "")', '') |
| 70 | tester('ntpath.join("a")', 'a') |
| 71 | tester('ntpath.join("/a")', '/a') |
| 72 | tester('ntpath.join("\\a")', '\\a') |
| 73 | tester('ntpath.join("a:")', 'a:') |
| 74 | tester('ntpath.join("a:", "b")', 'a:b') |
| 75 | tester('ntpath.join("a:", "/b")', 'a:/b') |
| 76 | tester('ntpath.join("a:", "\\b")', 'a:\\b') |
| 77 | tester('ntpath.join("a", "/b")', '/b') |
| 78 | tester('ntpath.join("a", "\\b")', '\\b') |
| 79 | tester('ntpath.join("a", "b", "c")', 'a\\b\\c') |
| 80 | tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c') |
| 81 | tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c') |
| 82 | tester('ntpath.join("a", "b", "\\c")', '\\c') |
| 83 | tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep') |
| 84 | tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b') |
| 85 | tester("ntpath.join('c:', '/a')", 'c:/a') |
| 86 | tester("ntpath.join('c:/', '/a')", 'c:/a') |
| 87 | tester("ntpath.join('c:/a', '/b')", '/b') |
| 88 | tester("ntpath.join('c:', 'd:/')", 'd:/') |
| 89 | tester("ntpath.join('c:/', 'd:/')", 'd:/') |
| 90 | tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b') |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 91 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 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', '', '', '', '')", 'a\\') |
| 99 | tester("ntpath.join('a\\', '')", 'a\\') |
| 100 | tester("ntpath.join('a\\', '', '', '', '')", 'a\\') |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 101 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 102 | def test_normpath(self): |
| 103 | tester("ntpath.normpath('A//////././//.//B')", r'A\B') |
| 104 | tester("ntpath.normpath('A/./B')", r'A\B') |
| 105 | tester("ntpath.normpath('A/foo/../B')", r'A\B') |
| 106 | tester("ntpath.normpath('C:A//B')", r'C:A\B') |
| 107 | tester("ntpath.normpath('D:A/./B')", r'D:A\B') |
| 108 | tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B') |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 109 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 110 | tester("ntpath.normpath('C:///A//B')", r'C:\A\B') |
| 111 | tester("ntpath.normpath('D:///A/./B')", r'D:\A\B') |
| 112 | tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B') |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 113 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 114 | tester("ntpath.normpath('..')", r'..') |
| 115 | tester("ntpath.normpath('.')", r'.') |
| 116 | tester("ntpath.normpath('')", r'.') |
| 117 | tester("ntpath.normpath('/')", '\\') |
| 118 | tester("ntpath.normpath('c:/')", 'c:\\') |
| 119 | tester("ntpath.normpath('/../.././..')", '\\') |
| 120 | tester("ntpath.normpath('c:/../../..')", 'c:\\') |
| 121 | tester("ntpath.normpath('../.././..')", r'..\..\..') |
| 122 | tester("ntpath.normpath('K:../.././..')", r'K:..\..\..') |
| 123 | tester("ntpath.normpath('C:////a/b')", r'C:\a\b') |
| 124 | tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b') |
Fred Drake | 5e99731 | 2002-01-15 03:46:43 +0000 | [diff] [blame] | 125 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 126 | def test_expandvars(self): |
| 127 | oldenv = os.environ.copy() |
| 128 | try: |
| 129 | os.environ.clear() |
| 130 | os.environ["foo"] = "bar" |
| 131 | os.environ["{foo"] = "baz1" |
| 132 | os.environ["{foo}"] = "baz2" |
| 133 | tester('ntpath.expandvars("foo")', "foo") |
| 134 | tester('ntpath.expandvars("$foo bar")', "bar bar") |
| 135 | tester('ntpath.expandvars("${foo}bar")', "barbar") |
| 136 | tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar") |
| 137 | tester('ntpath.expandvars("$bar bar")', "$bar bar") |
| 138 | tester('ntpath.expandvars("$?bar")', "$?bar") |
| 139 | tester('ntpath.expandvars("${foo}bar")', "barbar") |
| 140 | tester('ntpath.expandvars("$foo}bar")', "bar}bar") |
| 141 | tester('ntpath.expandvars("${foo")', "${foo") |
| 142 | tester('ntpath.expandvars("${{foo}}")', "baz1}") |
| 143 | tester('ntpath.expandvars("$foo$foo")', "barbar") |
| 144 | tester('ntpath.expandvars("$bar$bar")', "$bar$bar") |
| 145 | tester('ntpath.expandvars("%foo% bar")', "bar bar") |
| 146 | tester('ntpath.expandvars("%foo%bar")', "barbar") |
| 147 | tester('ntpath.expandvars("%foo%%foo%")', "barbar") |
| 148 | tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar") |
| 149 | tester('ntpath.expandvars("%?bar%")', "%?bar%") |
| 150 | tester('ntpath.expandvars("%foo%%bar")', "bar%bar") |
| 151 | tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar") |
| 152 | finally: |
| 153 | os.environ.clear() |
| 154 | os.environ.update(oldenv) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 155 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 156 | def test_abspath(self): |
| 157 | # ntpath.abspath() can only be used on a system with the "nt" module |
| 158 | # (reasonably), so we protect this test with "import nt". This allows |
| 159 | # the rest of the tests for the ntpath module to be run to completion |
| 160 | # on any platform, since most of the module is intended to be usable |
| 161 | # from any platform. |
| 162 | try: |
| 163 | import nt |
| 164 | except ImportError: |
| 165 | pass |
| 166 | else: |
| 167 | tester('ntpath.abspath("C:\\")', "C:\\") |
| 168 | |
| 169 | def test_relpath(self): |
| 170 | currentdir = os.path.split(os.getcwd())[-1] |
| 171 | tester('ntpath.relpath("a")', 'a') |
| 172 | tester('ntpath.relpath(os.path.abspath("a"))', 'a') |
| 173 | tester('ntpath.relpath("a/b")', 'a\\b') |
| 174 | tester('ntpath.relpath("../a/b")', '..\\a\\b') |
| 175 | tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a') |
| 176 | tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b') |
| 177 | tester('ntpath.relpath("a", "b/c")', '..\\..\\a') |
| 178 | tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a') |
| 179 | tester('ntpath.relpath("a", "a")', '.') |
| 180 | |
| 181 | |
| 182 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 183 | support.run_unittest(TestNtpath) |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 184 | |
| 185 | |
| 186 | if __name__ == "__main__": |
| 187 | unittest.main() |