Index: Lib/test/test_posix.py =================================================================== --- Lib/test/test_posix.py (revision 88853) +++ Lib/test/test_posix.py (working copy) @@ -10,9 +10,11 @@ import os import pwd import shutil +import stat import unittest import warnings +_DUMMY_SYMLINK = '%s/dummy-symlink' % os.getenv('TMPDIR', '/tmp') warnings.filterwarnings('ignore', '.* potential security risk .*', RuntimeWarning) @@ -23,9 +25,11 @@ # create empty file fp = open(test_support.TESTFN, 'w+') fp.close() + self.teardown_files = [ test_support.TESTFN ] def tearDown(self): - os.unlink(test_support.TESTFN) + for teardown_file in self.teardown_files: + os.unlink(teardown_file) def testNoArgFunctions(self): # test posix functions which take no arguments and have @@ -254,7 +258,7 @@ def test_lchown(self): os.unlink(test_support.TESTFN) # create a symlink - os.symlink('/tmp/dummy-symlink-target', test_support.TESTFN) + os.symlink(_DUMMY_SYMLINK, test_support.TESTFN) self._test_all_chown_common(posix.lchown, test_support.TESTFN) def test_chdir(self): @@ -307,18 +311,51 @@ posix.utime(test_support.TESTFN, (int(now), int(now))) posix.utime(test_support.TESTFN, (now, now)) + def _test_chflags_regular_file(self, chflags_func, target_file): + + st = os.stat(target_file) + self.assertTrue(hasattr(st, 'st_flags')) + posix.chflags(target_file, st.st_flags | stat.UF_IMMUTABLE) + try: + new_st = os.stat(target_file) + self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags) + try: + fd = open(target_file, 'w+') + except IOError as e: + self.assertEqual(e.errno, errno.EPERM) + finally: + posix.chflags(target_file, st.st_flags) + + @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()') def test_chflags(self): - if hasattr(posix, 'chflags'): - st = os.stat(test_support.TESTFN) - if hasattr(st, 'st_flags'): - posix.chflags(test_support.TESTFN, st.st_flags) + self._test_chflags_regular_file(posix.chflags, test_support.TESTFN) - def test_lchflags(self): - if hasattr(posix, 'lchflags'): - st = os.stat(test_support.TESTFN) - if hasattr(st, 'st_flags'): - posix.lchflags(test_support.TESTFN, st.st_flags) + @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') + def test_lchflags_regular_file(self): + self._test_chflags_regular_file(posix.lchflags, test_support.TESTFN) + @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') + def test_lchflags_symlink(self): + testfn_st = os.stat(test_support.TESTFN) + + self.assertTrue(hasattr(testfn_st, 'st_flags')) + + os.symlink(test_support.TESTFN, _DUMMY_SYMLINK) + self.teardown_files.append(_DUMMY_SYMLINK) + dummy_symlink_st = os.lstat(_DUMMY_SYMLINK) + + posix.lchflags(_DUMMY_SYMLINK, + dummy_symlink_st.st_flags | stat.UF_IMMUTABLE) + try: + new_testfn_st = os.stat(test_support.TESTFN) + new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK) + + self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags) + self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE, + new_dummy_symlink_st.st_flags) + finally: + posix.lchflags(_DUMMY_SYMLINK, dummy_symlink_st.st_flags) + def test_getcwd_long_pathnames(self): if hasattr(posix, 'getcwd'): dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'