Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 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 |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 3 | # portable than they had been thought to be. |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 4 | |
| 5 | import os |
| 6 | import unittest |
Jeremy Hylton | a7fc21b | 2001-08-20 20:10:01 +0000 | [diff] [blame] | 7 | import warnings |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8 | import sys |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 9 | from test import test_support |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 10 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11 | # Tests creating TESTFN |
| 12 | class FileTests(unittest.TestCase): |
| 13 | def setUp(self): |
| 14 | if os.path.exists(test_support.TESTFN): |
| 15 | os.unlink(test_support.TESTFN) |
| 16 | tearDown = setUp |
| 17 | |
| 18 | def test_access(self): |
| 19 | f = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR) |
| 20 | os.close(f) |
| 21 | self.assert_(os.access(test_support.TESTFN, os.W_OK)) |
| 22 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame^] | 23 | def test_closerange(self): |
| 24 | f = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR) |
| 25 | # close a fd that is open, and one that isn't |
| 26 | os.closerange(f, f+2) |
| 27 | self.assertRaises(OSError, os.write, f, "a") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 29 | # Test attributes on return values from os.*stat* family. |
| 30 | class StatAttributeTests(unittest.TestCase): |
| 31 | def setUp(self): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 32 | os.mkdir(test_support.TESTFN) |
| 33 | self.fname = os.path.join(test_support.TESTFN, "f1") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 34 | f = open(self.fname, 'wb') |
Guido van Rossum | 26d95c3 | 2007-08-27 23:18:54 +0000 | [diff] [blame] | 35 | f.write(b"ABC") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 36 | f.close() |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 38 | def tearDown(self): |
| 39 | os.unlink(self.fname) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 40 | os.rmdir(test_support.TESTFN) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 41 | |
| 42 | def test_stat_attributes(self): |
| 43 | if not hasattr(os, "stat"): |
| 44 | return |
| 45 | |
| 46 | import stat |
| 47 | result = os.stat(self.fname) |
| 48 | |
| 49 | # Make sure direct access works |
| 50 | self.assertEquals(result[stat.ST_SIZE], 3) |
| 51 | self.assertEquals(result.st_size, 3) |
| 52 | |
| 53 | import sys |
| 54 | |
| 55 | # Make sure all the attributes are there |
| 56 | members = dir(result) |
| 57 | for name in dir(stat): |
| 58 | if name[:3] == 'ST_': |
| 59 | attr = name.lower() |
Martin v. Löwis | 4d394df | 2005-01-23 09:19:22 +0000 | [diff] [blame] | 60 | if name.endswith("TIME"): |
| 61 | def trunc(x): return int(x) |
| 62 | else: |
| 63 | def trunc(x): return x |
| 64 | self.assertEquals(trunc(getattr(result, attr)), |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 65 | result[getattr(stat, name)]) |
| 66 | self.assert_(attr in members) |
| 67 | |
| 68 | try: |
| 69 | result[200] |
| 70 | self.fail("No exception thrown") |
| 71 | except IndexError: |
| 72 | pass |
| 73 | |
| 74 | # Make sure that assignment fails |
| 75 | try: |
| 76 | result.st_mode = 1 |
| 77 | self.fail("No exception thrown") |
Collin Winter | 42dae6a | 2007-03-28 21:44:53 +0000 | [diff] [blame] | 78 | except AttributeError: |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 79 | pass |
| 80 | |
| 81 | try: |
| 82 | result.st_rdev = 1 |
| 83 | self.fail("No exception thrown") |
Guido van Rossum | 1fff878 | 2001-10-18 21:19:31 +0000 | [diff] [blame] | 84 | except (AttributeError, TypeError): |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 85 | pass |
| 86 | |
| 87 | try: |
| 88 | result.parrot = 1 |
| 89 | self.fail("No exception thrown") |
| 90 | except AttributeError: |
| 91 | pass |
| 92 | |
| 93 | # Use the stat_result constructor with a too-short tuple. |
| 94 | try: |
| 95 | result2 = os.stat_result((10,)) |
| 96 | self.fail("No exception thrown") |
| 97 | except TypeError: |
| 98 | pass |
| 99 | |
| 100 | # Use the constructr with a too-long tuple. |
| 101 | try: |
| 102 | result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)) |
| 103 | except TypeError: |
| 104 | pass |
| 105 | |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 107 | def test_statvfs_attributes(self): |
| 108 | if not hasattr(os, "statvfs"): |
| 109 | return |
| 110 | |
| 111 | import statvfs |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 112 | try: |
| 113 | result = os.statvfs(self.fname) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 114 | except OSError as e: |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 115 | # On AtheOS, glibc always returns ENOSYS |
| 116 | import errno |
| 117 | if e.errno == errno.ENOSYS: |
| 118 | return |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 119 | |
| 120 | # Make sure direct access works |
| 121 | self.assertEquals(result.f_bfree, result[statvfs.F_BFREE]) |
| 122 | |
| 123 | # Make sure all the attributes are there |
| 124 | members = dir(result) |
| 125 | for name in dir(statvfs): |
| 126 | if name[:2] == 'F_': |
| 127 | attr = name.lower() |
| 128 | self.assertEquals(getattr(result, attr), |
| 129 | result[getattr(statvfs, name)]) |
| 130 | self.assert_(attr in members) |
| 131 | |
| 132 | # Make sure that assignment really fails |
| 133 | try: |
| 134 | result.f_bfree = 1 |
| 135 | self.fail("No exception thrown") |
Collin Winter | 42dae6a | 2007-03-28 21:44:53 +0000 | [diff] [blame] | 136 | except AttributeError: |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 137 | pass |
| 138 | |
| 139 | try: |
| 140 | result.parrot = 1 |
| 141 | self.fail("No exception thrown") |
| 142 | except AttributeError: |
| 143 | pass |
| 144 | |
| 145 | # Use the constructor with a too-short tuple. |
| 146 | try: |
| 147 | result2 = os.statvfs_result((10,)) |
| 148 | self.fail("No exception thrown") |
| 149 | except TypeError: |
| 150 | pass |
| 151 | |
| 152 | # Use the constructr with a too-long tuple. |
| 153 | try: |
| 154 | result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)) |
| 155 | except TypeError: |
| 156 | pass |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 157 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 158 | def test_utime_dir(self): |
| 159 | delta = 1000000 |
| 160 | st = os.stat(test_support.TESTFN) |
| 161 | # round to int, because some systems may support sub-second |
| 162 | # time stamps in stat, but not in utime. |
| 163 | os.utime(test_support.TESTFN, (st.st_atime, int(st.st_mtime-delta))) |
| 164 | st2 = os.stat(test_support.TESTFN) |
| 165 | self.assertEquals(st2.st_mtime, int(st.st_mtime-delta)) |
| 166 | |
| 167 | # Restrict test to Win32, since there is no guarantee other |
| 168 | # systems support centiseconds |
| 169 | if sys.platform == 'win32': |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 170 | def get_file_system(path): |
| 171 | import os |
| 172 | root = os.path.splitdrive(os.path.realpath("."))[0] + '\\' |
| 173 | import ctypes |
| 174 | kernel32 = ctypes.windll.kernel32 |
| 175 | buf = ctypes.create_string_buffer("", 100) |
| 176 | if kernel32.GetVolumeInformationA(root, None, 0, None, None, None, buf, len(buf)): |
| 177 | return buf.value |
| 178 | |
| 179 | if get_file_system(test_support.TESTFN) == "NTFS": |
| 180 | def test_1565150(self): |
| 181 | t1 = 1159195039.25 |
| 182 | os.utime(self.fname, (t1, t1)) |
| 183 | self.assertEquals(os.stat(self.fname).st_mtime, t1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 185 | def test_1686475(self): |
| 186 | # Verify that an open file can be stat'ed |
| 187 | try: |
| 188 | os.stat(r"c:\pagefile.sys") |
| 189 | except WindowsError as e: |
| 190 | if e == 2: # file does not exist; cannot run test |
| 191 | return |
| 192 | self.fail("Could not stat pagefile.sys") |
| 193 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 194 | from test import mapping_tests |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 195 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 196 | class EnvironTests(mapping_tests.BasicTestMappingProtocol): |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 197 | """check that os.environ object conform to mapping protocol""" |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 198 | type2test = None |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 199 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 200 | def setUp(self): |
| 201 | self.__save = dict(os.environ) |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 202 | for key, value in self._reference().items(): |
| 203 | os.environ[key] = value |
| 204 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 205 | def tearDown(self): |
| 206 | os.environ.clear() |
| 207 | os.environ.update(self.__save) |
| 208 | |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 209 | def _reference(self): |
| 210 | return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"} |
| 211 | |
| 212 | def _empty_mapping(self): |
| 213 | os.environ.clear() |
| 214 | return os.environ |
| 215 | |
Martin v. Löwis | 1d11de6 | 2005-01-29 13:29:23 +0000 | [diff] [blame] | 216 | # Bug 1110478 |
Martin v. Löwis | 5510f65 | 2005-02-17 21:23:20 +0000 | [diff] [blame] | 217 | def test_update2(self): |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 218 | os.environ.clear() |
Martin v. Löwis | 1d11de6 | 2005-01-29 13:29:23 +0000 | [diff] [blame] | 219 | if os.path.exists("/bin/sh"): |
| 220 | os.environ.update(HELLO="World") |
| 221 | value = os.popen("/bin/sh -c 'echo $HELLO'").read().strip() |
| 222 | self.assertEquals(value, "World") |
| 223 | |
Christian Heimes | 1a13d59 | 2007-11-08 14:16:55 +0000 | [diff] [blame] | 224 | def test_os_popen_iter(self): |
| 225 | if os.path.exists("/bin/sh"): |
| 226 | popen = os.popen("/bin/sh -c 'echo \"line1\nline2\nline3\"'") |
| 227 | it = iter(popen) |
| 228 | self.assertEquals(next(it), "line1\n") |
| 229 | self.assertEquals(next(it), "line2\n") |
| 230 | self.assertEquals(next(it), "line3\n") |
| 231 | self.assertRaises(StopIteration, next, it) |
| 232 | |
Guido van Rossum | 67aca9e | 2007-06-13 21:51:27 +0000 | [diff] [blame] | 233 | # Verify environ keys and values from the OS are of the |
| 234 | # correct str type. |
| 235 | def test_keyvalue_types(self): |
| 236 | for key, val in os.environ.items(): |
| 237 | self.assertEquals(type(key), str) |
| 238 | self.assertEquals(type(val), str) |
| 239 | |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 240 | def test_items(self): |
| 241 | for key, value in self._reference().items(): |
| 242 | self.assertEqual(os.environ.get(key), value) |
| 243 | |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 244 | class WalkTests(unittest.TestCase): |
| 245 | """Tests for os.walk().""" |
| 246 | |
| 247 | def test_traversal(self): |
| 248 | import os |
| 249 | from os.path import join |
| 250 | |
| 251 | # Build: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 252 | # TESTFN/ |
| 253 | # TEST1/ a file kid and two directory kids |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 254 | # tmp1 |
| 255 | # SUB1/ a file kid and a directory kid |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 256 | # tmp2 |
| 257 | # SUB11/ no kids |
| 258 | # SUB2/ a file kid and a dirsymlink kid |
| 259 | # tmp3 |
| 260 | # link/ a symlink to TESTFN.2 |
| 261 | # TEST2/ |
| 262 | # tmp4 a lone file |
| 263 | walk_path = join(test_support.TESTFN, "TEST1") |
| 264 | sub1_path = join(walk_path, "SUB1") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 265 | sub11_path = join(sub1_path, "SUB11") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 266 | sub2_path = join(walk_path, "SUB2") |
| 267 | tmp1_path = join(walk_path, "tmp1") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 268 | tmp2_path = join(sub1_path, "tmp2") |
| 269 | tmp3_path = join(sub2_path, "tmp3") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 270 | link_path = join(sub2_path, "link") |
| 271 | t2_path = join(test_support.TESTFN, "TEST2") |
| 272 | tmp4_path = join(test_support.TESTFN, "TEST2", "tmp4") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 273 | |
| 274 | # Create stuff. |
| 275 | os.makedirs(sub11_path) |
| 276 | os.makedirs(sub2_path) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 277 | os.makedirs(t2_path) |
| 278 | for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path: |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 279 | f = open(path, "w") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 280 | f.write("I'm " + path + " and proud of it. Blame test_os.\n") |
| 281 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 282 | if hasattr(os, "symlink"): |
| 283 | os.symlink(os.path.abspath(t2_path), link_path) |
| 284 | sub2_tree = (sub2_path, ["link"], ["tmp3"]) |
| 285 | else: |
| 286 | sub2_tree = (sub2_path, [], ["tmp3"]) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 287 | |
| 288 | # Walk top-down. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 289 | all = list(os.walk(walk_path)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 290 | self.assertEqual(len(all), 4) |
| 291 | # We can't know which order SUB1 and SUB2 will appear in. |
| 292 | # Not flipped: TESTFN, SUB1, SUB11, SUB2 |
| 293 | # flipped: TESTFN, SUB2, SUB1, SUB11 |
| 294 | flipped = all[0][1][0] != "SUB1" |
| 295 | all[0][1].sort() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 296 | self.assertEqual(all[0], (walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 297 | self.assertEqual(all[1 + flipped], (sub1_path, ["SUB11"], ["tmp2"])) |
| 298 | self.assertEqual(all[2 + flipped], (sub11_path, [], [])) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 299 | self.assertEqual(all[3 - 2 * flipped], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 300 | |
| 301 | # Prune the search. |
| 302 | all = [] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 303 | for root, dirs, files in os.walk(walk_path): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 304 | all.append((root, dirs, files)) |
| 305 | # Don't descend into SUB1. |
| 306 | if 'SUB1' in dirs: |
| 307 | # Note that this also mutates the dirs we appended to all! |
| 308 | dirs.remove('SUB1') |
| 309 | self.assertEqual(len(all), 2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 310 | self.assertEqual(all[0], (walk_path, ["SUB2"], ["tmp1"])) |
| 311 | self.assertEqual(all[1], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 312 | |
| 313 | # Walk bottom-up. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 314 | all = list(os.walk(walk_path, topdown=False)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 315 | self.assertEqual(len(all), 4) |
| 316 | # We can't know which order SUB1 and SUB2 will appear in. |
| 317 | # Not flipped: SUB11, SUB1, SUB2, TESTFN |
| 318 | # flipped: SUB2, SUB11, SUB1, TESTFN |
| 319 | flipped = all[3][1][0] != "SUB1" |
| 320 | all[3][1].sort() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 321 | self.assertEqual(all[3], (walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 322 | self.assertEqual(all[flipped], (sub11_path, [], [])) |
| 323 | self.assertEqual(all[flipped + 1], (sub1_path, ["SUB11"], ["tmp2"])) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 324 | self.assertEqual(all[2 - 2 * flipped], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 326 | if hasattr(os, "symlink"): |
| 327 | # Walk, following symlinks. |
| 328 | for root, dirs, files in os.walk(walk_path, followlinks=True): |
| 329 | if root == link_path: |
| 330 | self.assertEqual(dirs, []) |
| 331 | self.assertEqual(files, ["tmp4"]) |
| 332 | break |
| 333 | else: |
| 334 | self.fail("Didn't follow symlink with followlinks=True") |
| 335 | |
| 336 | def tearDown(self): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 337 | # Tear everything down. This is a decent use for bottom-up on |
| 338 | # Windows, which doesn't have a recursive delete command. The |
| 339 | # (not so) subtlety is that rmdir will fail unless the dir's |
| 340 | # kids are removed first, so bottom up is essential. |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 341 | for root, dirs, files in os.walk(test_support.TESTFN, topdown=False): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 342 | for name in files: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 343 | os.remove(os.path.join(root, name)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 344 | for name in dirs: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 345 | dirname = os.path.join(root, name) |
| 346 | if not os.path.islink(dirname): |
| 347 | os.rmdir(dirname) |
| 348 | else: |
| 349 | os.remove(dirname) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 350 | os.rmdir(test_support.TESTFN) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 351 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 352 | class MakedirTests(unittest.TestCase): |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 353 | def setUp(self): |
| 354 | os.mkdir(test_support.TESTFN) |
| 355 | |
| 356 | def test_makedir(self): |
| 357 | base = test_support.TESTFN |
| 358 | path = os.path.join(base, 'dir1', 'dir2', 'dir3') |
| 359 | os.makedirs(path) # Should work |
| 360 | path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4') |
| 361 | os.makedirs(path) |
| 362 | |
| 363 | # Try paths with a '.' in them |
| 364 | self.failUnlessRaises(OSError, os.makedirs, os.curdir) |
| 365 | path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os.curdir) |
| 366 | os.makedirs(path) |
| 367 | path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4', |
| 368 | 'dir5', 'dir6') |
| 369 | os.makedirs(path) |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 370 | |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 371 | def tearDown(self): |
| 372 | path = os.path.join(test_support.TESTFN, 'dir1', 'dir2', 'dir3', |
| 373 | 'dir4', 'dir5', 'dir6') |
| 374 | # If the tests failed, the bottom-most directory ('../dir6') |
| 375 | # may not have been created, so we look for the outermost directory |
| 376 | # that exists. |
| 377 | while not os.path.exists(path) and path != test_support.TESTFN: |
| 378 | path = os.path.dirname(path) |
| 379 | |
| 380 | os.removedirs(path) |
| 381 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 382 | class DevNullTests(unittest.TestCase): |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 383 | def test_devnull(self): |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 384 | f = open(os.devnull, 'w') |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 385 | f.write('hello') |
| 386 | f.close() |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 387 | f = open(os.devnull, 'r') |
Tim Peters | 4182cfd | 2004-06-08 20:34:34 +0000 | [diff] [blame] | 388 | self.assertEqual(f.read(), '') |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 389 | f.close() |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 390 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 391 | class URandomTests(unittest.TestCase): |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 392 | def test_urandom(self): |
| 393 | try: |
| 394 | self.assertEqual(len(os.urandom(1)), 1) |
| 395 | self.assertEqual(len(os.urandom(10)), 10) |
| 396 | self.assertEqual(len(os.urandom(100)), 100) |
| 397 | self.assertEqual(len(os.urandom(1000)), 1000) |
| 398 | except NotImplementedError: |
| 399 | pass |
| 400 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 401 | class ExecTests(unittest.TestCase): |
| 402 | def test_execvpe_with_bad_program(self): |
Thomas Heller | bd315c5 | 2007-08-30 17:57:21 +0000 | [diff] [blame] | 403 | self.assertRaises(OSError, os.execvpe, 'no such app-', ['no such app-'], None) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 404 | |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 405 | def test_execvpe_with_bad_arglist(self): |
| 406 | self.assertRaises(ValueError, os.execvpe, 'notepad', [], None) |
| 407 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 408 | class Win32ErrorTests(unittest.TestCase): |
| 409 | def test_rename(self): |
| 410 | self.assertRaises(WindowsError, os.rename, test_support.TESTFN, test_support.TESTFN+".bak") |
| 411 | |
| 412 | def test_remove(self): |
| 413 | self.assertRaises(WindowsError, os.remove, test_support.TESTFN) |
| 414 | |
| 415 | def test_chdir(self): |
| 416 | self.assertRaises(WindowsError, os.chdir, test_support.TESTFN) |
| 417 | |
| 418 | def test_mkdir(self): |
| 419 | self.assertRaises(WindowsError, os.chdir, test_support.TESTFN) |
| 420 | |
| 421 | def test_utime(self): |
| 422 | self.assertRaises(WindowsError, os.utime, test_support.TESTFN, None) |
| 423 | |
| 424 | def test_access(self): |
| 425 | self.assertRaises(WindowsError, os.utime, test_support.TESTFN, 0) |
| 426 | |
| 427 | def test_chmod(self): |
| 428 | self.assertRaises(WindowsError, os.utime, test_support.TESTFN, 0) |
| 429 | |
| 430 | if sys.platform != 'win32': |
| 431 | class Win32ErrorTests(unittest.TestCase): |
| 432 | pass |
| 433 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 434 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 435 | test_support.run_unittest( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 436 | FileTests, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 437 | StatAttributeTests, |
| 438 | EnvironTests, |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 439 | WalkTests, |
| 440 | MakedirTests, |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 441 | DevNullTests, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 442 | URandomTests, |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 443 | ExecTests, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 444 | Win32ErrorTests |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 445 | ) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 446 | |
| 447 | if __name__ == "__main__": |
| 448 | test_main() |