blob: 749afbce8734881e08fa76bc90092beb8b03536a [file] [log] [blame]
Guido van Rossumead9d8d1999-02-03 17:21:21 +00001import ntpath
Mark Hammond673c6cf2000-08-14 06:21:26 +00002import os
Georg Brandla4f46e12010-02-07 17:03:15 +00003from test.test_support import TestFailed
Florent Xiclunadc1531c2010-03-06 18:07:18 +00004from test import test_support, test_genericpath
Jerry Seutterfc7b3e32008-03-26 05:58:14 +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:
Jerry Seutterfc7b3e32008-03-26 05:58:14 +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
Guido van Rossumead9d8d1999-02-03 17:21:21 +000015
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000016class 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 Rossumead9d8d1999-02-03 17:21:21 +000028
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000029 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 Rossumead9d8d1999-02-03 17:21:21 +000034
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000035 def test_splitunc(self):
Serhiy Storchakadd5a46c2013-12-16 15:15:29 +020036 tester('ntpath.splitunc("c:\\foo\\bar")',
37 ('', 'c:\\foo\\bar'))
38 tester('ntpath.splitunc("c:/foo/bar")',
39 ('', 'c:/foo/bar'))
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000040 tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")',
41 ('\\\\conky\\mountpoint', '\\foo\\bar'))
42 tester('ntpath.splitunc("//conky/mountpoint/foo/bar")',
43 ('//conky/mountpoint', '/foo/bar'))
Serhiy Storchakadd5a46c2013-12-16 15:15:29 +020044 tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")',
45 ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
46 tester('ntpath.splitunc("///conky/mountpoint/foo/bar")',
47 ('', '///conky/mountpoint/foo/bar'))
48 tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")',
49 ('', '\\\\conky\\\\mountpoint\\foo\\bar'))
50 tester('ntpath.splitunc("//conky//mountpoint/foo/bar")',
51 ('', '//conky//mountpoint/foo/bar'))
52 self.assertEqual(ntpath.splitunc(u'//conky/MOUNTPO\u0130NT/foo/bar'),
53 (u'//conky/MOUNTPO\u0130NT', u'/foo/bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000054
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000055 def test_split(self):
56 tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
57 tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
58 ('\\\\conky\\mountpoint\\foo', 'bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000059
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000060 tester('ntpath.split("c:\\")', ('c:\\', ''))
61 tester('ntpath.split("\\\\conky\\mountpoint\\")',
62 ('\\\\conky\\mountpoint', ''))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000063
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000064 tester('ntpath.split("c:/")', ('c:/', ''))
65 tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint', ''))
Mark Hammond673c6cf2000-08-14 06:21:26 +000066
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000067 def test_isabs(self):
68 tester('ntpath.isabs("c:\\")', 1)
69 tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
70 tester('ntpath.isabs("\\foo")', 1)
71 tester('ntpath.isabs("\\foo\\bar")', 1)
Tim Petersd4f7f602001-07-19 19:11:41 +000072
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000073 def test_commonprefix(self):
74 tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
75 "/home/swen")
76 tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
77 "\\home\\swen\\")
78 tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
79 "/home/swen/spam")
Tim Peters6a3e5f12001-11-05 21:25:02 +000080
Jerry Seutterfc7b3e32008-03-26 05:58:14 +000081 def test_join(self):
82 tester('ntpath.join("")', '')
83 tester('ntpath.join("", "", "")', '')
84 tester('ntpath.join("a")', 'a')
85 tester('ntpath.join("/a")', '/a')
86 tester('ntpath.join("\\a")', '\\a')
87 tester('ntpath.join("a:")', 'a:')
88 tester('ntpath.join("a:", "b")', 'a:b')
89 tester('ntpath.join("a:", "/b")', 'a:/b')
90 tester('ntpath.join("a:", "\\b")', 'a:\\b')
91 tester('ntpath.join("a", "/b")', '/b')
92 tester('ntpath.join("a", "\\b")', '\\b')
93 tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
94 tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
95 tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
96 tester('ntpath.join("a", "b", "\\c")', '\\c')
97 tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
98 tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
99 tester("ntpath.join('c:', '/a')", 'c:/a')
100 tester("ntpath.join('c:/', '/a')", 'c:/a')
101 tester("ntpath.join('c:/a', '/b')", '/b')
102 tester("ntpath.join('c:', 'd:/')", 'd:/')
103 tester("ntpath.join('c:/', 'd:/')", 'd:/')
104 tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b')
Tim Peters54a14a32001-08-30 22:05:26 +0000105
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000106 tester("ntpath.join('')", '')
107 tester("ntpath.join('', '', '', '', '')", '')
108 tester("ntpath.join('a')", 'a')
109 tester("ntpath.join('', 'a')", 'a')
110 tester("ntpath.join('', '', '', '', 'a')", 'a')
111 tester("ntpath.join('a', '')", 'a\\')
112 tester("ntpath.join('a', '', '', '', '')", 'a\\')
113 tester("ntpath.join('a\\', '')", 'a\\')
114 tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
Tim Peters54a14a32001-08-30 22:05:26 +0000115
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000116 def test_normpath(self):
117 tester("ntpath.normpath('A//////././//.//B')", r'A\B')
118 tester("ntpath.normpath('A/./B')", r'A\B')
119 tester("ntpath.normpath('A/foo/../B')", r'A\B')
120 tester("ntpath.normpath('C:A//B')", r'C:A\B')
121 tester("ntpath.normpath('D:A/./B')", r'D:A\B')
122 tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
Tim Peters54a14a32001-08-30 22:05:26 +0000123
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000124 tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
125 tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
126 tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
Sjoerd Mullender33a0a062007-01-16 16:42:38 +0000127
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000128 tester("ntpath.normpath('..')", r'..')
129 tester("ntpath.normpath('.')", r'.')
130 tester("ntpath.normpath('')", r'.')
131 tester("ntpath.normpath('/')", '\\')
132 tester("ntpath.normpath('c:/')", 'c:\\')
133 tester("ntpath.normpath('/../.././..')", '\\')
134 tester("ntpath.normpath('c:/../../..')", 'c:\\')
135 tester("ntpath.normpath('../.././..')", r'..\..\..')
136 tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
137 tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
138 tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
Fred Drake5e997312002-01-15 03:46:43 +0000139
Georg Brandle2773252010-08-01 19:14:56 +0000140 tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL')
141 tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z')
142
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000143 def test_expandvars(self):
Walter Dörwald6733bed2009-05-01 17:35:37 +0000144 with test_support.EnvironmentVarGuard() as env:
145 env.clear()
146 env["foo"] = "bar"
147 env["{foo"] = "baz1"
148 env["{foo}"] = "baz2"
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000149 tester('ntpath.expandvars("foo")', "foo")
150 tester('ntpath.expandvars("$foo bar")', "bar bar")
151 tester('ntpath.expandvars("${foo}bar")', "barbar")
152 tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
153 tester('ntpath.expandvars("$bar bar")', "$bar bar")
154 tester('ntpath.expandvars("$?bar")', "$?bar")
155 tester('ntpath.expandvars("${foo}bar")', "barbar")
156 tester('ntpath.expandvars("$foo}bar")', "bar}bar")
157 tester('ntpath.expandvars("${foo")', "${foo")
158 tester('ntpath.expandvars("${{foo}}")', "baz1}")
159 tester('ntpath.expandvars("$foo$foo")', "barbar")
160 tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
161 tester('ntpath.expandvars("%foo% bar")', "bar bar")
162 tester('ntpath.expandvars("%foo%bar")', "barbar")
163 tester('ntpath.expandvars("%foo%%foo%")', "barbar")
164 tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
165 tester('ntpath.expandvars("%?bar%")', "%?bar%")
166 tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
167 tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
Collin Winter6f187742007-03-16 22:16:08 +0000168
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000169 def test_abspath(self):
170 # ntpath.abspath() can only be used on a system with the "nt" module
171 # (reasonably), so we protect this test with "import nt". This allows
172 # the rest of the tests for the ntpath module to be run to completion
173 # on any platform, since most of the module is intended to be usable
174 # from any platform.
Ezio Melotti4cc80ca2010-02-20 08:09:39 +0000175 # XXX this needs more tests
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000176 try:
177 import nt
178 except ImportError:
Ezio Melotti4cc80ca2010-02-20 08:09:39 +0000179 # check that the function is there even if we are not on Windows
180 ntpath.abspath
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000181 else:
182 tester('ntpath.abspath("C:\\")', "C:\\")
183
184 def test_relpath(self):
185 currentdir = os.path.split(os.getcwd())[-1]
186 tester('ntpath.relpath("a")', 'a')
187 tester('ntpath.relpath(os.path.abspath("a"))', 'a')
188 tester('ntpath.relpath("a/b")', 'a\\b')
189 tester('ntpath.relpath("../a/b")', '..\\a\\b')
190 tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
191 tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
192 tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
193 tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
194 tester('ntpath.relpath("a", "a")', '.')
Hirokazu Yamamoto50f7d7e2010-10-18 13:55:29 +0000195 tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
196 tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
197 tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
198 tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
199 tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
200 tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
201 tester('ntpath.relpath("/", "/")', '.')
202 tester('ntpath.relpath("/a", "/a")', '.')
203 tester('ntpath.relpath("/a/b", "/a/b")', '.')
204 tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000205
206
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000207class NtCommonTest(test_genericpath.CommonTest):
Florent Xicluna985478d2010-03-06 18:52:52 +0000208 pathmodule = ntpath
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000209 attributes = ['relpath', 'splitunc']
210
211
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000212def test_main():
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000213 test_support.run_unittest(TestNtpath, NtCommonTest)
Jerry Seutterfc7b3e32008-03-26 05:58:14 +0000214
215
216if __name__ == "__main__":
217 unittest.main()