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 # more portable than they had been thought to be.
9 warnings
.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__
)
10 warnings
.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__
)
12 from test_support
import TESTFN
, run_unittest
14 class TemporaryFileTests(unittest
.TestCase
):
20 for name
in self
.files
:
24 def check_tempfile(self
, name
):
25 # make sure it doesn't already exist:
26 self
.failIf(os
.path
.exists(name
),
27 "file already exists for temporary file")
28 # make sure we can create the file
30 self
.files
.append(name
)
32 def test_tempnam(self
):
33 if not hasattr(os
, "tempnam"):
35 warnings
.filterwarnings("ignore", "tempnam", RuntimeWarning,
37 self
.check_tempfile(os
.tempnam())
39 name
= os
.tempnam(TESTFN
)
40 self
.check_tempfile(name
)
42 name
= os
.tempnam(TESTFN
, "pfx")
43 self
.assert_(os
.path
.basename(name
)[:3] == "pfx")
44 self
.check_tempfile(name
)
46 def test_tmpfile(self
):
47 if not hasattr(os
, "tmpfile"):
54 self
.assert_(s
== "foobar")
56 def test_tmpnam(self
):
57 if not hasattr(os
, "tmpnam"):
59 warnings
.filterwarnings("ignore", "tmpnam", RuntimeWarning,
61 self
.check_tempfile(os
.tmpnam())
63 # Test attributes on return values from os.*stat* family.
64 class StatAttributeTests(unittest
.TestCase
):
67 self
.fname
= os
.path
.join(TESTFN
, "f1")
68 f
= open(self
.fname
, 'wb')
76 def test_stat_attributes(self
):
77 if not hasattr(os
, "stat"):
81 result
= os
.stat(self
.fname
)
83 # Make sure direct access works
84 self
.assertEquals(result
[stat
.ST_SIZE
], 3)
85 self
.assertEquals(result
.st_size
, 3)
89 # Make sure all the attributes are there
91 for name
in dir(stat
):
94 self
.assertEquals(getattr(result
, attr
),
95 result
[getattr(stat
, name
)])
96 self
.assert_(attr
in members
)
100 self
.fail("No exception thrown")
104 # Make sure that assignment fails
107 self
.fail("No exception thrown")
113 self
.fail("No exception thrown")
114 except (AttributeError, TypeError):
119 self
.fail("No exception thrown")
120 except AttributeError:
123 # Use the stat_result constructor with a too-short tuple.
125 result2
= os
.stat_result((10,))
126 self
.fail("No exception thrown")
130 # Use the constructr with a too-long tuple.
132 result2
= os
.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
137 def test_statvfs_attributes(self
):
138 if not hasattr(os
, "statvfs"):
143 result
= os
.statvfs(self
.fname
)
145 # On AtheOS, glibc always returns ENOSYS
147 if e
.errno
== errno
.ENOSYS
:
150 # Make sure direct access works
151 self
.assertEquals(result
.f_bfree
, result
[statvfs
.F_BFREE
])
153 # Make sure all the attributes are there
154 members
= dir(result
)
155 for name
in dir(statvfs
):
158 self
.assertEquals(getattr(result
, attr
),
159 result
[getattr(statvfs
, name
)])
160 self
.assert_(attr
in members
)
162 # Make sure that assignment really fails
165 self
.fail("No exception thrown")
171 self
.fail("No exception thrown")
172 except AttributeError:
175 # Use the constructor with a too-short tuple.
177 result2
= os
.statvfs_result((10,))
178 self
.fail("No exception thrown")
182 # Use the constructr with a too-long tuple.
184 result2
= os
.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
189 run_unittest(TemporaryFileTests
)
190 run_unittest(StatAttributeTests
)
192 if __name__
== "__main__":