diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2012-06-23 19:28:15 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2012-06-23 19:28:15 (GMT) |
commit | 2f8a75c7a750ba06f9eeaf8e949d953852136e37 (patch) | |
tree | 54140146cea0fc72d4963109a80c414ecdfb9d9c | |
parent | d5ecd49af9863d76254809baf88c646beab5a5d7 (diff) | |
download | cpython-2f8a75c7a750ba06f9eeaf8e949d953852136e37.zip cpython-2f8a75c7a750ba06f9eeaf8e949d953852136e37.tar.gz cpython-2f8a75c7a750ba06f9eeaf8e949d953852136e37.tar.bz2 |
Proper cleanup in test_shutil, even in case of error.
-rw-r--r-- | Lib/test/test_shutil.py | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index b12d259..a830935 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -120,6 +120,8 @@ class TestShutil(unittest.TestCase): def test_on_error(self): self.errorState = 0 os.mkdir(TESTFN) + self.addCleanup(shutil.rmtree, TESTFN) + self.child_file_path = os.path.join(TESTFN, 'a') self.child_dir_path = os.path.join(TESTFN, 'b') support.create_empty_file(self.child_file_path) @@ -133,20 +135,16 @@ class TestShutil(unittest.TestCase): os.chmod(self.child_dir_path, new_mode) os.chmod(TESTFN, new_mode) + self.addCleanup(os.chmod, TESTFN, old_dir_mode) + self.addCleanup(os.chmod, self.child_file_path, old_child_file_mode) + self.addCleanup(os.chmod, self.child_dir_path, old_child_dir_mode) + shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror) # Test whether onerror has actually been called. self.assertEqual(self.errorState, 3, "Expected call to onerror function did not " "happen.") - # Make writable again. - os.chmod(TESTFN, old_dir_mode) - os.chmod(self.child_file_path, old_child_file_mode) - os.chmod(self.child_dir_path, old_child_dir_mode) - - # Clean up. - shutil.rmtree(TESTFN) - def check_args_to_onerror(self, func, arg, exc): # test_rmtree_errors deliberately runs rmtree # on a directory that is chmod 500, which will fail. |