diff options
Diffstat (limited to 'tests/regressiontests/admin_scripts/tests.py')
| -rw-r--r-- | tests/regressiontests/admin_scripts/tests.py | 65 |
1 files changed, 33 insertions, 32 deletions
diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py index 3bb8bb0b50..d0ca9d26df 100644 --- a/tests/regressiontests/admin_scripts/tests.py +++ b/tests/regressiontests/admin_scripts/tests.py @@ -19,13 +19,15 @@ from django.conf import settings from django.db import connection from django.test.simple import DjangoTestSuiteRunner from django.utils import unittest +from django.utils.encoding import force_str, force_text +from django.utils._os import upath from django.test import LiveServerTestCase -test_dir = os.path.dirname(os.path.dirname(__file__)) +test_dir = os.path.dirname(os.path.dirname(upath(__file__))) class AdminScriptTestCase(unittest.TestCase): def write_settings(self, filename, apps=None, is_dir=False, sdict=None): - test_dir = os.path.dirname(os.path.dirname(__file__)) + test_dir = os.path.dirname(os.path.dirname(upath(__file__))) if is_dir: settings_dir = os.path.join(test_dir, filename) os.mkdir(settings_dir) @@ -94,6 +96,7 @@ class AdminScriptTestCase(unittest.TestCase): return paths def run_test(self, script, args, settings_file=None, apps=None): + test_dir = os.path.dirname(os.path.dirname(__file__)) project_dir = os.path.dirname(test_dir) base_dir = os.path.dirname(project_dir) ext_backend_base_dirs = self._ext_backend_paths() @@ -134,11 +137,17 @@ class AdminScriptTestCase(unittest.TestCase): return out, err def run_django_admin(self, args, settings_file=None): - bin_dir = os.path.abspath(os.path.dirname(bin.__file__)) + bin_dir = os.path.abspath(os.path.dirname(upath(bin.__file__))) return self.run_test(os.path.join(bin_dir, 'django-admin.py'), args, settings_file) def run_manage(self, args, settings_file=None): - conf_dir = os.path.dirname(conf.__file__) + def safe_remove(path): + try: + os.remove(path) + except OSError: + pass + + conf_dir = os.path.dirname(upath(conf.__file__)) template_manage_py = os.path.join(conf_dir, 'project_template', 'manage.py') test_manage_py = os.path.join(test_dir, 'manage.py') @@ -150,13 +159,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" @@ -164,10 +169,12 @@ class AdminScriptTestCase(unittest.TestCase): def assertOutput(self, stream, msg): "Utility assertion: assert that the given message exists in the output" + stream = force_text(stream) self.assertTrue(msg in stream, "'%s' does not match actual output text '%s'" % (msg, stream)) def assertNotInOutput(self, stream, msg): "Utility assertion: assert that the given message doesn't exist in the output" + stream = force_text(stream) self.assertFalse(msg in stream, "'%s' matches actual output text '%s'" % (msg, stream)) ########################################################################## @@ -1410,9 +1417,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 +1430,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 +1443,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 +1459,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 +1471,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 +1483,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 +1496,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 +1509,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 +1522,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 +1534,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,12 +1553,12 @@ 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: - content = fp.read() + content = force_text(fp.read()) self.assertIn("project_name = 'another_project'", content) self.assertIn("project_directory = '%s'" % testproject_dir, content) @@ -1564,19 +1566,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 +1597,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 +1613,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' ###") |
