Commit 07da7657 by Ned Batchelder

Merge pull request #7354 from edx/ned/clean-test-turds

Clean up temp file management, mostly in tests
parents 593ee740 514d85c1
...@@ -59,8 +59,6 @@ jscover.log.* ...@@ -59,8 +59,6 @@ jscover.log.*
.tddium* .tddium*
common/test/data/test_unicode/static/ common/test/data/test_unicode/static/
django-pyfs django-pyfs
test_root/uploads/*.txt
test_root/uploads/badges/*.png
### Installation artifacts ### Installation artifacts
*.egg-info *.egg-info
......
...@@ -4,7 +4,6 @@ Modulestore configuration for test cases. ...@@ -4,7 +4,6 @@ Modulestore configuration for test cases.
""" """
import datetime import datetime
import pytz import pytz
from tempfile import mkdtemp
from uuid import uuid4 from uuid import uuid4
from mock import patch from mock import patch
...@@ -16,6 +15,8 @@ from django.test.utils import override_settings ...@@ -16,6 +15,8 @@ from django.test.utils import override_settings
from request_cache.middleware import RequestCache from request_cache.middleware import RequestCache
from courseware.field_overrides import OverrideFieldData # pylint: disable=import-error from courseware.field_overrides import OverrideFieldData # pylint: disable=import-error
from openedx.core.lib.tempdir import mkdtemp_clean
from xmodule.contentstore.django import _CONTENTSTORE from xmodule.contentstore.django import _CONTENTSTORE
from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.django import modulestore, clear_existing_modulestores from xmodule.modulestore.django import modulestore, clear_existing_modulestores
...@@ -184,13 +185,13 @@ TEST_DATA_MIXED_GRADED_MODULESTORE = mixed_store_config( ...@@ -184,13 +185,13 @@ TEST_DATA_MIXED_GRADED_MODULESTORE = mixed_store_config(
# All store requests now go through mixed # All store requests now go through mixed
# Use this modulestore if you specifically want to test mongo and not a mocked modulestore. # Use this modulestore if you specifically want to test mongo and not a mocked modulestore.
# This modulestore definition below will not load any xml courses. # This modulestore definition below will not load any xml courses.
TEST_DATA_MONGO_MODULESTORE = mixed_store_config(mkdtemp(), {}, include_xml=False) TEST_DATA_MONGO_MODULESTORE = mixed_store_config(mkdtemp_clean(), {}, include_xml=False)
# All store requests now go through mixed # All store requests now go through mixed
# Use this modulestore if you specifically want to test split-mongo and not a mocked modulestore. # Use this modulestore if you specifically want to test split-mongo and not a mocked modulestore.
# This modulestore definition below will not load any xml courses. # This modulestore definition below will not load any xml courses.
TEST_DATA_SPLIT_MODULESTORE = mixed_store_config( TEST_DATA_SPLIT_MODULESTORE = mixed_store_config(
mkdtemp(), mkdtemp_clean(),
{}, {},
include_xml=False, include_xml=False,
store_order=[StoreConstructors.split, StoreConstructors.draft] store_order=[StoreConstructors.split, StoreConstructors.draft]
...@@ -235,7 +236,7 @@ class ModuleStoreTestCase(TestCase): ...@@ -235,7 +236,7 @@ class ModuleStoreTestCase(TestCase):
your `setUp()` method. your `setUp()` method.
""" """
MODULESTORE = mixed_store_config(mkdtemp(), {}, include_xml=False) MODULESTORE = mixed_store_config(mkdtemp_clean(), {}, include_xml=False)
def setUp(self, **kwargs): def setUp(self, **kwargs):
""" """
......
...@@ -3,7 +3,8 @@ Tests for testing the modulestore settings migration code. ...@@ -3,7 +3,8 @@ Tests for testing the modulestore settings migration code.
""" """
import copy import copy
import ddt import ddt
from tempfile import mkdtemp
from openedx.core.lib.tempdir import mkdtemp_clean
from unittest import TestCase from unittest import TestCase
from xmodule.modulestore.modulestore_settings import ( from xmodule.modulestore.modulestore_settings import (
...@@ -37,7 +38,7 @@ class ModuleStoreSettingsMigration(TestCase): ...@@ -37,7 +38,7 @@ class ModuleStoreSettingsMigration(TestCase):
"collection": "modulestore", "collection": "modulestore",
"db": "edxapp", "db": "edxapp",
"default_class": "xmodule.hidden_module.HiddenDescriptor", "default_class": "xmodule.hidden_module.HiddenDescriptor",
"fs_root": mkdtemp(), "fs_root": mkdtemp_clean(),
"host": "localhost", "host": "localhost",
"password": "password", "password": "password",
"port": 27017, "port": 27017,
......
...@@ -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('-')
......
...@@ -40,10 +40,10 @@ class TestTaskFailure(Exception): ...@@ -40,10 +40,10 @@ class TestTaskFailure(Exception):
class TestInstructorTasks(InstructorTaskModuleTestCase): class TestInstructorTasks(InstructorTaskModuleTestCase):
def setUp(self): def setUp(self):
super(InstructorTaskModuleTestCase, self).setUp() super(TestInstructorTasks, self).setUp()
self.initialize_course() self.initialize_course()
self.instructor = self.create_instructor('instructor') self.instructor = self.create_instructor('instructor')
self.location = InstructorTaskModuleTestCase.problem_location(PROBLEM_URL_NAME) self.location = self.problem_location(PROBLEM_URL_NAME)
def _create_input_entry(self, student_ident=None, use_problem_url=True, course_id=None): def _create_input_entry(self, student_ident=None, use_problem_url=True, course_id=None):
"""Creates a InstructorTask entry for testing.""" """Creates a InstructorTask entry for testing."""
......
...@@ -21,10 +21,11 @@ sessions. Assumes structure: ...@@ -21,10 +21,11 @@ sessions. Assumes structure:
from .common import * from .common import *
import os import os
from path import path from path import path
from tempfile import mkdtemp
from uuid import uuid4 from uuid import uuid4
from warnings import filterwarnings, simplefilter from warnings import filterwarnings, simplefilter
from openedx.core.lib.tempdir import mkdtemp_clean
# Silence noisy logs to make troubleshooting easier when tests fail. # Silence noisy logs to make troubleshooting easier when tests fail.
import logging import logging
LOG_OVERRIDES = [ LOG_OVERRIDES = [
...@@ -151,7 +152,7 @@ update_module_store_settings( ...@@ -151,7 +152,7 @@ update_module_store_settings(
'fs_root': TEST_ROOT / "data", 'fs_root': TEST_ROOT / "data",
}, },
xml_store_options={ xml_store_options={
'data_dir': mkdtemp(dir=TEST_ROOT), # never inadvertently load all the XML courses 'data_dir': mkdtemp_clean(dir=TEST_ROOT), # never inadvertently load all the XML courses
}, },
doc_store_settings={ doc_store_settings={
'host': MONGO_HOST, 'host': MONGO_HOST,
......
...@@ -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:
......
*.csv *.csv
*.jpg *.jpg
*.png *.png
*.txt
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