blob: 9ea1d06d08356efd74628af01db3f66003e12d27 [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
3# more portable than they had been thought to be.
4
5import os
6import unittest
Jeremy Hyltona7fc21b2001-08-20 20:10:01 +00007import warnings
Fred Drake38c2ef02001-07-17 20:52:51 +00008
Barry Warsaw60f01882001-08-22 19:24:42 +00009warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__)
10warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__)
11
Raymond Hettinger9dcbbea2003-04-27 07:54:23 +000012from test.test_support import TESTFN, run_classtests
Fred Drake38c2ef02001-07-17 20:52:51 +000013
Fred Drake38c2ef02001-07-17 20:52:51 +000014class TemporaryFileTests(unittest.TestCase):
15 def setUp(self):
16 self.files = []
17 os.mkdir(TESTFN)
18
19 def tearDown(self):
20 for name in self.files:
21 os.unlink(name)
22 os.rmdir(TESTFN)
23
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
29 open(name, "w")
30 self.files.append(name)
31
32 def test_tempnam(self):
33 if not hasattr(os, "tempnam"):
34 return
Jeremy Hyltona7fc21b2001-08-20 20:10:01 +000035 warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
Tim Petersd3925062002-04-16 01:27:44 +000036 r"test_os$")
Fred Drake38c2ef02001-07-17 20:52:51 +000037 self.check_tempfile(os.tempnam())
38
39 name = os.tempnam(TESTFN)
Fred Drake38c2ef02001-07-17 20:52:51 +000040 self.check_tempfile(name)
41
42 name = os.tempnam(TESTFN, "pfx")
Fred Drake38c2ef02001-07-17 20:52:51 +000043 self.assert_(os.path.basename(name)[:3] == "pfx")
44 self.check_tempfile(name)
45
46 def test_tmpfile(self):
47 if not hasattr(os, "tmpfile"):
48 return
49 fp = os.tmpfile()
50 fp.write("foobar")
51 fp.seek(0,0)
52 s = fp.read()
53 fp.close()
54 self.assert_(s == "foobar")
55
56 def test_tmpnam(self):
Tim Peters5501b5e2003-04-28 03:13:03 +000057 import sys
Fred Drake38c2ef02001-07-17 20:52:51 +000058 if not hasattr(os, "tmpnam"):
59 return
Jeremy Hyltona7fc21b2001-08-20 20:10:01 +000060 warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning,
Tim Petersd3925062002-04-16 01:27:44 +000061 r"test_os$")
Tim Peters5501b5e2003-04-28 03:13:03 +000062 name = os.tmpnam()
63 if sys.platform in ("win32",):
64 # The Windows tmpnam() seems useless. From the MS docs:
65 #
66 # The character string that tmpnam creates consists of
67 # the path prefix, defined by the entry P_tmpdir in the
68 # file STDIO.H, followed by a sequence consisting of the
69 # digit characters '0' through '9'; the numerical value
70 # of this string is in the range 1 - 65,535. Changing the
71 # definitions of L_tmpnam or P_tmpdir in STDIO.H does not
72 # change the operation of tmpnam.
73 #
74 # The really bizarre part is that, at least under MSVC6,
75 # P_tmpdir is "\\". That is, the path returned refers to
76 # the root of the current drive. That's a terrible place to
77 # put temp files, and, depending on privileges, the user
78 # may not even be able to open a file in the root directory.
79 self.failIf(os.path.exists(name),
80 "file already exists for temporary file")
81 else:
82 self.check_tempfile(name)
Tim Peters87cc0c32001-07-21 01:41:30 +000083
Guido van Rossum98bf58f2001-10-18 20:34:25 +000084# Test attributes on return values from os.*stat* family.
85class StatAttributeTests(unittest.TestCase):
86 def setUp(self):
87 os.mkdir(TESTFN)
88 self.fname = os.path.join(TESTFN, "f1")
89 f = open(self.fname, 'wb')
90 f.write("ABC")
91 f.close()
Tim Peterse0c446b2001-10-18 21:57:37 +000092
Guido van Rossum98bf58f2001-10-18 20:34:25 +000093 def tearDown(self):
94 os.unlink(self.fname)
95 os.rmdir(TESTFN)
96
97 def test_stat_attributes(self):
98 if not hasattr(os, "stat"):
99 return
100
101 import stat
102 result = os.stat(self.fname)
103
104 # Make sure direct access works
105 self.assertEquals(result[stat.ST_SIZE], 3)
106 self.assertEquals(result.st_size, 3)
107
108 import sys
109
110 # Make sure all the attributes are there
111 members = dir(result)
112 for name in dir(stat):
113 if name[:3] == 'ST_':
114 attr = name.lower()
115 self.assertEquals(getattr(result, attr),
116 result[getattr(stat, name)])
117 self.assert_(attr in members)
118
119 try:
120 result[200]
121 self.fail("No exception thrown")
122 except IndexError:
123 pass
124
125 # Make sure that assignment fails
126 try:
127 result.st_mode = 1
128 self.fail("No exception thrown")
129 except TypeError:
130 pass
131
132 try:
133 result.st_rdev = 1
134 self.fail("No exception thrown")
Guido van Rossum1fff8782001-10-18 21:19:31 +0000135 except (AttributeError, TypeError):
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000136 pass
137
138 try:
139 result.parrot = 1
140 self.fail("No exception thrown")
141 except AttributeError:
142 pass
143
144 # Use the stat_result constructor with a too-short tuple.
145 try:
146 result2 = os.stat_result((10,))
147 self.fail("No exception thrown")
148 except TypeError:
149 pass
150
151 # Use the constructr with a too-long tuple.
152 try:
153 result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
154 except TypeError:
155 pass
156
Tim Peterse0c446b2001-10-18 21:57:37 +0000157
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000158 def test_statvfs_attributes(self):
159 if not hasattr(os, "statvfs"):
160 return
161
162 import statvfs
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000163 try:
164 result = os.statvfs(self.fname)
165 except OSError, e:
166 # On AtheOS, glibc always returns ENOSYS
167 import errno
168 if e.errno == errno.ENOSYS:
169 return
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000170
171 # Make sure direct access works
172 self.assertEquals(result.f_bfree, result[statvfs.F_BFREE])
173
174 # Make sure all the attributes are there
175 members = dir(result)
176 for name in dir(statvfs):
177 if name[:2] == 'F_':
178 attr = name.lower()
179 self.assertEquals(getattr(result, attr),
180 result[getattr(statvfs, name)])
181 self.assert_(attr in members)
182
183 # Make sure that assignment really fails
184 try:
185 result.f_bfree = 1
186 self.fail("No exception thrown")
187 except TypeError:
188 pass
189
190 try:
191 result.parrot = 1
192 self.fail("No exception thrown")
193 except AttributeError:
194 pass
195
196 # Use the constructor with a too-short tuple.
197 try:
198 result2 = os.statvfs_result((10,))
199 self.fail("No exception thrown")
200 except TypeError:
201 pass
202
203 # Use the constructr with a too-long tuple.
204 try:
205 result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
206 except TypeError:
207 pass
Fred Drake38c2ef02001-07-17 20:52:51 +0000208
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000209from test_userdict import TestMappingProtocol
210
211class EnvironTests(TestMappingProtocol):
212 """check that os.environ object conform to mapping protocol"""
213 _tested_class = None
214 def _reference(self):
215 return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"}
216 def _empty_mapping(self):
217 os.environ.clear()
218 return os.environ
219 def setUp(self):
220 self.__save = dict(os.environ)
221 os.environ.clear()
222 def tearDown(self):
223 os.environ.clear()
224 os.environ.update(self.__save)
225
Tim Petersc4e09402003-04-25 07:11:48 +0000226class WalkTests(unittest.TestCase):
227 """Tests for os.walk()."""
228
229 def test_traversal(self):
230 import os
231 from os.path import join
232
233 # Build:
234 # TESTFN/ a file kid and two directory kids
235 # tmp1
236 # SUB1/ a file kid and a directory kid
237 # tmp2
238 # SUB11/ no kids
239 # SUB2/ just a file kid
240 # tmp3
241 sub1_path = join(TESTFN, "SUB1")
242 sub11_path = join(sub1_path, "SUB11")
243 sub2_path = join(TESTFN, "SUB2")
244 tmp1_path = join(TESTFN, "tmp1")
245 tmp2_path = join(sub1_path, "tmp2")
246 tmp3_path = join(sub2_path, "tmp3")
247
248 # Create stuff.
249 os.makedirs(sub11_path)
250 os.makedirs(sub2_path)
251 for path in tmp1_path, tmp2_path, tmp3_path:
252 f = file(path, "w")
253 f.write("I'm " + path + " and proud of it. Blame test_os.\n")
254 f.close()
255
256 # Walk top-down.
257 all = list(os.walk(TESTFN))
258 self.assertEqual(len(all), 4)
259 # We can't know which order SUB1 and SUB2 will appear in.
260 # Not flipped: TESTFN, SUB1, SUB11, SUB2
261 # flipped: TESTFN, SUB2, SUB1, SUB11
262 flipped = all[0][1][0] != "SUB1"
263 all[0][1].sort()
264 self.assertEqual(all[0], (TESTFN, ["SUB1", "SUB2"], ["tmp1"]))
265 self.assertEqual(all[1 + flipped], (sub1_path, ["SUB11"], ["tmp2"]))
266 self.assertEqual(all[2 + flipped], (sub11_path, [], []))
267 self.assertEqual(all[3 - 2 * flipped], (sub2_path, [], ["tmp3"]))
268
269 # Prune the search.
270 all = []
271 for root, dirs, files in os.walk(TESTFN):
272 all.append((root, dirs, files))
273 # Don't descend into SUB1.
274 if 'SUB1' in dirs:
275 # Note that this also mutates the dirs we appended to all!
276 dirs.remove('SUB1')
277 self.assertEqual(len(all), 2)
278 self.assertEqual(all[0], (TESTFN, ["SUB2"], ["tmp1"]))
279 self.assertEqual(all[1], (sub2_path, [], ["tmp3"]))
280
281 # Walk bottom-up.
282 all = list(os.walk(TESTFN, topdown=False))
283 self.assertEqual(len(all), 4)
284 # We can't know which order SUB1 and SUB2 will appear in.
285 # Not flipped: SUB11, SUB1, SUB2, TESTFN
286 # flipped: SUB2, SUB11, SUB1, TESTFN
287 flipped = all[3][1][0] != "SUB1"
288 all[3][1].sort()
289 self.assertEqual(all[3], (TESTFN, ["SUB1", "SUB2"], ["tmp1"]))
290 self.assertEqual(all[flipped], (sub11_path, [], []))
291 self.assertEqual(all[flipped + 1], (sub1_path, ["SUB11"], ["tmp2"]))
292 self.assertEqual(all[2 - 2 * flipped], (sub2_path, [], ["tmp3"]))
293
294 # Tear everything down. This is a decent use for bottom-up on
295 # Windows, which doesn't have a recursive delete command. The
296 # (not so) subtlety is that rmdir will fail unless the dir's
297 # kids are removed first, so bottom up is essential.
298 for root, dirs, files in os.walk(TESTFN, topdown=False):
299 for name in files:
300 os.remove(join(root, name))
301 for name in dirs:
302 os.rmdir(join(root, name))
303 os.rmdir(TESTFN)
304
Fred Drake2e2be372001-09-20 21:33:42 +0000305def test_main():
Raymond Hettinger9dcbbea2003-04-27 07:54:23 +0000306 run_classtests(TemporaryFileTests,
307 StatAttributeTests,
308 EnvironTests,
309 WalkTests)
Fred Drake2e2be372001-09-20 21:33:42 +0000310
311if __name__ == "__main__":
312 test_main()