From 04717ee9b1d1f8ccc0f37107f7008662726689c2 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Mon, 29 Oct 2012 19:08:07 +0100 Subject: [PATCH] [1.5.x] Prevented leftover files and dirs in admin_scripts tests Backport of d30516e1 from master. --- tests/regressiontests/admin_scripts/tests.py | 50 +++++++++----------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py index 3bb8bb0b50..a26d7a6eaa 100644 --- a/tests/regressiontests/admin_scripts/tests.py +++ b/tests/regressiontests/admin_scripts/tests.py @@ -138,6 +138,12 @@ class AdminScriptTestCase(unittest.TestCase): 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 @@ class AdminScriptTestCase(unittest.TestCase): "{{ 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 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): "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 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): 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 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): 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 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): 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 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): 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 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): 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 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): 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 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): 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 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): 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 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): 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 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): 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 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): "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 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): 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' ###")