Benjamin Peterson | 3bef935 | 2014-06-22 19:07:38 -0700 | [diff] [blame^] | 1 | # coding: utf-8 |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 2 | import ntpath |
Mark Hammond | 673c6cf | 2000-08-14 06:21:26 +0000 | [diff] [blame] | 3 | import os |
Serhiy Storchaka | 2ac9d31 | 2014-02-19 23:27:37 +0200 | [diff] [blame] | 4 | import sys |
Georg Brandl | a4f46e1 | 2010-02-07 17:03:15 +0000 | [diff] [blame] | 5 | from test.test_support import TestFailed |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 6 | from test import test_support, test_genericpath |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 7 | import unittest |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 8 | |
Serhiy Storchaka | 2ac9d31 | 2014-02-19 23:27:37 +0200 | [diff] [blame] | 9 | def tester0(fn, wantResult): |
| 10 | gotResult = eval(fn) |
| 11 | if wantResult != gotResult: |
| 12 | raise TestFailed, "%s should return: %r but returned: %r" \ |
| 13 | %(fn, wantResult, gotResult) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 14 | |
| 15 | def tester(fn, wantResult): |
Eric S. Raymond | fc170b1 | 2001-02-09 11:51:27 +0000 | [diff] [blame] | 16 | fn = fn.replace("\\", "\\\\") |
Serhiy Storchaka | 2ac9d31 | 2014-02-19 23:27:37 +0200 | [diff] [blame] | 17 | tester0(fn, wantResult) |
Tim Peters | 6578dc9 | 2002-12-24 18:31:27 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 19 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 20 | class TestNtpath(unittest.TestCase): |
| 21 | def test_splitext(self): |
| 22 | tester('ntpath.splitext("foo.ext")', ('foo', '.ext')) |
| 23 | tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext')) |
| 24 | tester('ntpath.splitext(".ext")', ('.ext', '')) |
| 25 | tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', '')) |
| 26 | tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', '')) |
| 27 | tester('ntpath.splitext("")', ('', '')) |
| 28 | tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext')) |
| 29 | tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext')) |
| 30 | tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext')) |
| 31 | 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] | 32 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 33 | def test_splitdrive(self): |
| 34 | tester('ntpath.splitdrive("c:\\foo\\bar")', |
| 35 | ('c:', '\\foo\\bar')) |
| 36 | tester('ntpath.splitdrive("c:/foo/bar")', |
| 37 | ('c:', '/foo/bar')) |
Benjamin Peterson | 3bef935 | 2014-06-22 19:07:38 -0700 | [diff] [blame^] | 38 | tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")', |
| 39 | ('\\\\conky\\mountpoint', '\\foo\\bar')) |
| 40 | tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")', |
| 41 | ('//conky/mountpoint', '/foo/bar')) |
| 42 | tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")', |
| 43 | ('', '\\\\\\conky\\mountpoint\\foo\\bar')) |
| 44 | tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")', |
| 45 | ('', '///conky/mountpoint/foo/bar')) |
| 46 | tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")', |
| 47 | ('', '\\\\conky\\\\mountpoint\\foo\\bar')) |
| 48 | tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")', |
| 49 | ('', '//conky//mountpoint/foo/bar')) |
| 50 | # Issue #19911: UNC part containing U+0130 |
| 51 | self.assertEqual(ntpath.splitdrive(u'//conky/MOUNTPOİNT/foo/bar'), |
| 52 | (u'//conky/MOUNTPOİNT', '/foo/bar')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 53 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 54 | def test_splitunc(self): |
Serhiy Storchaka | dd5a46c | 2013-12-16 15:15:29 +0200 | [diff] [blame] | 55 | tester('ntpath.splitunc("c:\\foo\\bar")', |
| 56 | ('', 'c:\\foo\\bar')) |
| 57 | tester('ntpath.splitunc("c:/foo/bar")', |
| 58 | ('', 'c:/foo/bar')) |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 59 | tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")', |
| 60 | ('\\\\conky\\mountpoint', '\\foo\\bar')) |
| 61 | tester('ntpath.splitunc("//conky/mountpoint/foo/bar")', |
| 62 | ('//conky/mountpoint', '/foo/bar')) |
Serhiy Storchaka | dd5a46c | 2013-12-16 15:15:29 +0200 | [diff] [blame] | 63 | tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")', |
| 64 | ('', '\\\\\\conky\\mountpoint\\foo\\bar')) |
| 65 | tester('ntpath.splitunc("///conky/mountpoint/foo/bar")', |
| 66 | ('', '///conky/mountpoint/foo/bar')) |
| 67 | tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")', |
| 68 | ('', '\\\\conky\\\\mountpoint\\foo\\bar')) |
| 69 | tester('ntpath.splitunc("//conky//mountpoint/foo/bar")', |
| 70 | ('', '//conky//mountpoint/foo/bar')) |
| 71 | self.assertEqual(ntpath.splitunc(u'//conky/MOUNTPO\u0130NT/foo/bar'), |
| 72 | (u'//conky/MOUNTPO\u0130NT', u'/foo/bar')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 73 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 74 | def test_split(self): |
| 75 | tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar')) |
| 76 | tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")', |
| 77 | ('\\\\conky\\mountpoint\\foo', 'bar')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 78 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 79 | tester('ntpath.split("c:\\")', ('c:\\', '')) |
| 80 | tester('ntpath.split("\\\\conky\\mountpoint\\")', |
Benjamin Peterson | 3bef935 | 2014-06-22 19:07:38 -0700 | [diff] [blame^] | 81 | ('\\\\conky\\mountpoint\\', '')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 82 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 83 | tester('ntpath.split("c:/")', ('c:/', '')) |
Benjamin Peterson | 3bef935 | 2014-06-22 19:07:38 -0700 | [diff] [blame^] | 84 | tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', '')) |
Mark Hammond | 673c6cf | 2000-08-14 06:21:26 +0000 | [diff] [blame] | 85 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 86 | def test_isabs(self): |
| 87 | tester('ntpath.isabs("c:\\")', 1) |
| 88 | tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1) |
| 89 | tester('ntpath.isabs("\\foo")', 1) |
| 90 | tester('ntpath.isabs("\\foo\\bar")', 1) |
Tim Peters | d4f7f60 | 2001-07-19 19:11:41 +0000 | [diff] [blame] | 91 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 92 | def test_commonprefix(self): |
| 93 | tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])', |
| 94 | "/home/swen") |
| 95 | tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])', |
| 96 | "\\home\\swen\\") |
| 97 | tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])', |
| 98 | "/home/swen/spam") |
Tim Peters | 6a3e5f1 | 2001-11-05 21:25:02 +0000 | [diff] [blame] | 99 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 100 | def test_join(self): |
| 101 | tester('ntpath.join("")', '') |
| 102 | tester('ntpath.join("", "", "")', '') |
| 103 | tester('ntpath.join("a")', 'a') |
| 104 | tester('ntpath.join("/a")', '/a') |
| 105 | tester('ntpath.join("\\a")', '\\a') |
| 106 | tester('ntpath.join("a:")', 'a:') |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 107 | tester('ntpath.join("a:", "\\b")', 'a:\\b') |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 108 | tester('ntpath.join("a", "\\b")', '\\b') |
| 109 | tester('ntpath.join("a", "b", "c")', 'a\\b\\c') |
| 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")', '\\c') |
| 113 | tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep') |
| 114 | tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b') |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 115 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 116 | tester("ntpath.join('', 'a')", 'a') |
| 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\\') |
Serhiy Storchaka | 31f5121 | 2014-01-27 23:14:51 +0200 | [diff] [blame] | 122 | tester("ntpath.join('a/', '')", 'a/') |
| 123 | |
| 124 | tester("ntpath.join('a/b', 'x/y')", 'a/b\\x/y') |
| 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('c:', 'x/y')", 'c:x/y') |
| 128 | tester("ntpath.join('c:a/b', 'x/y')", 'c:a/b\\x/y') |
| 129 | tester("ntpath.join('c:a/b/', 'x/y')", 'c:a/b/x/y') |
| 130 | tester("ntpath.join('c:/', 'x/y')", 'c:/x/y') |
| 131 | tester("ntpath.join('c:/a/b', 'x/y')", 'c:/a/b\\x/y') |
| 132 | tester("ntpath.join('c:/a/b/', 'x/y')", 'c:/a/b/x/y') |
Benjamin Peterson | 3bef935 | 2014-06-22 19:07:38 -0700 | [diff] [blame^] | 133 | tester("ntpath.join('//computer/share', 'x/y')", '//computer/share\\x/y') |
| 134 | tester("ntpath.join('//computer/share/', 'x/y')", '//computer/share/x/y') |
| 135 | tester("ntpath.join('//computer/share/a/b', 'x/y')", '//computer/share/a/b\\x/y') |
Serhiy Storchaka | 31f5121 | 2014-01-27 23:14:51 +0200 | [diff] [blame] | 136 | |
| 137 | tester("ntpath.join('a/b', '/x/y')", '/x/y') |
| 138 | tester("ntpath.join('/a/b', '/x/y')", '/x/y') |
| 139 | tester("ntpath.join('c:', '/x/y')", 'c:/x/y') |
| 140 | tester("ntpath.join('c:a/b', '/x/y')", 'c:/x/y') |
| 141 | tester("ntpath.join('c:/', '/x/y')", 'c:/x/y') |
| 142 | tester("ntpath.join('c:/a/b', '/x/y')", 'c:/x/y') |
Benjamin Peterson | 3bef935 | 2014-06-22 19:07:38 -0700 | [diff] [blame^] | 143 | tester("ntpath.join('//computer/share', '/x/y')", '//computer/share/x/y') |
| 144 | tester("ntpath.join('//computer/share/', '/x/y')", '//computer/share/x/y') |
| 145 | tester("ntpath.join('//computer/share/a', '/x/y')", '//computer/share/x/y') |
Serhiy Storchaka | 31f5121 | 2014-01-27 23:14:51 +0200 | [diff] [blame] | 146 | |
| 147 | tester("ntpath.join('c:', 'C:x/y')", 'C:x/y') |
| 148 | tester("ntpath.join('c:a/b', 'C:x/y')", 'C:a/b\\x/y') |
| 149 | tester("ntpath.join('c:/', 'C:x/y')", 'C:/x/y') |
| 150 | tester("ntpath.join('c:/a/b', 'C:x/y')", 'C:/a/b\\x/y') |
| 151 | |
| 152 | for x in ('', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b'): |
| 153 | for y in ('d:', 'd:x/y', 'd:/', 'd:/x/y'): |
| 154 | tester("ntpath.join(%r, %r)" % (x, y), y) |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 155 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 156 | def test_normpath(self): |
| 157 | tester("ntpath.normpath('A//////././//.//B')", r'A\B') |
| 158 | tester("ntpath.normpath('A/./B')", r'A\B') |
| 159 | tester("ntpath.normpath('A/foo/../B')", r'A\B') |
| 160 | tester("ntpath.normpath('C:A//B')", r'C:A\B') |
| 161 | tester("ntpath.normpath('D:A/./B')", r'D:A\B') |
| 162 | tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B') |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 163 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 164 | tester("ntpath.normpath('C:///A//B')", r'C:\A\B') |
| 165 | tester("ntpath.normpath('D:///A/./B')", r'D:\A\B') |
| 166 | tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B') |
Sjoerd Mullender | 33a0a06 | 2007-01-16 16:42:38 +0000 | [diff] [blame] | 167 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 168 | tester("ntpath.normpath('..')", r'..') |
| 169 | tester("ntpath.normpath('.')", r'.') |
| 170 | tester("ntpath.normpath('')", r'.') |
| 171 | tester("ntpath.normpath('/')", '\\') |
| 172 | tester("ntpath.normpath('c:/')", 'c:\\') |
| 173 | tester("ntpath.normpath('/../.././..')", '\\') |
| 174 | tester("ntpath.normpath('c:/../../..')", 'c:\\') |
| 175 | tester("ntpath.normpath('../.././..')", r'..\..\..') |
| 176 | tester("ntpath.normpath('K:../.././..')", r'K:..\..\..') |
| 177 | tester("ntpath.normpath('C:////a/b')", r'C:\a\b') |
| 178 | tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b') |
Fred Drake | 5e99731 | 2002-01-15 03:46:43 +0000 | [diff] [blame] | 179 | |
Georg Brandl | e277325 | 2010-08-01 19:14:56 +0000 | [diff] [blame] | 180 | tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL') |
| 181 | tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z') |
| 182 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 183 | def test_expandvars(self): |
Walter Dörwald | 6733bed | 2009-05-01 17:35:37 +0000 | [diff] [blame] | 184 | with test_support.EnvironmentVarGuard() as env: |
| 185 | env.clear() |
| 186 | env["foo"] = "bar" |
| 187 | env["{foo"] = "baz1" |
| 188 | env["{foo}"] = "baz2" |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 189 | tester('ntpath.expandvars("foo")', "foo") |
| 190 | tester('ntpath.expandvars("$foo bar")', "bar bar") |
| 191 | tester('ntpath.expandvars("${foo}bar")', "barbar") |
| 192 | tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar") |
| 193 | tester('ntpath.expandvars("$bar bar")', "$bar bar") |
| 194 | tester('ntpath.expandvars("$?bar")', "$?bar") |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 195 | tester('ntpath.expandvars("$foo}bar")', "bar}bar") |
| 196 | tester('ntpath.expandvars("${foo")', "${foo") |
| 197 | tester('ntpath.expandvars("${{foo}}")', "baz1}") |
| 198 | tester('ntpath.expandvars("$foo$foo")', "barbar") |
| 199 | tester('ntpath.expandvars("$bar$bar")', "$bar$bar") |
| 200 | tester('ntpath.expandvars("%foo% bar")', "bar bar") |
| 201 | tester('ntpath.expandvars("%foo%bar")', "barbar") |
| 202 | tester('ntpath.expandvars("%foo%%foo%")', "barbar") |
| 203 | tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar") |
| 204 | tester('ntpath.expandvars("%?bar%")', "%?bar%") |
| 205 | tester('ntpath.expandvars("%foo%%bar")', "bar%bar") |
| 206 | tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar") |
Collin Winter | 6f18774 | 2007-03-16 22:16:08 +0000 | [diff] [blame] | 207 | |
Serhiy Storchaka | 2ac9d31 | 2014-02-19 23:27:37 +0200 | [diff] [blame] | 208 | @unittest.skipUnless(test_support.FS_NONASCII, 'need test_support.FS_NONASCII') |
| 209 | def test_expandvars_nonascii(self): |
| 210 | encoding = sys.getfilesystemencoding() |
| 211 | def check(value, expected): |
| 212 | tester0("ntpath.expandvars(%r)" % value, expected) |
| 213 | tester0("ntpath.expandvars(%r)" % value.decode(encoding), |
| 214 | expected.decode(encoding)) |
| 215 | with test_support.EnvironmentVarGuard() as env: |
| 216 | env.clear() |
| 217 | unonascii = test_support.FS_NONASCII |
| 218 | snonascii = unonascii.encode(encoding) |
| 219 | env['spam'] = snonascii |
| 220 | env[snonascii] = 'ham' + snonascii |
| 221 | check('$spam bar', '%s bar' % snonascii) |
| 222 | check('$%s bar' % snonascii, '$%s bar' % snonascii) |
| 223 | check('${spam}bar', '%sbar' % snonascii) |
| 224 | check('${%s}bar' % snonascii, 'ham%sbar' % snonascii) |
| 225 | check('$spam}bar', '%s}bar' % snonascii) |
| 226 | check('$%s}bar' % snonascii, '$%s}bar' % snonascii) |
| 227 | check('%spam% bar', '%s bar' % snonascii) |
| 228 | check('%{}% bar'.format(snonascii), 'ham%s bar' % snonascii) |
| 229 | check('%spam%bar', '%sbar' % snonascii) |
| 230 | check('%{}%bar'.format(snonascii), 'ham%sbar' % snonascii) |
| 231 | |
Serhiy Storchaka | 49b2086 | 2014-05-28 18:11:08 +0300 | [diff] [blame] | 232 | def test_expanduser(self): |
| 233 | tester('ntpath.expanduser("test")', 'test') |
| 234 | |
| 235 | with test_support.EnvironmentVarGuard() as env: |
| 236 | env.clear() |
| 237 | tester('ntpath.expanduser("~test")', '~test') |
| 238 | |
| 239 | env['HOMEPATH'] = 'eric\\idle' |
| 240 | env['HOMEDRIVE'] = 'C:\\' |
| 241 | tester('ntpath.expanduser("~test")', 'C:\\eric\\test') |
| 242 | tester('ntpath.expanduser("~")', 'C:\\eric\\idle') |
| 243 | |
| 244 | del env['HOMEDRIVE'] |
| 245 | tester('ntpath.expanduser("~test")', 'eric\\test') |
| 246 | tester('ntpath.expanduser("~")', 'eric\\idle') |
| 247 | |
| 248 | env.clear() |
| 249 | env['USERPROFILE'] = 'C:\\eric\\idle' |
| 250 | tester('ntpath.expanduser("~test")', 'C:\\eric\\test') |
| 251 | tester('ntpath.expanduser("~")', 'C:\\eric\\idle') |
| 252 | |
| 253 | env.clear() |
| 254 | env['HOME'] = 'C:\\idle\\eric' |
| 255 | tester('ntpath.expanduser("~test")', 'C:\\idle\\test') |
| 256 | tester('ntpath.expanduser("~")', 'C:\\idle\\eric') |
| 257 | |
| 258 | tester('ntpath.expanduser("~test\\foo\\bar")', |
| 259 | 'C:\\idle\\test\\foo\\bar') |
| 260 | tester('ntpath.expanduser("~test/foo/bar")', |
| 261 | 'C:\\idle\\test/foo/bar') |
| 262 | tester('ntpath.expanduser("~\\foo\\bar")', |
| 263 | 'C:\\idle\\eric\\foo\\bar') |
| 264 | tester('ntpath.expanduser("~/foo/bar")', |
| 265 | 'C:\\idle\\eric/foo/bar') |
| 266 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 267 | def test_abspath(self): |
| 268 | # ntpath.abspath() can only be used on a system with the "nt" module |
| 269 | # (reasonably), so we protect this test with "import nt". This allows |
| 270 | # the rest of the tests for the ntpath module to be run to completion |
| 271 | # on any platform, since most of the module is intended to be usable |
| 272 | # from any platform. |
Ezio Melotti | 4cc80ca | 2010-02-20 08:09:39 +0000 | [diff] [blame] | 273 | # XXX this needs more tests |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 274 | try: |
| 275 | import nt |
| 276 | except ImportError: |
Ezio Melotti | 4cc80ca | 2010-02-20 08:09:39 +0000 | [diff] [blame] | 277 | # check that the function is there even if we are not on Windows |
| 278 | ntpath.abspath |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 279 | else: |
| 280 | tester('ntpath.abspath("C:\\")', "C:\\") |
| 281 | |
| 282 | def test_relpath(self): |
| 283 | currentdir = os.path.split(os.getcwd())[-1] |
| 284 | tester('ntpath.relpath("a")', 'a') |
| 285 | tester('ntpath.relpath(os.path.abspath("a"))', 'a') |
| 286 | tester('ntpath.relpath("a/b")', 'a\\b') |
| 287 | tester('ntpath.relpath("../a/b")', '..\\a\\b') |
| 288 | tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a') |
| 289 | tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b') |
| 290 | tester('ntpath.relpath("a", "b/c")', '..\\..\\a') |
| 291 | tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a') |
| 292 | tester('ntpath.relpath("a", "a")', '.') |
Hirokazu Yamamoto | 50f7d7e | 2010-10-18 13:55:29 +0000 | [diff] [blame] | 293 | tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat') |
| 294 | tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat') |
| 295 | tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat') |
| 296 | tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..') |
| 297 | tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat') |
| 298 | tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x') |
| 299 | tester('ntpath.relpath("/", "/")', '.') |
| 300 | tester('ntpath.relpath("/a", "/a")', '.') |
| 301 | tester('ntpath.relpath("/a/b", "/a/b")', '.') |
| 302 | tester('ntpath.relpath("c:/foo", "C:/FOO")', '.') |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 303 | |
| 304 | |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 305 | class NtCommonTest(test_genericpath.CommonTest): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 306 | pathmodule = ntpath |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 307 | attributes = ['relpath', 'splitunc'] |
| 308 | |
| 309 | |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 310 | def test_main(): |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 311 | test_support.run_unittest(TestNtpath, NtCommonTest) |
Jerry Seutter | fc7b3e3 | 2008-03-26 05:58:14 +0000 | [diff] [blame] | 312 | |
| 313 | |
| 314 | if __name__ == "__main__": |
| 315 | unittest.main() |