Commit 3494d584 by David Ormsbee

Merge pull request #1532 from MITx/feature/ned/clean_up_tests

Make sure temp directories are properly cleaned up so running tests does...
parents ba7959fc c3b571bf
...@@ -5,7 +5,7 @@ from django.test.utils import override_settings ...@@ -5,7 +5,7 @@ from django.test.utils import override_settings
from django.conf import settings from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from path import path from path import path
from tempfile import mkdtemp from tempdir import mkdtemp_clean
import json import json
from fs.osfs import OSFS from fs.osfs import OSFS
import copy import copy
...@@ -194,7 +194,7 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): ...@@ -194,7 +194,7 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase):
import_from_xml(ms, 'common/test/data/', ['full']) import_from_xml(ms, 'common/test/data/', ['full'])
location = CourseDescriptor.id_to_location('edX/full/6.002_Spring_2012') location = CourseDescriptor.id_to_location('edX/full/6.002_Spring_2012')
root_dir = path(mkdtemp()) root_dir = path(mkdtemp_clean())
print 'Exporting to tempdir = {0}'.format(root_dir) print 'Exporting to tempdir = {0}'.format(root_dir)
......
...@@ -4,7 +4,6 @@ from django.test.client import Client ...@@ -4,7 +4,6 @@ from django.test.client import Client
from django.conf import settings from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from path import path from path import path
from tempfile import mkdtemp
import json import json
from fs.osfs import OSFS from fs.osfs import OSFS
import copy import copy
......
...@@ -20,7 +20,6 @@ Longer TODO: ...@@ -20,7 +20,6 @@ Longer TODO:
""" """
import sys import sys
import tempfile
import os.path import os.path
import os import os
import lms.envs.common import lms.envs.common
...@@ -59,7 +58,8 @@ sys.path.append(COMMON_ROOT / 'lib') ...@@ -59,7 +58,8 @@ sys.path.append(COMMON_ROOT / 'lib')
############################# WEB CONFIGURATION ############################# ############################# WEB CONFIGURATION #############################
# This is where we stick our compiled template files. # This is where we stick our compiled template files.
MAKO_MODULE_DIR = tempfile.mkdtemp('mako') from tempdir import mkdtemp_clean
MAKO_MODULE_DIR = mkdtemp_clean('mako')
MAKO_TEMPLATES = {} MAKO_TEMPLATES = {}
MAKO_TEMPLATES['main'] = [ MAKO_TEMPLATES['main'] = [
PROJECT_ROOT / 'templates', PROJECT_ROOT / 'templates',
......
...@@ -9,6 +9,7 @@ from django.template.loaders.app_directories import Loader as AppDirectoriesLoad ...@@ -9,6 +9,7 @@ from django.template.loaders.app_directories import Loader as AppDirectoriesLoad
from mitxmako.template import Template from mitxmako.template import Template
import mitxmako.middleware import mitxmako.middleware
import tempdir
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -30,7 +31,7 @@ class MakoLoader(object): ...@@ -30,7 +31,7 @@ class MakoLoader(object):
if module_directory is None: if module_directory is None:
log.warning("For more caching of mako templates, set the MAKO_MODULE_DIR in settings!") log.warning("For more caching of mako templates, set the MAKO_MODULE_DIR in settings!")
module_directory = tempfile.mkdtemp() module_directory = tempdir.mkdtemp_clean()
self.module_directory = module_directory self.module_directory = module_directory
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
# limitations under the License. # limitations under the License.
from mako.lookup import TemplateLookup from mako.lookup import TemplateLookup
import tempfile import tempdir
from django.template import RequestContext from django.template import RequestContext
from django.conf import settings from django.conf import settings
...@@ -29,7 +29,7 @@ class MakoMiddleware(object): ...@@ -29,7 +29,7 @@ class MakoMiddleware(object):
module_directory = getattr(settings, 'MAKO_MODULE_DIR', None) module_directory = getattr(settings, 'MAKO_MODULE_DIR', None)
if module_directory is None: if module_directory is None:
module_directory = tempfile.mkdtemp() module_directory = tempdir.mkdtemp_clean()
for location in template_locations: for location in template_locations:
lookup[location] = TemplateLookup(directories=template_locations[location], lookup[location] = TemplateLookup(directories=template_locations[location],
......
...@@ -7,6 +7,7 @@ import logging ...@@ -7,6 +7,7 @@ import logging
import os import os
from tempfile import mkdtemp from tempfile import mkdtemp
import cStringIO import cStringIO
import shutil
import sys import sys
from django.test import TestCase from django.test import TestCase
...@@ -143,23 +144,18 @@ class PearsonTestCase(TestCase): ...@@ -143,23 +144,18 @@ class PearsonTestCase(TestCase):
''' '''
Base class for tests running Pearson-related commands Base class for tests running Pearson-related commands
''' '''
import_dir = mkdtemp(prefix="import")
export_dir = mkdtemp(prefix="export")
def assertErrorContains(self, error_message, expected): def assertErrorContains(self, error_message, expected):
self.assertTrue(error_message.find(expected) >= 0, 'error message "{}" did not contain "{}"'.format(error_message, expected)) self.assertTrue(error_message.find(expected) >= 0, 'error message "{}" did not contain "{}"'.format(error_message, expected))
def tearDown(self): def setUp(self):
def delete_temp_dir(dirname): self.import_dir = mkdtemp(prefix="import")
if os.path.exists(dirname): self.addCleanup(shutil.rmtree, self.import_dir)
for filename in os.listdir(dirname): self.export_dir = mkdtemp(prefix="export")
os.remove(os.path.join(dirname, filename)) self.addCleanup(shutil.rmtree, self.export_dir)
os.rmdir(dirname)
# clean up after any test data was dumped to temp directory
delete_temp_dir(self.import_dir)
delete_temp_dir(self.export_dir)
def tearDown(self):
pass
# and clean up the database: # and clean up the database:
# TestCenterUser.objects.all().delete() # TestCenterUser.objects.all().delete()
# TestCenterRegistration.objects.all().delete() # TestCenterRegistration.objects.all().delete()
......
"""Make temporary directories nicely."""
import atexit
import os.path
import shutil
import tempfile
def mkdtemp_clean(suffix="", prefix="tmp", dir=None):
"""Just like mkdtemp, but the directory will be deleted when the process ends."""
the_dir = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=dir)
atexit.register(cleanup_tempdir, the_dir)
return the_dir
def cleanup_tempdir(the_dir):
"""Called on process exit to remove a temp directory."""
if os.path.exists(the_dir):
shutil.rmtree(the_dir)
...@@ -4,7 +4,7 @@ from fs.osfs import OSFS ...@@ -4,7 +4,7 @@ from fs.osfs import OSFS
from nose.tools import assert_equals, assert_true from nose.tools import assert_equals, assert_true
from path import path from path import path
from tempfile import mkdtemp from tempfile import mkdtemp
from shutil import copytree import shutil
from xmodule.modulestore.xml import XMLModuleStore from xmodule.modulestore.xml import XMLModuleStore
...@@ -46,11 +46,11 @@ class RoundTripTestCase(unittest.TestCase): ...@@ -46,11 +46,11 @@ class RoundTripTestCase(unittest.TestCase):
Thus we make sure that export and import work properly. Thus we make sure that export and import work properly.
''' '''
def check_export_roundtrip(self, data_dir, course_dir): def check_export_roundtrip(self, data_dir, course_dir):
root_dir = path(mkdtemp()) root_dir = path(self.temp_dir)
print "Copying test course to temp dir {0}".format(root_dir) print "Copying test course to temp dir {0}".format(root_dir)
data_dir = path(data_dir) data_dir = path(data_dir)
copytree(data_dir / course_dir, root_dir / course_dir) shutil.copytree(data_dir / course_dir, root_dir / course_dir)
print "Starting import" print "Starting import"
initial_import = XMLModuleStore(root_dir, course_dirs=[course_dir]) initial_import = XMLModuleStore(root_dir, course_dirs=[course_dir])
...@@ -108,6 +108,8 @@ class RoundTripTestCase(unittest.TestCase): ...@@ -108,6 +108,8 @@ class RoundTripTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.maxDiff = None self.maxDiff = None
self.temp_dir = mkdtemp()
self.addCleanup(shutil.rmtree, self.temp_dir)
def test_toy_roundtrip(self): def test_toy_roundtrip(self):
self.check_export_roundtrip(DATA_DIR, "toy") self.check_export_roundtrip(DATA_DIR, "toy")
......
...@@ -20,7 +20,6 @@ Longer TODO: ...@@ -20,7 +20,6 @@ Longer TODO:
""" """
import sys import sys
import os import os
import tempfile
from xmodule.static_content import write_module_styles, write_module_js from xmodule.static_content import write_module_styles, write_module_js
from path import path from path import path
...@@ -133,7 +132,8 @@ OPENID_PROVIDER_TRUSTED_ROOTS = ['cs50.net', '*.cs50.net'] ...@@ -133,7 +132,8 @@ OPENID_PROVIDER_TRUSTED_ROOTS = ['cs50.net', '*.cs50.net']
################################## MITXWEB ##################################### ################################## MITXWEB #####################################
# This is where we stick our compiled template files. Most of the app uses Mako # This is where we stick our compiled template files. Most of the app uses Mako
# templates # templates
MAKO_MODULE_DIR = tempfile.mkdtemp('mako') from tempdir import mkdtemp_clean
MAKO_MODULE_DIR = mkdtemp_clean('mako')
MAKO_TEMPLATES = {} MAKO_TEMPLATES = {}
MAKO_TEMPLATES['main'] = [PROJECT_ROOT / 'templates', MAKO_TEMPLATES['main'] = [PROJECT_ROOT / 'templates',
COMMON_ROOT / 'templates', COMMON_ROOT / 'templates',
......
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