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.
8 from test
import test_support
10 warnings
.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__
)
11 warnings
.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__
)
13 class TemporaryFileTests(unittest
.TestCase
):
16 os
.mkdir(test_support
.TESTFN
)
19 for name
in self
.files
:
21 os
.rmdir(test_support
.TESTFN
)
23 def check_tempfile(self
, name
):
24 # make sure it doesn't already exist:
25 self
.failIf(os
.path
.exists(name
),
26 "file already exists for temporary file")
27 # make sure we can create the file
29 self
.files
.append(name
)
31 def test_tempnam(self
):
32 if not hasattr(os
, "tempnam"):
34 warnings
.filterwarnings("ignore", "tempnam", RuntimeWarning,
36 self
.check_tempfile(os
.tempnam())
38 name
= os
.tempnam(test_support
.TESTFN
)
39 self
.check_tempfile(name
)
41 name
= os
.tempnam(test_support
.TESTFN
, "pfx")
42 self
.assert_(os
.path
.basename(name
)[:3] == "pfx")
43 self
.check_tempfile(name
)
45 def test_tmpfile(self
):
46 if not hasattr(os
, "tmpfile"):
53 self
.assert_(s
== "foobar")
55 def test_tmpnam(self
):
57 if not hasattr(os
, "tmpnam"):
59 warnings
.filterwarnings("ignore", "tmpnam", RuntimeWarning,
62 if sys
.platform
in ("win32",):
63 # The Windows tmpnam() seems useless. From the MS docs:
65 # The character string that tmpnam creates consists of
66 # the path prefix, defined by the entry P_tmpdir in the
67 # file STDIO.H, followed by a sequence consisting of the
68 # digit characters '0' through '9'; the numerical value
69 # of this string is in the range 1 - 65,535. Changing the
70 # definitions of L_tmpnam or P_tmpdir in STDIO.H does not
71 # change the operation of tmpnam.
73 # The really bizarre part is that, at least under MSVC6,
74 # P_tmpdir is "\\". That is, the path returned refers to
75 # the root of the current drive. That's a terrible place to
76 # put temp files, and, depending on privileges, the user
77 # may not even be able to open a file in the root directory.
78 self
.failIf(os
.path
.exists(name
),
79 "file already exists for temporary file")
81 self
.check_tempfile(name
)
83 # Test attributes on return values from os.*stat* family.
84 class StatAttributeTests(unittest
.TestCase
):
86 os
.mkdir(test_support
.TESTFN
)
87 self
.fname
= os
.path
.join(test_support
.TESTFN
, "f1")
88 f
= open(self
.fname
, 'wb')
94 os
.rmdir(test_support
.TESTFN
)
96 def test_stat_attributes(self
):
97 if not hasattr(os
, "stat"):
101 result
= os
.stat(self
.fname
)
103 # Make sure direct access works
104 self
.assertEquals(result
[stat
.ST_SIZE
], 3)
105 self
.assertEquals(result
.st_size
, 3)
109 # Make sure all the attributes are there
110 members
= dir(result
)
111 for name
in dir(stat
):
112 if name
[:3] == 'ST_':
114 self
.assertEquals(getattr(result
, attr
),
115 result
[getattr(stat
, name
)])
116 self
.assert_(attr
in members
)
120 self
.fail("No exception thrown")
124 # Make sure that assignment fails
127 self
.fail("No exception thrown")
133 self
.fail("No exception thrown")
134 except (AttributeError, TypeError):
139 self
.fail("No exception thrown")
140 except AttributeError:
143 # Use the stat_result constructor with a too-short tuple.
145 result2
= os
.stat_result((10,))
146 self
.fail("No exception thrown")
150 # Use the constructr with a too-long tuple.
152 result2
= os
.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
157 def test_statvfs_attributes(self
):
158 if not hasattr(os
, "statvfs"):
163 result
= os
.statvfs(self
.fname
)
165 # On AtheOS, glibc always returns ENOSYS
167 if e
.errno
== errno
.ENOSYS
:
170 # Make sure direct access works
171 self
.assertEquals(result
.f_bfree
, result
[statvfs
.F_BFREE
])
173 # Make sure all the attributes are there
174 members
= dir(result
)
175 for name
in dir(statvfs
):
178 self
.assertEquals(getattr(result
, attr
),
179 result
[getattr(statvfs
, name
)])
180 self
.assert_(attr
in members
)
182 # Make sure that assignment really fails
185 self
.fail("No exception thrown")
191 self
.fail("No exception thrown")
192 except AttributeError:
195 # Use the constructor with a too-short tuple.
197 result2
= os
.statvfs_result((10,))
198 self
.fail("No exception thrown")
202 # Use the constructr with a too-long tuple.
204 result2
= os
.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
208 from test
import mapping_tests
210 class EnvironTests(mapping_tests
.BasicTestMappingProtocol
):
211 """check that os.environ object conform to mapping protocol"""
213 def _reference(self
):
214 return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"}
215 def _empty_mapping(self
):
219 self
.__save
= dict(os
.environ
)
223 os
.environ
.update(self
.__save
)
225 class WalkTests(unittest
.TestCase
):
226 """Tests for os.walk()."""
228 def test_traversal(self
):
230 from os
.path
import join
233 # TESTFN/ a file kid and two directory kids
235 # SUB1/ a file kid and a directory kid
238 # SUB2/ just a file kid
240 sub1_path
= join(test_support
.TESTFN
, "SUB1")
241 sub11_path
= join(sub1_path
, "SUB11")
242 sub2_path
= join(test_support
.TESTFN
, "SUB2")
243 tmp1_path
= join(test_support
.TESTFN
, "tmp1")
244 tmp2_path
= join(sub1_path
, "tmp2")
245 tmp3_path
= join(sub2_path
, "tmp3")
248 os
.makedirs(sub11_path
)
249 os
.makedirs(sub2_path
)
250 for path
in tmp1_path
, tmp2_path
, tmp3_path
:
252 f
.write("I'm " + path
+ " and proud of it. Blame test_os.\n")
256 all
= list(os
.walk(test_support
.TESTFN
))
257 self
.assertEqual(len(all
), 4)
258 # We can't know which order SUB1 and SUB2 will appear in.
259 # Not flipped: TESTFN, SUB1, SUB11, SUB2
260 # flipped: TESTFN, SUB2, SUB1, SUB11
261 flipped
= all
[0][1][0] != "SUB1"
263 self
.assertEqual(all
[0], (test_support
.TESTFN
, ["SUB1", "SUB2"], ["tmp1"]))
264 self
.assertEqual(all
[1 + flipped
], (sub1_path
, ["SUB11"], ["tmp2"]))
265 self
.assertEqual(all
[2 + flipped
], (sub11_path
, [], []))
266 self
.assertEqual(all
[3 - 2 * flipped
], (sub2_path
, [], ["tmp3"]))
270 for root
, dirs
, files
in os
.walk(test_support
.TESTFN
):
271 all
.append((root
, dirs
, files
))
272 # Don't descend into SUB1.
274 # Note that this also mutates the dirs we appended to all!
276 self
.assertEqual(len(all
), 2)
277 self
.assertEqual(all
[0], (test_support
.TESTFN
, ["SUB2"], ["tmp1"]))
278 self
.assertEqual(all
[1], (sub2_path
, [], ["tmp3"]))
281 all
= list(os
.walk(test_support
.TESTFN
, topdown
=False))
282 self
.assertEqual(len(all
), 4)
283 # We can't know which order SUB1 and SUB2 will appear in.
284 # Not flipped: SUB11, SUB1, SUB2, TESTFN
285 # flipped: SUB2, SUB11, SUB1, TESTFN
286 flipped
= all
[3][1][0] != "SUB1"
288 self
.assertEqual(all
[3], (test_support
.TESTFN
, ["SUB1", "SUB2"], ["tmp1"]))
289 self
.assertEqual(all
[flipped
], (sub11_path
, [], []))
290 self
.assertEqual(all
[flipped
+ 1], (sub1_path
, ["SUB11"], ["tmp2"]))
291 self
.assertEqual(all
[2 - 2 * flipped
], (sub2_path
, [], ["tmp3"]))
293 # Tear everything down. This is a decent use for bottom-up on
294 # Windows, which doesn't have a recursive delete command. The
295 # (not so) subtlety is that rmdir will fail unless the dir's
296 # kids are removed first, so bottom up is essential.
297 for root
, dirs
, files
in os
.walk(test_support
.TESTFN
, topdown
=False):
299 os
.remove(join(root
, name
))
301 os
.rmdir(join(root
, name
))
302 os
.rmdir(test_support
.TESTFN
)
304 class MakedirTests (unittest
.TestCase
):
306 os
.mkdir(test_support
.TESTFN
)
308 def test_makedir(self
):
309 base
= test_support
.TESTFN
310 path
= os
.path
.join(base
, 'dir1', 'dir2', 'dir3')
311 os
.makedirs(path
) # Should work
312 path
= os
.path
.join(base
, 'dir1', 'dir2', 'dir3', 'dir4')
315 # Try paths with a '.' in them
316 self
.failUnlessRaises(OSError, os
.makedirs
, os
.curdir
)
317 path
= os
.path
.join(base
, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os
.curdir
)
319 path
= os
.path
.join(base
, 'dir1', os
.curdir
, 'dir2', 'dir3', 'dir4',
327 path
= os
.path
.join(test_support
.TESTFN
, 'dir1', 'dir2', 'dir3',
328 'dir4', 'dir5', 'dir6')
329 # If the tests failed, the bottom-most directory ('../dir6')
330 # may not have been created, so we look for the outermost directory
332 while not os
.path
.exists(path
) and path
!= test_support
.TESTFN
:
333 path
= os
.path
.dirname(path
)
337 class DevNullTests (unittest
.TestCase
):
338 def test_devnull(self
):
339 f
= file(os
.devnull
, 'w')
342 f
= file(os
.devnull
, 'r')
343 self
.assertEqual(f
.read(), '')
347 test_support
.run_unittest(
356 if __name__
== "__main__":