blob: 3dc7765c9d7d17b8fb43c53c0b9db0e633ee74ad [file] [log] [blame]
Guido van Rossumead9d8d1999-02-03 17:21:21 +00001import ntpath
Mark Hammond673c6cf2000-08-14 06:21:26 +00002import os
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test.support import verbose, TestFailed
4import test.support as support
Christian Heimesf6cd9672008-03-26 13:45:42 +00005import unittest
Guido van Rossumead9d8d1999-02-03 17:21:21 +00006
Guido van Rossumead9d8d1999-02-03 17:21:21 +00007
8def tester(fn, wantResult):
Eric S. Raymondfc170b12001-02-09 11:51:27 +00009 fn = fn.replace("\\", "\\\\")
Fred Drake004d5e62000-10-23 17:22:08 +000010 gotResult = eval(fn)
11 if wantResult != gotResult:
Christian Heimesf6cd9672008-03-26 13:45:42 +000012 raise TestFailed("%s should return: %s but returned: %s" \
13 %(str(fn), str(wantResult), str(gotResult)))
Tim Peters6578dc92002-12-24 18:31:27 +000014
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +000015 # 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 Rossumead9d8d1999-02-03 17:21:21 +000032
Christian Heimesf6cd9672008-03-26 13:45:42 +000033class TestNtpath(unittest.TestCase):
34 def test_splitext(self):
35 tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
36 tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext'))
37 tester('ntpath.splitext(".ext")', ('.ext', ''))
38 tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', ''))
39 tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', ''))
40 tester('ntpath.splitext("")', ('', ''))
41 tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext'))
42 tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext'))
43 tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext'))
44 tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000045
Christian Heimesf6cd9672008-03-26 13:45:42 +000046 def test_splitdrive(self):
47 tester('ntpath.splitdrive("c:\\foo\\bar")',
48 ('c:', '\\foo\\bar'))
49 tester('ntpath.splitdrive("c:/foo/bar")',
50 ('c:', '/foo/bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000051
Christian Heimesf6cd9672008-03-26 13:45:42 +000052 def test_splitunc(self):
53 tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")',
54 ('\\\\conky\\mountpoint', '\\foo\\bar'))
55 tester('ntpath.splitunc("//conky/mountpoint/foo/bar")',
56 ('//conky/mountpoint', '/foo/bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000057
Christian Heimesf6cd9672008-03-26 13:45:42 +000058 def test_split(self):
59 tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
60 tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
61 ('\\\\conky\\mountpoint\\foo', 'bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000062
Christian Heimesf6cd9672008-03-26 13:45:42 +000063 tester('ntpath.split("c:\\")', ('c:\\', ''))
64 tester('ntpath.split("\\\\conky\\mountpoint\\")',
65 ('\\\\conky\\mountpoint', ''))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000066
Christian Heimesf6cd9672008-03-26 13:45:42 +000067 tester('ntpath.split("c:/")', ('c:/', ''))
68 tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint', ''))
Mark Hammond673c6cf2000-08-14 06:21:26 +000069
Christian Heimesf6cd9672008-03-26 13:45:42 +000070 def test_isabs(self):
71 tester('ntpath.isabs("c:\\")', 1)
72 tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
73 tester('ntpath.isabs("\\foo")', 1)
74 tester('ntpath.isabs("\\foo\\bar")', 1)
Tim Petersd4f7f602001-07-19 19:11:41 +000075
Christian Heimesf6cd9672008-03-26 13:45:42 +000076 def test_commonprefix(self):
77 tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
78 "/home/swen")
79 tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
80 "\\home\\swen\\")
81 tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
82 "/home/swen/spam")
Tim Peters6a3e5f12001-11-05 21:25:02 +000083
Christian Heimesf6cd9672008-03-26 13:45:42 +000084 def test_join(self):
85 tester('ntpath.join("")', '')
86 tester('ntpath.join("", "", "")', '')
87 tester('ntpath.join("a")', 'a')
88 tester('ntpath.join("/a")', '/a')
89 tester('ntpath.join("\\a")', '\\a')
90 tester('ntpath.join("a:")', 'a:')
91 tester('ntpath.join("a:", "b")', 'a:b')
92 tester('ntpath.join("a:", "/b")', 'a:/b')
93 tester('ntpath.join("a:", "\\b")', 'a:\\b')
94 tester('ntpath.join("a", "/b")', '/b')
95 tester('ntpath.join("a", "\\b")', '\\b')
96 tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
97 tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
98 tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
99 tester('ntpath.join("a", "b", "\\c")', '\\c')
100 tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
101 tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
102 tester("ntpath.join('c:', '/a')", 'c:/a')
103 tester("ntpath.join('c:/', '/a')", 'c:/a')
104 tester("ntpath.join('c:/a', '/b')", '/b')
105 tester("ntpath.join('c:', 'd:/')", 'd:/')
106 tester("ntpath.join('c:/', 'd:/')", 'd:/')
107 tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b')
Tim Peters54a14a32001-08-30 22:05:26 +0000108
Christian Heimesf6cd9672008-03-26 13:45:42 +0000109 tester("ntpath.join('')", '')
110 tester("ntpath.join('', '', '', '', '')", '')
111 tester("ntpath.join('a')", 'a')
112 tester("ntpath.join('', 'a')", 'a')
113 tester("ntpath.join('', '', '', '', 'a')", 'a')
114 tester("ntpath.join('a', '')", 'a\\')
115 tester("ntpath.join('a', '', '', '', '')", 'a\\')
116 tester("ntpath.join('a\\', '')", 'a\\')
117 tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
Tim Peters54a14a32001-08-30 22:05:26 +0000118
Christian Heimesf6cd9672008-03-26 13:45:42 +0000119 def test_normpath(self):
120 tester("ntpath.normpath('A//////././//.//B')", r'A\B')
121 tester("ntpath.normpath('A/./B')", r'A\B')
122 tester("ntpath.normpath('A/foo/../B')", r'A\B')
123 tester("ntpath.normpath('C:A//B')", r'C:A\B')
124 tester("ntpath.normpath('D:A/./B')", r'D:A\B')
125 tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
Tim Peters54a14a32001-08-30 22:05:26 +0000126
Christian Heimesf6cd9672008-03-26 13:45:42 +0000127 tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
128 tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
129 tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
Thomas Woutersb2137042007-02-01 18:02:27 +0000130
Christian Heimesf6cd9672008-03-26 13:45:42 +0000131 tester("ntpath.normpath('..')", r'..')
132 tester("ntpath.normpath('.')", r'.')
133 tester("ntpath.normpath('')", r'.')
134 tester("ntpath.normpath('/')", '\\')
135 tester("ntpath.normpath('c:/')", 'c:\\')
136 tester("ntpath.normpath('/../.././..')", '\\')
137 tester("ntpath.normpath('c:/../../..')", 'c:\\')
138 tester("ntpath.normpath('../.././..')", r'..\..\..')
139 tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
140 tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
141 tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
Fred Drake5e997312002-01-15 03:46:43 +0000142
Christian Heimesf6cd9672008-03-26 13:45:42 +0000143 def test_expandvars(self):
144 oldenv = os.environ.copy()
145 try:
146 os.environ.clear()
147 os.environ["foo"] = "bar"
148 os.environ["{foo"] = "baz1"
149 os.environ["{foo}"] = "baz2"
150 tester('ntpath.expandvars("foo")', "foo")
151 tester('ntpath.expandvars("$foo bar")', "bar bar")
152 tester('ntpath.expandvars("${foo}bar")', "barbar")
153 tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
154 tester('ntpath.expandvars("$bar bar")', "$bar bar")
155 tester('ntpath.expandvars("$?bar")', "$?bar")
156 tester('ntpath.expandvars("${foo}bar")', "barbar")
157 tester('ntpath.expandvars("$foo}bar")', "bar}bar")
158 tester('ntpath.expandvars("${foo")', "${foo")
159 tester('ntpath.expandvars("${{foo}}")', "baz1}")
160 tester('ntpath.expandvars("$foo$foo")', "barbar")
161 tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
162 tester('ntpath.expandvars("%foo% bar")', "bar bar")
163 tester('ntpath.expandvars("%foo%bar")', "barbar")
164 tester('ntpath.expandvars("%foo%%foo%")', "barbar")
165 tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
166 tester('ntpath.expandvars("%?bar%")', "%?bar%")
167 tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
168 tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
169 finally:
170 os.environ.clear()
171 os.environ.update(oldenv)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000172
Christian Heimesf6cd9672008-03-26 13:45:42 +0000173 def test_abspath(self):
174 # ntpath.abspath() can only be used on a system with the "nt" module
175 # (reasonably), so we protect this test with "import nt". This allows
176 # the rest of the tests for the ntpath module to be run to completion
177 # on any platform, since most of the module is intended to be usable
178 # from any platform.
179 try:
180 import nt
181 except ImportError:
182 pass
183 else:
184 tester('ntpath.abspath("C:\\")', "C:\\")
185
186 def test_relpath(self):
187 currentdir = os.path.split(os.getcwd())[-1]
188 tester('ntpath.relpath("a")', 'a')
189 tester('ntpath.relpath(os.path.abspath("a"))', 'a')
190 tester('ntpath.relpath("a/b")', 'a\\b')
191 tester('ntpath.relpath("../a/b")', '..\\a\\b')
192 tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
193 tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
194 tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
195 tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
196 tester('ntpath.relpath("a", "a")', '.')
197
198
199def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000200 support.run_unittest(TestNtpath)
Christian Heimesf6cd9672008-03-26 13:45:42 +0000201
202
203if __name__ == "__main__":
204 unittest.main()