blob: fcf2ef56c6e69dcfdf6ddd5842fdbd8fa357a8f9 [file] [log] [blame]
Brett Cannonb47243a2003-06-16 21:54:50 +00001import unittest
2from test import test_support
Skip Montanaroe809b002000-07-12 00:20:08 +00003
Brett Cannonb47243a2003-06-16 21:54:50 +00004import posixpath, os
Skip Montanaroe809b002000-07-12 00:20:08 +00005
Brett Cannonb47243a2003-06-16 21:54:50 +00006class PosixPathTest(unittest.TestCase):
Skip Montanaroe809b002000-07-12 00:20:08 +00007
Brett Cannonb47243a2003-06-16 21:54:50 +00008 def assertIs(self, a, b):
9 self.assert_(a is b)
Skip Montanaroe809b002000-07-12 00:20:08 +000010
Brett Cannonb47243a2003-06-16 21:54:50 +000011 def test_normcase(self):
12 # Check that normcase() is idempotent
13 p = "FoO/./BaR"
14 p = posixpath.normcase(p)
15 self.assertEqual(p, posixpath.normcase(p))
Skip Montanaroe809b002000-07-12 00:20:08 +000016
Brett Cannonb47243a2003-06-16 21:54:50 +000017 self.assertRaises(TypeError, posixpath.normcase)
Skip Montanaroe809b002000-07-12 00:20:08 +000018
Brett Cannonb47243a2003-06-16 21:54:50 +000019 def test_join(self):
20 self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"), "/bar/baz")
21 self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz")
22 self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"), "/foo/bar/baz/")
Skip Montanaroe809b002000-07-12 00:20:08 +000023
Brett Cannonb47243a2003-06-16 21:54:50 +000024 self.assertRaises(TypeError, posixpath.join)
Skip Montanaroe809b002000-07-12 00:20:08 +000025
Brett Cannonb47243a2003-06-16 21:54:50 +000026 def test_splitdrive(self):
27 self.assertEqual(posixpath.splitdrive("/foo/bar"), ("", "/foo/bar"))
28
29 self.assertRaises(TypeError, posixpath.splitdrive)
30
31 def test_split(self):
32 self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))
33 self.assertEqual(posixpath.split("/"), ("/", ""))
34 self.assertEqual(posixpath.split("foo"), ("", "foo"))
35 self.assertEqual(posixpath.split("////foo"), ("////", "foo"))
36 self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar"))
37
38 self.assertRaises(TypeError, posixpath.split)
39
40 def test_splitext(self):
41 self.assertEqual(posixpath.splitext("foo.ext"), ("foo", ".ext"))
42 self.assertEqual(posixpath.splitext("/foo/foo.ext"), ("/foo/foo", ".ext"))
43 self.assertEqual(posixpath.splitext(".ext"), ("", ".ext"))
44 self.assertEqual(posixpath.splitext("/foo.ext/foo"), ("/foo.ext/foo", ""))
45 self.assertEqual(posixpath.splitext("foo.ext/"), ("foo.ext/", ""))
46 self.assertEqual(posixpath.splitext(""), ("", ""))
47 self.assertEqual(posixpath.splitext("foo.bar.ext"), ("foo.bar", ".ext"))
48
49 self.assertRaises(TypeError, posixpath.splitext)
50
51 def test_isabs(self):
52 self.assertIs(posixpath.isabs(""), False)
53 self.assertIs(posixpath.isabs("/"), True)
54 self.assertIs(posixpath.isabs("/foo"), True)
55 self.assertIs(posixpath.isabs("/foo/bar"), True)
56 self.assertIs(posixpath.isabs("foo/bar"), False)
57
58 self.assertRaises(TypeError, posixpath.isabs)
59
60 def test_splitdrive(self):
61 self.assertEqual(posixpath.splitdrive("/foo/bar"), ("", "/foo/bar"))
62
63 self.assertRaises(TypeError, posixpath.splitdrive)
64
65 def test_basename(self):
66 self.assertEqual(posixpath.basename("/foo/bar"), "bar")
67 self.assertEqual(posixpath.basename("/"), "")
68 self.assertEqual(posixpath.basename("foo"), "foo")
69 self.assertEqual(posixpath.basename("////foo"), "foo")
70 self.assertEqual(posixpath.basename("//foo//bar"), "bar")
71
72 self.assertRaises(TypeError, posixpath.basename)
73
74 def test_dirname(self):
75 self.assertEqual(posixpath.dirname("/foo/bar"), "/foo")
76 self.assertEqual(posixpath.dirname("/"), "/")
77 self.assertEqual(posixpath.dirname("foo"), "")
78 self.assertEqual(posixpath.dirname("////foo"), "////")
79 self.assertEqual(posixpath.dirname("//foo//bar"), "//foo")
80
81 self.assertRaises(TypeError, posixpath.dirname)
82
83 def test_commonprefix(self):
84 self.assertEqual(
85 posixpath.commonprefix([]),
86 ""
87 )
88 self.assertEqual(
89 posixpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
90 "/home/swen"
91 )
92 self.assertEqual(
93 posixpath.commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
94 "/home/swen/"
95 )
96 self.assertEqual(
97 posixpath.commonprefix(["/home/swen/spam", "/home/swen/spam"]),
98 "/home/swen/spam"
99 )
100
101 def test_getsize(self):
102 f = open(test_support.TESTFN, "wb")
103 try:
104 f.write("foo")
105 f.close()
106 self.assertEqual(posixpath.getsize(test_support.TESTFN), 3)
107 finally:
108 if not f.closed:
109 f.close()
110 os.remove(test_support.TESTFN)
111
112 def test_time(self):
113 f = open(test_support.TESTFN, "wb")
114 try:
115 f.write("foo")
116 f.close()
117 f = open(test_support.TESTFN, "ab")
118 f.write("bar")
119 f.close()
120 f = open(test_support.TESTFN, "rb")
121 d = f.read()
122 f.close()
123 self.assertEqual(d, "foobar")
124
125 self.assert_(
126 posixpath.getctime(test_support.TESTFN) <=
127 posixpath.getmtime(test_support.TESTFN)
128 )
129 finally:
130 if not f.closed:
131 f.close()
132 os.remove(test_support.TESTFN)
133
134 def test_islink(self):
135 self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
136 f = open(test_support.TESTFN + "1", "wb")
137 try:
138 f.write("foo")
139 f.close()
140 self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
141 if hasattr(os, "symlink"):
142 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
143 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
144 os.remove(test_support.TESTFN + "1")
145 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
146 self.assertIs(posixpath.exists(test_support.TESTFN + "2"), False)
147 finally:
148 if not f.close():
149 f.close()
150 try:
151 os.remove(test_support.TESTFN + "1")
152 except os.error:
153 pass
154 try:
155 os.remove(test_support.TESTFN + "2")
156 except os.error:
157 pass
158
159 self.assertRaises(TypeError, posixpath.islink)
160
161 def test_exists(self):
162 self.assertIs(posixpath.exists(test_support.TESTFN), False)
163 f = open(test_support.TESTFN, "wb")
164 try:
165 f.write("foo")
166 f.close()
167 self.assertIs(posixpath.exists(test_support.TESTFN), True)
168 finally:
169 if not f.close():
170 f.close()
171 try:
172 os.remove(test_support.TESTFN)
173 except os.error:
174 pass
175
176 self.assertRaises(TypeError, posixpath.exists)
177
178 def test_isdir(self):
179 self.assertIs(posixpath.isdir(test_support.TESTFN), False)
180 f = open(test_support.TESTFN, "wb")
181 try:
182 f.write("foo")
183 f.close()
184 self.assertIs(posixpath.isdir(test_support.TESTFN), False)
185 os.remove(test_support.TESTFN)
186 os.mkdir(test_support.TESTFN)
187 self.assertIs(posixpath.isdir(test_support.TESTFN), True)
188 os.rmdir(test_support.TESTFN)
189 finally:
190 if not f.close():
191 f.close()
192 try:
193 os.remove(test_support.TESTFN)
194 except os.error:
195 pass
196 try:
197 os.rmdir(test_support.TESTFN)
198 except os.error:
199 pass
200
201 self.assertRaises(TypeError, posixpath.isdir)
202
203 def test_isfile(self):
204 self.assertIs(posixpath.isfile(test_support.TESTFN), False)
205 f = open(test_support.TESTFN, "wb")
206 try:
207 f.write("foo")
208 f.close()
209 self.assertIs(posixpath.isfile(test_support.TESTFN), True)
210 os.remove(test_support.TESTFN)
211 os.mkdir(test_support.TESTFN)
212 self.assertIs(posixpath.isfile(test_support.TESTFN), False)
213 os.rmdir(test_support.TESTFN)
214 finally:
215 if not f.close():
216 f.close()
217 try:
218 os.remove(test_support.TESTFN)
219 except os.error:
220 pass
221 try:
222 os.rmdir(test_support.TESTFN)
223 except os.error:
224 pass
225
226 self.assertRaises(TypeError, posixpath.isdir)
227
228 def test_samefile(self):
229 f = open(test_support.TESTFN + "1", "wb")
230 try:
231 f.write("foo")
232 f.close()
233 self.assertIs(
234 posixpath.samefile(
235 test_support.TESTFN + "1",
236 test_support.TESTFN + "1"
237 ),
238 True
239 )
Tim Peters478c1052003-06-29 05:46:54 +0000240 # If we don't have links, assume that os.stat doesn't return resonable
Brett Cannonb47243a2003-06-16 21:54:50 +0000241 # inode information and thus, that samefile() doesn't work
242 if hasattr(os, "symlink"):
243 os.symlink(
244 test_support.TESTFN + "1",
245 test_support.TESTFN + "2"
246 )
247 self.assertIs(
248 posixpath.samefile(
249 test_support.TESTFN + "1",
250 test_support.TESTFN + "2"
251 ),
252 True
253 )
254 os.remove(test_support.TESTFN + "2")
255 f = open(test_support.TESTFN + "2", "wb")
256 f.write("bar")
257 f.close()
258 self.assertIs(
259 posixpath.samefile(
260 test_support.TESTFN + "1",
261 test_support.TESTFN + "2"
262 ),
263 False
264 )
265 finally:
266 if not f.close():
267 f.close()
268 try:
269 os.remove(test_support.TESTFN + "1")
270 except os.error:
271 pass
272 try:
273 os.remove(test_support.TESTFN + "2")
274 except os.error:
275 pass
276
277 self.assertRaises(TypeError, posixpath.samefile)
278
279 def test_samestat(self):
280 f = open(test_support.TESTFN + "1", "wb")
281 try:
282 f.write("foo")
283 f.close()
284 self.assertIs(
285 posixpath.samestat(
286 os.stat(test_support.TESTFN + "1"),
287 os.stat(test_support.TESTFN + "1")
288 ),
289 True
290 )
Tim Peters478c1052003-06-29 05:46:54 +0000291 # If we don't have links, assume that os.stat() doesn't return resonable
Brett Cannonb47243a2003-06-16 21:54:50 +0000292 # inode information and thus, that samefile() doesn't work
293 if hasattr(os, "symlink"):
294 if hasattr(os, "symlink"):
295 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
296 self.assertIs(
297 posixpath.samestat(
298 os.stat(test_support.TESTFN + "1"),
299 os.stat(test_support.TESTFN + "2")
300 ),
301 True
302 )
303 os.remove(test_support.TESTFN + "2")
304 f = open(test_support.TESTFN + "2", "wb")
305 f.write("bar")
306 f.close()
307 self.assertIs(
308 posixpath.samestat(
309 os.stat(test_support.TESTFN + "1"),
310 os.stat(test_support.TESTFN + "2")
311 ),
312 False
313 )
314 finally:
315 if not f.close():
316 f.close()
317 try:
318 os.remove(test_support.TESTFN + "1")
319 except os.error:
320 pass
321 try:
322 os.remove(test_support.TESTFN + "2")
323 except os.error:
324 pass
325
326 self.assertRaises(TypeError, posixpath.samestat)
327
328 def test_ismount(self):
329 self.assertIs(posixpath.ismount("/"), True)
330
331 self.assertRaises(TypeError, posixpath.ismount)
332
333 def test_expanduser(self):
334 self.assertEqual(posixpath.expanduser("foo"), "foo")
Brett Cannonb47243a2003-06-16 21:54:50 +0000335 try:
336 import pwd
337 except ImportError:
338 pass
339 else:
Walter Dörwalda9da5ae2003-06-19 10:21:14 +0000340 self.assert_(isinstance(posixpath.expanduser("~/"), basestring))
Neal Norwitz168e73d2003-07-01 03:33:31 +0000341 # if home directory == root directory, this test makes no sense
342 if posixpath.expanduser("~") != '/':
343 self.assertEqual(
344 posixpath.expanduser("~") + "/",
345 posixpath.expanduser("~/")
346 )
Brett Cannonb47243a2003-06-16 21:54:50 +0000347 self.assert_(isinstance(posixpath.expanduser("~root/"), basestring))
348 self.assert_(isinstance(posixpath.expanduser("~foo/"), basestring))
349
350 self.assertRaises(TypeError, posixpath.expanduser)
351
352 def test_expandvars(self):
353 oldenv = os.environ.copy()
354 try:
355 os.environ.clear()
356 os.environ["foo"] = "bar"
357 os.environ["{foo"] = "baz1"
358 os.environ["{foo}"] = "baz2"
359 self.assertEqual(posixpath.expandvars("foo"), "foo")
360 self.assertEqual(posixpath.expandvars("$foo bar"), "bar bar")
361 self.assertEqual(posixpath.expandvars("${foo}bar"), "barbar")
362 self.assertEqual(posixpath.expandvars("$[foo]bar"), "$[foo]bar")
363 self.assertEqual(posixpath.expandvars("$bar bar"), "$bar bar")
364 self.assertEqual(posixpath.expandvars("$?bar"), "$?bar")
365 self.assertEqual(posixpath.expandvars("${foo}bar"), "barbar")
366 self.assertEqual(posixpath.expandvars("$foo}bar"), "bar}bar")
367 self.assertEqual(posixpath.expandvars("${foo"), "${foo")
368 self.assertEqual(posixpath.expandvars("${{foo}}"), "baz1}")
369 finally:
370 os.environ.clear()
371 os.environ.update(oldenv)
372
373 self.assertRaises(TypeError, posixpath.expandvars)
374
375 def test_normpath(self):
376 self.assertEqual(posixpath.normpath(""), ".")
377 self.assertEqual(posixpath.normpath("/"), "/")
378 self.assertEqual(posixpath.normpath("//"), "//")
379 self.assertEqual(posixpath.normpath("///"), "/")
380 self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar")
381 self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz")
382 self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")
383
384 self.assertRaises(TypeError, posixpath.normpath)
385
386 def test_abspath(self):
387 self.assert_("foo" in posixpath.abspath("foo"))
388
389 self.assertRaises(TypeError, posixpath.abspath)
390
391 def test_realpath(self):
392 self.assert_("foo" in posixpath.realpath("foo"))
393
394 self.assertRaises(TypeError, posixpath.realpath)
395
396def test_main():
397 test_support.run_unittest(PosixPathTest)
398
399if __name__=="__main__":
400 test_main()