OLD | NEW |
1 # As a test suite for the os module, this is woefully inadequate, but this | 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 | 2 # does add tests for a few functions which have been determined to be more |
3 # portable than they had been thought to be. | 3 # portable than they had been thought to be. |
4 | 4 |
5 import os | 5 import os |
6 import errno | 6 import errno |
7 import unittest | 7 import unittest |
8 import warnings | 8 import warnings |
9 import sys | 9 import sys |
10 import signal | 10 import signal |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 os.rmdir(support.TESTFN) | 222 os.rmdir(support.TESTFN) |
223 | 223 |
224 def check_stat_attributes(self, fname): | 224 def check_stat_attributes(self, fname): |
225 if not hasattr(os, "stat"): | 225 if not hasattr(os, "stat"): |
226 return | 226 return |
227 | 227 |
228 import stat | 228 import stat |
229 result = os.stat(fname) | 229 result = os.stat(fname) |
230 | 230 |
231 # Make sure direct access works | 231 # Make sure direct access works |
232 self.assertEquals(result[stat.ST_SIZE], 3) | 232 self.assertEqual(result[stat.ST_SIZE], 3) |
233 self.assertEquals(result.st_size, 3) | 233 self.assertEqual(result.st_size, 3) |
234 | 234 |
235 # Make sure all the attributes are there | 235 # Make sure all the attributes are there |
236 members = dir(result) | 236 members = dir(result) |
237 for name in dir(stat): | 237 for name in dir(stat): |
238 if name[:3] == 'ST_': | 238 if name[:3] == 'ST_': |
239 attr = name.lower() | 239 attr = name.lower() |
240 if name.endswith("TIME"): | 240 if name.endswith("TIME"): |
241 def trunc(x): return int(x) | 241 def trunc(x): return int(x) |
242 else: | 242 else: |
243 def trunc(x): return x | 243 def trunc(x): return x |
244 self.assertEquals(trunc(getattr(result, attr)), | 244 self.assertEqual(trunc(getattr(result, attr)), |
245 result[getattr(stat, name)]) | 245 result[getattr(stat, name)]) |
246 self.assertIn(attr, members) | 246 self.assertIn(attr, members) |
247 | 247 |
248 try: | 248 try: |
249 result[200] | 249 result[200] |
250 self.fail("No exception thrown") | 250 self.fail("No exception thrown") |
251 except IndexError: | 251 except IndexError: |
252 pass | 252 pass |
253 | 253 |
254 # Make sure that assignment fails | 254 # Make sure that assignment fails |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 return | 298 return |
299 | 299 |
300 try: | 300 try: |
301 result = os.statvfs(self.fname) | 301 result = os.statvfs(self.fname) |
302 except OSError as e: | 302 except OSError as e: |
303 # On AtheOS, glibc always returns ENOSYS | 303 # On AtheOS, glibc always returns ENOSYS |
304 if e.errno == errno.ENOSYS: | 304 if e.errno == errno.ENOSYS: |
305 return | 305 return |
306 | 306 |
307 # Make sure direct access works | 307 # Make sure direct access works |
308 self.assertEquals(result.f_bfree, result[3]) | 308 self.assertEqual(result.f_bfree, result[3]) |
309 | 309 |
310 # Make sure all the attributes are there. | 310 # Make sure all the attributes are there. |
311 members = ('bsize', 'frsize', 'blocks', 'bfree', 'bavail', 'files', | 311 members = ('bsize', 'frsize', 'blocks', 'bfree', 'bavail', 'files', |
312 'ffree', 'favail', 'flag', 'namemax') | 312 'ffree', 'favail', 'flag', 'namemax') |
313 for value, member in enumerate(members): | 313 for value, member in enumerate(members): |
314 self.assertEquals(getattr(result, 'f_' + member), result[value]) | 314 self.assertEqual(getattr(result, 'f_' + member), result[value]) |
315 | 315 |
316 # Make sure that assignment really fails | 316 # Make sure that assignment really fails |
317 try: | 317 try: |
318 result.f_bfree = 1 | 318 result.f_bfree = 1 |
319 self.fail("No exception thrown") | 319 self.fail("No exception thrown") |
320 except AttributeError: | 320 except AttributeError: |
321 pass | 321 pass |
322 | 322 |
323 try: | 323 try: |
324 result.parrot = 1 | 324 result.parrot = 1 |
(...skipping 14 matching lines...) Expand all Loading... |
339 except TypeError: | 339 except TypeError: |
340 pass | 340 pass |
341 | 341 |
342 def test_utime_dir(self): | 342 def test_utime_dir(self): |
343 delta = 1000000 | 343 delta = 1000000 |
344 st = os.stat(support.TESTFN) | 344 st = os.stat(support.TESTFN) |
345 # round to int, because some systems may support sub-second | 345 # round to int, because some systems may support sub-second |
346 # time stamps in stat, but not in utime. | 346 # time stamps in stat, but not in utime. |
347 os.utime(support.TESTFN, (st.st_atime, int(st.st_mtime-delta))) | 347 os.utime(support.TESTFN, (st.st_atime, int(st.st_mtime-delta))) |
348 st2 = os.stat(support.TESTFN) | 348 st2 = os.stat(support.TESTFN) |
349 self.assertEquals(st2.st_mtime, int(st.st_mtime-delta)) | 349 self.assertEqual(st2.st_mtime, int(st.st_mtime-delta)) |
350 | 350 |
351 # Restrict test to Win32, since there is no guarantee other | 351 # Restrict test to Win32, since there is no guarantee other |
352 # systems support centiseconds | 352 # systems support centiseconds |
353 if sys.platform == 'win32': | 353 if sys.platform == 'win32': |
354 def get_file_system(path): | 354 def get_file_system(path): |
355 root = os.path.splitdrive(os.path.abspath(path))[0] + '\\' | 355 root = os.path.splitdrive(os.path.abspath(path))[0] + '\\' |
356 import ctypes | 356 import ctypes |
357 kernel32 = ctypes.windll.kernel32 | 357 kernel32 = ctypes.windll.kernel32 |
358 buf = ctypes.create_unicode_buffer("", 100) | 358 buf = ctypes.create_unicode_buffer("", 100) |
359 if kernel32.GetVolumeInformationW(root, None, 0, None, None, None, b
uf, len(buf)): | 359 if kernel32.GetVolumeInformationW(root, None, 0, None, None, None, b
uf, len(buf)): |
360 return buf.value | 360 return buf.value |
361 | 361 |
362 if get_file_system(support.TESTFN) == "NTFS": | 362 if get_file_system(support.TESTFN) == "NTFS": |
363 def test_1565150(self): | 363 def test_1565150(self): |
364 t1 = 1159195039.25 | 364 t1 = 1159195039.25 |
365 os.utime(self.fname, (t1, t1)) | 365 os.utime(self.fname, (t1, t1)) |
366 self.assertEquals(os.stat(self.fname).st_mtime, t1) | 366 self.assertEqual(os.stat(self.fname).st_mtime, t1) |
367 | 367 |
368 def test_1686475(self): | 368 def test_1686475(self): |
369 # Verify that an open file can be stat'ed | 369 # Verify that an open file can be stat'ed |
370 try: | 370 try: |
371 os.stat(r"c:\pagefile.sys") | 371 os.stat(r"c:\pagefile.sys") |
372 except WindowsError as e: | 372 except WindowsError as e: |
373 if e.errno == 2: # file does not exist; cannot run test | 373 if e.errno == 2: # file does not exist; cannot run test |
374 return | 374 return |
375 self.fail("Could not stat pagefile.sys") | 375 self.fail("Could not stat pagefile.sys") |
376 | 376 |
(...skipping 24 matching lines...) Expand all Loading... |
401 os.environ.clear() | 401 os.environ.clear() |
402 return os.environ | 402 return os.environ |
403 | 403 |
404 # Bug 1110478 | 404 # Bug 1110478 |
405 def test_update2(self): | 405 def test_update2(self): |
406 os.environ.clear() | 406 os.environ.clear() |
407 if os.path.exists("/bin/sh"): | 407 if os.path.exists("/bin/sh"): |
408 os.environ.update(HELLO="World") | 408 os.environ.update(HELLO="World") |
409 with os.popen("/bin/sh -c 'echo $HELLO'") as popen: | 409 with os.popen("/bin/sh -c 'echo $HELLO'") as popen: |
410 value = popen.read().strip() | 410 value = popen.read().strip() |
411 self.assertEquals(value, "World") | 411 self.assertEqual(value, "World") |
412 | 412 |
413 def test_os_popen_iter(self): | 413 def test_os_popen_iter(self): |
414 if os.path.exists("/bin/sh"): | 414 if os.path.exists("/bin/sh"): |
415 with os.popen( | 415 with os.popen( |
416 "/bin/sh -c 'echo \"line1\nline2\nline3\"'") as popen: | 416 "/bin/sh -c 'echo \"line1\nline2\nline3\"'") as popen: |
417 it = iter(popen) | 417 it = iter(popen) |
418 self.assertEquals(next(it), "line1\n") | 418 self.assertEqual(next(it), "line1\n") |
419 self.assertEquals(next(it), "line2\n") | 419 self.assertEqual(next(it), "line2\n") |
420 self.assertEquals(next(it), "line3\n") | 420 self.assertEqual(next(it), "line3\n") |
421 self.assertRaises(StopIteration, next, it) | 421 self.assertRaises(StopIteration, next, it) |
422 | 422 |
423 # Verify environ keys and values from the OS are of the | 423 # Verify environ keys and values from the OS are of the |
424 # correct str type. | 424 # correct str type. |
425 def test_keyvalue_types(self): | 425 def test_keyvalue_types(self): |
426 for key, val in os.environ.items(): | 426 for key, val in os.environ.items(): |
427 self.assertEquals(type(key), str) | 427 self.assertEqual(type(key), str) |
428 self.assertEquals(type(val), str) | 428 self.assertEqual(type(val), str) |
429 | 429 |
430 def test_items(self): | 430 def test_items(self): |
431 for key, value in self._reference().items(): | 431 for key, value in self._reference().items(): |
432 self.assertEqual(os.environ.get(key), value) | 432 self.assertEqual(os.environ.get(key), value) |
433 | 433 |
434 # Issue 7310 | 434 # Issue 7310 |
435 def test___repr__(self): | 435 def test___repr__(self): |
436 """Check that the repr() of os.environ looks like environ({...}).""" | 436 """Check that the repr() of os.environ looks like environ({...}).""" |
437 env = os.environ | 437 env = os.environ |
438 self.assertEqual(repr(env), 'environ({{{}}})'.format(', '.join( | 438 self.assertEqual(repr(env), 'environ({{{}}})'.format(', '.join( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 # os.environ -> os.environb | 486 # os.environ -> os.environb |
487 value = 'euro\u20ac' | 487 value = 'euro\u20ac' |
488 try: | 488 try: |
489 value_bytes = value.encode(sys.getfilesystemencoding(), | 489 value_bytes = value.encode(sys.getfilesystemencoding(), |
490 'surrogateescape') | 490 'surrogateescape') |
491 except UnicodeEncodeError: | 491 except UnicodeEncodeError: |
492 msg = "U+20AC character is not encodable to %s" % ( | 492 msg = "U+20AC character is not encodable to %s" % ( |
493 sys.getfilesystemencoding(),) | 493 sys.getfilesystemencoding(),) |
494 self.skipTest(msg) | 494 self.skipTest(msg) |
495 os.environ['unicode'] = value | 495 os.environ['unicode'] = value |
496 self.assertEquals(os.environ['unicode'], value) | 496 self.assertEqual(os.environ['unicode'], value) |
497 self.assertEquals(os.environb[b'unicode'], value_bytes) | 497 self.assertEqual(os.environb[b'unicode'], value_bytes) |
498 | 498 |
499 # os.environb -> os.environ | 499 # os.environb -> os.environ |
500 value = b'\xff' | 500 value = b'\xff' |
501 os.environb[b'bytes'] = value | 501 os.environb[b'bytes'] = value |
502 self.assertEquals(os.environb[b'bytes'], value) | 502 self.assertEqual(os.environb[b'bytes'], value) |
503 value_str = value.decode(sys.getfilesystemencoding(), 'surrogateescape') | 503 value_str = value.decode(sys.getfilesystemencoding(), 'surrogateescape') |
504 self.assertEquals(os.environ['bytes'], value_str) | 504 self.assertEqual(os.environ['bytes'], value_str) |
505 | 505 |
506 class WalkTests(unittest.TestCase): | 506 class WalkTests(unittest.TestCase): |
507 """Tests for os.walk().""" | 507 """Tests for os.walk().""" |
508 | 508 |
509 def test_traversal(self): | 509 def test_traversal(self): |
510 import os | 510 import os |
511 from os.path import join | 511 from os.path import join |
512 | 512 |
513 # Build: | 513 # Build: |
514 # TESTFN/ | 514 # TESTFN/ |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
951 except: | 951 except: |
952 shutil.rmtree(self.dir) | 952 shutil.rmtree(self.dir) |
953 raise | 953 raise |
954 | 954 |
955 def tearDown(self): | 955 def tearDown(self): |
956 shutil.rmtree(self.dir) | 956 shutil.rmtree(self.dir) |
957 | 957 |
958 def test_listdir(self): | 958 def test_listdir(self): |
959 expected = self.unicodefn | 959 expected = self.unicodefn |
960 found = set(os.listdir(self.dir)) | 960 found = set(os.listdir(self.dir)) |
961 self.assertEquals(found, expected) | 961 self.assertEqual(found, expected) |
962 | 962 |
963 def test_open(self): | 963 def test_open(self): |
964 for fn in self.unicodefn: | 964 for fn in self.unicodefn: |
965 f = open(os.path.join(self.dir, fn)) | 965 f = open(os.path.join(self.dir, fn)) |
966 f.close() | 966 f.close() |
967 | 967 |
968 def test_stat(self): | 968 def test_stat(self): |
969 for fn in self.unicodefn: | 969 for fn in self.unicodefn: |
970 os.stat(os.path.join(self.dir, fn)) | 970 os.stat(os.path.join(self.dir, fn)) |
971 else: | 971 else: |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1161 # consider allowing rmdir to remove directory links | 1161 # consider allowing rmdir to remove directory links |
1162 os.rmdir(self.missing_link) | 1162 os.rmdir(self.missing_link) |
1163 | 1163 |
1164 def check_stat(self, link, target): | 1164 def check_stat(self, link, target): |
1165 self.assertEqual(os.stat(link), os.stat(target)) | 1165 self.assertEqual(os.stat(link), os.stat(target)) |
1166 self.assertNotEqual(os.lstat(link), os.stat(link)) | 1166 self.assertNotEqual(os.lstat(link), os.stat(link)) |
1167 | 1167 |
1168 | 1168 |
1169 class FSEncodingTests(unittest.TestCase): | 1169 class FSEncodingTests(unittest.TestCase): |
1170 def test_nop(self): | 1170 def test_nop(self): |
1171 self.assertEquals(os.fsencode(b'abc\xff'), b'abc\xff') | 1171 self.assertEqual(os.fsencode(b'abc\xff'), b'abc\xff') |
1172 self.assertEquals(os.fsdecode('abc\u0141'), 'abc\u0141') | 1172 self.assertEqual(os.fsdecode('abc\u0141'), 'abc\u0141') |
1173 | 1173 |
1174 def test_identity(self): | 1174 def test_identity(self): |
1175 # assert fsdecode(fsencode(x)) == x | 1175 # assert fsdecode(fsencode(x)) == x |
1176 for fn in ('unicode\u0141', 'latin\xe9', 'ascii'): | 1176 for fn in ('unicode\u0141', 'latin\xe9', 'ascii'): |
1177 try: | 1177 try: |
1178 bytesfn = os.fsencode(fn) | 1178 bytesfn = os.fsencode(fn) |
1179 except UnicodeEncodeError: | 1179 except UnicodeEncodeError: |
1180 continue | 1180 continue |
1181 self.assertEquals(os.fsdecode(bytesfn), fn) | 1181 self.assertEqual(os.fsdecode(bytesfn), fn) |
1182 | 1182 |
1183 | 1183 |
1184 class PidTests(unittest.TestCase): | 1184 class PidTests(unittest.TestCase): |
1185 @unittest.skipUnless(hasattr(os, 'getppid'), "test needs os.getppid") | 1185 @unittest.skipUnless(hasattr(os, 'getppid'), "test needs os.getppid") |
1186 def test_getppid(self): | 1186 def test_getppid(self): |
1187 p = subprocess.Popen([sys.executable, '-c', | 1187 p = subprocess.Popen([sys.executable, '-c', |
1188 'import os; print(os.getppid())'], | 1188 'import os; print(os.getppid())'], |
1189 stdout=subprocess.PIPE) | 1189 stdout=subprocess.PIPE) |
1190 stdout, _ = p.communicate() | 1190 stdout, _ = p.communicate() |
1191 # We are the parent of our subprocess | 1191 # We are the parent of our subprocess |
(...skipping 26 matching lines...) Expand all Loading... |
1218 Pep383Tests, | 1218 Pep383Tests, |
1219 Win32KillTests, | 1219 Win32KillTests, |
1220 Win32SymlinkTests, | 1220 Win32SymlinkTests, |
1221 FSEncodingTests, | 1221 FSEncodingTests, |
1222 PidTests, | 1222 PidTests, |
1223 LoginTests, | 1223 LoginTests, |
1224 ) | 1224 ) |
1225 | 1225 |
1226 if __name__ == "__main__": | 1226 if __name__ == "__main__": |
1227 test_main() | 1227 test_main() |
OLD | NEW |