Permalink
Browse files

Prevented leftover files and dirs in admin_scripts tests

  • Loading branch information...
claudep committed Oct 29, 2012
1 parent b774c59 commit d30516e163c1870b2467c63f97c8263f9da40226
Showing with 23 additions and 27 deletions.
  1. +23 −27 tests/regressiontests/admin_scripts/tests.py
@@ -138,6 +138,12 @@ def run_django_admin(self, args, settings_file=None):
return self.run_test(os.path.join(bin_dir, 'django-admin.py'), args, settings_file)
def run_manage(self, args, settings_file=None):
+ def safe_remove(path):
+ try:
+ os.remove(path)
+ except OSError:
+ pass
+
conf_dir = os.path.dirname(conf.__file__)
template_manage_py = os.path.join(conf_dir, 'project_template', 'manage.py')
@@ -150,13 +156,9 @@ def run_manage(self, args, settings_file=None):
"{{ project_name }}", "regressiontests")
with open(test_manage_py, 'w') as fp:
fp.write(manage_py_contents)
+ self.addCleanup(safe_remove, test_manage_py)
- stdout, stderr = self.run_test('./manage.py', args, settings_file)
-
- # Cleanup - remove the generated manage.py script
- os.remove(test_manage_py)
-
- return stdout, stderr
+ return self.run_test('./manage.py', args, settings_file)
def assertNoOutput(self, stream):
"Utility assertion: assert that the given stream is empty"
@@ -1410,9 +1412,9 @@ def test_simple_project(self):
"Make sure the startproject management command creates a project"
args = ['startproject', 'testproject']
testproject_dir = os.path.join(test_dir, 'testproject')
+ self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
- self.addCleanup(shutil.rmtree, testproject_dir)
self.assertNoOutput(err)
self.assertTrue(os.path.isdir(testproject_dir))
@@ -1423,16 +1425,11 @@ def test_simple_project(self):
def test_invalid_project_name(self):
"Make sure the startproject management command validates a project name"
-
- def cleanup(p):
- if os.path.exists(p):
- shutil.rmtree(p)
-
args = ['startproject', '7testproject']
testproject_dir = os.path.join(test_dir, '7testproject')
+ self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
- self.addCleanup(cleanup, testproject_dir)
self.assertOutput(err, "Error: '7testproject' is not a valid project name. Please make sure the name begins with a letter or underscore.")
self.assertFalse(os.path.exists(testproject_dir))
@@ -1441,9 +1438,9 @@ def test_simple_project_different_directory(self):
args = ['startproject', 'testproject', 'othertestproject']
testproject_dir = os.path.join(test_dir, 'othertestproject')
os.mkdir(testproject_dir)
+ self.addCleanup(shutil.rmtree, testproject_dir)
out, err = self.run_django_admin(args)
- self.addCleanup(shutil.rmtree, testproject_dir)
self.assertNoOutput(err)
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'manage.py')))
@@ -1457,9 +1454,9 @@ def test_custom_project_template(self):
template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template')
args = ['startproject', '--template', template_path, 'customtestproject']
testproject_dir = os.path.join(test_dir, 'customtestproject')
+ self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
- self.addCleanup(shutil.rmtree, testproject_dir)
self.assertNoOutput(err)
self.assertTrue(os.path.isdir(testproject_dir))
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'additional_dir')))
@@ -1469,9 +1466,9 @@ def test_template_dir_with_trailing_slash(self):
template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template' + os.sep)
args = ['startproject', '--template', template_path, 'customtestproject']
testproject_dir = os.path.join(test_dir, 'customtestproject')
+ self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
- self.addCleanup(shutil.rmtree, testproject_dir)
self.assertNoOutput(err)
self.assertTrue(os.path.isdir(testproject_dir))
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'additional_dir')))
@@ -1481,9 +1478,9 @@ def test_custom_project_template_from_tarball_by_path(self):
template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template.tgz')
args = ['startproject', '--template', template_path, 'tarballtestproject']
testproject_dir = os.path.join(test_dir, 'tarballtestproject')
+ self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
- self.addCleanup(shutil.rmtree, testproject_dir)
self.assertNoOutput(err)
self.assertTrue(os.path.isdir(testproject_dir))
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'run.py')))
@@ -1494,9 +1491,9 @@ def test_custom_project_template_from_tarball_to_alternative_location(self):
args = ['startproject', '--template', template_path, 'tarballtestproject', 'altlocation']
testproject_dir = os.path.join(test_dir, 'altlocation')
os.mkdir(testproject_dir)
+ self.addCleanup(shutil.rmtree, testproject_dir)
out, err = self.run_django_admin(args)
- self.addCleanup(shutil.rmtree, testproject_dir)
self.assertNoOutput(err)
self.assertTrue(os.path.isdir(testproject_dir))
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'run.py')))
@@ -1507,9 +1504,9 @@ def test_custom_project_template_from_tarball_by_url(self):
args = ['startproject', '--template', template_url, 'urltestproject']
testproject_dir = os.path.join(test_dir, 'urltestproject')
+ self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
- self.addCleanup(shutil.rmtree, testproject_dir)
self.assertNoOutput(err)
self.assertTrue(os.path.isdir(testproject_dir))
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'run.py')))
@@ -1520,9 +1517,9 @@ def test_project_template_tarball_url(self):
args = ['startproject', '--template', template_url, 'urltestproject']
testproject_dir = os.path.join(test_dir, 'urltestproject')
+ self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
- self.addCleanup(shutil.rmtree, testproject_dir)
self.assertNoOutput(err)
self.assertTrue(os.path.isdir(testproject_dir))
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'run.py')))
@@ -1532,9 +1529,9 @@ def test_file_without_extension(self):
template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template')
args = ['startproject', '--template', template_path, 'customtestproject', '-e', 'txt', '-n', 'Procfile']
testproject_dir = os.path.join(test_dir, 'customtestproject')
+ self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
- self.addCleanup(shutil.rmtree, testproject_dir)
self.assertNoOutput(err)
self.assertTrue(os.path.isdir(testproject_dir))
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'additional_dir')))
@@ -1551,8 +1548,8 @@ def test_custom_project_template_context_variables(self):
args = ['startproject', '--template', template_path, 'another_project', 'project_dir']
testproject_dir = os.path.join(test_dir, 'project_dir')
os.mkdir(testproject_dir)
- out, err = self.run_django_admin(args)
self.addCleanup(shutil.rmtree, testproject_dir)
+ out, err = self.run_django_admin(args)
self.assertNoOutput(err)
test_manage_py = os.path.join(testproject_dir, 'manage.py')
with open(test_manage_py, 'r') as fp:
@@ -1564,19 +1561,18 @@ def test_no_escaping_of_project_variables(self):
"Make sure template context variables are not html escaped"
# We're using a custom command so we need the alternate settings
self.write_settings('alternate_settings.py')
+ self.addCleanup(self.remove_settings, 'alternate_settings.py')
template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template')
args = ['custom_startproject', '--template', template_path, 'another_project', 'project_dir', '--extra', '<&>', '--settings=alternate_settings']
testproject_dir = os.path.join(test_dir, 'project_dir')
os.mkdir(testproject_dir)
- out, err = self.run_manage(args)
self.addCleanup(shutil.rmtree, testproject_dir)
+ out, err = self.run_manage(args)
self.assertNoOutput(err)
test_manage_py = os.path.join(testproject_dir, 'additional_dir', 'extra.py')
with open(test_manage_py, 'r') as fp:
content = fp.read()
self.assertIn("<&>", content)
- # tidy up alternate settings
- self.remove_settings('alternate_settings.py')
def test_custom_project_destination_missing(self):
"""
@@ -1596,9 +1592,9 @@ def test_custom_project_template_with_non_ascii_templates(self):
template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template')
args = ['startproject', '--template', template_path, '--extension=txt', 'customtestproject']
testproject_dir = os.path.join(test_dir, 'customtestproject')
+ self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
- self.addCleanup(shutil.rmtree, testproject_dir)
self.assertNoOutput(err)
self.assertTrue(os.path.isdir(testproject_dir))
path = os.path.join(testproject_dir, 'ticket-18091-non-ascii-template.txt')
@@ -1612,8 +1608,8 @@ class DiffSettings(AdminScriptTestCase):
def test_basic(self):
"Runs without error and emits settings diff."
self.write_settings('settings_to_diff.py', sdict={'FOO': '"bar"'})
+ self.addCleanup(self.remove_settings, 'settings_to_diff.py')
args = ['diffsettings', '--settings=settings_to_diff']
out, err = self.run_manage(args)
- self.remove_settings('settings_to_diff.py')
self.assertNoOutput(err)
self.assertOutput(out, "FOO = 'bar' ###")

0 comments on commit d30516e

Please sign in to comment.