diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index 616082346b..397856912e 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -33,23 +33,38 @@ from django.utils._os import npath, upath from django.utils.encoding import force_text from django.utils.six import PY3, StringIO -test_dir = os.path.realpath(os.path.join(tempfile.gettempdir(), 'test_project')) -if not os.path.exists(test_dir): - os.mkdir(test_dir) - open(os.path.join(test_dir, '__init__.py'), 'w').close() - custom_templates_dir = os.path.join(os.path.dirname(upath(__file__)), 'custom_templates') + SYSTEM_CHECK_MSG = 'System check identified no issues' class AdminScriptTestCase(unittest.TestCase): + + @classmethod + def setUpClass(cls): + super(AdminScriptTestCase, cls).setUpClass() + cls.test_dir = os.path.realpath(os.path.join( + tempfile.gettempdir(), + cls.__name__, + 'test_project', + )) + if not os.path.exists(cls.test_dir): + os.makedirs(cls.test_dir) + with open(os.path.join(cls.test_dir, '__init__.py'), 'w'): + pass + + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.test_dir) + super(AdminScriptTestCase, cls).tearDownClass() + def write_settings(self, filename, apps=None, is_dir=False, sdict=None, extra=None): if is_dir: - settings_dir = os.path.join(test_dir, filename) + settings_dir = os.path.join(self.test_dir, filename) os.mkdir(settings_dir) settings_file_path = os.path.join(settings_dir, '__init__.py') else: - settings_file_path = os.path.join(test_dir, filename) + settings_file_path = os.path.join(self.test_dir, filename) with open(settings_file_path, 'w') as settings_file: settings_file.write('# -*- coding: utf-8 -*\n') @@ -78,7 +93,7 @@ class AdminScriptTestCase(unittest.TestCase): settings_file.write("%s = %s\n" % (k, v)) def remove_settings(self, filename, is_dir=False): - full_name = os.path.join(test_dir, filename) + full_name = os.path.join(self.test_dir, filename) if is_dir: shutil.rmtree(full_name) else: @@ -96,7 +111,7 @@ class AdminScriptTestCase(unittest.TestCase): except OSError: pass # Also remove a __pycache__ directory, if it exists - cache_name = os.path.join(test_dir, '__pycache__') + cache_name = os.path.join(self.test_dir, '__pycache__') if os.path.isdir(cache_name): shutil.rmtree(cache_name) @@ -115,7 +130,7 @@ class AdminScriptTestCase(unittest.TestCase): return paths def run_test(self, script, args, settings_file=None, apps=None): - base_dir = os.path.dirname(test_dir) + base_dir = os.path.dirname(self.test_dir) # The base dir for Django's tests is one level up. tests_dir = os.path.dirname(os.path.dirname(upath(__file__))) # The base dir for Django is one level above the test dir. We don't use @@ -145,7 +160,7 @@ class AdminScriptTestCase(unittest.TestCase): test_environ[str('PYTHONWARNINGS')] = str('') # Move to the test directory and run - os.chdir(test_dir) + os.chdir(self.test_dir) out, err = subprocess.Popen([sys.executable, script] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=test_environ, universal_newlines=True).communicate() @@ -168,7 +183,7 @@ class AdminScriptTestCase(unittest.TestCase): 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') + test_manage_py = os.path.join(self.test_dir, 'manage.py') shutil.copyfile(template_manage_py, test_manage_py) with open(test_manage_py, 'r') as fp: @@ -590,7 +605,7 @@ class DjangoAdminSettingsDirectory(AdminScriptTestCase): def test_setup_environ(self): "directory: startapp creates the correct directory" args = ['startapp', 'settings_test'] - app_path = os.path.join(test_dir, 'settings_test') + app_path = os.path.join(self.test_dir, 'settings_test') out, err = self.run_django_admin(args, 'test_project.settings') self.addCleanup(shutil.rmtree, app_path) self.assertNoOutput(err) @@ -611,7 +626,7 @@ class DjangoAdminSettingsDirectory(AdminScriptTestCase): "directory: startapp creates the correct directory with a custom template" template_path = os.path.join(custom_templates_dir, 'app_template') args = ['startapp', '--template', template_path, 'custom_settings_test'] - app_path = os.path.join(test_dir, 'custom_settings_test') + app_path = os.path.join(self.test_dir, 'custom_settings_test') out, err = self.run_django_admin(args, 'test_project.settings') self.addCleanup(shutil.rmtree, app_path) self.assertNoOutput(err) @@ -1047,7 +1062,7 @@ class ManageSettingsWithSettingsErrors(AdminScriptTestCase): self.remove_settings('settings.py') def write_settings_with_import_error(self, filename): - settings_file_path = os.path.join(test_dir, filename) + settings_file_path = os.path.join(self.test_dir, filename) with open(settings_file_path, 'w') as settings_file: settings_file.write('# Settings file automatically generated by admin_scripts test case\n') settings_file.write('# The next line will cause an import error:\nimport foo42bar\n') @@ -1802,7 +1817,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): 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') + testproject_dir = os.path.join(self.test_dir, 'testproject') self.addCleanup(shutil.rmtree, testproject_dir, True) out, err = self.run_django_admin(args) @@ -1818,7 +1833,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): "Make sure the startproject management command validates a project name" for bad_name in ('7testproject', '../testproject'): args = ['startproject', bad_name] - testproject_dir = os.path.join(test_dir, bad_name) + testproject_dir = os.path.join(self.test_dir, bad_name) self.addCleanup(shutil.rmtree, testproject_dir, True) out, err = self.run_django_admin(args) @@ -1829,7 +1844,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): def test_simple_project_different_directory(self): "Make sure the startproject management command creates a project in a specific directory" args = ['startproject', 'testproject', 'othertestproject'] - testproject_dir = os.path.join(test_dir, 'othertestproject') + testproject_dir = os.path.join(self.test_dir, 'othertestproject') os.mkdir(testproject_dir) self.addCleanup(shutil.rmtree, testproject_dir) @@ -1846,7 +1861,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): "Make sure the startproject management command is able to use a different project template" template_path = os.path.join(custom_templates_dir, 'project_template') args = ['startproject', '--template', template_path, 'customtestproject'] - testproject_dir = os.path.join(test_dir, 'customtestproject') + testproject_dir = os.path.join(self.test_dir, 'customtestproject') self.addCleanup(shutil.rmtree, testproject_dir, True) out, err = self.run_django_admin(args) @@ -1858,7 +1873,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): "Ticket 17475: Template dir passed has a trailing path separator" template_path = os.path.join(custom_templates_dir, 'project_template' + os.sep) args = ['startproject', '--template', template_path, 'customtestproject'] - testproject_dir = os.path.join(test_dir, 'customtestproject') + testproject_dir = os.path.join(self.test_dir, 'customtestproject') self.addCleanup(shutil.rmtree, testproject_dir, True) out, err = self.run_django_admin(args) @@ -1870,7 +1885,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): "Make sure the startproject management command is able to use a different project template from a tarball" template_path = os.path.join(custom_templates_dir, 'project_template.tgz') args = ['startproject', '--template', template_path, 'tarballtestproject'] - testproject_dir = os.path.join(test_dir, 'tarballtestproject') + testproject_dir = os.path.join(self.test_dir, 'tarballtestproject') self.addCleanup(shutil.rmtree, testproject_dir, True) out, err = self.run_django_admin(args) @@ -1882,7 +1897,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): "Startproject can use a project template from a tarball and create it in a specified location" template_path = os.path.join(custom_templates_dir, 'project_template.tgz') args = ['startproject', '--template', template_path, 'tarballtestproject', 'altlocation'] - testproject_dir = os.path.join(test_dir, 'altlocation') + testproject_dir = os.path.join(self.test_dir, 'altlocation') os.mkdir(testproject_dir) self.addCleanup(shutil.rmtree, testproject_dir) @@ -1896,7 +1911,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): template_url = '%s/custom_templates/project_template.tgz' % self.live_server_url args = ['startproject', '--template', template_url, 'urltestproject'] - testproject_dir = os.path.join(test_dir, 'urltestproject') + testproject_dir = os.path.join(self.test_dir, 'urltestproject') self.addCleanup(shutil.rmtree, testproject_dir, True) out, err = self.run_django_admin(args) @@ -1909,7 +1924,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): template_url = '%s/custom_templates/project_template.tgz/' % self.live_server_url args = ['startproject', '--template', template_url, 'urltestproject'] - testproject_dir = os.path.join(test_dir, 'urltestproject') + testproject_dir = os.path.join(self.test_dir, 'urltestproject') self.addCleanup(shutil.rmtree, testproject_dir, True) out, err = self.run_django_admin(args) @@ -1921,7 +1936,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): "Make sure the startproject management command is able to render custom files" template_path = os.path.join(custom_templates_dir, 'project_template') args = ['startproject', '--template', template_path, 'customtestproject', '-e', 'txt', '-n', 'Procfile'] - testproject_dir = os.path.join(test_dir, 'customtestproject') + testproject_dir = os.path.join(self.test_dir, 'customtestproject') self.addCleanup(shutil.rmtree, testproject_dir, True) out, err = self.run_django_admin(args) @@ -1939,7 +1954,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): "Make sure template context variables are rendered with proper values" template_path = os.path.join(custom_templates_dir, 'project_template') args = ['startproject', '--template', template_path, 'another_project', 'project_dir'] - testproject_dir = os.path.join(test_dir, 'project_dir') + testproject_dir = os.path.join(self.test_dir, 'project_dir') os.mkdir(testproject_dir) self.addCleanup(shutil.rmtree, testproject_dir) out, err = self.run_django_admin(args) @@ -1957,7 +1972,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): self.addCleanup(self.remove_settings, 'alternate_settings.py') template_path = os.path.join(custom_templates_dir, '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') + testproject_dir = os.path.join(self.test_dir, 'project_dir') os.mkdir(testproject_dir) self.addCleanup(shutil.rmtree, testproject_dir) out, err = self.run_manage(args) @@ -1974,7 +1989,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): """ template_path = os.path.join(custom_templates_dir, 'project_template') args = ['startproject', '--template', template_path, 'yet_another_project', 'project_dir2'] - testproject_dir = os.path.join(test_dir, 'project_dir2') + testproject_dir = os.path.join(self.test_dir, 'project_dir2') out, err = self.run_django_admin(args) self.assertNoOutput(out) self.assertOutput(err, "Destination directory '%s' does not exist, please create it first." % testproject_dir) @@ -1984,7 +1999,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): "Ticket 18091: Make sure the startproject management command is able to render templates with non-ASCII content" template_path = os.path.join(custom_templates_dir, 'project_template') args = ['startproject', '--template', template_path, '--extension=txt', 'customtestproject'] - testproject_dir = os.path.join(test_dir, 'customtestproject') + testproject_dir = os.path.join(self.test_dir, 'customtestproject') self.addCleanup(shutil.rmtree, testproject_dir, True) out, err = self.run_django_admin(args)