Make sure temp directories are properly cleaned up so running tests doesn't leave them behind.
This commit is contained in:
17
common/lib/tempdir.py
Normal file
17
common/lib/tempdir.py
Normal file
@@ -0,0 +1,17 @@
|
||||
"""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
|
||||
from nose.tools import assert_equals, assert_true
|
||||
from path import path
|
||||
from tempfile import mkdtemp
|
||||
from shutil import copytree
|
||||
import shutil
|
||||
|
||||
from xmodule.modulestore.xml import XMLModuleStore
|
||||
|
||||
@@ -46,11 +46,11 @@ class RoundTripTestCase(unittest.TestCase):
|
||||
Thus we make sure that export and import work properly.
|
||||
'''
|
||||
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)
|
||||
|
||||
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"
|
||||
initial_import = XMLModuleStore(root_dir, course_dirs=[course_dir])
|
||||
@@ -108,6 +108,8 @@ class RoundTripTestCase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.maxDiff = None
|
||||
self.temp_dir = mkdtemp()
|
||||
self.addCleanup(shutil.rmtree, self.temp_dir)
|
||||
|
||||
def test_toy_roundtrip(self):
|
||||
self.check_export_roundtrip(DATA_DIR, "toy")
|
||||
|
||||
Reference in New Issue
Block a user