|
15 | 15 | import unittest |
16 | 16 | import warnings |
17 | 17 |
|
| 18 | +_DUMMY_SYMLINK = '%s/dummy-symlink' % os.getenv('TMPDIR', '/tmp') |
18 | 19 |
|
19 | 20 | class PosixTester(unittest.TestCase): |
20 | 21 |
|
21 | 22 | def setUp(self): |
22 | 23 | # create empty file |
23 | 24 | fp = open(support.TESTFN, 'w+') |
24 | 25 | fp.close() |
| 26 | + self.teardown_files = [ support.TESTFN ] |
25 | 27 | self._warnings_manager = support.check_warnings() |
26 | 28 | self._warnings_manager.__enter__() |
27 | 29 | warnings.filterwarnings('ignore', '.* potential security risk .*', |
28 | 30 | RuntimeWarning) |
29 | 31 |
|
30 | 32 | def tearDown(self): |
31 | | - support.unlink(support.TESTFN) |
| 33 | + for teardown_file in self.teardown_files: |
| 34 | + support.unlink(teardown_file) |
32 | 35 | self._warnings_manager.__exit__(None, None, None) |
33 | 36 |
|
34 | 37 | def testNoArgFunctions(self): |
@@ -268,7 +271,7 @@ def test_fchown(self): |
268 | 271 | def test_lchown(self): |
269 | 272 | os.unlink(support.TESTFN) |
270 | 273 | # create a symlink |
271 | | - os.symlink('/tmp/dummy-symlink-target', support.TESTFN) |
| 274 | + os.symlink(_DUMMY_SYMLINK, support.TESTFN) |
272 | 275 | self._test_all_chown_common(posix.lchown, support.TESTFN) |
273 | 276 |
|
274 | 277 | def test_chdir(self): |
@@ -315,17 +318,49 @@ def test_utime(self): |
315 | 318 | posix.utime(support.TESTFN, (int(now), int(now))) |
316 | 319 | posix.utime(support.TESTFN, (now, now)) |
317 | 320 |
|
| 321 | + def _test_chflags_regular_file(self, chflags_func, target_file): |
| 322 | + st = os.stat(target_file) |
| 323 | + self.assertTrue(hasattr(st, 'st_flags')) |
| 324 | + chflags_func(target_file, st.st_flags | stat.UF_IMMUTABLE) |
| 325 | + try: |
| 326 | + new_st = os.stat(target_file) |
| 327 | + self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags) |
| 328 | + try: |
| 329 | + fd = open(target_file, 'w+') |
| 330 | + except IOError as e: |
| 331 | + self.assertEqual(e.errno, errno.EPERM) |
| 332 | + finally: |
| 333 | + posix.chflags(target_file, st.st_flags) |
| 334 | + |
| 335 | + @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()') |
318 | 336 | def test_chflags(self): |
319 | | - if hasattr(posix, 'chflags'): |
320 | | - st = os.stat(support.TESTFN) |
321 | | - if hasattr(st, 'st_flags'): |
322 | | - posix.chflags(support.TESTFN, st.st_flags) |
323 | | - |
324 | | - def test_lchflags(self): |
325 | | - if hasattr(posix, 'lchflags'): |
326 | | - st = os.stat(support.TESTFN) |
327 | | - if hasattr(st, 'st_flags'): |
328 | | - posix.lchflags(support.TESTFN, st.st_flags) |
| 337 | + self._test_chflags_regular_file(posix.chflags, support.TESTFN) |
| 338 | + |
| 339 | + @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') |
| 340 | + def test_lchflags_regular_file(self): |
| 341 | + self._test_chflags_regular_file(posix.lchflags, support.TESTFN) |
| 342 | + |
| 343 | + @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') |
| 344 | + def test_lchflags_symlink(self): |
| 345 | + testfn_st = os.stat(support.TESTFN) |
| 346 | + |
| 347 | + self.assertTrue(hasattr(testfn_st, 'st_flags')) |
| 348 | + |
| 349 | + os.symlink(support.TESTFN, _DUMMY_SYMLINK) |
| 350 | + self.teardown_files.append(_DUMMY_SYMLINK) |
| 351 | + dummy_symlink_st = os.lstat(_DUMMY_SYMLINK) |
| 352 | + |
| 353 | + posix.lchflags(_DUMMY_SYMLINK, |
| 354 | + dummy_symlink_st.st_flags | stat.UF_IMMUTABLE) |
| 355 | + try: |
| 356 | + new_testfn_st = os.stat(support.TESTFN) |
| 357 | + new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK) |
| 358 | + |
| 359 | + self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags) |
| 360 | + self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE, |
| 361 | + new_dummy_symlink_st.st_flags) |
| 362 | + finally: |
| 363 | + posix.lchflags(_DUMMY_SYMLINK, dummy_symlink_st.st_flags) |
329 | 364 |
|
330 | 365 | def test_environ(self): |
331 | 366 | if os.name == "nt": |
|
0 commit comments