blob: 5f233366a20fee7334fb98fedd86a742079d1972 [file] [log] [blame]
Fred Drake38c2ef02001-07-17 20:52:51 +00001# As a test suite for the os module, this is woefully inadequate, but this
2# does add tests for a few functions which have been determined to be more
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00003# portable than they had been thought to be.
Fred Drake38c2ef02001-07-17 20:52:51 +00004
5import os
6import unittest
Jeremy Hyltona7fc21b2001-08-20 20:10:01 +00007import warnings
Thomas Wouters477c8d52006-05-27 19:21:47 +00008import sys
Walter Dörwald21d3a322003-05-01 17:45:56 +00009from test import test_support
Fred Drake38c2ef02001-07-17 20:52:51 +000010
Thomas Wouters0e3f5912006-08-11 14:57:12 +000011# Tests creating TESTFN
12class FileTests(unittest.TestCase):
13 def setUp(self):
14 if os.path.exists(test_support.TESTFN):
15 os.unlink(test_support.TESTFN)
16 tearDown = setUp
17
18 def test_access(self):
19 f = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR)
20 os.close(f)
21 self.assert_(os.access(test_support.TESTFN, os.W_OK))
22
Christian Heimesfdab48e2008-01-20 09:06:41 +000023 def test_closerange(self):
24 f = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR)
25 # close a fd that is open, and one that isn't
26 os.closerange(f, f+2)
27 self.assertRaises(OSError, os.write, f, "a")
Thomas Wouters0e3f5912006-08-11 14:57:12 +000028
Christian Heimesdd15f6c2008-03-16 00:07:10 +000029class TemporaryFileTests(unittest.TestCase):
30 def setUp(self):
31 self.files = []
32 os.mkdir(test_support.TESTFN)
33
34 def tearDown(self):
35 for name in self.files:
36 os.unlink(name)
37 os.rmdir(test_support.TESTFN)
38
39 def check_tempfile(self, name):
40 # make sure it doesn't already exist:
41 self.failIf(os.path.exists(name),
42 "file already exists for temporary file")
43 # make sure we can create the file
44 open(name, "w")
45 self.files.append(name)
46
47 def test_tempnam(self):
48 if not hasattr(os, "tempnam"):
49 return
50 warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
51 r"test_os$")
52 self.check_tempfile(os.tempnam())
53
54 name = os.tempnam(test_support.TESTFN)
55 self.check_tempfile(name)
56
57 name = os.tempnam(test_support.TESTFN, "pfx")
58 self.assert_(os.path.basename(name)[:3] == "pfx")
59 self.check_tempfile(name)
60
61 def test_tmpfile(self):
62 if not hasattr(os, "tmpfile"):
63 return
64 # As with test_tmpnam() below, the Windows implementation of tmpfile()
65 # attempts to create a file in the root directory of the current drive.
66 # On Vista and Server 2008, this test will always fail for normal users
67 # as writing to the root directory requires elevated privileges. With
68 # XP and below, the semantics of tmpfile() are the same, but the user
69 # running the test is more likely to have administrative privileges on
70 # their account already. If that's the case, then os.tmpfile() should
71 # work. In order to make this test as useful as possible, rather than
72 # trying to detect Windows versions or whether or not the user has the
73 # right permissions, just try and create a file in the root directory
74 # and see if it raises a 'Permission denied' OSError. If it does, then
75 # test that a subsequent call to os.tmpfile() raises the same error. If
76 # it doesn't, assume we're on XP or below and the user running the test
77 # has administrative privileges, and proceed with the test as normal.
78 if sys.platform == 'win32':
79 name = '\\python_test_os_test_tmpfile.txt'
80 if os.path.exists(name):
81 os.remove(name)
82 try:
83 fp = open(name, 'w')
84 except IOError as first:
85 # open() failed, assert tmpfile() fails in the same way.
86 # Although open() raises an IOError and os.tmpfile() raises an
87 # OSError(), 'args' will be (13, 'Permission denied') in both
88 # cases.
89 try:
90 fp = os.tmpfile()
91 except OSError as second:
92 self.assertEqual(first.args, second.args)
93 else:
94 self.fail("expected os.tmpfile() to raise OSError")
95 return
96 else:
97 # open() worked, therefore, tmpfile() should work. Close our
98 # dummy file and proceed with the test as normal.
99 fp.close()
100 os.remove(name)
101
102 fp = os.tmpfile()
103 fp.write("foobar")
104 fp.seek(0,0)
105 s = fp.read()
106 fp.close()
107 self.assert_(s == "foobar")
108
109 def test_tmpnam(self):
110 import sys
111 if not hasattr(os, "tmpnam"):
112 return
113 warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning,
114 r"test_os$")
115 name = os.tmpnam()
116 if sys.platform in ("win32",):
117 # The Windows tmpnam() seems useless. From the MS docs:
118 #
119 # The character string that tmpnam creates consists of
120 # the path prefix, defined by the entry P_tmpdir in the
121 # file STDIO.H, followed by a sequence consisting of the
122 # digit characters '0' through '9'; the numerical value
123 # of this string is in the range 1 - 65,535. Changing the
124 # definitions of L_tmpnam or P_tmpdir in STDIO.H does not
125 # change the operation of tmpnam.
126 #
127 # The really bizarre part is that, at least under MSVC6,
128 # P_tmpdir is "\\". That is, the path returned refers to
129 # the root of the current drive. That's a terrible place to
130 # put temp files, and, depending on privileges, the user
131 # may not even be able to open a file in the root directory.
132 self.failIf(os.path.exists(name),
133 "file already exists for temporary file")
134 else:
135 self.check_tempfile(name)
136
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000137# Test attributes on return values from os.*stat* family.
138class StatAttributeTests(unittest.TestCase):
139 def setUp(self):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000140 os.mkdir(test_support.TESTFN)
141 self.fname = os.path.join(test_support.TESTFN, "f1")
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000142 f = open(self.fname, 'wb')
Guido van Rossum26d95c32007-08-27 23:18:54 +0000143 f.write(b"ABC")
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000144 f.close()
Tim Peterse0c446b2001-10-18 21:57:37 +0000145
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000146 def tearDown(self):
147 os.unlink(self.fname)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000148 os.rmdir(test_support.TESTFN)
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000149
150 def test_stat_attributes(self):
151 if not hasattr(os, "stat"):
152 return
153
154 import stat
155 result = os.stat(self.fname)
156
157 # Make sure direct access works
158 self.assertEquals(result[stat.ST_SIZE], 3)
159 self.assertEquals(result.st_size, 3)
160
161 import sys
162
163 # Make sure all the attributes are there
164 members = dir(result)
165 for name in dir(stat):
166 if name[:3] == 'ST_':
167 attr = name.lower()
Martin v. Löwis4d394df2005-01-23 09:19:22 +0000168 if name.endswith("TIME"):
169 def trunc(x): return int(x)
170 else:
171 def trunc(x): return x
172 self.assertEquals(trunc(getattr(result, attr)),
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000173 result[getattr(stat, name)])
174 self.assert_(attr in members)
175
176 try:
177 result[200]
178 self.fail("No exception thrown")
179 except IndexError:
180 pass
181
182 # Make sure that assignment fails
183 try:
184 result.st_mode = 1
185 self.fail("No exception thrown")
Collin Winter42dae6a2007-03-28 21:44:53 +0000186 except AttributeError:
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000187 pass
188
189 try:
190 result.st_rdev = 1
191 self.fail("No exception thrown")
Guido van Rossum1fff8782001-10-18 21:19:31 +0000192 except (AttributeError, TypeError):
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000193 pass
194
195 try:
196 result.parrot = 1
197 self.fail("No exception thrown")
198 except AttributeError:
199 pass
200
201 # Use the stat_result constructor with a too-short tuple.
202 try:
203 result2 = os.stat_result((10,))
204 self.fail("No exception thrown")
205 except TypeError:
206 pass
207
208 # Use the constructr with a too-long tuple.
209 try:
210 result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
211 except TypeError:
212 pass
213
Tim Peterse0c446b2001-10-18 21:57:37 +0000214
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000215 def test_statvfs_attributes(self):
216 if not hasattr(os, "statvfs"):
217 return
218
219 import statvfs
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000220 try:
221 result = os.statvfs(self.fname)
Guido van Rossumb940e112007-01-10 16:19:56 +0000222 except OSError as e:
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000223 # On AtheOS, glibc always returns ENOSYS
224 import errno
225 if e.errno == errno.ENOSYS:
226 return
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000227
228 # Make sure direct access works
229 self.assertEquals(result.f_bfree, result[statvfs.F_BFREE])
230
231 # Make sure all the attributes are there
232 members = dir(result)
233 for name in dir(statvfs):
234 if name[:2] == 'F_':
235 attr = name.lower()
236 self.assertEquals(getattr(result, attr),
237 result[getattr(statvfs, name)])
238 self.assert_(attr in members)
239
240 # Make sure that assignment really fails
241 try:
242 result.f_bfree = 1
243 self.fail("No exception thrown")
Collin Winter42dae6a2007-03-28 21:44:53 +0000244 except AttributeError:
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000245 pass
246
247 try:
248 result.parrot = 1
249 self.fail("No exception thrown")
250 except AttributeError:
251 pass
252
253 # Use the constructor with a too-short tuple.
254 try:
255 result2 = os.statvfs_result((10,))
256 self.fail("No exception thrown")
257 except TypeError:
258 pass
259
260 # Use the constructr with a too-long tuple.
261 try:
262 result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
263 except TypeError:
264 pass
Fred Drake38c2ef02001-07-17 20:52:51 +0000265
Thomas Wouters89f507f2006-12-13 04:49:30 +0000266 def test_utime_dir(self):
267 delta = 1000000
268 st = os.stat(test_support.TESTFN)
269 # round to int, because some systems may support sub-second
270 # time stamps in stat, but not in utime.
271 os.utime(test_support.TESTFN, (st.st_atime, int(st.st_mtime-delta)))
272 st2 = os.stat(test_support.TESTFN)
273 self.assertEquals(st2.st_mtime, int(st.st_mtime-delta))
274
275 # Restrict test to Win32, since there is no guarantee other
276 # systems support centiseconds
277 if sys.platform == 'win32':
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000278 def get_file_system(path):
279 import os
280 root = os.path.splitdrive(os.path.realpath("."))[0] + '\\'
281 import ctypes
282 kernel32 = ctypes.windll.kernel32
283 buf = ctypes.create_string_buffer("", 100)
284 if kernel32.GetVolumeInformationA(root, None, 0, None, None, None, buf, len(buf)):
285 return buf.value
286
287 if get_file_system(test_support.TESTFN) == "NTFS":
288 def test_1565150(self):
289 t1 = 1159195039.25
290 os.utime(self.fname, (t1, t1))
291 self.assertEquals(os.stat(self.fname).st_mtime, t1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000292
Guido van Rossumd8faa362007-04-27 19:54:29 +0000293 def test_1686475(self):
294 # Verify that an open file can be stat'ed
295 try:
296 os.stat(r"c:\pagefile.sys")
297 except WindowsError as e:
298 if e == 2: # file does not exist; cannot run test
299 return
300 self.fail("Could not stat pagefile.sys")
301
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000302from test import mapping_tests
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000303
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000304class EnvironTests(mapping_tests.BasicTestMappingProtocol):
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000305 """check that os.environ object conform to mapping protocol"""
Walter Dörwald118f9312004-06-02 18:42:25 +0000306 type2test = None
Christian Heimes90333392007-11-01 19:08:42 +0000307
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000308 def setUp(self):
309 self.__save = dict(os.environ)
Christian Heimes90333392007-11-01 19:08:42 +0000310 for key, value in self._reference().items():
311 os.environ[key] = value
312
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000313 def tearDown(self):
314 os.environ.clear()
315 os.environ.update(self.__save)
316
Christian Heimes90333392007-11-01 19:08:42 +0000317 def _reference(self):
318 return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"}
319
320 def _empty_mapping(self):
321 os.environ.clear()
322 return os.environ
323
Martin v. Löwis1d11de62005-01-29 13:29:23 +0000324 # Bug 1110478
Martin v. Löwis5510f652005-02-17 21:23:20 +0000325 def test_update2(self):
Christian Heimes90333392007-11-01 19:08:42 +0000326 os.environ.clear()
Martin v. Löwis1d11de62005-01-29 13:29:23 +0000327 if os.path.exists("/bin/sh"):
328 os.environ.update(HELLO="World")
329 value = os.popen("/bin/sh -c 'echo $HELLO'").read().strip()
330 self.assertEquals(value, "World")
331
Christian Heimes1a13d592007-11-08 14:16:55 +0000332 def test_os_popen_iter(self):
333 if os.path.exists("/bin/sh"):
334 popen = os.popen("/bin/sh -c 'echo \"line1\nline2\nline3\"'")
335 it = iter(popen)
336 self.assertEquals(next(it), "line1\n")
337 self.assertEquals(next(it), "line2\n")
338 self.assertEquals(next(it), "line3\n")
339 self.assertRaises(StopIteration, next, it)
340
Guido van Rossum67aca9e2007-06-13 21:51:27 +0000341 # Verify environ keys and values from the OS are of the
342 # correct str type.
343 def test_keyvalue_types(self):
344 for key, val in os.environ.items():
345 self.assertEquals(type(key), str)
346 self.assertEquals(type(val), str)
347
Christian Heimes90333392007-11-01 19:08:42 +0000348 def test_items(self):
349 for key, value in self._reference().items():
350 self.assertEqual(os.environ.get(key), value)
351
Tim Petersc4e09402003-04-25 07:11:48 +0000352class WalkTests(unittest.TestCase):
353 """Tests for os.walk()."""
354
355 def test_traversal(self):
356 import os
357 from os.path import join
358
359 # Build:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000360 # TESTFN/
361 # TEST1/ a file kid and two directory kids
Tim Petersc4e09402003-04-25 07:11:48 +0000362 # tmp1
363 # SUB1/ a file kid and a directory kid
Guido van Rossumd8faa362007-04-27 19:54:29 +0000364 # tmp2
365 # SUB11/ no kids
366 # SUB2/ a file kid and a dirsymlink kid
367 # tmp3
368 # link/ a symlink to TESTFN.2
369 # TEST2/
370 # tmp4 a lone file
371 walk_path = join(test_support.TESTFN, "TEST1")
372 sub1_path = join(walk_path, "SUB1")
Tim Petersc4e09402003-04-25 07:11:48 +0000373 sub11_path = join(sub1_path, "SUB11")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000374 sub2_path = join(walk_path, "SUB2")
375 tmp1_path = join(walk_path, "tmp1")
Tim Petersc4e09402003-04-25 07:11:48 +0000376 tmp2_path = join(sub1_path, "tmp2")
377 tmp3_path = join(sub2_path, "tmp3")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000378 link_path = join(sub2_path, "link")
379 t2_path = join(test_support.TESTFN, "TEST2")
380 tmp4_path = join(test_support.TESTFN, "TEST2", "tmp4")
Tim Petersc4e09402003-04-25 07:11:48 +0000381
382 # Create stuff.
383 os.makedirs(sub11_path)
384 os.makedirs(sub2_path)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000385 os.makedirs(t2_path)
386 for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
Alex Martelli01c77c62006-08-24 02:58:11 +0000387 f = open(path, "w")
Tim Petersc4e09402003-04-25 07:11:48 +0000388 f.write("I'm " + path + " and proud of it. Blame test_os.\n")
389 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000390 if hasattr(os, "symlink"):
391 os.symlink(os.path.abspath(t2_path), link_path)
392 sub2_tree = (sub2_path, ["link"], ["tmp3"])
393 else:
394 sub2_tree = (sub2_path, [], ["tmp3"])
Tim Petersc4e09402003-04-25 07:11:48 +0000395
396 # Walk top-down.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000397 all = list(os.walk(walk_path))
Tim Petersc4e09402003-04-25 07:11:48 +0000398 self.assertEqual(len(all), 4)
399 # We can't know which order SUB1 and SUB2 will appear in.
400 # Not flipped: TESTFN, SUB1, SUB11, SUB2
401 # flipped: TESTFN, SUB2, SUB1, SUB11
402 flipped = all[0][1][0] != "SUB1"
403 all[0][1].sort()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000404 self.assertEqual(all[0], (walk_path, ["SUB1", "SUB2"], ["tmp1"]))
Tim Petersc4e09402003-04-25 07:11:48 +0000405 self.assertEqual(all[1 + flipped], (sub1_path, ["SUB11"], ["tmp2"]))
406 self.assertEqual(all[2 + flipped], (sub11_path, [], []))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000407 self.assertEqual(all[3 - 2 * flipped], sub2_tree)
Tim Petersc4e09402003-04-25 07:11:48 +0000408
409 # Prune the search.
410 all = []
Guido van Rossumd8faa362007-04-27 19:54:29 +0000411 for root, dirs, files in os.walk(walk_path):
Tim Petersc4e09402003-04-25 07:11:48 +0000412 all.append((root, dirs, files))
413 # Don't descend into SUB1.
414 if 'SUB1' in dirs:
415 # Note that this also mutates the dirs we appended to all!
416 dirs.remove('SUB1')
417 self.assertEqual(len(all), 2)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000418 self.assertEqual(all[0], (walk_path, ["SUB2"], ["tmp1"]))
419 self.assertEqual(all[1], sub2_tree)
Tim Petersc4e09402003-04-25 07:11:48 +0000420
421 # Walk bottom-up.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000422 all = list(os.walk(walk_path, topdown=False))
Tim Petersc4e09402003-04-25 07:11:48 +0000423 self.assertEqual(len(all), 4)
424 # We can't know which order SUB1 and SUB2 will appear in.
425 # Not flipped: SUB11, SUB1, SUB2, TESTFN
426 # flipped: SUB2, SUB11, SUB1, TESTFN
427 flipped = all[3][1][0] != "SUB1"
428 all[3][1].sort()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000429 self.assertEqual(all[3], (walk_path, ["SUB1", "SUB2"], ["tmp1"]))
Tim Petersc4e09402003-04-25 07:11:48 +0000430 self.assertEqual(all[flipped], (sub11_path, [], []))
431 self.assertEqual(all[flipped + 1], (sub1_path, ["SUB11"], ["tmp2"]))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000432 self.assertEqual(all[2 - 2 * flipped], sub2_tree)
Tim Petersc4e09402003-04-25 07:11:48 +0000433
Guido van Rossumd8faa362007-04-27 19:54:29 +0000434 if hasattr(os, "symlink"):
435 # Walk, following symlinks.
436 for root, dirs, files in os.walk(walk_path, followlinks=True):
437 if root == link_path:
438 self.assertEqual(dirs, [])
439 self.assertEqual(files, ["tmp4"])
440 break
441 else:
442 self.fail("Didn't follow symlink with followlinks=True")
443
444 def tearDown(self):
Tim Petersc4e09402003-04-25 07:11:48 +0000445 # Tear everything down. This is a decent use for bottom-up on
446 # Windows, which doesn't have a recursive delete command. The
447 # (not so) subtlety is that rmdir will fail unless the dir's
448 # kids are removed first, so bottom up is essential.
Walter Dörwald21d3a322003-05-01 17:45:56 +0000449 for root, dirs, files in os.walk(test_support.TESTFN, topdown=False):
Tim Petersc4e09402003-04-25 07:11:48 +0000450 for name in files:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000451 os.remove(os.path.join(root, name))
Tim Petersc4e09402003-04-25 07:11:48 +0000452 for name in dirs:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000453 dirname = os.path.join(root, name)
454 if not os.path.islink(dirname):
455 os.rmdir(dirname)
456 else:
457 os.remove(dirname)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000458 os.rmdir(test_support.TESTFN)
Tim Petersc4e09402003-04-25 07:11:48 +0000459
Guido van Rossume7ba4952007-06-06 23:52:48 +0000460class MakedirTests(unittest.TestCase):
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000461 def setUp(self):
462 os.mkdir(test_support.TESTFN)
463
464 def test_makedir(self):
465 base = test_support.TESTFN
466 path = os.path.join(base, 'dir1', 'dir2', 'dir3')
467 os.makedirs(path) # Should work
468 path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4')
469 os.makedirs(path)
470
471 # Try paths with a '.' in them
472 self.failUnlessRaises(OSError, os.makedirs, os.curdir)
473 path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os.curdir)
474 os.makedirs(path)
475 path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4',
476 'dir5', 'dir6')
477 os.makedirs(path)
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000478
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000479 def tearDown(self):
480 path = os.path.join(test_support.TESTFN, 'dir1', 'dir2', 'dir3',
481 'dir4', 'dir5', 'dir6')
482 # If the tests failed, the bottom-most directory ('../dir6')
483 # may not have been created, so we look for the outermost directory
484 # that exists.
485 while not os.path.exists(path) and path != test_support.TESTFN:
486 path = os.path.dirname(path)
487
488 os.removedirs(path)
489
Guido van Rossume7ba4952007-06-06 23:52:48 +0000490class DevNullTests(unittest.TestCase):
Martin v. Löwisbdec50f2004-06-08 08:29:33 +0000491 def test_devnull(self):
Alex Martelli01c77c62006-08-24 02:58:11 +0000492 f = open(os.devnull, 'w')
Martin v. Löwisbdec50f2004-06-08 08:29:33 +0000493 f.write('hello')
494 f.close()
Alex Martelli01c77c62006-08-24 02:58:11 +0000495 f = open(os.devnull, 'r')
Tim Peters4182cfd2004-06-08 20:34:34 +0000496 self.assertEqual(f.read(), '')
Martin v. Löwisbdec50f2004-06-08 08:29:33 +0000497 f.close()
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000498
Guido van Rossume7ba4952007-06-06 23:52:48 +0000499class URandomTests(unittest.TestCase):
Martin v. Löwisdc3883f2004-08-29 15:46:35 +0000500 def test_urandom(self):
501 try:
502 self.assertEqual(len(os.urandom(1)), 1)
503 self.assertEqual(len(os.urandom(10)), 10)
504 self.assertEqual(len(os.urandom(100)), 100)
505 self.assertEqual(len(os.urandom(1000)), 1000)
506 except NotImplementedError:
507 pass
508
Guido van Rossume7ba4952007-06-06 23:52:48 +0000509class ExecTests(unittest.TestCase):
510 def test_execvpe_with_bad_program(self):
Thomas Hellerbd315c52007-08-30 17:57:21 +0000511 self.assertRaises(OSError, os.execvpe, 'no such app-', ['no such app-'], None)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000512
Thomas Heller6790d602007-08-30 17:15:14 +0000513 def test_execvpe_with_bad_arglist(self):
514 self.assertRaises(ValueError, os.execvpe, 'notepad', [], None)
515
Thomas Wouters477c8d52006-05-27 19:21:47 +0000516class Win32ErrorTests(unittest.TestCase):
517 def test_rename(self):
518 self.assertRaises(WindowsError, os.rename, test_support.TESTFN, test_support.TESTFN+".bak")
519
520 def test_remove(self):
521 self.assertRaises(WindowsError, os.remove, test_support.TESTFN)
522
523 def test_chdir(self):
524 self.assertRaises(WindowsError, os.chdir, test_support.TESTFN)
525
526 def test_mkdir(self):
527 self.assertRaises(WindowsError, os.chdir, test_support.TESTFN)
528
529 def test_utime(self):
530 self.assertRaises(WindowsError, os.utime, test_support.TESTFN, None)
531
532 def test_access(self):
533 self.assertRaises(WindowsError, os.utime, test_support.TESTFN, 0)
534
535 def test_chmod(self):
536 self.assertRaises(WindowsError, os.utime, test_support.TESTFN, 0)
537
538if sys.platform != 'win32':
539 class Win32ErrorTests(unittest.TestCase):
540 pass
541
Fred Drake2e2be372001-09-20 21:33:42 +0000542def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000543 test_support.run_unittest(
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000544 FileTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000545 StatAttributeTests,
546 EnvironTests,
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000547 WalkTests,
548 MakedirTests,
Martin v. Löwisbdec50f2004-06-08 08:29:33 +0000549 DevNullTests,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000550 URandomTests,
Guido van Rossume7ba4952007-06-06 23:52:48 +0000551 ExecTests,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000552 Win32ErrorTests
Walter Dörwald21d3a322003-05-01 17:45:56 +0000553 )
Fred Drake2e2be372001-09-20 21:33:42 +0000554
555if __name__ == "__main__":
556 test_main()