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.
This commit is contained in:
Jon Dufresne 2019-02-05 09:11:54 -08:00 committed by Tim Graham
parent 099c36d546
commit 487d904bf2
5 changed files with 30 additions and 140 deletions

View File

@ -39,24 +39,14 @@ SYSTEM_CHECK_MSG = 'System check identified no issues'
class AdminScriptTestCase(SimpleTestCase): class AdminScriptTestCase(SimpleTestCase):
def setUp(self):
@classmethod tmpdir = tempfile.TemporaryDirectory()
def setUpClass(cls): self.addCleanup(tmpdir.cleanup)
super().setUpClass() self.test_dir = os.path.join(tmpdir.name, 'test_project')
cls.test_dir = os.path.realpath(os.path.join( os.mkdir(self.test_dir)
tempfile.gettempdir(), with open(os.path.join(self.test_dir, '__init__.py'), 'w'):
cls.__name__,
'test_project',
))
os.makedirs(cls.test_dir)
with open(os.path.join(cls.test_dir, '__init__.py'), 'w'):
pass 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): def write_settings(self, filename, apps=None, is_dir=False, sdict=None, extra=None):
if is_dir: if is_dir:
settings_dir = os.path.join(self.test_dir, filename) settings_dir = os.path.join(self.test_dir, filename)
@ -90,19 +80,6 @@ class AdminScriptTestCase(SimpleTestCase):
for k, v in sdict.items(): for k, v in sdict.items():
settings_file.write("%s = %s\n" % (k, v)) 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): def _ext_backend_paths(self):
""" """
Returns the paths for any external backend packages. 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) 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 run_manage(self, args, settings_file=None, configured_settings=False):
def safe_remove(path):
try:
os.remove(path)
except OSError:
pass
template_manage_py = ( template_manage_py = (
os.path.join(os.path.dirname(__file__), 'configured_settings_manage.py') os.path.join(os.path.dirname(__file__), 'configured_settings_manage.py')
if configured_settings else if configured_settings else
@ -171,7 +142,6 @@ class AdminScriptTestCase(SimpleTestCase):
"{{ project_name }}", "test_project") "{{ project_name }}", "test_project")
with open(test_manage_py, 'w') as fp: with open(test_manage_py, 'w') as fp:
fp.write(manage_py_contents) fp.write(manage_py_contents)
self.addCleanup(safe_remove, test_manage_py)
return self.run_test('./manage.py', args, settings_file) return self.run_test('./manage.py', args, settings_file)
@ -240,11 +210,9 @@ class DjangoAdminDefaultSettings(AdminScriptTestCase):
contains the test application. contains the test application.
""" """
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py') self.write_settings('settings.py')
def tearDown(self):
self.remove_settings('settings.py')
def test_builtin_command(self): def test_builtin_command(self):
"default: django-admin builtin commands fail with an error when no settings provided" "default: django-admin builtin commands fail with an error when no settings provided"
args = ['check', 'admin_scripts'] args = ['check', 'admin_scripts']
@ -308,12 +276,10 @@ class DjangoAdminFullPathDefaultSettings(AdminScriptTestCase):
contains the test application specified using a full path. contains the test application specified using a full path.
""" """
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py', ['django.contrib.auth', 'django.contrib.contenttypes', self.write_settings('settings.py', ['django.contrib.auth', 'django.contrib.contenttypes',
'admin_scripts', 'admin_scripts.complex_app']) 'admin_scripts', 'admin_scripts.complex_app'])
def tearDown(self):
self.remove_settings('settings.py')
def test_builtin_command(self): def test_builtin_command(self):
"fulldefault: django-admin builtin commands fail with an error when no settings provided" "fulldefault: django-admin builtin commands fail with an error when no settings provided"
args = ['check', 'admin_scripts'] args = ['check', 'admin_scripts']
@ -377,11 +343,9 @@ class DjangoAdminMinimalSettings(AdminScriptTestCase):
doesn't contain the test application. doesn't contain the test application.
""" """
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes']) 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): def test_builtin_command(self):
"minimal: django-admin builtin commands fail with an error when no settings provided" "minimal: django-admin builtin commands fail with an error when no settings provided"
args = ['check', 'admin_scripts'] args = ['check', 'admin_scripts']
@ -445,11 +409,9 @@ class DjangoAdminAlternateSettings(AdminScriptTestCase):
with a name other than 'settings.py'. with a name other than 'settings.py'.
""" """
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('alternate_settings.py') self.write_settings('alternate_settings.py')
def tearDown(self):
self.remove_settings('alternate_settings.py')
def test_builtin_command(self): def test_builtin_command(self):
"alternate: django-admin builtin commands fail with an error when no settings provided" "alternate: django-admin builtin commands fail with an error when no settings provided"
args = ['check', 'admin_scripts'] args = ['check', 'admin_scripts']
@ -515,13 +477,10 @@ class DjangoAdminMultipleSettings(AdminScriptTestCase):
alternate settings must be used by the running script. alternate settings must be used by the running script.
""" """
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes']) self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
self.write_settings('alternate_settings.py') 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): def test_builtin_command(self):
"alternate: django-admin builtin commands fail with an error when no settings provided" "alternate: django-admin builtin commands fail with an error when no settings provided"
args = ['check', 'admin_scripts'] args = ['check', 'admin_scripts']
@ -586,17 +545,14 @@ class DjangoAdminSettingsDirectory(AdminScriptTestCase):
""" """
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings', is_dir=True) self.write_settings('settings', is_dir=True)
def tearDown(self):
self.remove_settings('settings', is_dir=True)
def test_setup_environ(self): def test_setup_environ(self):
"directory: startapp creates the correct directory" "directory: startapp creates the correct directory"
args = ['startapp', 'settings_test'] args = ['startapp', 'settings_test']
app_path = os.path.join(self.test_dir, 'settings_test') app_path = os.path.join(self.test_dir, 'settings_test')
out, err = self.run_django_admin(args, 'test_project.settings') out, err = self.run_django_admin(args, 'test_project.settings')
self.addCleanup(shutil.rmtree, app_path)
self.assertNoOutput(err) self.assertNoOutput(err)
self.assertTrue(os.path.exists(app_path)) self.assertTrue(os.path.exists(app_path))
with open(os.path.join(app_path, 'apps.py')) as f: 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'] args = ['startapp', '--template', template_path, 'custom_settings_test']
app_path = os.path.join(self.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') out, err = self.run_django_admin(args, 'test_project.settings')
self.addCleanup(shutil.rmtree, app_path)
self.assertNoOutput(err) self.assertNoOutput(err)
self.assertTrue(os.path.exists(app_path)) self.assertTrue(os.path.exists(app_path))
self.assertTrue(os.path.exists(os.path.join(app_path, 'api.py'))) self.assertTrue(os.path.exists(os.path.join(app_path, 'api.py')))
@ -620,7 +575,6 @@ class DjangoAdminSettingsDirectory(AdminScriptTestCase):
args = ['startapp', 'こんにちは'] args = ['startapp', 'こんにちは']
app_path = os.path.join(self.test_dir, 'こんにちは') app_path = os.path.join(self.test_dir, 'こんにちは')
out, err = self.run_django_admin(args, 'test_project.settings') out, err = self.run_django_admin(args, 'test_project.settings')
self.addCleanup(shutil.rmtree, app_path)
self.assertNoOutput(err) self.assertNoOutput(err)
self.assertTrue(os.path.exists(app_path)) self.assertTrue(os.path.exists(app_path))
with open(os.path.join(app_path, 'apps.py'), encoding='utf8') as f: with open(os.path.join(app_path, 'apps.py'), encoding='utf8') as f:
@ -707,11 +661,9 @@ class ManageDefaultSettings(AdminScriptTestCase):
contains the test application. contains the test application.
""" """
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py') self.write_settings('settings.py')
def tearDown(self):
self.remove_settings('settings.py')
def test_builtin_command(self): def test_builtin_command(self):
"default: manage.py builtin commands succeed when default settings are appropriate" "default: manage.py builtin commands succeed when default settings are appropriate"
args = ['check', 'admin_scripts'] args = ['check', 'admin_scripts']
@ -774,11 +726,9 @@ class ManageFullPathDefaultSettings(AdminScriptTestCase):
contains the test application specified using a full path. contains the test application specified using a full path.
""" """
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py', ['django.contrib.auth', 'django.contrib.contenttypes', 'admin_scripts']) 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): def test_builtin_command(self):
"fulldefault: manage.py builtin commands succeed when default settings are appropriate" "fulldefault: manage.py builtin commands succeed when default settings are appropriate"
args = ['check', 'admin_scripts'] args = ['check', 'admin_scripts']
@ -841,11 +791,9 @@ class ManageMinimalSettings(AdminScriptTestCase):
doesn't contain the test application. doesn't contain the test application.
""" """
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes']) 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): def test_builtin_command(self):
"minimal: manage.py builtin commands fail with an error when no settings provided" "minimal: manage.py builtin commands fail with an error when no settings provided"
args = ['check', 'admin_scripts'] args = ['check', 'admin_scripts']
@ -908,11 +856,9 @@ class ManageAlternateSettings(AdminScriptTestCase):
with a name other than 'settings.py'. with a name other than 'settings.py'.
""" """
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('alternate_settings.py') self.write_settings('alternate_settings.py')
def tearDown(self):
self.remove_settings('alternate_settings.py')
def test_builtin_command(self): def test_builtin_command(self):
"alternate: manage.py builtin commands fail with an error when no default settings provided" "alternate: manage.py builtin commands fail with an error when no default settings provided"
args = ['check', 'admin_scripts'] args = ['check', 'admin_scripts']
@ -999,13 +945,10 @@ class ManageMultipleSettings(AdminScriptTestCase):
alternate settings must be used by the running script. alternate settings must be used by the running script.
""" """
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes']) self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
self.write_settings('alternate_settings.py') 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): def test_builtin_command(self):
"multiple: manage.py builtin commands fail with an error when no settings provided" "multiple: manage.py builtin commands fail with an error when no settings provided"
args = ['check', 'admin_scripts'] args = ['check', 'admin_scripts']
@ -1068,9 +1011,6 @@ class ManageSettingsWithSettingsErrors(AdminScriptTestCase):
Tests for manage.py when using the default settings.py file containing Tests for manage.py when using the default settings.py file containing
runtime errors. runtime errors.
""" """
def tearDown(self):
self.remove_settings('settings.py')
def write_settings_with_import_error(self, filename): def write_settings_with_import_error(self, filename):
settings_file_path = os.path.join(self.test_dir, filename) settings_file_path = os.path.join(self.test_dir, filename)
with open(settings_file_path, 'w') as settings_file: with open(settings_file_path, 'w') as settings_file:
@ -1124,9 +1064,6 @@ class ManageSettingsWithSettingsErrors(AdminScriptTestCase):
class ManageCheck(AdminScriptTestCase): class ManageCheck(AdminScriptTestCase):
def tearDown(self):
self.remove_settings('settings.py')
def test_nonexistent_app(self): def test_nonexistent_app(self):
"""check reports an error on a nonexistent app in INSTALLED_APPS.""" """check reports an error on a nonexistent app in INSTALLED_APPS."""
self.write_settings( self.write_settings(
@ -1274,7 +1211,7 @@ class ManageCheck(AdminScriptTestCase):
self.assertNoOutput(out) self.assertNoOutput(out)
class ManageRunserver(AdminScriptTestCase): class ManageRunserver(SimpleTestCase):
def setUp(self): def setUp(self):
def monkey_run(*args, **options): def monkey_run(*args, **options):
return return
@ -1392,21 +1329,19 @@ class ManageRunserverMigrationWarning(TestCase):
class ManageRunserverEmptyAllowedHosts(AdminScriptTestCase): class ManageRunserverEmptyAllowedHosts(AdminScriptTestCase):
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py', sdict={ self.write_settings('settings.py', sdict={
'ALLOWED_HOSTS': [], 'ALLOWED_HOSTS': [],
'DEBUG': False, 'DEBUG': False,
}) })
def tearDown(self):
self.remove_settings('settings.py')
def test_empty_allowed_hosts_error(self): def test_empty_allowed_hosts_error(self):
out, err = self.run_manage(['runserver']) out, err = self.run_manage(['runserver'])
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, 'CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.') 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='') @mock.patch.object(TestserverCommand, 'handle', return_value='')
def test_testserver_handle_params(self, mock_handle): def test_testserver_handle_params(self, mock_handle):
@ -1462,11 +1397,9 @@ class ColorCommand(BaseCommand):
class CommandTypes(AdminScriptTestCase): class CommandTypes(AdminScriptTestCase):
"Tests for the various types of base command types that can be defined." "Tests for the various types of base command types that can be defined."
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py') self.write_settings('settings.py')
def tearDown(self):
self.remove_settings('settings.py')
def test_version(self): def test_version(self):
"version is handled as a special case" "version is handled as a special case"
args = ['version'] args = ['version']
@ -1883,13 +1816,10 @@ class ArgumentOrder(AdminScriptTestCase):
individual command. individual command.
""" """
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes']) self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
self.write_settings('alternate_settings.py') 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): def test_setting_then_option(self):
""" Options passed after settings are correctly handled. """ """ Options passed after settings are correctly handled. """
args = ['base_command', 'testlabel', '--settings=alternate_settings', '--option_a=x'] 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" "Make sure the startproject management command creates a project"
args = ['startproject', 'testproject'] args = ['startproject', 'testproject']
testproject_dir = os.path.join(self.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) out, err = self.run_django_admin(args)
self.assertNoOutput(err) self.assertNoOutput(err)
@ -1966,7 +1895,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
for bad_name in ('7testproject', '../testproject'): for bad_name in ('7testproject', '../testproject'):
args = ['startproject', bad_name] args = ['startproject', bad_name]
testproject_dir = os.path.join(self.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) out, err = self.run_django_admin(args)
self.assertOutput( self.assertOutput(
@ -1984,7 +1912,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
bad_name = 'os' bad_name = 'os'
args = ['startproject', bad_name] args = ['startproject', bad_name]
testproject_dir = os.path.join(self.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) out, err = self.run_django_admin(args)
self.assertOutput( self.assertOutput(
@ -2000,7 +1927,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
args = ['startproject', 'testproject', 'othertestproject'] args = ['startproject', 'testproject', 'othertestproject']
testproject_dir = os.path.join(self.test_dir, 'othertestproject') testproject_dir = os.path.join(self.test_dir, 'othertestproject')
os.mkdir(testproject_dir) os.mkdir(testproject_dir)
self.addCleanup(shutil.rmtree, testproject_dir)
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(err) self.assertNoOutput(err)
@ -2016,7 +1942,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
template_path = os.path.join(custom_templates_dir, 'project_template') template_path = os.path.join(custom_templates_dir, 'project_template')
args = ['startproject', '--template', template_path, 'customtestproject'] args = ['startproject', '--template', template_path, 'customtestproject']
testproject_dir = os.path.join(self.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) out, err = self.run_django_admin(args)
self.assertNoOutput(err) self.assertNoOutput(err)
@ -2028,7 +1953,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
template_path = os.path.join(custom_templates_dir, 'project_template' + os.sep) template_path = os.path.join(custom_templates_dir, 'project_template' + os.sep)
args = ['startproject', '--template', template_path, 'customtestproject'] args = ['startproject', '--template', template_path, 'customtestproject']
testproject_dir = os.path.join(self.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) out, err = self.run_django_admin(args)
self.assertNoOutput(err) self.assertNoOutput(err)
@ -2040,7 +1964,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
template_path = os.path.join(custom_templates_dir, 'project_template.tgz') template_path = os.path.join(custom_templates_dir, 'project_template.tgz')
args = ['startproject', '--template', template_path, 'tarballtestproject'] args = ['startproject', '--template', template_path, 'tarballtestproject']
testproject_dir = os.path.join(self.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) out, err = self.run_django_admin(args)
self.assertNoOutput(err) self.assertNoOutput(err)
@ -2053,7 +1976,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
args = ['startproject', '--template', template_path, 'tarballtestproject', 'altlocation'] args = ['startproject', '--template', template_path, 'tarballtestproject', 'altlocation']
testproject_dir = os.path.join(self.test_dir, 'altlocation') testproject_dir = os.path.join(self.test_dir, 'altlocation')
os.mkdir(testproject_dir) os.mkdir(testproject_dir)
self.addCleanup(shutil.rmtree, testproject_dir)
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(err) self.assertNoOutput(err)
@ -2069,7 +1991,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
args = ['startproject', '--template', template_url, 'urltestproject'] args = ['startproject', '--template', template_url, 'urltestproject']
testproject_dir = os.path.join(self.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) out, err = self.run_django_admin(args)
self.assertNoOutput(err) self.assertNoOutput(err)
@ -2082,7 +2003,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
args = ['startproject', '--template', template_url, 'urltestproject'] args = ['startproject', '--template', template_url, 'urltestproject']
testproject_dir = os.path.join(self.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) out, err = self.run_django_admin(args)
self.assertNoOutput(err) self.assertNoOutput(err)
@ -2094,7 +2014,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
template_path = os.path.join(custom_templates_dir, 'project_template') template_path = os.path.join(custom_templates_dir, 'project_template')
args = ['startproject', '--template', template_path, 'customtestproject', '-e', 'txt', '-n', 'Procfile'] args = ['startproject', '--template', template_path, 'customtestproject', '-e', 'txt', '-n', 'Procfile']
testproject_dir = os.path.join(self.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) out, err = self.run_django_admin(args)
self.assertNoOutput(err) self.assertNoOutput(err)
@ -2112,7 +2031,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
args = ['startproject', '--template', template_path, 'another_project', 'project_dir'] args = ['startproject', '--template', template_path, 'another_project', 'project_dir']
testproject_dir = os.path.join(self.test_dir, 'project_dir') testproject_dir = os.path.join(self.test_dir, 'project_dir')
os.mkdir(testproject_dir) os.mkdir(testproject_dir)
self.addCleanup(shutil.rmtree, testproject_dir)
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(err) self.assertNoOutput(err)
test_manage_py = os.path.join(testproject_dir, 'manage.py') 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" "Make sure template context variables are not html escaped"
# We're using a custom command so we need the alternate settings # We're using a custom command so we need the alternate settings
self.write_settings('alternate_settings.py') self.write_settings('alternate_settings.py')
self.addCleanup(self.remove_settings, 'alternate_settings.py')
template_path = os.path.join(custom_templates_dir, 'project_template') template_path = os.path.join(custom_templates_dir, 'project_template')
args = [ args = [
'custom_startproject', '--template', template_path, 'custom_startproject', '--template', template_path,
@ -2134,7 +2051,6 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
] ]
testproject_dir = os.path.join(self.test_dir, 'project_dir') testproject_dir = os.path.join(self.test_dir, 'project_dir')
os.mkdir(testproject_dir) os.mkdir(testproject_dir)
self.addCleanup(shutil.rmtree, testproject_dir)
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(err) self.assertNoOutput(err)
test_manage_py = os.path.join(testproject_dir, 'additional_dir', 'extra.py') 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') template_path = os.path.join(custom_templates_dir, 'project_template')
args = ['startproject', '--template', template_path, '--extension=txt', 'customtestproject'] args = ['startproject', '--template', template_path, '--extension=txt', 'customtestproject']
testproject_dir = os.path.join(self.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) out, err = self.run_django_admin(args)
self.assertNoOutput(err) self.assertNoOutput(err)
@ -2182,7 +2097,6 @@ class StartApp(AdminScriptTestCase):
for bad_name in ('7testproject', '../testproject'): for bad_name in ('7testproject', '../testproject'):
args = ['startapp', bad_name] args = ['startapp', bad_name]
testproject_dir = os.path.join(self.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) out, err = self.run_django_admin(args)
self.assertOutput( self.assertOutput(
@ -2200,7 +2114,6 @@ class StartApp(AdminScriptTestCase):
bad_name = 'os' bad_name = 'os'
args = ['startapp', bad_name] args = ['startapp', bad_name]
testproject_dir = os.path.join(self.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) out, err = self.run_django_admin(args)
self.assertOutput( self.assertOutput(
@ -2218,7 +2131,6 @@ class DiffSettings(AdminScriptTestCase):
def test_basic(self): def test_basic(self):
"""Runs without error and emits settings diff.""" """Runs without error and emits settings diff."""
self.write_settings('settings_to_diff.py', sdict={'FOO': '"bar"'}) 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'] args = ['diffsettings', '--settings=settings_to_diff']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(err) self.assertNoOutput(err)
@ -2232,7 +2144,6 @@ class DiffSettings(AdminScriptTestCase):
def test_all(self): def test_all(self):
"""The all option also shows settings with the default value.""" """The all option also shows settings with the default value."""
self.write_settings('settings_to_diff.py', sdict={'STATIC_URL': 'None'}) 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'] args = ['diffsettings', '--settings=settings_to_diff', '--all']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(err) self.assertNoOutput(err)
@ -2244,9 +2155,7 @@ class DiffSettings(AdminScriptTestCase):
comparison. comparison.
""" """
self.write_settings('settings_default.py', sdict={'FOO': '"foo"', 'BAR': '"bar1"'}) 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.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']) out, err = self.run_manage(['diffsettings', '--settings=settings_to_diff', '--default=settings_default'])
self.assertNoOutput(err) self.assertNoOutput(err)
self.assertNotInOutput(out, "FOO") self.assertNotInOutput(out, "FOO")
@ -2255,7 +2164,6 @@ class DiffSettings(AdminScriptTestCase):
def test_unified(self): def test_unified(self):
"""--output=unified emits settings diff in unified mode.""" """--output=unified emits settings diff in unified mode."""
self.write_settings('settings_to_diff.py', sdict={'FOO': '"bar"'}) 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'] args = ['diffsettings', '--settings=settings_to_diff', '--output=unified']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(err) self.assertNoOutput(err)
@ -2270,7 +2178,6 @@ class DiffSettings(AdminScriptTestCase):
settings with the default value. settings with the default value.
""" """
self.write_settings('settings_to_diff.py', sdict={'FOO': '"bar"'}) 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'] args = ['diffsettings', '--settings=settings_to_diff', '--output=unified', '--all']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(err) self.assertNoOutput(err)
@ -2283,11 +2190,9 @@ class Dumpdata(AdminScriptTestCase):
"""Tests for dumpdata management command.""" """Tests for dumpdata management command."""
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py') self.write_settings('settings.py')
def tearDown(self):
self.remove_settings('settings.py')
def test_pks_parsing(self): def test_pks_parsing(self):
"""Regression for #20509 """Regression for #20509
@ -2314,11 +2219,9 @@ class MainModule(AdminScriptTestCase):
class DjangoAdminSuggestions(AdminScriptTestCase): class DjangoAdminSuggestions(AdminScriptTestCase):
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py') self.write_settings('settings.py')
def tearDown(self):
self.remove_settings('settings.py')
def test_suggestions(self): def test_suggestions(self):
args = ['rnserver', '--settings=test_project.settings'] args = ['rnserver', '--settings=test_project.settings']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)

View File

@ -439,6 +439,7 @@ class SettingsConfigTest(AdminScriptTestCase):
a circular import error. a circular import error.
""" """
def setUp(self): def setUp(self):
super().setUp()
log_config = """{ log_config = """{
'version': 1, 'version': 1,
'handlers': { 'handlers': {
@ -450,9 +451,6 @@ class SettingsConfigTest(AdminScriptTestCase):
}""" }"""
self.write_settings('settings.py', sdict={'LOGGING': log_config}) self.write_settings('settings.py', sdict={'LOGGING': log_config})
def tearDown(self):
self.remove_settings('settings.py')
def test_circular_dependency(self): def test_circular_dependency(self):
# validate is just an example command to trigger settings configuration # validate is just an example command to trigger settings configuration
out, err = self.run_manage(['check']) out, err = self.run_manage(['check'])
@ -518,6 +516,7 @@ class SettingsCustomLoggingTest(AdminScriptTestCase):
callable in LOGGING_CONFIG (i.e., logging.config.fileConfig). callable in LOGGING_CONFIG (i.e., logging.config.fileConfig).
""" """
def setUp(self): def setUp(self):
super().setUp()
logging_conf = """ logging_conf = """
[loggers] [loggers]
keys=root keys=root
@ -544,7 +543,6 @@ format=%(message)s
def tearDown(self): def tearDown(self):
self.temp_file.close() self.temp_file.close()
self.remove_settings('settings.py')
def test_custom_logging(self): def test_custom_logging(self):
out, err = self.run_manage(['check']) out, err = self.run_manage(['check'])

View File

@ -155,14 +155,12 @@ class CustomTestRunnerOptionsSettingsTests(AdminScriptTestCase):
through a settings file. through a settings file.
""" """
def setUp(self): def setUp(self):
super().setUp()
settings = { settings = {
'TEST_RUNNER': '\'test_runner.runner.CustomOptionsTestRunner\'', 'TEST_RUNNER': '\'test_runner.runner.CustomOptionsTestRunner\'',
} }
self.write_settings('settings.py', sdict=settings) self.write_settings('settings.py', sdict=settings)
def tearDown(self):
self.remove_settings('settings.py')
def test_default_options(self): def test_default_options(self):
args = ['test', '--settings=test_project.settings'] args = ['test', '--settings=test_project.settings']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
@ -195,11 +193,9 @@ class CustomTestRunnerOptionsCmdlineTests(AdminScriptTestCase):
using --testrunner. using --testrunner.
""" """
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py') self.write_settings('settings.py')
def tearDown(self):
self.remove_settings('settings.py')
def test_testrunner_option(self): def test_testrunner_option(self):
args = [ args = [
'test', '--testrunner', 'test_runner.runner.CustomOptionsTestRunner', 'test', '--testrunner', 'test_runner.runner.CustomOptionsTestRunner',
@ -228,11 +224,9 @@ class CustomTestRunnerOptionsCmdlineTests(AdminScriptTestCase):
class Ticket17477RegressionTests(AdminScriptTestCase): class Ticket17477RegressionTests(AdminScriptTestCase):
def setUp(self): def setUp(self):
super().setUp()
self.write_settings('settings.py') self.write_settings('settings.py')
def tearDown(self):
self.remove_settings('settings.py')
def test_ticket_17477(self): def test_ticket_17477(self):
"""'manage.py help test' works after r16352.""" """'manage.py help test' works after r16352."""
args = ['help', 'test'] args = ['help', 'test']

View File

@ -534,14 +534,12 @@ class ReverseLazySettingsTest(AdminScriptTestCase):
import error. import error.
""" """
def setUp(self): def setUp(self):
super().setUp()
self.write_settings( self.write_settings(
'settings.py', 'settings.py',
extra="from django.urls import reverse_lazy\nLOGIN_URL = reverse_lazy('login')", extra="from django.urls import reverse_lazy\nLOGIN_URL = reverse_lazy('login')",
) )
def tearDown(self):
self.remove_settings('settings.py')
def test_lazy_in_settings(self): def test_lazy_in_settings(self):
out, err = self.run_manage(['check']) out, err = self.run_manage(['check'])
self.assertNoOutput(err) self.assertNoOutput(err)

View File

@ -234,9 +234,6 @@ class CommandRunTests(AdminScriptTestCase):
""" """
Tests that need to run by simulating the command line, not by call_command. Tests that need to run by simulating the command line, not by call_command.
""" """
def tearDown(self):
self.remove_settings('settings.py')
def test_script_prefix_set_in_commands(self): def test_script_prefix_set_in_commands(self):
self.write_settings('settings.py', apps=['user_commands'], sdict={ self.write_settings('settings.py', apps=['user_commands'], sdict={
'ROOT_URLCONF': '"user_commands.urls"', 'ROOT_URLCONF': '"user_commands.urls"',