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 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 29 | class TemporaryFileTests(unittest.TestCase): |
| 30 | def setUp(self): |
| 31 | self.files = [] |
| 32 | os.mkdir(test_support.TESTFN) |
| 33 | |
| 34 | def tearDown(self): |
| 35 | for name in self.files: |
| 36 | os.unlink(name) |
| 37 | os.rmdir(test_support.TESTFN) |
| 38 | |
| 39 | def check_tempfile(self, name): |
| 40 | # make sure it doesn't already exist: |
| 41 | self.failIf(os.path.exists(name), |
| 42 | "file already exists for temporary file") |
| 43 | # make sure we can create the file |
| 44 | open(name, "w") |
| 45 | self.files.append(name) |
| 46 | |
| 47 | def test_tempnam(self): |
| 48 | if not hasattr(os, "tempnam"): |
| 49 | return |
| 50 | warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, |
| 51 | r"test_os$") |
| 52 | self.check_tempfile(os.tempnam()) |
| 53 | |
| 54 | name = os.tempnam(test_support.TESTFN) |
| 55 | self.check_tempfile(name) |
| 56 | |
| 57 | name = os.tempnam(test_support.TESTFN, "pfx") |
| 58 | self.assert_(os.path.basename(name)[:3] == "pfx") |
| 59 | self.check_tempfile(name) |
| 60 | |
| 61 | def test_tmpfile(self): |
| 62 | if not hasattr(os, "tmpfile"): |
| 63 | return |
| 64 | # As with test_tmpnam() below, the Windows implementation of tmpfile() |
| 65 | # attempts to create a file in the root directory of the current drive. |
| 66 | # On Vista and Server 2008, this test will always fail for normal users |
| 67 | # as writing to the root directory requires elevated privileges. With |
| 68 | # XP and below, the semantics of tmpfile() are the same, but the user |
| 69 | # running the test is more likely to have administrative privileges on |
| 70 | # their account already. If that's the case, then os.tmpfile() should |
| 71 | # work. In order to make this test as useful as possible, rather than |
| 72 | # trying to detect Windows versions or whether or not the user has the |
| 73 | # right permissions, just try and create a file in the root directory |
| 74 | # and see if it raises a 'Permission denied' OSError. If it does, then |
| 75 | # test that a subsequent call to os.tmpfile() raises the same error. If |
| 76 | # it doesn't, assume we're on XP or below and the user running the test |
| 77 | # has administrative privileges, and proceed with the test as normal. |
| 78 | if sys.platform == 'win32': |
| 79 | name = '\\python_test_os_test_tmpfile.txt' |
| 80 | if os.path.exists(name): |
| 81 | os.remove(name) |
| 82 | try: |
| 83 | fp = open(name, 'w') |
| 84 | except IOError as first: |
| 85 | # open() failed, assert tmpfile() fails in the same way. |
| 86 | # Although open() raises an IOError and os.tmpfile() raises an |
| 87 | # OSError(), 'args' will be (13, 'Permission denied') in both |
| 88 | # cases. |
| 89 | try: |
| 90 | fp = os.tmpfile() |
| 91 | except OSError as second: |
| 92 | self.assertEqual(first.args, second.args) |
| 93 | else: |
| 94 | self.fail("expected os.tmpfile() to raise OSError") |
| 95 | return |
| 96 | else: |
| 97 | # open() worked, therefore, tmpfile() should work. Close our |
| 98 | # dummy file and proceed with the test as normal. |
| 99 | fp.close() |
| 100 | os.remove(name) |
| 101 | |
| 102 | fp = os.tmpfile() |
| 103 | fp.write("foobar") |
| 104 | fp.seek(0,0) |
| 105 | s = fp.read() |
| 106 | fp.close() |
| 107 | self.assert_(s == "foobar") |
| 108 | |
| 109 | def test_tmpnam(self): |
| 110 | import sys |
| 111 | if not hasattr(os, "tmpnam"): |
| 112 | return |
| 113 | warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, |
| 114 | r"test_os$") |
| 115 | name = os.tmpnam() |
| 116 | if sys.platform in ("win32",): |
| 117 | # The Windows tmpnam() seems useless. From the MS docs: |
| 118 | # |
| 119 | # The character string that tmpnam creates consists of |
| 120 | # the path prefix, defined by the entry P_tmpdir in the |
| 121 | # file STDIO.H, followed by a sequence consisting of the |
| 122 | # digit characters '0' through '9'; the numerical value |
| 123 | # of this string is in the range 1 - 65,535. Changing the |
| 124 | # definitions of L_tmpnam or P_tmpdir in STDIO.H does not |
| 125 | # change the operation of tmpnam. |
| 126 | # |
| 127 | # The really bizarre part is that, at least under MSVC6, |
| 128 | # P_tmpdir is "\\". That is, the path returned refers to |
| 129 | # the root of the current drive. That's a terrible place to |
| 130 | # put temp files, and, depending on privileges, the user |
| 131 | # may not even be able to open a file in the root directory. |
| 132 | self.failIf(os.path.exists(name), |
| 133 | "file already exists for temporary file") |
| 134 | else: |
| 135 | self.check_tempfile(name) |
| 136 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 137 | # Test attributes on return values from os.*stat* family. |
| 138 | class StatAttributeTests(unittest.TestCase): |
| 139 | def setUp(self): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 140 | os.mkdir(test_support.TESTFN) |
| 141 | self.fname = os.path.join(test_support.TESTFN, "f1") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 142 | f = open(self.fname, 'wb') |
Guido van Rossum | 26d95c3 | 2007-08-27 23:18:54 +0000 | [diff] [blame] | 143 | f.write(b"ABC") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 144 | f.close() |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 146 | def tearDown(self): |
| 147 | os.unlink(self.fname) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 148 | os.rmdir(test_support.TESTFN) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 149 | |
| 150 | def test_stat_attributes(self): |
| 151 | if not hasattr(os, "stat"): |
| 152 | return |
| 153 | |
| 154 | import stat |
| 155 | result = os.stat(self.fname) |
| 156 | |
| 157 | # Make sure direct access works |
| 158 | self.assertEquals(result[stat.ST_SIZE], 3) |
| 159 | self.assertEquals(result.st_size, 3) |
| 160 | |
| 161 | import sys |
| 162 | |
| 163 | # Make sure all the attributes are there |
| 164 | members = dir(result) |
| 165 | for name in dir(stat): |
| 166 | if name[:3] == 'ST_': |
| 167 | attr = name.lower() |
Martin v. Löwis | 4d394df | 2005-01-23 09:19:22 +0000 | [diff] [blame] | 168 | if name.endswith("TIME"): |
| 169 | def trunc(x): return int(x) |
| 170 | else: |
| 171 | def trunc(x): return x |
| 172 | self.assertEquals(trunc(getattr(result, attr)), |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 173 | result[getattr(stat, name)]) |
| 174 | self.assert_(attr in members) |
| 175 | |
| 176 | try: |
| 177 | result[200] |
| 178 | self.fail("No exception thrown") |
| 179 | except IndexError: |
| 180 | pass |
| 181 | |
| 182 | # Make sure that assignment fails |
| 183 | try: |
| 184 | result.st_mode = 1 |
| 185 | self.fail("No exception thrown") |
Collin Winter | 42dae6a | 2007-03-28 21:44:53 +0000 | [diff] [blame] | 186 | except AttributeError: |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 187 | pass |
| 188 | |
| 189 | try: |
| 190 | result.st_rdev = 1 |
| 191 | self.fail("No exception thrown") |
Guido van Rossum | 1fff878 | 2001-10-18 21:19:31 +0000 | [diff] [blame] | 192 | except (AttributeError, TypeError): |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 193 | pass |
| 194 | |
| 195 | try: |
| 196 | result.parrot = 1 |
| 197 | self.fail("No exception thrown") |
| 198 | except AttributeError: |
| 199 | pass |
| 200 | |
| 201 | # Use the stat_result constructor with a too-short tuple. |
| 202 | try: |
| 203 | result2 = os.stat_result((10,)) |
| 204 | self.fail("No exception thrown") |
| 205 | except TypeError: |
| 206 | pass |
| 207 | |
| 208 | # Use the constructr with a too-long tuple. |
| 209 | try: |
| 210 | result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)) |
| 211 | except TypeError: |
| 212 | pass |
| 213 | |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 214 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 215 | def test_statvfs_attributes(self): |
| 216 | if not hasattr(os, "statvfs"): |
| 217 | return |
| 218 | |
| 219 | import statvfs |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 220 | try: |
| 221 | result = os.statvfs(self.fname) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 222 | except OSError as e: |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 223 | # On AtheOS, glibc always returns ENOSYS |
| 224 | import errno |
| 225 | if e.errno == errno.ENOSYS: |
| 226 | return |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 227 | |
| 228 | # Make sure direct access works |
| 229 | self.assertEquals(result.f_bfree, result[statvfs.F_BFREE]) |
| 230 | |
| 231 | # Make sure all the attributes are there |
| 232 | members = dir(result) |
| 233 | for name in dir(statvfs): |
| 234 | if name[:2] == 'F_': |
| 235 | attr = name.lower() |
| 236 | self.assertEquals(getattr(result, attr), |
| 237 | result[getattr(statvfs, name)]) |
| 238 | self.assert_(attr in members) |
| 239 | |
| 240 | # Make sure that assignment really fails |
| 241 | try: |
| 242 | result.f_bfree = 1 |
| 243 | self.fail("No exception thrown") |
Collin Winter | 42dae6a | 2007-03-28 21:44:53 +0000 | [diff] [blame] | 244 | except AttributeError: |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 245 | pass |
| 246 | |
| 247 | try: |
| 248 | result.parrot = 1 |
| 249 | self.fail("No exception thrown") |
| 250 | except AttributeError: |
| 251 | pass |
| 252 | |
| 253 | # Use the constructor with a too-short tuple. |
| 254 | try: |
| 255 | result2 = os.statvfs_result((10,)) |
| 256 | self.fail("No exception thrown") |
| 257 | except TypeError: |
| 258 | pass |
| 259 | |
| 260 | # Use the constructr with a too-long tuple. |
| 261 | try: |
| 262 | result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)) |
| 263 | except TypeError: |
| 264 | pass |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 265 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 266 | def test_utime_dir(self): |
| 267 | delta = 1000000 |
| 268 | st = os.stat(test_support.TESTFN) |
| 269 | # round to int, because some systems may support sub-second |
| 270 | # time stamps in stat, but not in utime. |
| 271 | os.utime(test_support.TESTFN, (st.st_atime, int(st.st_mtime-delta))) |
| 272 | st2 = os.stat(test_support.TESTFN) |
| 273 | self.assertEquals(st2.st_mtime, int(st.st_mtime-delta)) |
| 274 | |
| 275 | # Restrict test to Win32, since there is no guarantee other |
| 276 | # systems support centiseconds |
| 277 | if sys.platform == 'win32': |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 278 | def get_file_system(path): |
| 279 | import os |
| 280 | root = os.path.splitdrive(os.path.realpath("."))[0] + '\\' |
| 281 | import ctypes |
| 282 | kernel32 = ctypes.windll.kernel32 |
| 283 | buf = ctypes.create_string_buffer("", 100) |
| 284 | if kernel32.GetVolumeInformationA(root, None, 0, None, None, None, buf, len(buf)): |
| 285 | return buf.value |
| 286 | |
| 287 | if get_file_system(test_support.TESTFN) == "NTFS": |
| 288 | def test_1565150(self): |
| 289 | t1 = 1159195039.25 |
| 290 | os.utime(self.fname, (t1, t1)) |
| 291 | self.assertEquals(os.stat(self.fname).st_mtime, t1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 292 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 293 | def test_1686475(self): |
| 294 | # Verify that an open file can be stat'ed |
| 295 | try: |
| 296 | os.stat(r"c:\pagefile.sys") |
| 297 | except WindowsError as e: |
| 298 | if e == 2: # file does not exist; cannot run test |
| 299 | return |
| 300 | self.fail("Could not stat pagefile.sys") |
| 301 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 302 | from test import mapping_tests |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 303 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 304 | class EnvironTests(mapping_tests.BasicTestMappingProtocol): |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 305 | """check that os.environ object conform to mapping protocol""" |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 306 | type2test = None |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 307 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 308 | def setUp(self): |
| 309 | self.__save = dict(os.environ) |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 310 | for key, value in self._reference().items(): |
| 311 | os.environ[key] = value |
| 312 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 313 | def tearDown(self): |
| 314 | os.environ.clear() |
| 315 | os.environ.update(self.__save) |
| 316 | |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 317 | def _reference(self): |
| 318 | return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"} |
| 319 | |
| 320 | def _empty_mapping(self): |
| 321 | os.environ.clear() |
| 322 | return os.environ |
| 323 | |
Martin v. Löwis | 1d11de6 | 2005-01-29 13:29:23 +0000 | [diff] [blame] | 324 | # Bug 1110478 |
Martin v. Löwis | 5510f65 | 2005-02-17 21:23:20 +0000 | [diff] [blame] | 325 | def test_update2(self): |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 326 | os.environ.clear() |
Martin v. Löwis | 1d11de6 | 2005-01-29 13:29:23 +0000 | [diff] [blame] | 327 | if os.path.exists("/bin/sh"): |
| 328 | os.environ.update(HELLO="World") |
| 329 | value = os.popen("/bin/sh -c 'echo $HELLO'").read().strip() |
| 330 | self.assertEquals(value, "World") |
| 331 | |
Christian Heimes | 1a13d59 | 2007-11-08 14:16:55 +0000 | [diff] [blame] | 332 | def test_os_popen_iter(self): |
| 333 | if os.path.exists("/bin/sh"): |
| 334 | popen = os.popen("/bin/sh -c 'echo \"line1\nline2\nline3\"'") |
| 335 | it = iter(popen) |
| 336 | self.assertEquals(next(it), "line1\n") |
| 337 | self.assertEquals(next(it), "line2\n") |
| 338 | self.assertEquals(next(it), "line3\n") |
| 339 | self.assertRaises(StopIteration, next, it) |
| 340 | |
Guido van Rossum | 67aca9e | 2007-06-13 21:51:27 +0000 | [diff] [blame] | 341 | # Verify environ keys and values from the OS are of the |
| 342 | # correct str type. |
| 343 | def test_keyvalue_types(self): |
| 344 | for key, val in os.environ.items(): |
| 345 | self.assertEquals(type(key), str) |
| 346 | self.assertEquals(type(val), str) |
| 347 | |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 348 | def test_items(self): |
| 349 | for key, value in self._reference().items(): |
| 350 | self.assertEqual(os.environ.get(key), value) |
| 351 | |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 352 | class WalkTests(unittest.TestCase): |
| 353 | """Tests for os.walk().""" |
| 354 | |
| 355 | def test_traversal(self): |
| 356 | import os |
| 357 | from os.path import join |
| 358 | |
| 359 | # Build: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 360 | # TESTFN/ |
| 361 | # TEST1/ a file kid and two directory kids |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 362 | # tmp1 |
| 363 | # SUB1/ a file kid and a directory kid |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 364 | # tmp2 |
| 365 | # SUB11/ no kids |
| 366 | # SUB2/ a file kid and a dirsymlink kid |
| 367 | # tmp3 |
| 368 | # link/ a symlink to TESTFN.2 |
| 369 | # TEST2/ |
| 370 | # tmp4 a lone file |
| 371 | walk_path = join(test_support.TESTFN, "TEST1") |
| 372 | sub1_path = join(walk_path, "SUB1") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 373 | sub11_path = join(sub1_path, "SUB11") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 374 | sub2_path = join(walk_path, "SUB2") |
| 375 | tmp1_path = join(walk_path, "tmp1") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 376 | tmp2_path = join(sub1_path, "tmp2") |
| 377 | tmp3_path = join(sub2_path, "tmp3") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 378 | link_path = join(sub2_path, "link") |
| 379 | t2_path = join(test_support.TESTFN, "TEST2") |
| 380 | tmp4_path = join(test_support.TESTFN, "TEST2", "tmp4") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 381 | |
| 382 | # Create stuff. |
| 383 | os.makedirs(sub11_path) |
| 384 | os.makedirs(sub2_path) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 385 | os.makedirs(t2_path) |
| 386 | for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path: |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 387 | f = open(path, "w") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 388 | f.write("I'm " + path + " and proud of it. Blame test_os.\n") |
| 389 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 390 | if hasattr(os, "symlink"): |
| 391 | os.symlink(os.path.abspath(t2_path), link_path) |
| 392 | sub2_tree = (sub2_path, ["link"], ["tmp3"]) |
| 393 | else: |
| 394 | sub2_tree = (sub2_path, [], ["tmp3"]) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 395 | |
| 396 | # Walk top-down. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 397 | all = list(os.walk(walk_path)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 398 | self.assertEqual(len(all), 4) |
| 399 | # We can't know which order SUB1 and SUB2 will appear in. |
| 400 | # Not flipped: TESTFN, SUB1, SUB11, SUB2 |
| 401 | # flipped: TESTFN, SUB2, SUB1, SUB11 |
| 402 | flipped = all[0][1][0] != "SUB1" |
| 403 | all[0][1].sort() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 404 | self.assertEqual(all[0], (walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 405 | self.assertEqual(all[1 + flipped], (sub1_path, ["SUB11"], ["tmp2"])) |
| 406 | self.assertEqual(all[2 + flipped], (sub11_path, [], [])) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 407 | self.assertEqual(all[3 - 2 * flipped], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 408 | |
| 409 | # Prune the search. |
| 410 | all = [] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 411 | for root, dirs, files in os.walk(walk_path): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 412 | all.append((root, dirs, files)) |
| 413 | # Don't descend into SUB1. |
| 414 | if 'SUB1' in dirs: |
| 415 | # Note that this also mutates the dirs we appended to all! |
| 416 | dirs.remove('SUB1') |
| 417 | self.assertEqual(len(all), 2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 418 | self.assertEqual(all[0], (walk_path, ["SUB2"], ["tmp1"])) |
| 419 | self.assertEqual(all[1], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 420 | |
| 421 | # Walk bottom-up. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 422 | all = list(os.walk(walk_path, topdown=False)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 423 | self.assertEqual(len(all), 4) |
| 424 | # We can't know which order SUB1 and SUB2 will appear in. |
| 425 | # Not flipped: SUB11, SUB1, SUB2, TESTFN |
| 426 | # flipped: SUB2, SUB11, SUB1, TESTFN |
| 427 | flipped = all[3][1][0] != "SUB1" |
| 428 | all[3][1].sort() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 429 | self.assertEqual(all[3], (walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 430 | self.assertEqual(all[flipped], (sub11_path, [], [])) |
| 431 | self.assertEqual(all[flipped + 1], (sub1_path, ["SUB11"], ["tmp2"])) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 432 | self.assertEqual(all[2 - 2 * flipped], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 433 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 434 | if hasattr(os, "symlink"): |
| 435 | # Walk, following symlinks. |
| 436 | for root, dirs, files in os.walk(walk_path, followlinks=True): |
| 437 | if root == link_path: |
| 438 | self.assertEqual(dirs, []) |
| 439 | self.assertEqual(files, ["tmp4"]) |
| 440 | break |
| 441 | else: |
| 442 | self.fail("Didn't follow symlink with followlinks=True") |
| 443 | |
| 444 | def tearDown(self): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 445 | # Tear everything down. This is a decent use for bottom-up on |
| 446 | # Windows, which doesn't have a recursive delete command. The |
| 447 | # (not so) subtlety is that rmdir will fail unless the dir's |
| 448 | # kids are removed first, so bottom up is essential. |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 449 | for root, dirs, files in os.walk(test_support.TESTFN, topdown=False): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 450 | for name in files: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 451 | os.remove(os.path.join(root, name)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 452 | for name in dirs: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 453 | dirname = os.path.join(root, name) |
| 454 | if not os.path.islink(dirname): |
| 455 | os.rmdir(dirname) |
| 456 | else: |
| 457 | os.remove(dirname) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 458 | os.rmdir(test_support.TESTFN) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 459 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 460 | class MakedirTests(unittest.TestCase): |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 461 | def setUp(self): |
| 462 | os.mkdir(test_support.TESTFN) |
| 463 | |
| 464 | def test_makedir(self): |
| 465 | base = test_support.TESTFN |
| 466 | path = os.path.join(base, 'dir1', 'dir2', 'dir3') |
| 467 | os.makedirs(path) # Should work |
| 468 | path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4') |
| 469 | os.makedirs(path) |
| 470 | |
| 471 | # Try paths with a '.' in them |
| 472 | self.failUnlessRaises(OSError, os.makedirs, os.curdir) |
| 473 | path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os.curdir) |
| 474 | os.makedirs(path) |
| 475 | path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4', |
| 476 | 'dir5', 'dir6') |
| 477 | os.makedirs(path) |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 478 | |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 479 | def tearDown(self): |
| 480 | path = os.path.join(test_support.TESTFN, 'dir1', 'dir2', 'dir3', |
| 481 | 'dir4', 'dir5', 'dir6') |
| 482 | # If the tests failed, the bottom-most directory ('../dir6') |
| 483 | # may not have been created, so we look for the outermost directory |
| 484 | # that exists. |
| 485 | while not os.path.exists(path) and path != test_support.TESTFN: |
| 486 | path = os.path.dirname(path) |
| 487 | |
| 488 | os.removedirs(path) |
| 489 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 490 | class DevNullTests(unittest.TestCase): |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 491 | def test_devnull(self): |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 492 | f = open(os.devnull, 'w') |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 493 | f.write('hello') |
| 494 | f.close() |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 495 | f = open(os.devnull, 'r') |
Tim Peters | 4182cfd | 2004-06-08 20:34:34 +0000 | [diff] [blame] | 496 | self.assertEqual(f.read(), '') |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 497 | f.close() |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 498 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 499 | class URandomTests(unittest.TestCase): |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 500 | def test_urandom(self): |
| 501 | try: |
| 502 | self.assertEqual(len(os.urandom(1)), 1) |
| 503 | self.assertEqual(len(os.urandom(10)), 10) |
| 504 | self.assertEqual(len(os.urandom(100)), 100) |
| 505 | self.assertEqual(len(os.urandom(1000)), 1000) |
| 506 | except NotImplementedError: |
| 507 | pass |
| 508 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 509 | class ExecTests(unittest.TestCase): |
| 510 | def test_execvpe_with_bad_program(self): |
Thomas Heller | bd315c5 | 2007-08-30 17:57:21 +0000 | [diff] [blame] | 511 | 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] | 512 | |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 513 | def test_execvpe_with_bad_arglist(self): |
| 514 | self.assertRaises(ValueError, os.execvpe, 'notepad', [], None) |
| 515 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 516 | class Win32ErrorTests(unittest.TestCase): |
| 517 | def test_rename(self): |
| 518 | self.assertRaises(WindowsError, os.rename, test_support.TESTFN, test_support.TESTFN+".bak") |
| 519 | |
| 520 | def test_remove(self): |
| 521 | self.assertRaises(WindowsError, os.remove, test_support.TESTFN) |
| 522 | |
| 523 | def test_chdir(self): |
| 524 | self.assertRaises(WindowsError, os.chdir, test_support.TESTFN) |
| 525 | |
| 526 | def test_mkdir(self): |
| 527 | self.assertRaises(WindowsError, os.chdir, test_support.TESTFN) |
| 528 | |
| 529 | def test_utime(self): |
| 530 | self.assertRaises(WindowsError, os.utime, test_support.TESTFN, None) |
| 531 | |
| 532 | def test_access(self): |
| 533 | self.assertRaises(WindowsError, os.utime, test_support.TESTFN, 0) |
| 534 | |
| 535 | def test_chmod(self): |
| 536 | self.assertRaises(WindowsError, os.utime, test_support.TESTFN, 0) |
| 537 | |
| 538 | if sys.platform != 'win32': |
| 539 | class Win32ErrorTests(unittest.TestCase): |
| 540 | pass |
| 541 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 542 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 543 | test_support.run_unittest( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 544 | FileTests, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 545 | StatAttributeTests, |
| 546 | EnvironTests, |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 547 | WalkTests, |
| 548 | MakedirTests, |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 549 | DevNullTests, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 550 | URandomTests, |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 551 | ExecTests, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 552 | Win32ErrorTests |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 553 | ) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 554 | |
| 555 | if __name__ == "__main__": |
| 556 | test_main() |