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