Commit e6e5a8d8 by Ned Batchelder

Convert some try/finally to addCleanup

parent cb0624bf
...@@ -571,12 +571,10 @@ class TestMongoModuleStore(TestMongoModuleStoreBase): ...@@ -571,12 +571,10 @@ class TestMongoModuleStore(TestMongoModuleStoreBase):
self.content_store.find(location) self.content_store.find(location)
root_dir = path(mkdtemp()) root_dir = path(mkdtemp())
try: self.addCleanup(shutil.rmtree, root_dir)
export_course_to_xml(self.draft_store, self.content_store, course_key, root_dir, 'test_export') export_course_to_xml(self.draft_store, self.content_store, course_key, root_dir, 'test_export')
assert_true(path(root_dir / 'test_export/static/images/course_image.jpg').isfile()) self.assertTrue(path(root_dir / 'test_export/static/images/course_image.jpg').isfile())
assert_true(path(root_dir / 'test_export/static/images_course_image.jpg').isfile()) self.assertTrue(path(root_dir / 'test_export/static/images_course_image.jpg').isfile())
finally:
shutil.rmtree(root_dir)
@patch('xmodule.tabs.CourseTab.from_json', side_effect=mock_tab_from_json) @patch('xmodule.tabs.CourseTab.from_json', side_effect=mock_tab_from_json)
def test_export_course_image_nondefault(self, _from_json): def test_export_course_image_nondefault(self, _from_json):
...@@ -588,12 +586,10 @@ class TestMongoModuleStore(TestMongoModuleStoreBase): ...@@ -588,12 +586,10 @@ class TestMongoModuleStore(TestMongoModuleStoreBase):
assert_true(course.course_image, 'just_a_test.jpg') assert_true(course.course_image, 'just_a_test.jpg')
root_dir = path(mkdtemp()) root_dir = path(mkdtemp())
try: self.addCleanup(shutil.rmtree, root_dir)
export_course_to_xml(self.draft_store, self.content_store, course.id, root_dir, 'test_export') export_course_to_xml(self.draft_store, self.content_store, course.id, root_dir, 'test_export')
assert_true(path(root_dir / 'test_export/static/just_a_test.jpg').isfile()) self.assertTrue(path(root_dir / 'test_export/static/just_a_test.jpg').isfile())
assert_false(path(root_dir / 'test_export/static/images/course_image.jpg').isfile()) self.assertFalse(path(root_dir / 'test_export/static/images/course_image.jpg').isfile())
finally:
shutil.rmtree(root_dir)
def test_course_without_image(self): def test_course_without_image(self):
""" """
...@@ -602,12 +598,10 @@ class TestMongoModuleStore(TestMongoModuleStoreBase): ...@@ -602,12 +598,10 @@ class TestMongoModuleStore(TestMongoModuleStoreBase):
""" """
course = self.draft_store.get_course(SlashSeparatedCourseKey('edX', 'simple_with_draft', '2012_Fall')) course = self.draft_store.get_course(SlashSeparatedCourseKey('edX', 'simple_with_draft', '2012_Fall'))
root_dir = path(mkdtemp()) root_dir = path(mkdtemp())
try: self.addCleanup(shutil.rmtree, root_dir)
export_course_to_xml(self.draft_store, self.content_store, course.id, root_dir, 'test_export') export_course_to_xml(self.draft_store, self.content_store, course.id, root_dir, 'test_export')
assert_false(path(root_dir / 'test_export/static/images/course_image.jpg').isfile()) self.assertFalse(path(root_dir / 'test_export/static/images/course_image.jpg').isfile())
assert_false(path(root_dir / 'test_export/static/images_course_image.jpg').isfile()) self.assertFalse(path(root_dir / 'test_export/static/images_course_image.jpg').isfile())
finally:
shutil.rmtree(root_dir)
def _create_test_tree(self, name, user_id=None): def _create_test_tree(self, name, user_id=None):
""" """
...@@ -728,15 +722,13 @@ class TestMongoModuleStore(TestMongoModuleStoreBase): ...@@ -728,15 +722,13 @@ class TestMongoModuleStore(TestMongoModuleStoreBase):
self.assertEqual(unicode(component.link_to_location), unicode(problem_location)) self.assertEqual(unicode(component.link_to_location), unicode(problem_location))
root_dir = path(mkdtemp()) root_dir = path(mkdtemp())
self.addCleanup(shutil.rmtree, root_dir)
# export_course_to_xml should work. # export_course_to_xml should work.
try: export_course_to_xml(
export_course_to_xml( self.draft_store, self.content_store, interface_location.course_key,
self.draft_store, self.content_store, interface_location.course_key, root_dir, 'test_export'
root_dir, 'test_export' )
)
finally:
shutil.rmtree(root_dir)
def test_draft_modulestore_create_child_with_position(self): def test_draft_modulestore_create_child_with_position(self):
""" """
......
...@@ -175,14 +175,12 @@ class CommandsTestBase(ModuleStoreTestCase): ...@@ -175,14 +175,12 @@ class CommandsTestBase(ModuleStoreTestCase):
def test_export_course(self): def test_export_course(self):
tmp_dir = path(mkdtemp()) tmp_dir = path(mkdtemp())
self.addCleanup(shutil.rmtree, tmp_dir)
filename = tmp_dir / 'test.tar.gz' filename = tmp_dir / 'test.tar.gz'
try:
self.run_export_course(filename)
with tarfile.open(filename) as tar_file:
self.check_export_file(tar_file)
finally: self.run_export_course(filename)
shutil.rmtree(tmp_dir) with tarfile.open(filename) as tar_file:
self.check_export_file(tar_file)
def test_export_course_stdout(self): def test_export_course_stdout(self):
output = self.run_export_course('-') output = self.run_export_course('-')
......
...@@ -331,11 +331,7 @@ class EmailOptInListTest(ModuleStoreTestCase): ...@@ -331,11 +331,7 @@ class EmailOptInListTest(ModuleStoreTestCase):
# Create a temporary directory for the output # Create a temporary directory for the output
# Delete it when we're finished # Delete it when we're finished
temp_dir_path = tempfile.mkdtemp() temp_dir_path = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, temp_dir_path)
def _cleanup(): # pylint: disable=missing-docstring
shutil.rmtree(temp_dir_path)
self.addCleanup(_cleanup)
# Sanitize the arguments # Sanitize the arguments
if other_names is None: if other_names is None:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment