blob: 503244a24ffc8a6cfc446c92b24956f19098cddb [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
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000219 try:
220 result = os.statvfs(self.fname)
Guido van Rossumb940e112007-01-10 16:19:56 +0000221 except OSError as e:
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000222 # On AtheOS, glibc always returns ENOSYS
223 import errno
224 if e.errno == errno.ENOSYS:
225 return
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000226
227 # Make sure direct access works
Brett Cannoncfaf10c2008-05-16 00:45:35 +0000228 self.assertEquals(result.f_bfree, result[3])
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000229
Brett Cannoncfaf10c2008-05-16 00:45:35 +0000230 # Make sure all the attributes are there.
231 members = ('bsize', 'frsize', 'blocks', 'bfree', 'bavail', 'files',
232 'ffree', 'favail', 'flag', 'namemax')
233 for value, member in enumerate(members):
234 self.assertEquals(getattr(result, 'f_' + member), result[value])
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000235
236 # Make sure that assignment really fails
237 try:
238 result.f_bfree = 1
239 self.fail("No exception thrown")
Collin Winter42dae6a2007-03-28 21:44:53 +0000240 except AttributeError:
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000241 pass
242
243 try:
244 result.parrot = 1
245 self.fail("No exception thrown")
246 except AttributeError:
247 pass
248
249 # Use the constructor with a too-short tuple.
250 try:
251 result2 = os.statvfs_result((10,))
252 self.fail("No exception thrown")
253 except TypeError:
254 pass
255
256 # Use the constructr with a too-long tuple.
257 try:
258 result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
259 except TypeError:
260 pass
Fred Drake38c2ef02001-07-17 20:52:51 +0000261
Thomas Wouters89f507f2006-12-13 04:49:30 +0000262 def test_utime_dir(self):
263 delta = 1000000
264 st = os.stat(test_support.TESTFN)
265 # round to int, because some systems may support sub-second
266 # time stamps in stat, but not in utime.
267 os.utime(test_support.TESTFN, (st.st_atime, int(st.st_mtime-delta)))
268 st2 = os.stat(test_support.TESTFN)
269 self.assertEquals(st2.st_mtime, int(st.st_mtime-delta))
270
271 # Restrict test to Win32, since there is no guarantee other
272 # systems support centiseconds
273 if sys.platform == 'win32':
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000274 def get_file_system(path):
275 import os
276 root = os.path.splitdrive(os.path.realpath("."))[0] + '\\'
277 import ctypes
278 kernel32 = ctypes.windll.kernel32
279 buf = ctypes.create_string_buffer("", 100)
280 if kernel32.GetVolumeInformationA(root, None, 0, None, None, None, buf, len(buf)):
281 return buf.value
282
283 if get_file_system(test_support.TESTFN) == "NTFS":
284 def test_1565150(self):
285 t1 = 1159195039.25
286 os.utime(self.fname, (t1, t1))
287 self.assertEquals(os.stat(self.fname).st_mtime, t1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000288
Guido van Rossumd8faa362007-04-27 19:54:29 +0000289 def test_1686475(self):
290 # Verify that an open file can be stat'ed
291 try:
292 os.stat(r"c:\pagefile.sys")
293 except WindowsError as e:
294 if e == 2: # file does not exist; cannot run test
295 return
296 self.fail("Could not stat pagefile.sys")
297
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000298from test import mapping_tests
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000299
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000300class EnvironTests(mapping_tests.BasicTestMappingProtocol):
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000301 """check that os.environ object conform to mapping protocol"""
Walter Dörwald118f9312004-06-02 18:42:25 +0000302 type2test = None
Christian Heimes90333392007-11-01 19:08:42 +0000303
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000304 def setUp(self):
305 self.__save = dict(os.environ)
Christian Heimes90333392007-11-01 19:08:42 +0000306 for key, value in self._reference().items():
307 os.environ[key] = value
308
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000309 def tearDown(self):
310 os.environ.clear()
311 os.environ.update(self.__save)
312
Christian Heimes90333392007-11-01 19:08:42 +0000313 def _reference(self):
314 return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"}
315
316 def _empty_mapping(self):
317 os.environ.clear()
318 return os.environ
319
Martin v. Löwis1d11de62005-01-29 13:29:23 +0000320 # Bug 1110478
Martin v. Löwis5510f652005-02-17 21:23:20 +0000321 def test_update2(self):
Christian Heimes90333392007-11-01 19:08:42 +0000322 os.environ.clear()
Martin v. Löwis1d11de62005-01-29 13:29:23 +0000323 if os.path.exists("/bin/sh"):
324 os.environ.update(HELLO="World")
325 value = os.popen("/bin/sh -c 'echo $HELLO'").read().strip()
326 self.assertEquals(value, "World")
327
Christian Heimes1a13d592007-11-08 14:16:55 +0000328 def test_os_popen_iter(self):
329 if os.path.exists("/bin/sh"):
330 popen = os.popen("/bin/sh -c 'echo \"line1\nline2\nline3\"'")
331 it = iter(popen)
332 self.assertEquals(next(it), "line1\n")
333 self.assertEquals(next(it), "line2\n")
334 self.assertEquals(next(it), "line3\n")
335 self.assertRaises(StopIteration, next, it)
336
Guido van Rossum67aca9e2007-06-13 21:51:27 +0000337 # Verify environ keys and values from the OS are of the
338 # correct str type.
339 def test_keyvalue_types(self):
340 for key, val in os.environ.items():
341 self.assertEquals(type(key), str)
342 self.assertEquals(type(val), str)
343
Christian Heimes90333392007-11-01 19:08:42 +0000344 def test_items(self):
345 for key, value in self._reference().items():
346 self.assertEqual(os.environ.get(key), value)
347
Tim Petersc4e09402003-04-25 07:11:48 +0000348class WalkTests(unittest.TestCase):
349 """Tests for os.walk()."""
350
351 def test_traversal(self):
352 import os
353 from os.path import join
354
355 # Build:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000356 # TESTFN/
357 # TEST1/ a file kid and two directory kids
Tim Petersc4e09402003-04-25 07:11:48 +0000358 # tmp1
359 # SUB1/ a file kid and a directory kid
Guido van Rossumd8faa362007-04-27 19:54:29 +0000360 # tmp2
361 # SUB11/ no kids
362 # SUB2/ a file kid and a dirsymlink kid
363 # tmp3
364 # link/ a symlink to TESTFN.2
365 # TEST2/
366 # tmp4 a lone file
367 walk_path = join(test_support.TESTFN, "TEST1")
368 sub1_path = join(walk_path, "SUB1")
Tim Petersc4e09402003-04-25 07:11:48 +0000369 sub11_path = join(sub1_path, "SUB11")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000370 sub2_path = join(walk_path, "SUB2")
371 tmp1_path = join(walk_path, "tmp1")
Tim Petersc4e09402003-04-25 07:11:48 +0000372 tmp2_path = join(sub1_path, "tmp2")
373 tmp3_path = join(sub2_path, "tmp3")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000374 link_path = join(sub2_path, "link")
375 t2_path = join(test_support.TESTFN, "TEST2")
376 tmp4_path = join(test_support.TESTFN, "TEST2", "tmp4")
Tim Petersc4e09402003-04-25 07:11:48 +0000377
378 # Create stuff.
379 os.makedirs(sub11_path)
380 os.makedirs(sub2_path)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000381 os.makedirs(t2_path)
382 for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
Alex Martelli01c77c62006-08-24 02:58:11 +0000383 f = open(path, "w")
Tim Petersc4e09402003-04-25 07:11:48 +0000384 f.write("I'm " + path + " and proud of it. Blame test_os.\n")
385 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000386 if hasattr(os, "symlink"):
387 os.symlink(os.path.abspath(t2_path), link_path)
388 sub2_tree = (sub2_path, ["link"], ["tmp3"])
389 else:
390 sub2_tree = (sub2_path, [], ["tmp3"])
Tim Petersc4e09402003-04-25 07:11:48 +0000391
392 # Walk top-down.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000393 all = list(os.walk(walk_path))
Tim Petersc4e09402003-04-25 07:11:48 +0000394 self.assertEqual(len(all), 4)
395 # We can't know which order SUB1 and SUB2 will appear in.
396 # Not flipped: TESTFN, SUB1, SUB11, SUB2
397 # flipped: TESTFN, SUB2, SUB1, SUB11
398 flipped = all[0][1][0] != "SUB1"
399 all[0][1].sort()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000400 self.assertEqual(all[0], (walk_path, ["SUB1", "SUB2"], ["tmp1"]))
Tim Petersc4e09402003-04-25 07:11:48 +0000401 self.assertEqual(all[1 + flipped], (sub1_path, ["SUB11"], ["tmp2"]))
402 self.assertEqual(all[2 + flipped], (sub11_path, [], []))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000403 self.assertEqual(all[3 - 2 * flipped], sub2_tree)
Tim Petersc4e09402003-04-25 07:11:48 +0000404
405 # Prune the search.
406 all = []
Guido van Rossumd8faa362007-04-27 19:54:29 +0000407 for root, dirs, files in os.walk(walk_path):
Tim Petersc4e09402003-04-25 07:11:48 +0000408 all.append((root, dirs, files))
409 # Don't descend into SUB1.
410 if 'SUB1' in dirs:
411 # Note that this also mutates the dirs we appended to all!
412 dirs.remove('SUB1')
413 self.assertEqual(len(all), 2)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000414 self.assertEqual(all[0], (walk_path, ["SUB2"], ["tmp1"]))
415 self.assertEqual(all[1], sub2_tree)
Tim Petersc4e09402003-04-25 07:11:48 +0000416
417 # Walk bottom-up.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000418 all = list(os.walk(walk_path, topdown=False))
Tim Petersc4e09402003-04-25 07:11:48 +0000419 self.assertEqual(len(all), 4)
420 # We can't know which order SUB1 and SUB2 will appear in.
421 # Not flipped: SUB11, SUB1, SUB2, TESTFN
422 # flipped: SUB2, SUB11, SUB1, TESTFN
423 flipped = all[3][1][0] != "SUB1"
424 all[3][1].sort()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000425 self.assertEqual(all[3], (walk_path, ["SUB1", "SUB2"], ["tmp1"]))
Tim Petersc4e09402003-04-25 07:11:48 +0000426 self.assertEqual(all[flipped], (sub11_path, [], []))
427 self.assertEqual(all[flipped + 1], (sub1_path, ["SUB11"], ["tmp2"]))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000428 self.assertEqual(all[2 - 2 * flipped], sub2_tree)
Tim Petersc4e09402003-04-25 07:11:48 +0000429
Guido van Rossumd8faa362007-04-27 19:54:29 +0000430 if hasattr(os, "symlink"):
431 # Walk, following symlinks.
432 for root, dirs, files in os.walk(walk_path, followlinks=True):
433 if root == link_path:
434 self.assertEqual(dirs, [])
435 self.assertEqual(files, ["tmp4"])
436 break
437 else:
438 self.fail("Didn't follow symlink with followlinks=True")
439
440 def tearDown(self):
Tim Petersc4e09402003-04-25 07:11:48 +0000441 # Tear everything down. This is a decent use for bottom-up on
442 # Windows, which doesn't have a recursive delete command. The
443 # (not so) subtlety is that rmdir will fail unless the dir's
444 # kids are removed first, so bottom up is essential.
Walter Dörwald21d3a322003-05-01 17:45:56 +0000445 for root, dirs, files in os.walk(test_support.TESTFN, topdown=False):
Tim Petersc4e09402003-04-25 07:11:48 +0000446 for name in files:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000447 os.remove(os.path.join(root, name))
Tim Petersc4e09402003-04-25 07:11:48 +0000448 for name in dirs:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000449 dirname = os.path.join(root, name)
450 if not os.path.islink(dirname):
451 os.rmdir(dirname)
452 else:
453 os.remove(dirname)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000454 os.rmdir(test_support.TESTFN)
Tim Petersc4e09402003-04-25 07:11:48 +0000455
Guido van Rossume7ba4952007-06-06 23:52:48 +0000456class MakedirTests(unittest.TestCase):
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000457 def setUp(self):
458 os.mkdir(test_support.TESTFN)
459
460 def test_makedir(self):
461 base = test_support.TESTFN
462 path = os.path.join(base, 'dir1', 'dir2', 'dir3')
463 os.makedirs(path) # Should work
464 path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4')
465 os.makedirs(path)
466
467 # Try paths with a '.' in them
468 self.failUnlessRaises(OSError, os.makedirs, os.curdir)
469 path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os.curdir)
470 os.makedirs(path)
471 path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4',
472 'dir5', 'dir6')
473 os.makedirs(path)
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000474
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000475 def tearDown(self):
476 path = os.path.join(test_support.TESTFN, 'dir1', 'dir2', 'dir3',
477 'dir4', 'dir5', 'dir6')
478 # If the tests failed, the bottom-most directory ('../dir6')
479 # may not have been created, so we look for the outermost directory
480 # that exists.
481 while not os.path.exists(path) and path != test_support.TESTFN:
482 path = os.path.dirname(path)
483
484 os.removedirs(path)
485
Guido van Rossume7ba4952007-06-06 23:52:48 +0000486class DevNullTests(unittest.TestCase):
Martin v. Löwisbdec50f2004-06-08 08:29:33 +0000487 def test_devnull(self):
Alex Martelli01c77c62006-08-24 02:58:11 +0000488 f = open(os.devnull, 'w')
Martin v. Löwisbdec50f2004-06-08 08:29:33 +0000489 f.write('hello')
490 f.close()
Alex Martelli01c77c62006-08-24 02:58:11 +0000491 f = open(os.devnull, 'r')
Tim Peters4182cfd2004-06-08 20:34:34 +0000492 self.assertEqual(f.read(), '')
Martin v. Löwisbdec50f2004-06-08 08:29:33 +0000493 f.close()
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000494
Guido van Rossume7ba4952007-06-06 23:52:48 +0000495class URandomTests(unittest.TestCase):
Martin v. Löwisdc3883f2004-08-29 15:46:35 +0000496 def test_urandom(self):
497 try:
498 self.assertEqual(len(os.urandom(1)), 1)
499 self.assertEqual(len(os.urandom(10)), 10)
500 self.assertEqual(len(os.urandom(100)), 100)
501 self.assertEqual(len(os.urandom(1000)), 1000)
502 except NotImplementedError:
503 pass
504
Guido van Rossume7ba4952007-06-06 23:52:48 +0000505class ExecTests(unittest.TestCase):
506 def test_execvpe_with_bad_program(self):
Thomas Hellerbd315c52007-08-30 17:57:21 +0000507 self.assertRaises(OSError, os.execvpe, 'no such app-', ['no such app-'], None)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000508
Thomas Heller6790d602007-08-30 17:15:14 +0000509 def test_execvpe_with_bad_arglist(self):
510 self.assertRaises(ValueError, os.execvpe, 'notepad', [], None)
511
Thomas Wouters477c8d52006-05-27 19:21:47 +0000512class Win32ErrorTests(unittest.TestCase):
513 def test_rename(self):
514 self.assertRaises(WindowsError, os.rename, test_support.TESTFN, test_support.TESTFN+".bak")
515
516 def test_remove(self):
517 self.assertRaises(WindowsError, os.remove, test_support.TESTFN)
518
519 def test_chdir(self):
520 self.assertRaises(WindowsError, os.chdir, test_support.TESTFN)
521
522 def test_mkdir(self):
523 self.assertRaises(WindowsError, os.chdir, test_support.TESTFN)
524
525 def test_utime(self):
526 self.assertRaises(WindowsError, os.utime, test_support.TESTFN, None)
527
528 def test_access(self):
529 self.assertRaises(WindowsError, os.utime, test_support.TESTFN, 0)
530
531 def test_chmod(self):
532 self.assertRaises(WindowsError, os.utime, test_support.TESTFN, 0)
533
534if sys.platform != 'win32':
535 class Win32ErrorTests(unittest.TestCase):
536 pass
537
Fred Drake2e2be372001-09-20 21:33:42 +0000538def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000539 test_support.run_unittest(
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000540 FileTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000541 StatAttributeTests,
542 EnvironTests,
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000543 WalkTests,
544 MakedirTests,
Martin v. Löwisbdec50f2004-06-08 08:29:33 +0000545 DevNullTests,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000546 URandomTests,
Guido van Rossume7ba4952007-06-06 23:52:48 +0000547 ExecTests,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000548 Win32ErrorTests
Walter Dörwald21d3a322003-05-01 17:45:56 +0000549 )
Fred Drake2e2be372001-09-20 21:33:42 +0000550
551if __name__ == "__main__":
552 test_main()