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