1 # 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
3 # portable than they had been thought to be.
10 from test
import test_support
12 warnings
.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__
)
13 warnings
.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__
)
15 # Tests creating TESTFN
16 class FileTests(unittest
.TestCase
):
18 if os
.path
.exists(test_support
.TESTFN
):
19 os
.unlink(test_support
.TESTFN
)
22 def test_access(self
):
23 f
= os
.open(test_support
.TESTFN
, os
.O_CREAT|os
.O_RDWR
)
25 self
.assertTrue(os
.access(test_support
.TESTFN
, os
.W_OK
))
27 def test_closerange(self
):
28 first
= os
.open(test_support
.TESTFN
, os
.O_CREAT|os
.O_RDWR
)
29 # We must allocate two consecutive file descriptors, otherwise
30 # it will mess up other file descriptors (perhaps even the three
32 second
= os
.dup(first
)
35 while second
!= first
+ 1:
40 self
.skipTest("couldn't allocate two consecutive fds")
41 first
, second
= second
, os
.dup(second
)
44 # close a fd that is open, and one that isn't
45 os
.closerange(first
, first
+ 2)
46 self
.assertRaises(OSError, os
.write
, first
, "a")
48 def test_rename(self
):
49 path
= unicode(test_support
.TESTFN
)
50 old
= sys
.getrefcount(path
)
51 self
.assertRaises(TypeError, os
.rename
, path
, 0)
52 new
= sys
.getrefcount(path
)
53 self
.assertEqual(old
, new
)
56 class TemporaryFileTests(unittest
.TestCase
):
59 os
.mkdir(test_support
.TESTFN
)
62 for name
in self
.files
:
64 os
.rmdir(test_support
.TESTFN
)
66 def check_tempfile(self
, name
):
67 # make sure it doesn't already exist:
68 self
.assertFalse(os
.path
.exists(name
),
69 "file already exists for temporary file")
70 # make sure we can create the file
72 self
.files
.append(name
)
74 def test_tempnam(self
):
75 if not hasattr(os
, "tempnam"):
77 warnings
.filterwarnings("ignore", "tempnam", RuntimeWarning,
79 self
.check_tempfile(os
.tempnam())
81 name
= os
.tempnam(test_support
.TESTFN
)
82 self
.check_tempfile(name
)
84 name
= os
.tempnam(test_support
.TESTFN
, "pfx")
85 self
.assertTrue(os
.path
.basename(name
)[:3] == "pfx")
86 self
.check_tempfile(name
)
88 def test_tmpfile(self
):
89 if not hasattr(os
, "tmpfile"):
91 # As with test_tmpnam() below, the Windows implementation of tmpfile()
92 # attempts to create a file in the root directory of the current drive.
93 # On Vista and Server 2008, this test will always fail for normal users
94 # as writing to the root directory requires elevated privileges. With
95 # XP and below, the semantics of tmpfile() are the same, but the user
96 # running the test is more likely to have administrative privileges on
97 # their account already. If that's the case, then os.tmpfile() should
98 # work. In order to make this test as useful as possible, rather than
99 # trying to detect Windows versions or whether or not the user has the
100 # right permissions, just try and create a file in the root directory
101 # and see if it raises a 'Permission denied' OSError. If it does, then
102 # test that a subsequent call to os.tmpfile() raises the same error. If
103 # it doesn't, assume we're on XP or below and the user running the test
104 # has administrative privileges, and proceed with the test as normal.
105 if sys
.platform
== 'win32':
106 name
= '\\python_test_os_test_tmpfile.txt'
107 if os
.path
.exists(name
):
111 except IOError, first
:
112 # open() failed, assert tmpfile() fails in the same way.
113 # Although open() raises an IOError and os.tmpfile() raises an
114 # OSError(), 'args' will be (13, 'Permission denied') in both
118 except OSError, second
:
119 self
.assertEqual(first
.args
, second
.args
)
121 self
.fail("expected os.tmpfile() to raise OSError")
124 # open() worked, therefore, tmpfile() should work. Close our
125 # dummy file and proceed with the test as normal.
134 self
.assertTrue(s
== "foobar")
136 def test_tmpnam(self
):
138 if not hasattr(os
, "tmpnam"):
140 warnings
.filterwarnings("ignore", "tmpnam", RuntimeWarning,
143 if sys
.platform
in ("win32",):
144 # The Windows tmpnam() seems useless. From the MS docs:
146 # The character string that tmpnam creates consists of
147 # the path prefix, defined by the entry P_tmpdir in the
148 # file STDIO.H, followed by a sequence consisting of the
149 # digit characters '0' through '9'; the numerical value
150 # of this string is in the range 1 - 65,535. Changing the
151 # definitions of L_tmpnam or P_tmpdir in STDIO.H does not
152 # change the operation of tmpnam.
154 # The really bizarre part is that, at least under MSVC6,
155 # P_tmpdir is "\\". That is, the path returned refers to
156 # the root of the current drive. That's a terrible place to
157 # put temp files, and, depending on privileges, the user
158 # may not even be able to open a file in the root directory.
159 self
.assertFalse(os
.path
.exists(name
),
160 "file already exists for temporary file")
162 self
.check_tempfile(name
)
164 # Test attributes on return values from os.*stat* family.
165 class StatAttributeTests(unittest
.TestCase
):
167 os
.mkdir(test_support
.TESTFN
)
168 self
.fname
= os
.path
.join(test_support
.TESTFN
, "f1")
169 f
= open(self
.fname
, 'wb')
174 os
.unlink(self
.fname
)
175 os
.rmdir(test_support
.TESTFN
)
177 def test_stat_attributes(self
):
178 if not hasattr(os
, "stat"):
182 result
= os
.stat(self
.fname
)
184 # Make sure direct access works
185 self
.assertEquals(result
[stat
.ST_SIZE
], 3)
186 self
.assertEquals(result
.st_size
, 3)
190 # Make sure all the attributes are there
191 members
= dir(result
)
192 for name
in dir(stat
):
193 if name
[:3] == 'ST_':
195 if name
.endswith("TIME"):
196 def trunc(x
): return int(x
)
198 def trunc(x
): return x
199 self
.assertEquals(trunc(getattr(result
, attr
)),
200 result
[getattr(stat
, name
)])
201 self
.assertTrue(attr
in members
)
205 self
.fail("No exception thrown")
209 # Make sure that assignment fails
212 self
.fail("No exception thrown")
218 self
.fail("No exception thrown")
219 except (AttributeError, TypeError):
224 self
.fail("No exception thrown")
225 except AttributeError:
228 # Use the stat_result constructor with a too-short tuple.
230 result2
= os
.stat_result((10,))
231 self
.fail("No exception thrown")
235 # Use the constructr with a too-long tuple.
237 result2
= os
.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
242 def test_statvfs_attributes(self
):
243 if not hasattr(os
, "statvfs"):
247 result
= os
.statvfs(self
.fname
)
249 # On AtheOS, glibc always returns ENOSYS
250 if e
.errno
== errno
.ENOSYS
:
253 # Make sure direct access works
254 self
.assertEquals(result
.f_bfree
, result
[3])
256 # Make sure all the attributes are there.
257 members
= ('bsize', 'frsize', 'blocks', 'bfree', 'bavail', 'files',
258 'ffree', 'favail', 'flag', 'namemax')
259 for value
, member
in enumerate(members
):
260 self
.assertEquals(getattr(result
, 'f_' + member
), result
[value
])
262 # Make sure that assignment really fails
265 self
.fail("No exception thrown")
271 self
.fail("No exception thrown")
272 except AttributeError:
275 # Use the constructor with a too-short tuple.
277 result2
= os
.statvfs_result((10,))
278 self
.fail("No exception thrown")
282 # Use the constructr with a too-long tuple.
284 result2
= os
.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
288 def test_utime_dir(self
):
290 st
= os
.stat(test_support
.TESTFN
)
291 # round to int, because some systems may support sub-second
292 # time stamps in stat, but not in utime.
293 os
.utime(test_support
.TESTFN
, (st
.st_atime
, int(st
.st_mtime
-delta
)))
294 st2
= os
.stat(test_support
.TESTFN
)
295 self
.assertEquals(st2
.st_mtime
, int(st
.st_mtime
-delta
))
297 # Restrict test to Win32, since there is no guarantee other
298 # systems support centiseconds
299 if sys
.platform
== 'win32':
300 def get_file_system(path
):
301 root
= os
.path
.splitdrive(os
.path
.abspath(path
))[0] + '\\'
303 kernel32
= ctypes
.windll
.kernel32
304 buf
= ctypes
.create_string_buffer("", 100)
305 if kernel32
.GetVolumeInformationA(root
, None, 0, None, None, None, buf
, len(buf
)):
308 if get_file_system(test_support
.TESTFN
) == "NTFS":
309 def test_1565150(self
):
311 os
.utime(self
.fname
, (t1
, t1
))
312 self
.assertEquals(os
.stat(self
.fname
).st_mtime
, t1
)
314 def test_1686475(self
):
315 # Verify that an open file can be stat'ed
317 os
.stat(r
"c:\pagefile.sys")
318 except WindowsError, e
:
319 if e
.errno
== 2: # file does not exist; cannot run test
321 self
.fail("Could not stat pagefile.sys")
323 from test
import mapping_tests
325 class EnvironTests(mapping_tests
.BasicTestMappingProtocol
):
326 """check that os.environ object conform to mapping protocol"""
328 def _reference(self
):
329 return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"}
330 def _empty_mapping(self
):
334 self
.__save
= dict(os
.environ
)
338 os
.environ
.update(self
.__save
)
341 def test_update2(self
):
342 if os
.path
.exists("/bin/sh"):
343 os
.environ
.update(HELLO
="World")
344 value
= os
.popen("/bin/sh -c 'echo $HELLO'").read().strip()
345 self
.assertEquals(value
, "World")
347 class WalkTests(unittest
.TestCase
):
348 """Tests for os.walk()."""
350 def test_traversal(self
):
352 from os
.path
import join
356 # TEST1/ a file kid and two directory kids
358 # SUB1/ a file kid and a directory kid
361 # SUB2/ a file kid and a dirsymlink kid
363 # link/ a symlink to TESTFN.2
366 walk_path
= join(test_support
.TESTFN
, "TEST1")
367 sub1_path
= join(walk_path
, "SUB1")
368 sub11_path
= join(sub1_path
, "SUB11")
369 sub2_path
= join(walk_path
, "SUB2")
370 tmp1_path
= join(walk_path
, "tmp1")
371 tmp2_path
= join(sub1_path
, "tmp2")
372 tmp3_path
= join(sub2_path
, "tmp3")
373 link_path
= join(sub2_path
, "link")
374 t2_path
= join(test_support
.TESTFN
, "TEST2")
375 tmp4_path
= join(test_support
.TESTFN
, "TEST2", "tmp4")
378 os
.makedirs(sub11_path
)
379 os
.makedirs(sub2_path
)
381 for path
in tmp1_path
, tmp2_path
, tmp3_path
, tmp4_path
:
383 f
.write("I'm " + path
+ " and proud of it. Blame test_os.\n")
385 if hasattr(os
, "symlink"):
386 os
.symlink(os
.path
.abspath(t2_path
), link_path
)
387 sub2_tree
= (sub2_path
, ["link"], ["tmp3"])
389 sub2_tree
= (sub2_path
, [], ["tmp3"])
392 all
= list(os
.walk(walk_path
))
393 self
.assertEqual(len(all
), 4)
394 # We can't know which order SUB1 and SUB2 will appear in.
395 # Not flipped: TESTFN, SUB1, SUB11, SUB2
396 # flipped: TESTFN, SUB2, SUB1, SUB11
397 flipped
= all
[0][1][0] != "SUB1"
399 self
.assertEqual(all
[0], (walk_path
, ["SUB1", "SUB2"], ["tmp1"]))
400 self
.assertEqual(all
[1 + flipped
], (sub1_path
, ["SUB11"], ["tmp2"]))
401 self
.assertEqual(all
[2 + flipped
], (sub11_path
, [], []))
402 self
.assertEqual(all
[3 - 2 * flipped
], sub2_tree
)
406 for root
, dirs
, files
in os
.walk(walk_path
):
407 all
.append((root
, dirs
, files
))
408 # Don't descend into SUB1.
410 # Note that this also mutates the dirs we appended to all!
412 self
.assertEqual(len(all
), 2)
413 self
.assertEqual(all
[0], (walk_path
, ["SUB2"], ["tmp1"]))
414 self
.assertEqual(all
[1], sub2_tree
)
417 all
= list(os
.walk(walk_path
, topdown
=False))
418 self
.assertEqual(len(all
), 4)
419 # We can't know which order SUB1 and SUB2 will appear in.
420 # Not flipped: SUB11, SUB1, SUB2, TESTFN
421 # flipped: SUB2, SUB11, SUB1, TESTFN
422 flipped
= all
[3][1][0] != "SUB1"
424 self
.assertEqual(all
[3], (walk_path
, ["SUB1", "SUB2"], ["tmp1"]))
425 self
.assertEqual(all
[flipped
], (sub11_path
, [], []))
426 self
.assertEqual(all
[flipped
+ 1], (sub1_path
, ["SUB11"], ["tmp2"]))
427 self
.assertEqual(all
[2 - 2 * flipped
], sub2_tree
)
429 if hasattr(os
, "symlink"):
430 # Walk, following symlinks.
431 for root
, dirs
, files
in os
.walk(walk_path
, followlinks
=True):
432 if root
== link_path
:
433 self
.assertEqual(dirs
, [])
434 self
.assertEqual(files
, ["tmp4"])
437 self
.fail("Didn't follow symlink with followlinks=True")
440 # Tear everything down. This is a decent use for bottom-up on
441 # Windows, which doesn't have a recursive delete command. The
442 # (not so) subtlety is that rmdir will fail unless the dir's
443 # kids are removed first, so bottom up is essential.
444 for root
, dirs
, files
in os
.walk(test_support
.TESTFN
, topdown
=False):
446 os
.remove(os
.path
.join(root
, name
))
448 dirname
= os
.path
.join(root
, name
)
449 if not os
.path
.islink(dirname
):
453 os
.rmdir(test_support
.TESTFN
)
455 class MakedirTests (unittest
.TestCase
):
457 os
.mkdir(test_support
.TESTFN
)
459 def test_makedir(self
):
460 base
= test_support
.TESTFN
461 path
= os
.path
.join(base
, 'dir1', 'dir2', 'dir3')
462 os
.makedirs(path
) # Should work
463 path
= os
.path
.join(base
, 'dir1', 'dir2', 'dir3', 'dir4')
466 # Try paths with a '.' in them
467 self
.assertRaises(OSError, os
.makedirs
, os
.curdir
)
468 path
= os
.path
.join(base
, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os
.curdir
)
470 path
= os
.path
.join(base
, 'dir1', os
.curdir
, 'dir2', 'dir3', 'dir4',
478 path
= os
.path
.join(test_support
.TESTFN
, 'dir1', 'dir2', 'dir3',
479 'dir4', 'dir5', 'dir6')
480 # If the tests failed, the bottom-most directory ('../dir6')
481 # may not have been created, so we look for the outermost directory
483 while not os
.path
.exists(path
) and path
!= test_support
.TESTFN
:
484 path
= os
.path
.dirname(path
)
488 class DevNullTests (unittest
.TestCase
):
489 def test_devnull(self
):
490 f
= file(os
.devnull
, 'w')
493 f
= file(os
.devnull
, 'r')
494 self
.assertEqual(f
.read(), '')
497 class URandomTests (unittest
.TestCase
):
498 def test_urandom(self
):
500 self
.assertEqual(len(os
.urandom(1)), 1)
501 self
.assertEqual(len(os
.urandom(10)), 10)
502 self
.assertEqual(len(os
.urandom(100)), 100)
503 self
.assertEqual(len(os
.urandom(1000)), 1000)
504 # see http://bugs.python.org/issue3708
505 self
.assertRaises(TypeError, os
.urandom
, 0.9)
506 self
.assertRaises(TypeError, os
.urandom
, 1.1)
507 self
.assertRaises(TypeError, os
.urandom
, 2.0)
508 except NotImplementedError:
511 class Win32ErrorTests(unittest
.TestCase
):
512 def test_rename(self
):
513 self
.assertRaises(WindowsError, os
.rename
, test_support
.TESTFN
, test_support
.TESTFN
+".bak")
515 def test_remove(self
):
516 self
.assertRaises(WindowsError, os
.remove
, test_support
.TESTFN
)
518 def test_chdir(self
):
519 self
.assertRaises(WindowsError, os
.chdir
, test_support
.TESTFN
)
521 def test_mkdir(self
):
522 f
= open(test_support
.TESTFN
, "w")
524 self
.assertRaises(WindowsError, os
.mkdir
, test_support
.TESTFN
)
527 os
.unlink(test_support
.TESTFN
)
529 def test_utime(self
):
530 self
.assertRaises(WindowsError, os
.utime
, test_support
.TESTFN
, None)
532 def test_chmod(self
):
533 self
.assertRaises(WindowsError, os
.chmod
, test_support
.TESTFN
, 0)
535 class TestInvalidFD(unittest
.TestCase
):
536 singles
= ["fchdir", "fdopen", "dup", "fdatasync", "fstat",
537 "fstatvfs", "fsync", "tcgetpgrp", "ttyname"]
538 #singles.append("close")
539 #We omit close because it doesn'r raise an exception on some platforms
543 self
.check(getattr(os
, f
))
546 locals()["test_"+f
] = get_single(f
)
548 def check(self
, f
, *args
):
550 f(test_support
.make_bad_fd(), *args
)
552 self
.assertEqual(e
.errno
, errno
.EBADF
)
554 self
.fail("%r didn't raise a OSError with a bad file descriptor"
557 def test_isatty(self
):
558 if hasattr(os
, "isatty"):
559 self
.assertEqual(os
.isatty(test_support
.make_bad_fd()), False)
561 def test_closerange(self
):
562 if hasattr(os
, "closerange"):
563 fd
= test_support
.make_bad_fd()
564 # Make sure none of the descriptors we are about to close are
565 # currently valid (issue 6542).
573 raise unittest
.SkipTest(
574 "Unable to acquire a range of invalid file descriptors")
575 self
.assertEqual(os
.closerange(fd
, fd
+ i
-1), None)
578 if hasattr(os
, "dup2"):
579 self
.check(os
.dup2
, 20)
581 def test_fchmod(self
):
582 if hasattr(os
, "fchmod"):
583 self
.check(os
.fchmod
, 0)
585 def test_fchown(self
):
586 if hasattr(os
, "fchown"):
587 self
.check(os
.fchown
, -1, -1)
589 def test_fpathconf(self
):
590 if hasattr(os
, "fpathconf"):
591 self
.check(os
.fpathconf
, "PC_NAME_MAX")
593 def test_ftruncate(self
):
594 if hasattr(os
, "ftruncate"):
595 self
.check(os
.ftruncate
, 0)
597 def test_lseek(self
):
598 if hasattr(os
, "lseek"):
599 self
.check(os
.lseek
, 0, 0)
602 if hasattr(os
, "read"):
603 self
.check(os
.read
, 1)
605 def test_tcsetpgrpt(self
):
606 if hasattr(os
, "tcsetpgrp"):
607 self
.check(os
.tcsetpgrp
, 0)
609 def test_write(self
):
610 if hasattr(os
, "write"):
611 self
.check(os
.write
, " ")
613 if sys
.platform
!= 'win32':
614 class Win32ErrorTests(unittest
.TestCase
):
617 class PosixUidGidTests(unittest
.TestCase
):
618 if hasattr(os
, 'setuid'):
619 def test_setuid(self
):
621 self
.assertRaises(os
.error
, os
.setuid
, 0)
622 self
.assertRaises(OverflowError, os
.setuid
, 1<<32)
624 if hasattr(os
, 'setgid'):
625 def test_setgid(self
):
627 self
.assertRaises(os
.error
, os
.setgid
, 0)
628 self
.assertRaises(OverflowError, os
.setgid
, 1<<32)
630 if hasattr(os
, 'seteuid'):
631 def test_seteuid(self
):
633 self
.assertRaises(os
.error
, os
.seteuid
, 0)
634 self
.assertRaises(OverflowError, os
.seteuid
, 1<<32)
636 if hasattr(os
, 'setegid'):
637 def test_setegid(self
):
639 self
.assertRaises(os
.error
, os
.setegid
, 0)
640 self
.assertRaises(OverflowError, os
.setegid
, 1<<32)
642 if hasattr(os
, 'setreuid'):
643 def test_setreuid(self
):
645 self
.assertRaises(os
.error
, os
.setreuid
, 0, 0)
646 self
.assertRaises(OverflowError, os
.setreuid
, 1<<32, 0)
647 self
.assertRaises(OverflowError, os
.setreuid
, 0, 1<<32)
649 if hasattr(os
, 'setregid'):
650 def test_setregid(self
):
652 self
.assertRaises(os
.error
, os
.setregid
, 0, 0)
653 self
.assertRaises(OverflowError, os
.setregid
, 1<<32, 0)
654 self
.assertRaises(OverflowError, os
.setregid
, 0, 1<<32)
656 class PosixUidGidTests(unittest
.TestCase
):
660 test_support
.run_unittest(
674 if __name__
== "__main__":