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