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