blob: 028ec08574c6630ce56bfe1f633efadee1035e27 [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
Walter Dörwald21d3a322003-05-01 17:45:56 +00008from test import test_support
Fred Drake38c2ef02001-07-17 20:52:51 +00009
Barry Warsaw60f01882001-08-22 19:24:42 +000010warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__)
11warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__)
12
Fred Drake38c2ef02001-07-17 20:52:51 +000013class TemporaryFileTests(unittest.TestCase):
14 def setUp(self):
15 self.files = []
Walter Dörwald21d3a322003-05-01 17:45:56 +000016 os.mkdir(test_support.TESTFN)
Fred Drake38c2ef02001-07-17 20:52:51 +000017
18 def tearDown(self):
19 for name in self.files:
20 os.unlink(name)
Walter Dörwald21d3a322003-05-01 17:45:56 +000021 os.rmdir(test_support.TESTFN)
Fred Drake38c2ef02001-07-17 20:52:51 +000022
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
28 open(name, "w")
29 self.files.append(name)
30
31 def test_tempnam(self):
32 if not hasattr(os, "tempnam"):
33 return
Jeremy Hyltona7fc21b2001-08-20 20:10:01 +000034 warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
Tim Petersd3925062002-04-16 01:27:44 +000035 r"test_os$")
Fred Drake38c2ef02001-07-17 20:52:51 +000036 self.check_tempfile(os.tempnam())
37
Walter Dörwald21d3a322003-05-01 17:45:56 +000038 name = os.tempnam(test_support.TESTFN)
Fred Drake38c2ef02001-07-17 20:52:51 +000039 self.check_tempfile(name)
40
Walter Dörwald21d3a322003-05-01 17:45:56 +000041 name = os.tempnam(test_support.TESTFN, "pfx")
Fred Drake38c2ef02001-07-17 20:52:51 +000042 self.assert_(os.path.basename(name)[:3] == "pfx")
43 self.check_tempfile(name)
44
45 def test_tmpfile(self):
46 if not hasattr(os, "tmpfile"):
47 return
48 fp = os.tmpfile()
49 fp.write("foobar")
50 fp.seek(0,0)
51 s = fp.read()
52 fp.close()
53 self.assert_(s == "foobar")
54
55 def test_tmpnam(self):
Tim Peters5501b5e2003-04-28 03:13:03 +000056 import sys
Fred Drake38c2ef02001-07-17 20:52:51 +000057 if not hasattr(os, "tmpnam"):
58 return
Jeremy Hyltona7fc21b2001-08-20 20:10:01 +000059 warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning,
Tim Petersd3925062002-04-16 01:27:44 +000060 r"test_os$")
Tim Peters5501b5e2003-04-28 03:13:03 +000061 name = os.tmpnam()
62 if sys.platform in ("win32",):
63 # The Windows tmpnam() seems useless. From the MS docs:
64 #
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.
72 #
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")
80 else:
81 self.check_tempfile(name)
Tim Peters87cc0c32001-07-21 01:41:30 +000082
Guido van Rossum98bf58f2001-10-18 20:34:25 +000083# Test attributes on return values from os.*stat* family.
84class StatAttributeTests(unittest.TestCase):
85 def setUp(self):
Walter Dörwald21d3a322003-05-01 17:45:56 +000086 os.mkdir(test_support.TESTFN)
87 self.fname = os.path.join(test_support.TESTFN, "f1")
Guido van Rossum98bf58f2001-10-18 20:34:25 +000088 f = open(self.fname, 'wb')
89 f.write("ABC")
90 f.close()
Tim Peterse0c446b2001-10-18 21:57:37 +000091
Guido van Rossum98bf58f2001-10-18 20:34:25 +000092 def tearDown(self):
93 os.unlink(self.fname)
Walter Dörwald21d3a322003-05-01 17:45:56 +000094 os.rmdir(test_support.TESTFN)
Guido van Rossum98bf58f2001-10-18 20:34:25 +000095
96 def test_stat_attributes(self):
97 if not hasattr(os, "stat"):
98 return
99
100 import stat
101 result = os.stat(self.fname)
102
103 # Make sure direct access works
104 self.assertEquals(result[stat.ST_SIZE], 3)
105 self.assertEquals(result.st_size, 3)
106
107 import sys
108
109 # Make sure all the attributes are there
110 members = dir(result)
111 for name in dir(stat):
112 if name[:3] == 'ST_':
113 attr = name.lower()
114 self.assertEquals(getattr(result, attr),
115 result[getattr(stat, name)])
116 self.assert_(attr in members)
117
118 try:
119 result[200]
120 self.fail("No exception thrown")
121 except IndexError:
122 pass
123
124 # Make sure that assignment fails
125 try:
126 result.st_mode = 1
127 self.fail("No exception thrown")
128 except TypeError:
129 pass
130
131 try:
132 result.st_rdev = 1
133 self.fail("No exception thrown")
Guido van Rossum1fff8782001-10-18 21:19:31 +0000134 except (AttributeError, TypeError):
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000135 pass
136
137 try:
138 result.parrot = 1
139 self.fail("No exception thrown")
140 except AttributeError:
141 pass
142
143 # Use the stat_result constructor with a too-short tuple.
144 try:
145 result2 = os.stat_result((10,))
146 self.fail("No exception thrown")
147 except TypeError:
148 pass
149
150 # Use the constructr with a too-long tuple.
151 try:
152 result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
153 except TypeError:
154 pass
155
Tim Peterse0c446b2001-10-18 21:57:37 +0000156
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000157 def test_statvfs_attributes(self):
158 if not hasattr(os, "statvfs"):
159 return
160
161 import statvfs
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000162 try:
163 result = os.statvfs(self.fname)
164 except OSError, e:
165 # On AtheOS, glibc always returns ENOSYS
166 import errno
167 if e.errno == errno.ENOSYS:
168 return
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000169
170 # Make sure direct access works
171 self.assertEquals(result.f_bfree, result[statvfs.F_BFREE])
172
173 # Make sure all the attributes are there
174 members = dir(result)
175 for name in dir(statvfs):
176 if name[:2] == 'F_':
177 attr = name.lower()
178 self.assertEquals(getattr(result, attr),
179 result[getattr(statvfs, name)])
180 self.assert_(attr in members)
181
182 # Make sure that assignment really fails
183 try:
184 result.f_bfree = 1
185 self.fail("No exception thrown")
186 except TypeError:
187 pass
188
189 try:
190 result.parrot = 1
191 self.fail("No exception thrown")
192 except AttributeError:
193 pass
194
195 # Use the constructor with a too-short tuple.
196 try:
197 result2 = os.statvfs_result((10,))
198 self.fail("No exception thrown")
199 except TypeError:
200 pass
201
202 # Use the constructr with a too-long tuple.
203 try:
204 result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
205 except TypeError:
206 pass
Fred Drake38c2ef02001-07-17 20:52:51 +0000207
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000208from test_userdict import TestMappingProtocol
209
210class EnvironTests(TestMappingProtocol):
211 """check that os.environ object conform to mapping protocol"""
212 _tested_class = None
213 def _reference(self):
214 return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"}
215 def _empty_mapping(self):
216 os.environ.clear()
217 return os.environ
218 def setUp(self):
219 self.__save = dict(os.environ)
220 os.environ.clear()
221 def tearDown(self):
222 os.environ.clear()
223 os.environ.update(self.__save)
224
Tim Petersc4e09402003-04-25 07:11:48 +0000225class WalkTests(unittest.TestCase):
226 """Tests for os.walk()."""
227
228 def test_traversal(self):
229 import os
230 from os.path import join
231
232 # Build:
233 # TESTFN/ a file kid and two directory kids
234 # tmp1
235 # SUB1/ a file kid and a directory kid
236 # tmp2
237 # SUB11/ no kids
238 # SUB2/ just a file kid
239 # tmp3
Walter Dörwald21d3a322003-05-01 17:45:56 +0000240 sub1_path = join(test_support.TESTFN, "SUB1")
Tim Petersc4e09402003-04-25 07:11:48 +0000241 sub11_path = join(sub1_path, "SUB11")
Walter Dörwald21d3a322003-05-01 17:45:56 +0000242 sub2_path = join(test_support.TESTFN, "SUB2")
243 tmp1_path = join(test_support.TESTFN, "tmp1")
Tim Petersc4e09402003-04-25 07:11:48 +0000244 tmp2_path = join(sub1_path, "tmp2")
245 tmp3_path = join(sub2_path, "tmp3")
246
247 # Create stuff.
248 os.makedirs(sub11_path)
249 os.makedirs(sub2_path)
250 for path in tmp1_path, tmp2_path, tmp3_path:
251 f = file(path, "w")
252 f.write("I'm " + path + " and proud of it. Blame test_os.\n")
253 f.close()
254
255 # Walk top-down.
Walter Dörwald21d3a322003-05-01 17:45:56 +0000256 all = list(os.walk(test_support.TESTFN))
Tim Petersc4e09402003-04-25 07:11:48 +0000257 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"
262 all[0][1].sort()
Walter Dörwald21d3a322003-05-01 17:45:56 +0000263 self.assertEqual(all[0], (test_support.TESTFN, ["SUB1", "SUB2"], ["tmp1"]))
Tim Petersc4e09402003-04-25 07:11:48 +0000264 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"]))
267
268 # Prune the search.
269 all = []
Walter Dörwald21d3a322003-05-01 17:45:56 +0000270 for root, dirs, files in os.walk(test_support.TESTFN):
Tim Petersc4e09402003-04-25 07:11:48 +0000271 all.append((root, dirs, files))
272 # Don't descend into SUB1.
273 if 'SUB1' in dirs:
274 # Note that this also mutates the dirs we appended to all!
275 dirs.remove('SUB1')
276 self.assertEqual(len(all), 2)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000277 self.assertEqual(all[0], (test_support.TESTFN, ["SUB2"], ["tmp1"]))
Tim Petersc4e09402003-04-25 07:11:48 +0000278 self.assertEqual(all[1], (sub2_path, [], ["tmp3"]))
279
280 # Walk bottom-up.
Walter Dörwald21d3a322003-05-01 17:45:56 +0000281 all = list(os.walk(test_support.TESTFN, topdown=False))
Tim Petersc4e09402003-04-25 07:11:48 +0000282 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"
287 all[3][1].sort()
Walter Dörwald21d3a322003-05-01 17:45:56 +0000288 self.assertEqual(all[3], (test_support.TESTFN, ["SUB1", "SUB2"], ["tmp1"]))
Tim Petersc4e09402003-04-25 07:11:48 +0000289 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"]))
292
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.
Walter Dörwald21d3a322003-05-01 17:45:56 +0000297 for root, dirs, files in os.walk(test_support.TESTFN, topdown=False):
Tim Petersc4e09402003-04-25 07:11:48 +0000298 for name in files:
299 os.remove(join(root, name))
300 for name in dirs:
301 os.rmdir(join(root, name))
Walter Dörwald21d3a322003-05-01 17:45:56 +0000302 os.rmdir(test_support.TESTFN)
Tim Petersc4e09402003-04-25 07:11:48 +0000303
Fred Drake2e2be372001-09-20 21:33:42 +0000304def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000305 test_support.run_unittest(
306 TemporaryFileTests,
307 StatAttributeTests,
308 EnvironTests,
309 WalkTests
310 )
Fred Drake2e2be372001-09-20 21:33:42 +0000311
312if __name__ == "__main__":
313 test_main()