summaryrefslogtreecommitdiff
path: root/tests/admin_scripts
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2019-02-05 09:11:54 -0800
committerTim Graham <timograham@gmail.com>2019-02-05 12:11:54 -0500
commit487d904bf253de2f5633f181a168f94086bcd6cb (patch)
treeb5b8bbe9e05ac5c4f978c9ede6061737f210322d /tests/admin_scripts
parent099c36d546cda842b44a93067d45e146e4ae77e5 (diff)
downloaddjango-487d904bf253de2f5633f181a168f94086bcd6cb.tar.gz
Simplified temporary directory handling in AdminScriptTestCase.
Use tempfile.TemporaryDirectory() in AdminScriptTestCase.setUp() to create and destroy a temporary directory for each test. It removes the need for individual tests to delete files. For test classes that don't use the temporary directory, inherit from SimpleTestCase.
Diffstat (limited to 'tests/admin_scripts')
-rw-r--r--tests/admin_scripts/tests.py145
1 files changed, 24 insertions, 121 deletions
diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py
index 79ff14ca4c..ecfa267d07 100644
--- a/tests/admin_scripts/tests.py
+++ b/tests/admin_scripts/tests.py
@@ -39,24 +39,14 @@ SYSTEM_CHECK_MSG = 'System check identified no issues'
class AdminScriptTestCase(SimpleTestCase):
-
- @classmethod
- def setUpClass(cls):
- super().setUpClass()
- cls.test_dir = os.path.realpath(os.path.join(
- tempfile.gettempdir(),
- cls.__name__,
- 'test_project',
- ))
- os.makedirs(cls.test_dir)
- with open(os.path.join(cls.test_dir, '__init__.py'), 'w'):
+ def setUp(self):
+ tmpdir = tempfile.TemporaryDirectory()
+ self.addCleanup(tmpdir.cleanup)
+ self.test_dir = os.path.join(tmpdir.name, 'test_project')
+ os.mkdir(self.test_dir)
+ with open(os.path.join(self.test_dir, '__init__.py'), 'w'):
pass
- @classmethod
- def tearDownClass(cls):
- shutil.rmtree(cls.test_dir)
- super().tearDownClass()
-
def write_settings(self, filename, apps=None, is_dir=False, sdict=None, extra=None):
if is_dir:
settings_dir = os.path.join(self.test_dir, filename)
@@ -90,19 +80,6 @@ class AdminScriptTestCase(SimpleTestCase):
for k, v in sdict.items():
settings_file.write("%s = %s\n" % (k, v))
- def remove_settings(self, filename, is_dir=False):
- full_name = os.path.join(self.test_dir, filename)
- if is_dir:
- shutil.rmtree(full_name)
- else:
- os.remove(full_name)
-
- # Also remove a __pycache__ directory, if it exists; it could
- # mess up later tests that depend upon the .py file not existing
- cache_name = os.path.join(self.test_dir, '__pycache__')
- if os.path.isdir(cache_name):
- shutil.rmtree(cache_name)
-
def _ext_backend_paths(self):
"""
Returns the paths for any external backend packages.
@@ -151,12 +128,6 @@ class AdminScriptTestCase(SimpleTestCase):
return self.run_test(os.path.join(script_dir, 'django-admin.py'), args, settings_file)
def run_manage(self, args, settings_file=None, configured_settings=False):
- def safe_remove(path):
- try:
- os.remove(path)
- except OSError:
- pass
-
template_manage_py = (
os.path.join(os.path.dirname(__file__), 'configured_settings_manage.py')
if configured_settings else
@@ -171,7 +142,6 @@ class AdminScriptTestCase(SimpleTestCase):
"{{ project_name }}", "test_project")
with open(test_manage_py, 'w') as fp:
fp.write(manage_py_contents)
- self.addCleanup(safe_remove, test_manage_py)
return self.run_test('./manage.py', args, settings_file)
@@ -240,11 +210,9 @@ class DjangoAdminDefaultSettings(AdminScriptTestCase):
contains the test application.
"""
def setUp(self):
+ super().setUp()
self.write_settings('settings.py')
- def tearDown(self):
- self.remove_settings('settings.py')
-
def test_builtin_command(self):
"default: django-admin builtin commands fail with an error when no settings provided"
args = ['check', 'admin_scripts']
@@ -308,12 +276,10 @@ class DjangoAdminFullPathDefaultSettings(AdminScriptTestCase):
contains the test application specified using a full path.
"""
def setUp(self):
+ super().setUp()
self.write_settings('settings.py', ['django.contrib.auth', 'django.contrib.contenttypes',
'admin_scripts', 'admin_scripts.complex_app'])
- def tearDown(self):
- self.remove_settings('settings.py')
-
def test_builtin_command(self):
"fulldefault: django-admin builtin commands fail with an error when no settings provided"
args = ['check', 'admin_scripts']
@@ -377,11 +343,9 @@ class DjangoAdminMinimalSettings(AdminScriptTestCase):
doesn't contain the test application.
"""
def setUp(self):
+ super().setUp()
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
- def tearDown(self):
- self.remove_settings('settings.py')
-
def test_builtin_command(self):
"minimal: django-admin builtin commands fail with an error when no settings provided"
args = ['check', 'admin_scripts']
@@ -445,11 +409,9 @@ class DjangoAdminAlternateSettings(AdminScriptTestCase):
with a name other than 'settings.py'.
"""
def setUp(self):
+ super().setUp()
self.write_settings('alternate_settings.py')
- def tearDown(self):
- self.remove_settings('alternate_settings.py')
-
def test_builtin_command(self):
"alternate: django-admin builtin commands fail with an error when no settings provided"
args = ['check', 'admin_scripts']
@@ -515,13 +477,10 @@ class DjangoAdminMultipleSettings(AdminScriptTestCase):
alternate settings must be used by the running script.
"""
def setUp(self):
+ super().setUp()
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
self.write_settings('alternate_settings.py')
- def tearDown(self):
- self.remove_settings('settings.py')
- self.remove_settings('alternate_settings.py')
-
def test_builtin_command(self):
"alternate: django-admin builtin commands fail with an error when no settings provided"
args = ['check', 'admin_scripts']
@@ -586,17 +545,14 @@ class DjangoAdminSettingsDirectory(AdminScriptTestCase):
"""
def setUp(self):
+ super().setUp()
self.write_settings('settings', is_dir=True)
- def tearDown(self):
- self.remove_settings('settings', is_dir=True)
-
def test_setup_environ(self):
"directory: startapp creates the correct directory"
args = ['startapp', '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)
self.assertTrue(os.path.exists(app_path))
with open(os.path.join(app_path, 'apps.py')) as f:
@@ -610,7 +566,6 @@ class DjangoAdminSettingsDirectory(AdminScriptTestCase):
args = ['startapp', '--template', template_path, '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)
self.assertTrue(os.path.exists(app_path))
self.assertTrue(os.path.exists(os.path.join(app_path, 'api.py')))
@@ -620,7 +575,6 @@ class DjangoAdminSettingsDirectory(AdminScriptTestCase):
args = ['startapp', 'こんにちは']
app_path = os.path.join(self.test_dir, 'こんにちは')
out, err = self.run_django_admin(args, 'test_project.settings')
- self.addCleanup(shutil.rmtree, app_path)
self.assertNoOutput(err)
self.assertTrue(os.path.exists(app_path))
with open(os.path.join(app_path, 'apps.py'), encoding='utf8') as f:
@@ -707,11 +661,9 @@ class ManageDefaultSettings(AdminScriptTestCase):
contains the test application.
"""
def setUp(self):
+ super().setUp()
self.write_settings('settings.py')
- def tearDown(self):
- self.remove_settings('settings.py')
-
def test_builtin_command(self):
"default: manage.py builtin commands succeed when default settings are appropriate"
args = ['check', 'admin_scripts']
@@ -774,11 +726,9 @@ class ManageFullPathDefaultSettings(AdminScriptTestCase):
contains the test application specified using a full path.
"""
def setUp(self):
+ super().setUp()
self.write_settings('settings.py', ['django.contrib.auth', 'django.contrib.contenttypes', 'admin_scripts'])
- def tearDown(self):
- self.remove_settings('settings.py')
-
def test_builtin_command(self):
"fulldefault: manage.py builtin commands succeed when default settings are appropriate"
args = ['check', 'admin_scripts']
@@ -841,11 +791,9 @@ class ManageMinimalSettings(AdminScriptTestCase):
doesn't contain the test application.
"""
def setUp(self):
+ super().setUp()
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
- def tearDown(self):
- self.remove_settings('settings.py')
-
def test_builtin_command(self):
"minimal: manage.py builtin commands fail with an error when no settings provided"
args = ['check', 'admin_scripts']
@@ -908,11 +856,9 @@ class ManageAlternateSettings(AdminScriptTestCase):
with a name other than 'settings.py'.
"""
def setUp(self):
+ super().setUp()
self.write_settings('alternate_settings.py')
- def tearDown(self):
- self.remove_settings('alternate_settings.py')
-
def test_builtin_command(self):
"alternate: manage.py builtin commands fail with an error when no default settings provided"
args = ['check', 'admin_scripts']
@@ -999,13 +945,10 @@ class ManageMultipleSettings(AdminScriptTestCase):
alternate settings must be used by the running script.
"""
def setUp(self):
+ super().setUp()
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
self.write_settings('alternate_settings.py')
- def tearDown(self):
- self.remove_settings('settings.py')
- self.remove_settings('alternate_settings.py')
-
def test_builtin_command(self):
"multiple: manage.py builtin commands fail with an error when no settings provided"
args = ['check', 'admin_scripts']
@@ -1068,9 +1011,6 @@ class ManageSettingsWithSettingsErrors(AdminScriptTestCase):
Tests for manage.py when using the default settings.py file containing
runtime errors.
"""
- def tearDown(self):
- self.remove_settings('settings.py')
-
def write_settings_with_import_error(self, filename):
settings_file_path = os.path.join(self.test_dir, filename)
with open(settings_file_path, 'w') as settings_file:
@@ -1124,9 +1064,6 @@ class ManageSettingsWithSettingsErrors(AdminScriptTestCase):
class ManageCheck(AdminScriptTestCase):
- def tearDown(self):
- self.remove_settings('settings.py')
-
def test_nonexistent_app(self):
"""check reports an error on a nonexistent app in INSTALLED_APPS."""
self.write_settings(
@@ -1274,7 +1211,7 @@ class ManageCheck(AdminScriptTestCase):
self.assertNoOutput(out)
-class ManageRunserver(AdminScriptTestCase):
+class ManageRunserver(SimpleTestCase):
def setUp(self):
def monkey_run(*args, **options):
return
@@ -1392,21 +1329,19 @@ class ManageRunserverMigrationWarning(TestCase):
class ManageRunserverEmptyAllowedHosts(AdminScriptTestCase):
def setUp(self):
+ super().setUp()
self.write_settings('settings.py', sdict={
'ALLOWED_HOSTS': [],
'DEBUG': False,
})
- def tearDown(self):
- self.remove_settings('settings.py')
-
def test_empty_allowed_hosts_error(self):
out, err = self.run_manage(['runserver'])
self.assertNoOutput(out)
self.assertOutput(err, 'CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.')
-class ManageTestserver(AdminScriptTestCase):
+class ManageTestserver(SimpleTestCase):
@mock.patch.object(TestserverCommand, 'handle', return_value='')
def test_testserver_handle_params(self, mock_handle):
@@ -1462,11 +1397,9 @@ class ColorCommand(BaseCommand):
class CommandTypes(AdminScriptTestCase):
"Tests for the various types of base command types that can be defined."
def setUp(self):
+ super().setUp()
self.write_settings('settings.py')
- def tearDown(self):
- self.remove_settings('settings.py')
-
def test_version(self):
"version is handled as a special case"
args = ['version']
@@ -1883,13 +1816,10 @@ class ArgumentOrder(AdminScriptTestCase):
individual command.
"""
def setUp(self):
+ super().setUp()
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
self.write_settings('alternate_settings.py')
- def tearDown(self):
- self.remove_settings('settings.py')
- self.remove_settings('alternate_settings.py')
-
def test_setting_then_option(self):
""" Options passed after settings are correctly handled. """
args = ['base_command', 'testlabel', '--settings=alternate_settings', '--option_a=x']
@@ -1950,7 +1880,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
"Make sure the startproject management command creates a project"
args = ['startproject', 'testproject']
testproject_dir = os.path.join(self.test_dir, 'testproject')
- self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
self.assertNoOutput(err)
@@ -1966,7 +1895,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
for bad_name in ('7testproject', '../testproject'):
args = ['startproject', 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)
self.assertOutput(
@@ -1984,7 +1912,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
bad_name = 'os'
args = ['startproject', 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)
self.assertOutput(
@@ -2000,7 +1927,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
args = ['startproject', 'testproject', 'othertestproject']
testproject_dir = os.path.join(self.test_dir, 'othertestproject')
os.mkdir(testproject_dir)
- self.addCleanup(shutil.rmtree, testproject_dir)
out, err = self.run_django_admin(args)
self.assertNoOutput(err)
@@ -2016,7 +1942,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
template_path = os.path.join(custom_templates_dir, 'project_template')
args = ['startproject', '--template', template_path, 'customtestproject']
testproject_dir = os.path.join(self.test_dir, 'customtestproject')
- self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
self.assertNoOutput(err)
@@ -2028,7 +1953,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
template_path = os.path.join(custom_templates_dir, 'project_template' + os.sep)
args = ['startproject', '--template', template_path, 'customtestproject']
testproject_dir = os.path.join(self.test_dir, 'customtestproject')
- self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
self.assertNoOutput(err)
@@ -2040,7 +1964,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
template_path = os.path.join(custom_templates_dir, 'project_template.tgz')
args = ['startproject', '--template', template_path, 'tarballtestproject']
testproject_dir = os.path.join(self.test_dir, 'tarballtestproject')
- self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
self.assertNoOutput(err)
@@ -2053,7 +1976,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
args = ['startproject', '--template', template_path, 'tarballtestproject', 'altlocation']
testproject_dir = os.path.join(self.test_dir, 'altlocation')
os.mkdir(testproject_dir)
- self.addCleanup(shutil.rmtree, testproject_dir)
out, err = self.run_django_admin(args)
self.assertNoOutput(err)
@@ -2069,7 +1991,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
args = ['startproject', '--template', template_url, 'urltestproject']
testproject_dir = os.path.join(self.test_dir, 'urltestproject')
- self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
self.assertNoOutput(err)
@@ -2082,7 +2003,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
args = ['startproject', '--template', template_url, 'urltestproject']
testproject_dir = os.path.join(self.test_dir, 'urltestproject')
- self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
self.assertNoOutput(err)
@@ -2094,7 +2014,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
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(self.test_dir, 'customtestproject')
- self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
self.assertNoOutput(err)
@@ -2112,7 +2031,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
args = ['startproject', '--template', template_path, 'another_project', '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)
self.assertNoOutput(err)
test_manage_py = os.path.join(testproject_dir, 'manage.py')
@@ -2125,7 +2043,6 @@ 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(custom_templates_dir, 'project_template')
args = [
'custom_startproject', '--template', template_path,
@@ -2134,7 +2051,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
]
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)
self.assertNoOutput(err)
test_manage_py = os.path.join(testproject_dir, 'additional_dir', 'extra.py')
@@ -2163,7 +2079,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
template_path = os.path.join(custom_templates_dir, 'project_template')
args = ['startproject', '--template', template_path, '--extension=txt', 'customtestproject']
testproject_dir = os.path.join(self.test_dir, 'customtestproject')
- self.addCleanup(shutil.rmtree, testproject_dir, True)
out, err = self.run_django_admin(args)
self.assertNoOutput(err)
@@ -2182,7 +2097,6 @@ class StartApp(AdminScriptTestCase):
for bad_name in ('7testproject', '../testproject'):
args = ['startapp', 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)
self.assertOutput(
@@ -2200,7 +2114,6 @@ class StartApp(AdminScriptTestCase):
bad_name = 'os'
args = ['startapp', 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)
self.assertOutput(
@@ -2218,7 +2131,6 @@ 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.assertNoOutput(err)
@@ -2232,7 +2144,6 @@ class DiffSettings(AdminScriptTestCase):
def test_all(self):
"""The all option also shows settings with the default value."""
self.write_settings('settings_to_diff.py', sdict={'STATIC_URL': 'None'})
- self.addCleanup(self.remove_settings, 'settings_to_diff.py')
args = ['diffsettings', '--settings=settings_to_diff', '--all']
out, err = self.run_manage(args)
self.assertNoOutput(err)
@@ -2244,9 +2155,7 @@ class DiffSettings(AdminScriptTestCase):
comparison.
"""
self.write_settings('settings_default.py', sdict={'FOO': '"foo"', 'BAR': '"bar1"'})
- self.addCleanup(self.remove_settings, 'settings_default.py')
self.write_settings('settings_to_diff.py', sdict={'FOO': '"foo"', 'BAR': '"bar2"'})
- self.addCleanup(self.remove_settings, 'settings_to_diff.py')
out, err = self.run_manage(['diffsettings', '--settings=settings_to_diff', '--default=settings_default'])
self.assertNoOutput(err)
self.assertNotInOutput(out, "FOO")
@@ -2255,7 +2164,6 @@ class DiffSettings(AdminScriptTestCase):
def test_unified(self):
"""--output=unified emits settings diff in unified mode."""
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', '--output=unified']
out, err = self.run_manage(args)
self.assertNoOutput(err)
@@ -2270,7 +2178,6 @@ class DiffSettings(AdminScriptTestCase):
settings with the default value.
"""
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', '--output=unified', '--all']
out, err = self.run_manage(args)
self.assertNoOutput(err)
@@ -2283,11 +2190,9 @@ class Dumpdata(AdminScriptTestCase):
"""Tests for dumpdata management command."""
def setUp(self):
+ super().setUp()
self.write_settings('settings.py')
- def tearDown(self):
- self.remove_settings('settings.py')
-
def test_pks_parsing(self):
"""Regression for #20509
@@ -2314,11 +2219,9 @@ class MainModule(AdminScriptTestCase):
class DjangoAdminSuggestions(AdminScriptTestCase):
def setUp(self):
+ super().setUp()
self.write_settings('settings.py')
- def tearDown(self):
- self.remove_settings('settings.py')
-
def test_suggestions(self):
args = ['rnserver', '--settings=test_project.settings']
out, err = self.run_django_admin(args)