3 from test
.test_support
import TestSkipped
, TestFailed
, TESTFN
, run_suite
8 raise TestSkipped
, "posix is not available"
15 warnings
.filterwarnings('ignore', '.* potential security risk .*',
18 class PosixTester(unittest
.TestCase
):
22 fp
= open(TESTFN
, 'w+')
28 def testNoArgFunctions(self
):
29 # test posix functions which take no arguments and have
30 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
31 NO_ARG_FUNCTIONS
= [ "ctermid", "getcwd", "getcwdu", "uname",
32 "times", "getloadavg", "tmpnam",
33 "getegid", "geteuid", "getgid", "getgroups",
34 "getpid", "getpgrp", "getppid", "getuid",
37 for name
in NO_ARG_FUNCTIONS
:
38 posix_func
= getattr(posix
, name
, None)
39 if posix_func
is not None:
41 self
.assertRaises(TypeError, posix_func
, 1)
43 def test_statvfs(self
):
44 if hasattr(posix
, 'statvfs'):
45 self
.assert_(posix
.statvfs(os
.curdir
))
47 def test_fstatvfs(self
):
48 if hasattr(posix
, 'fstatvfs'):
51 self
.assert_(posix
.fstatvfs(fp
.fileno()))
55 def test_ftruncate(self
):
56 if hasattr(posix
, 'ftruncate'):
57 fp
= open(TESTFN
, 'w+')
59 # we need to have some data to truncate
62 posix
.ftruncate(fp
.fileno(), 0)
67 if hasattr(posix
, 'dup'):
70 fd
= posix
.dup(fp
.fileno())
71 self
.assert_(isinstance(fd
, int))
77 if hasattr(posix
, 'dup2'):
81 posix
.dup2(fp1
.fileno(), fp2
.fileno())
86 def fdopen_helper(self
, *args
):
87 fd
= os
.open(TESTFN
, os
.O_RDONLY
)
88 fp2
= posix
.fdopen(fd
, *args
)
91 def test_fdopen(self
):
92 if hasattr(posix
, 'fdopen'):
94 self
.fdopen_helper('r')
95 self
.fdopen_helper('r', 100)
98 if hasattr(posix
, 'fstat'):
101 self
.assert_(posix
.fstat(fp
.fileno()))
106 if hasattr(posix
, 'stat'):
107 self
.assert_(posix
.stat(TESTFN
))
109 def test_chdir(self
):
110 if hasattr(posix
, 'chdir'):
111 posix
.chdir(os
.curdir
)
112 self
.assertRaises(OSError, posix
.chdir
, TESTFN
)
114 def test_lsdir(self
):
115 if hasattr(posix
, 'lsdir'):
116 self
.assert_(TESTFN
in posix
.lsdir(os
.curdir
))
118 def test_access(self
):
119 if hasattr(posix
, 'access'):
120 self
.assert_(posix
.access(TESTFN
, os
.R_OK
))
122 def test_umask(self
):
123 if hasattr(posix
, 'umask'):
124 old_mask
= posix
.umask(0)
125 self
.assert_(isinstance(old_mask
, int))
126 posix
.umask(old_mask
)
128 def test_strerror(self
):
129 if hasattr(posix
, 'strerror'):
130 self
.assert_(posix
.strerror(0))
133 if hasattr(posix
, 'pipe'):
134 reader
, writer
= posix
.pipe()
138 def test_tempnam(self
):
139 if hasattr(posix
, 'tempnam'):
140 self
.assert_(posix
.tempnam())
141 self
.assert_(posix
.tempnam(os
.curdir
))
142 self
.assert_(posix
.tempnam(os
.curdir
, 'blah'))
144 def test_tmpfile(self
):
145 if hasattr(posix
, 'tmpfile'):
149 def test_utime(self
):
150 if hasattr(posix
, 'utime'):
152 posix
.utime(TESTFN
, None)
153 posix
.utime(TESTFN
, (now
, now
))
156 suite
= unittest
.TestSuite()
157 suite
.addTest(unittest
.makeSuite(PosixTester
))
160 if __name__
== '__main__':