Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1)

Side by Side Diff: Lib/test/test_posix.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_platform.py ('k') | Lib/test/test_pydoc.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 "Test posix functions" 1 "Test posix functions"
2 2
3 from test import support 3 from test import support
4 4
5 # Skip these tests if there is no posix module. 5 # Skip these tests if there is no posix module.
6 posix = support.import_module('posix') 6 posix = support.import_module('posix')
7 7
8 import errno 8 import errno
9 import sys 9 import sys
10 import time 10 import time
11 import os 11 import os
12 import fcntl 12 import fcntl
13 import pwd 13 import pwd
14 import shutil 14 import shutil
15 import stat 15 import stat
16 import unittest 16 import unittest
17 import warnings 17 import warnings
18 18
19 _DUMMY_SYMLINK = '%s/dummy-symlink' % os.getenv('TMPDIR', '/tmp')
20 19
21 class PosixTester(unittest.TestCase): 20 class PosixTester(unittest.TestCase):
22 21
23 def setUp(self): 22 def setUp(self):
24 # create empty file 23 # create empty file
25 fp = open(support.TESTFN, 'w+') 24 fp = open(support.TESTFN, 'w+')
26 fp.close() 25 fp.close()
27 self.teardown_files = [ support.TESTFN ]
28 self._warnings_manager = support.check_warnings() 26 self._warnings_manager = support.check_warnings()
29 self._warnings_manager.__enter__() 27 self._warnings_manager.__enter__()
30 warnings.filterwarnings('ignore', '.* potential security risk .*', 28 warnings.filterwarnings('ignore', '.* potential security risk .*',
31 RuntimeWarning) 29 RuntimeWarning)
32 30
33 def tearDown(self): 31 def tearDown(self):
34 for teardown_file in self.teardown_files: 32 support.unlink(support.TESTFN)
35 support.unlink(teardown_file)
36 self._warnings_manager.__exit__(None, None, None) 33 self._warnings_manager.__exit__(None, None, None)
37 34
38 def testNoArgFunctions(self): 35 def testNoArgFunctions(self):
39 # test posix functions which take no arguments and have 36 # test posix functions which take no arguments and have
40 # no side-effects which we need to cleanup (e.g., fork, wait, abort) 37 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
41 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdb", "uname", 38 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdb", "uname",
42 "times", "getloadavg", 39 "times", "getloadavg",
43 "getegid", "geteuid", "getgid", "getgroups", 40 "getegid", "geteuid", "getgid", "getgroups",
44 "getpid", "getpgrp", "getppid", "getuid", "sync", 41 "getpid", "getpgrp", "getppid", "getuid", "sync",
45 ] 42 ]
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 # test a successful chown call 400 # test a successful chown call
404 chown_func(first_param, os.getuid(), os.getgid()) 401 chown_func(first_param, os.getuid(), os.getgid())
405 402
406 @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()") 403 @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
407 def test_chown(self): 404 def test_chown(self):
408 # raise an OSError if the file does not exist 405 # raise an OSError if the file does not exist
409 os.unlink(support.TESTFN) 406 os.unlink(support.TESTFN)
410 self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1) 407 self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1)
411 408
412 # re-create the file 409 # re-create the file
413 support.create_empty_file(support.TESTFN) 410 open(support.TESTFN, 'w').close()
414 self._test_all_chown_common(posix.chown, support.TESTFN) 411 self._test_all_chown_common(posix.chown, support.TESTFN)
415 412
416 @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()") 413 @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
417 def test_fchown(self): 414 def test_fchown(self):
418 os.unlink(support.TESTFN) 415 os.unlink(support.TESTFN)
419 416
420 # re-create the file 417 # re-create the file
421 test_file = open(support.TESTFN, 'w') 418 test_file = open(support.TESTFN, 'w')
422 try: 419 try:
423 fd = test_file.fileno() 420 fd = test_file.fileno()
424 self._test_all_chown_common(posix.fchown, fd) 421 self._test_all_chown_common(posix.fchown, fd)
425 finally: 422 finally:
426 test_file.close() 423 test_file.close()
427 424
428 @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()") 425 @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()")
429 def test_lchown(self): 426 def test_lchown(self):
430 os.unlink(support.TESTFN) 427 os.unlink(support.TESTFN)
431 # create a symlink 428 # create a symlink
432 os.symlink(_DUMMY_SYMLINK, support.TESTFN) 429 os.symlink('/tmp/dummy-symlink-target', support.TESTFN)
433 self._test_all_chown_common(posix.lchown, support.TESTFN) 430 self._test_all_chown_common(posix.lchown, support.TESTFN)
434 431
435 def test_chdir(self): 432 def test_chdir(self):
436 if hasattr(posix, 'chdir'): 433 if hasattr(posix, 'chdir'):
437 posix.chdir(os.curdir) 434 posix.chdir(os.curdir)
438 self.assertRaises(OSError, posix.chdir, support.TESTFN) 435 self.assertRaises(OSError, posix.chdir, support.TESTFN)
439 436
440 def test_listdir(self): 437 def test_listdir(self):
441 if hasattr(posix, 'listdir'): 438 if hasattr(posix, 'listdir'):
442 self.assertTrue(support.TESTFN in posix.listdir(os.curdir)) 439 self.assertTrue(support.TESTFN in posix.listdir(os.curdir))
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 def test_utime(self): 504 def test_utime(self):
508 if hasattr(posix, 'utime'): 505 if hasattr(posix, 'utime'):
509 now = time.time() 506 now = time.time()
510 posix.utime(support.TESTFN, None) 507 posix.utime(support.TESTFN, None)
511 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, Non e)) 508 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, Non e))
512 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None )) 509 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None ))
513 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now )) 510 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now ))
514 posix.utime(support.TESTFN, (int(now), int(now))) 511 posix.utime(support.TESTFN, (int(now), int(now)))
515 posix.utime(support.TESTFN, (now, now)) 512 posix.utime(support.TESTFN, (now, now))
516 513
517 def _test_chflags_regular_file(self, chflags_func, target_file): 514 def test_chflags(self):
518 st = os.stat(target_file) 515 if hasattr(posix, 'chflags'):
519 self.assertTrue(hasattr(st, 'st_flags')) 516 st = os.stat(support.TESTFN)
520 chflags_func(target_file, st.st_flags | stat.UF_IMMUTABLE) 517 if hasattr(st, 'st_flags'):
521 try: 518 posix.chflags(support.TESTFN, st.st_flags)
522 new_st = os.stat(target_file)
523 self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags)
524 try:
525 fd = open(target_file, 'w+')
526 except IOError as e:
527 self.assertEqual(e.errno, errno.EPERM)
528 finally:
529 posix.chflags(target_file, st.st_flags)
530 519
531 @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()') 520 def test_lchflags(self):
532 def test_chflags(self): 521 if hasattr(posix, 'lchflags'):
533 self._test_chflags_regular_file(posix.chflags, support.TESTFN) 522 st = os.stat(support.TESTFN)
534 523 if hasattr(st, 'st_flags'):
535 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') 524 posix.lchflags(support.TESTFN, st.st_flags)
536 def test_lchflags_regular_file(self):
537 self._test_chflags_regular_file(posix.lchflags, support.TESTFN)
538
539 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
540 def test_lchflags_symlink(self):
541 testfn_st = os.stat(support.TESTFN)
542
543 self.assertTrue(hasattr(testfn_st, 'st_flags'))
544
545 os.symlink(support.TESTFN, _DUMMY_SYMLINK)
546 self.teardown_files.append(_DUMMY_SYMLINK)
547 dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
548
549 posix.lchflags(_DUMMY_SYMLINK,
550 dummy_symlink_st.st_flags | stat.UF_IMMUTABLE)
551 try:
552 new_testfn_st = os.stat(support.TESTFN)
553 new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
554
555 self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags)
556 self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE,
557 new_dummy_symlink_st.st_flags)
558 finally:
559 posix.lchflags(_DUMMY_SYMLINK, dummy_symlink_st.st_flags)
560 525
561 def test_environ(self): 526 def test_environ(self):
562 if os.name == "nt": 527 if os.name == "nt":
563 item_type = str 528 item_type = str
564 else: 529 else:
565 item_type = bytes 530 item_type = bytes
566 for k, v in posix.environ.items(): 531 for k, v in posix.environ.items():
567 self.assertEqual(type(k), item_type) 532 self.assertEqual(type(k), item_type)
568 self.assertEqual(type(v), item_type) 533 self.assertEqual(type(v), item_type)
569 534
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 posix.fchmodat(f, support.TESTFN, stat.S_IRUSR | stat.S_IWUSR) 619 posix.fchmodat(f, support.TESTFN, stat.S_IRUSR | stat.S_IWUSR)
655 620
656 s = posix.stat(support.TESTFN) 621 s = posix.stat(support.TESTFN)
657 self.assertEqual(s[0] & stat.S_IRWXU, stat.S_IRUSR | stat.S_IWUSR) 622 self.assertEqual(s[0] & stat.S_IRWXU, stat.S_IRUSR | stat.S_IWUSR)
658 finally: 623 finally:
659 posix.close(f) 624 posix.close(f)
660 625
661 @unittest.skipUnless(hasattr(posix, 'fchownat'), "test needs posix.fchownat( )") 626 @unittest.skipUnless(hasattr(posix, 'fchownat'), "test needs posix.fchownat( )")
662 def test_fchownat(self): 627 def test_fchownat(self):
663 support.unlink(support.TESTFN) 628 support.unlink(support.TESTFN)
664 support.create_empty_file(support.TESTFN) 629 open(support.TESTFN, 'w').close()
665 630
666 f = posix.open(posix.getcwd(), posix.O_RDONLY) 631 f = posix.open(posix.getcwd(), posix.O_RDONLY)
667 try: 632 try:
668 posix.fchownat(f, support.TESTFN, os.getuid(), os.getgid()) 633 posix.fchownat(f, support.TESTFN, os.getuid(), os.getgid())
669 finally: 634 finally:
670 posix.close(f) 635 posix.close(f)
671 636
672 @unittest.skipUnless(hasattr(posix, 'fstatat'), "test needs posix.fstatat()" ) 637 @unittest.skipUnless(hasattr(posix, 'fstatat'), "test needs posix.fstatat()" )
673 def test_fstatat(self): 638 def test_fstatat(self):
674 support.unlink(support.TESTFN) 639 support.unlink(support.TESTFN)
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
759 try: 724 try:
760 self.assertEqual(posix.readlink(support.TESTFN + 'link'), 725 self.assertEqual(posix.readlink(support.TESTFN + 'link'),
761 posix.readlinkat(f, support.TESTFN + 'link')) 726 posix.readlinkat(f, support.TESTFN + 'link'))
762 finally: 727 finally:
763 support.unlink(support.TESTFN + 'link') 728 support.unlink(support.TESTFN + 'link')
764 posix.close(f) 729 posix.close(f)
765 730
766 @unittest.skipUnless(hasattr(posix, 'renameat'), "test needs posix.renameat( )") 731 @unittest.skipUnless(hasattr(posix, 'renameat'), "test needs posix.renameat( )")
767 def test_renameat(self): 732 def test_renameat(self):
768 support.unlink(support.TESTFN) 733 support.unlink(support.TESTFN)
769 support.create_empty_file(support.TESTFN + 'ren') 734 open(support.TESTFN + 'ren', 'w').close()
770 f = posix.open(posix.getcwd(), posix.O_RDONLY) 735 f = posix.open(posix.getcwd(), posix.O_RDONLY)
771 try: 736 try:
772 posix.renameat(f, support.TESTFN + 'ren', f, support.TESTFN) 737 posix.renameat(f, support.TESTFN + 'ren', f, support.TESTFN)
773 except: 738 except:
774 posix.rename(support.TESTFN + 'ren', support.TESTFN) 739 posix.rename(support.TESTFN + 'ren', support.TESTFN)
775 raise 740 raise
776 else: 741 else:
777 posix.stat(support.TESTFN) # should not throw exception 742 posix.stat(support.TESTFN) # should not throw exception
778 finally: 743 finally:
779 posix.close(f) 744 posix.close(f)
780 745
781 @unittest.skipUnless(hasattr(posix, 'symlinkat'), "test needs posix.symlinka t()") 746 @unittest.skipUnless(hasattr(posix, 'symlinkat'), "test needs posix.symlinka t()")
782 def test_symlinkat(self): 747 def test_symlinkat(self):
783 f = posix.open(posix.getcwd(), posix.O_RDONLY) 748 f = posix.open(posix.getcwd(), posix.O_RDONLY)
784 try: 749 try:
785 posix.symlinkat(support.TESTFN, f, support.TESTFN + 'link') 750 posix.symlinkat(support.TESTFN, f, support.TESTFN + 'link')
786 self.assertEqual(posix.readlink(support.TESTFN + 'link'), support.TE STFN) 751 self.assertEqual(posix.readlink(support.TESTFN + 'link'), support.TE STFN)
787 finally: 752 finally:
788 posix.close(f) 753 posix.close(f)
789 support.unlink(support.TESTFN + 'link') 754 support.unlink(support.TESTFN + 'link')
790 755
791 @unittest.skipUnless(hasattr(posix, 'unlinkat'), "test needs posix.unlinkat( )") 756 @unittest.skipUnless(hasattr(posix, 'unlinkat'), "test needs posix.unlinkat( )")
792 def test_unlinkat(self): 757 def test_unlinkat(self):
793 f = posix.open(posix.getcwd(), posix.O_RDONLY) 758 f = posix.open(posix.getcwd(), posix.O_RDONLY)
794 support.create_empty_file(support.TESTFN + 'del') 759 open(support.TESTFN + 'del', 'w').close()
795 posix.stat(support.TESTFN + 'del') # should not throw exception 760 posix.stat(support.TESTFN + 'del') # should not throw exception
796 try: 761 try:
797 posix.unlinkat(f, support.TESTFN + 'del') 762 posix.unlinkat(f, support.TESTFN + 'del')
798 except: 763 except:
799 support.unlink(support.TESTFN + 'del') 764 support.unlink(support.TESTFN + 'del')
800 raise 765 raise
801 else: 766 else:
802 self.assertRaises(OSError, posix.stat, support.TESTFN + 'link') 767 self.assertRaises(OSError, posix.stat, support.TESTFN + 'link')
803 finally: 768 finally:
804 posix.close(f) 769 posix.close(f)
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
864 829
865 830
866 def test_main(): 831 def test_main():
867 try: 832 try:
868 support.run_unittest(PosixTester, PosixGroupsTester) 833 support.run_unittest(PosixTester, PosixGroupsTester)
869 finally: 834 finally:
870 support.reap_children() 835 support.reap_children()
871 836
872 if __name__ == '__main__': 837 if __name__ == '__main__':
873 test_main() 838 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_platform.py ('k') | Lib/test/test_pydoc.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+