/tests/admin_scripts/tests.py
Python | 1102 lines | 1055 code | 21 blank | 26 comment | 78 complexity | cbce6512a768a7b8f562dc78f1e06955 MD5 | raw file
- # -*- coding: utf-8 -*-
- """
- A series of tests to establish that the command-line management tools work as
- advertised - especially with regards to the handling of the
- DJANGO_SETTINGS_MODULE and default settings.py files.
- """
- from __future__ import unicode_literals
- import codecs
- import os
- import re
- import shutil
- import socket
- import subprocess
- import sys
- import tempfile
- import unittest
- import django
- from django import conf, get_version
- from django.conf import settings
- from django.core.management import (
- BaseCommand, CommandError, call_command, color,
- )
- from django.db import ConnectionHandler
- from django.db.migrations.exceptions import MigrationSchemaMissing
- from django.db.migrations.recorder import MigrationRecorder
- from django.test import (
- LiveServerTestCase, SimpleTestCase, TestCase, mock, override_settings,
- )
- from django.test.runner import DiscoverRunner
- from django.utils._os import npath, upath
- from django.utils.encoding import force_text
- from django.utils.six import PY2, PY3, StringIO
- 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(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(self.test_dir, filename)
- with open(settings_file_path, 'w') as settings_file:
- settings_file.write('# -*- coding: utf-8 -*\n')
- settings_file.write('# Settings file automatically generated by admin_scripts test case\n')
- if extra:
- settings_file.write("%s\n" % extra)
- exports = [
- 'DATABASES',
- 'ROOT_URLCONF',
- 'SECRET_KEY',
- ]
- for s in exports:
- if hasattr(settings, s):
- o = getattr(settings, s)
- if not isinstance(o, (dict, tuple, list)):
- o = "'%s'" % o
- settings_file.write("%s = %s\n" % (s, o))
- if apps is None:
- apps = ['django.contrib.auth', 'django.contrib.contenttypes', 'admin_scripts']
- settings_file.write("INSTALLED_APPS = %s\n" % apps)
- if sdict:
- 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 try to remove the compiled file; if it exists, it could
- # mess up later tests that depend upon the .py file not existing
- try:
- if sys.platform.startswith('java'):
- # Jython produces module$py.class files
- os.remove(re.sub(r'\.py$', '$py.class', full_name))
- else:
- # CPython produces module.pyc files
- os.remove(full_name + 'c')
- except OSError:
- pass
- # Also remove a __pycache__ directory, if it exists
- 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.
- """
- paths = []
- first_package_re = re.compile(r'(^[^\.]+)\.')
- for backend in settings.DATABASES.values():
- result = first_package_re.findall(backend['ENGINE'])
- if result and result != ['django']:
- backend_pkg = __import__(result[0])
- backend_dir = os.path.dirname(backend_pkg.__file__)
- paths.append(os.path.dirname(backend_dir))
- return paths
- def run_test(self, script, args, settings_file=None, apps=None):
- 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
- # `import django` to figure that out, so we don't pick up a Django
- # from site-packages or similar.
- django_dir = os.path.dirname(tests_dir)
- ext_backend_base_dirs = self._ext_backend_paths()
- # Define a temporary environment for the subprocess
- test_environ = os.environ.copy()
- if sys.platform.startswith('java'):
- python_path_var_name = 'JYTHONPATH'
- else:
- python_path_var_name = 'PYTHONPATH'
- old_cwd = os.getcwd()
- # Set the test environment
- if settings_file:
- test_environ['DJANGO_SETTINGS_MODULE'] = str(settings_file)
- elif 'DJANGO_SETTINGS_MODULE' in test_environ:
- del test_environ['DJANGO_SETTINGS_MODULE']
- python_path = [base_dir, django_dir, tests_dir]
- python_path.extend(ext_backend_base_dirs)
- # Use native strings for better compatibility
- test_environ[str(python_path_var_name)] = npath(os.pathsep.join(python_path))
- test_environ[str('PYTHONWARNINGS')] = str('')
- # Move to the test directory and run
- 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()
- # Move back to the old working directory
- os.chdir(old_cwd)
- return out, err
- def run_django_admin(self, args, settings_file=None):
- script_dir = os.path.abspath(os.path.join(os.path.dirname(upath(django.__file__)), 'bin'))
- return self.run_test(os.path.join(script_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(upath(conf.__file__))
- template_manage_py = os.path.join(conf_dir, 'project_template', '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:
- manage_py_contents = fp.read()
- manage_py_contents = manage_py_contents.replace(
- "{{ 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)
- def assertNoOutput(self, stream):
- "Utility assertion: assert that the given stream is empty"
- self.assertEqual(len(stream), 0, "Stream should be empty: actually contains '%s'" % stream)
- def assertOutput(self, stream, msg, regex=False):
- "Utility assertion: assert that the given message exists in the output"
- stream = force_text(stream)
- if regex:
- self.assertIsNotNone(re.search(msg, stream),
- "'%s' does not match actual output text '%s'" % (msg, stream))
- else:
- self.assertIn(msg, 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.assertNotIn(msg, stream, "'%s' matches actual output text '%s'" % (msg, stream))
- ##########################################################################
- # DJANGO ADMIN TESTS
- # This first series of test classes checks the environment processing
- # of the django-admin.py script
- ##########################################################################
- class DjangoAdminNoSettings(AdminScriptTestCase):
- "A series of tests for django-admin.py when there is no settings.py file."
- def test_builtin_command(self):
- "no settings: django-admin builtin commands fail with an error when no settings provided"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, 'settings are not configured')
- def test_builtin_with_bad_settings(self):
- "no settings: django-admin builtin commands fail if settings file (from argument) doesn't exist"
- args = ['check', '--settings=bad_settings', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_builtin_with_bad_environment(self):
- "no settings: django-admin builtin commands fail if settings file (from environment) doesn't exist"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args, 'bad_settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- class DjangoAdminDefaultSettings(AdminScriptTestCase):
- """A series of tests for django-admin.py when using a settings.py file that
- contains the test application.
- """
- def setUp(self):
- 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']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, 'settings are not configured')
- def test_builtin_with_settings(self):
- "default: django-admin builtin commands succeed if settings are provided as argument"
- args = ['check', '--settings=test_project.settings', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_environment(self):
- "default: django-admin builtin commands succeed if settings are provided in the environment"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args, 'test_project.settings')
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_bad_settings(self):
- "default: django-admin builtin commands fail if settings file (from argument) doesn't exist"
- args = ['check', '--settings=bad_settings', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_builtin_with_bad_environment(self):
- "default: django-admin builtin commands fail if settings file (from environment) doesn't exist"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args, 'bad_settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_custom_command(self):
- "default: django-admin can't execute user commands if it isn't provided settings"
- args = ['noargs_command']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No Django settings specified")
- self.assertOutput(err, "Unknown command: 'noargs_command'")
- def test_custom_command_with_settings(self):
- "default: django-admin can execute user commands if settings are provided as argument"
- args = ['noargs_command', '--settings=test_project.settings']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- def test_custom_command_with_environment(self):
- "default: django-admin can execute user commands if settings are provided in environment"
- args = ['noargs_command']
- out, err = self.run_django_admin(args, 'test_project.settings')
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- class DjangoAdminFullPathDefaultSettings(AdminScriptTestCase):
- """A series of tests for django-admin.py when using a settings.py file that
- contains the test application specified using a full path.
- """
- def setUp(self):
- 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']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, 'settings are not configured')
- def test_builtin_with_settings(self):
- "fulldefault: django-admin builtin commands succeed if a settings file is provided"
- args = ['check', '--settings=test_project.settings', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_environment(self):
- "fulldefault: django-admin builtin commands succeed if the environment contains settings"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args, 'test_project.settings')
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_bad_settings(self):
- "fulldefault: django-admin builtin commands fail if settings file (from argument) doesn't exist"
- args = ['check', '--settings=bad_settings', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_builtin_with_bad_environment(self):
- "fulldefault: django-admin builtin commands fail if settings file (from environment) doesn't exist"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args, 'bad_settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_custom_command(self):
- "fulldefault: django-admin can't execute user commands unless settings are provided"
- args = ['noargs_command']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No Django settings specified")
- self.assertOutput(err, "Unknown command: 'noargs_command'")
- def test_custom_command_with_settings(self):
- "fulldefault: django-admin can execute user commands if settings are provided as argument"
- args = ['noargs_command', '--settings=test_project.settings']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- def test_custom_command_with_environment(self):
- "fulldefault: django-admin can execute user commands if settings are provided in environment"
- args = ['noargs_command']
- out, err = self.run_django_admin(args, 'test_project.settings')
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- class DjangoAdminMinimalSettings(AdminScriptTestCase):
- """A series of tests for django-admin.py when using a settings.py file that
- doesn't contain the test application.
- """
- def setUp(self):
- 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']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, 'settings are not configured')
- def test_builtin_with_settings(self):
- "minimal: django-admin builtin commands fail if settings are provided as argument"
- args = ['check', '--settings=test_project.settings', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No installed app with label 'admin_scripts'.")
- def test_builtin_with_environment(self):
- "minimal: django-admin builtin commands fail if settings are provided in the environment"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args, 'test_project.settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No installed app with label 'admin_scripts'.")
- def test_builtin_with_bad_settings(self):
- "minimal: django-admin builtin commands fail if settings file (from argument) doesn't exist"
- args = ['check', '--settings=bad_settings', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_builtin_with_bad_environment(self):
- "minimal: django-admin builtin commands fail if settings file (from environment) doesn't exist"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args, 'bad_settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_custom_command(self):
- "minimal: django-admin can't execute user commands unless settings are provided"
- args = ['noargs_command']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No Django settings specified")
- self.assertOutput(err, "Unknown command: 'noargs_command'")
- def test_custom_command_with_settings(self):
- "minimal: django-admin can't execute user commands, even if settings are provided as argument"
- args = ['noargs_command', '--settings=test_project.settings']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "Unknown command: 'noargs_command'")
- def test_custom_command_with_environment(self):
- "minimal: django-admin can't execute user commands, even if settings are provided in environment"
- args = ['noargs_command']
- out, err = self.run_django_admin(args, 'test_project.settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "Unknown command: 'noargs_command'")
- class DjangoAdminAlternateSettings(AdminScriptTestCase):
- """A series of tests for django-admin.py when using a settings file
- with a name other than 'settings.py'.
- """
- def setUp(self):
- 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']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, 'settings are not configured')
- def test_builtin_with_settings(self):
- "alternate: django-admin builtin commands succeed if settings are provided as argument"
- args = ['check', '--settings=test_project.alternate_settings', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_environment(self):
- "alternate: django-admin builtin commands succeed if settings are provided in the environment"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args, 'test_project.alternate_settings')
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_bad_settings(self):
- "alternate: django-admin builtin commands fail if settings file (from argument) doesn't exist"
- args = ['check', '--settings=bad_settings', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_builtin_with_bad_environment(self):
- "alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args, 'bad_settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_custom_command(self):
- "alternate: django-admin can't execute user commands unless settings are provided"
- args = ['noargs_command']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No Django settings specified")
- self.assertOutput(err, "Unknown command: 'noargs_command'")
- def test_custom_command_with_settings(self):
- "alternate: django-admin can execute user commands if settings are provided as argument"
- args = ['noargs_command', '--settings=test_project.alternate_settings']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- def test_custom_command_with_environment(self):
- "alternate: django-admin can execute user commands if settings are provided in environment"
- args = ['noargs_command']
- out, err = self.run_django_admin(args, 'test_project.alternate_settings')
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- class DjangoAdminMultipleSettings(AdminScriptTestCase):
- """A series of tests for django-admin.py when multiple settings files
- (including the default 'settings.py') are available. The default settings
- file is insufficient for performing the operations described, so the
- alternate settings must be used by the running script.
- """
- def setUp(self):
- 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']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, 'settings are not configured')
- def test_builtin_with_settings(self):
- "alternate: django-admin builtin commands succeed if settings are provided as argument"
- args = ['check', '--settings=test_project.alternate_settings', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_environment(self):
- "alternate: django-admin builtin commands succeed if settings are provided in the environment"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args, 'test_project.alternate_settings')
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_bad_settings(self):
- "alternate: django-admin builtin commands fail if settings file (from argument) doesn't exist"
- args = ['check', '--settings=bad_settings', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_builtin_with_bad_environment(self):
- "alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args, 'bad_settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_custom_command(self):
- "alternate: django-admin can't execute user commands unless settings are provided"
- args = ['noargs_command']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No Django settings specified")
- self.assertOutput(err, "Unknown command: 'noargs_command'")
- def test_custom_command_with_settings(self):
- "alternate: django-admin can execute user commands if settings are provided as argument"
- args = ['noargs_command', '--settings=test_project.alternate_settings']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- def test_custom_command_with_environment(self):
- "alternate: django-admin can execute user commands if settings are provided in environment"
- args = ['noargs_command']
- out, err = self.run_django_admin(args, 'test_project.alternate_settings')
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- class DjangoAdminSettingsDirectory(AdminScriptTestCase):
- """
- A series of tests for django-admin.py when the settings file is in a
- directory. (see #9751).
- """
- def setUp(self):
- 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))
- unicode_literals_import = "from __future__ import unicode_literals\n"
- with open(os.path.join(app_path, 'apps.py'), 'r') as f:
- content = f.read()
- self.assertIn("class SettingsTestConfig(AppConfig)", content)
- self.assertIn("name = 'settings_test'", content)
- if not PY3:
- self.assertIn(unicode_literals_import, content)
- if not PY3:
- with open(os.path.join(app_path, 'models.py'), 'r') as fp:
- content = fp.read()
- self.assertIn(unicode_literals_import, content)
- def test_setup_environ_custom_template(self):
- "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(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')))
- @unittest.skipIf(PY2, "Python 2 doesn't support Unicode package names.")
- def test_startapp_unicode_name(self):
- "directory: startapp creates the correct directory with unicode characters"
- 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'), 'r', encoding='utf8') as f:
- content = f.read()
- self.assertIn("class こんにちはConfig(AppConfig)", content)
- self.assertIn("name = 'こんにちは'", content)
- def test_builtin_command(self):
- "directory: django-admin builtin commands fail with an error when no settings provided"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, 'settings are not configured')
- def test_builtin_with_bad_settings(self):
- "directory: django-admin builtin commands fail if settings file (from argument) doesn't exist"
- args = ['check', '--settings=bad_settings', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_builtin_with_bad_environment(self):
- "directory: django-admin builtin commands fail if settings file (from environment) doesn't exist"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args, 'bad_settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_custom_command(self):
- "directory: django-admin can't execute user commands unless settings are provided"
- args = ['noargs_command']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No Django settings specified")
- self.assertOutput(err, "Unknown command: 'noargs_command'")
- def test_builtin_with_settings(self):
- "directory: django-admin builtin commands succeed if settings are provided as argument"
- args = ['check', '--settings=test_project.settings', 'admin_scripts']
- out, err = self.run_django_admin(args)
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_environment(self):
- "directory: django-admin builtin commands succeed if settings are provided in the environment"
- args = ['check', 'admin_scripts']
- out, err = self.run_django_admin(args, 'test_project.settings')
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- ##########################################################################
- # MANAGE.PY TESTS
- # This next series of test classes checks the environment processing
- # of the generated manage.py script
- ##########################################################################
- class ManageNoSettings(AdminScriptTestCase):
- "A series of tests for manage.py when there is no settings.py file."
- def test_builtin_command(self):
- "no settings: manage.py builtin commands fail with an error when no settings provided"
- args = ['check', 'admin_scripts']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?(test_project\.)?settings'?", regex=True)
- def test_builtin_with_bad_settings(self):
- "no settings: manage.py builtin commands fail if settings file (from argument) doesn't exist"
- args = ['check', '--settings=bad_settings', 'admin_scripts']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_builtin_with_bad_environment(self):
- "no settings: manage.py builtin commands fail if settings file (from environment) doesn't exist"
- args = ['check', 'admin_scripts']
- out, err = self.run_manage(args, 'bad_settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- class ManageDefaultSettings(AdminScriptTestCase):
- """A series of tests for manage.py when using a settings.py file that
- contains the test application.
- """
- def setUp(self):
- 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']
- out, err = self.run_manage(args)
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_settings(self):
- "default: manage.py builtin commands succeed if settings are provided as argument"
- args = ['check', '--settings=test_project.settings', 'admin_scripts']
- out, err = self.run_manage(args)
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_environment(self):
- "default: manage.py builtin commands succeed if settings are provided in the environment"
- args = ['check', 'admin_scripts']
- out, err = self.run_manage(args, 'test_project.settings')
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_bad_settings(self):
- "default: manage.py builtin commands succeed if settings file (from argument) doesn't exist"
- args = ['check', '--settings=bad_settings', 'admin_scripts']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_builtin_with_bad_environment(self):
- "default: manage.py builtin commands fail if settings file (from environment) doesn't exist"
- args = ['check', 'admin_scripts']
- out, err = self.run_manage(args, 'bad_settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_custom_command(self):
- "default: manage.py can execute user commands when default settings are appropriate"
- args = ['noargs_command']
- out, err = self.run_manage(args)
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- def test_custom_command_with_settings(self):
- "default: manage.py can execute user commands when settings are provided as argument"
- args = ['noargs_command', '--settings=test_project.settings']
- out, err = self.run_manage(args)
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- def test_custom_command_with_environment(self):
- "default: manage.py can execute user commands when settings are provided in environment"
- args = ['noargs_command']
- out, err = self.run_manage(args, 'test_project.settings')
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- class ManageFullPathDefaultSettings(AdminScriptTestCase):
- """A series of tests for manage.py when using a settings.py file that
- contains the test application specified using a full path.
- """
- def setUp(self):
- 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']
- out, err = self.run_manage(args)
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_settings(self):
- "fulldefault: manage.py builtin commands succeed if settings are provided as argument"
- args = ['check', '--settings=test_project.settings', 'admin_scripts']
- out, err = self.run_manage(args)
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_environment(self):
- "fulldefault: manage.py builtin commands succeed if settings are provided in the environment"
- args = ['check', 'admin_scripts']
- out, err = self.run_manage(args, 'test_project.settings')
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_bad_settings(self):
- "fulldefault: manage.py builtin commands succeed if settings file (from argument) doesn't exist"
- args = ['check', '--settings=bad_settings', 'admin_scripts']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_builtin_with_bad_environment(self):
- "fulldefault: manage.py builtin commands fail if settings file (from environment) doesn't exist"
- args = ['check', 'admin_scripts']
- out, err = self.run_manage(args, 'bad_settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_custom_command(self):
- "fulldefault: manage.py can execute user commands when default settings are appropriate"
- args = ['noargs_command']
- out, err = self.run_manage(args)
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- def test_custom_command_with_settings(self):
- "fulldefault: manage.py can execute user commands when settings are provided as argument"
- args = ['noargs_command', '--settings=test_project.settings']
- out, err = self.run_manage(args)
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- def test_custom_command_with_environment(self):
- "fulldefault: manage.py can execute user commands when settings are provided in environment"
- args = ['noargs_command']
- out, err = self.run_manage(args, 'test_project.settings')
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- class ManageMinimalSettings(AdminScriptTestCase):
- """A series of tests for manage.py when using a settings.py file that
- doesn't contain the test application.
- """
- def setUp(self):
- 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']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No installed app with label 'admin_scripts'.")
- def test_builtin_with_settings(self):
- "minimal: manage.py builtin commands fail if settings are provided as argument"
- args = ['check', '--settings=test_project.settings', 'admin_scripts']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No installed app with label 'admin_scripts'.")
- def test_builtin_with_environment(self):
- "minimal: manage.py builtin commands fail if settings are provided in the environment"
- args = ['check', 'admin_scripts']
- out, err = self.run_manage(args, 'test_project.settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No installed app with label 'admin_scripts'.")
- def test_builtin_with_bad_settings(self):
- "minimal: manage.py builtin commands fail if settings file (from argument) doesn't exist"
- args = ['check', '--settings=bad_settings', 'admin_scripts']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_builtin_with_bad_environment(self):
- "minimal: manage.py builtin commands fail if settings file (from environment) doesn't exist"
- args = ['check', 'admin_scripts']
- out, err = self.run_manage(args, 'bad_settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_custom_command(self):
- "minimal: manage.py can't execute user commands without appropriate settings"
- args = ['noargs_command']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "Unknown command: 'noargs_command'")
- def test_custom_command_with_settings(self):
- "minimal: manage.py can't execute user commands, even if settings are provided as argument"
- args = ['noargs_command', '--settings=test_project.settings']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "Unknown command: 'noargs_command'")
- def test_custom_command_with_environment(self):
- "minimal: manage.py can't execute user commands, even if settings are provided in environment"
- args = ['noargs_command']
- out, err = self.run_manage(args, 'test_project.settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "Unknown command: 'noargs_command'")
- class ManageAlternateSettings(AdminScriptTestCase):
- """A series of tests for manage.py when using a settings file
- with a name other than 'settings.py'.
- """
- def setUp(self):
- 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']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?(test_project\.)?settings'?", regex=True)
- def test_builtin_with_settings(self):
- "alternate: manage.py builtin commands work with settings provided as argument"
- args = ['check', '--settings=alternate_settings', 'admin_scripts']
- out, err = self.run_manage(args)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- self.assertNoOutput(err)
- def test_builtin_with_environment(self):
- "alternate: manage.py builtin commands work if settings are provided in the environment"
- args = ['check', 'admin_scripts']
- out, err = self.run_manage(args, 'alternate_settings')
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- self.assertNoOutput(err)
- def test_builtin_with_bad_settings(self):
- "alternate: manage.py builtin commands fail if settings file (from argument) doesn't exist"
- args = ['check', '--settings=bad_settings', 'admin_scripts']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_builtin_with_bad_environment(self):
- "alternate: manage.py builtin commands fail if settings file (from environment) doesn't exist"
- args = ['check', 'admin_scripts']
- out, err = self.run_manage(args, 'bad_settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_custom_command(self):
- "alternate: manage.py can't execute user commands without settings"
- args = ['noargs_command']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?(test_project\.)?settings'?", regex=True)
- def test_custom_command_with_settings(self):
- "alternate: manage.py can execute user commands if settings are provided as argument"
- args = ['noargs_command', '--settings=alternate_settings']
- out, err = self.run_manage(args)
- self.assertOutput(
- out,
- "EXECUTE: noargs_command options=[('no_color', False), "
- "('pythonpath', None), ('settings', 'alternate_settings'), "
- "('traceback', False), ('verbosity', 1)]"
- )
- self.assertNoOutput(err)
- def test_custom_command_with_environment(self):
- "alternate: manage.py can execute user commands if settings are provided in environment"
- args = ['noargs_command']
- out, err = self.run_manage(args, 'alternate_settings')
- self.assertOutput(
- out,
- "EXECUTE: noargs_command options=[('no_color', False), "
- "('pythonpath', None), ('settings', None), ('traceback', False), "
- "('verbosity', 1)]"
- )
- self.assertNoOutput(err)
- def test_custom_command_output_color(self):
- "alternate: manage.py output syntax color can be deactivated with the `--no-color` option"
- args = ['noargs_command', '--no-color', '--settings=alternate_settings']
- out, err = self.run_manage(args)
- self.assertOutput(
- out,
- "EXECUTE: noargs_command options=[('no_color', True), "
- "('pythonpath', None), ('settings', 'alternate_settings'), "
- "('traceback', False), ('verbosity', 1)]"
- )
- self.assertNoOutput(err)
- class ManageMultipleSettings(AdminScriptTestCase):
- """A series of tests for manage.py when multiple settings files
- (including the default 'settings.py') are available. The default settings
- file is insufficient for performing the operations described, so the
- alternate settings must be used by the running script.
- """
- def setUp(self):
- 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']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No installed app with label 'admin_scripts'.")
- def test_builtin_with_settings(self):
- "multiple: manage.py builtin commands succeed if settings are provided as argument"
- args = ['check', '--settings=alternate_settings', 'admin_scripts']
- out, err = self.run_manage(args)
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_environment(self):
- "multiple: manage.py can execute builtin commands if settings are provided in the environment"
- args = ['check', 'admin_scripts']
- out, err = self.run_manage(args, 'alternate_settings')
- self.assertNoOutput(err)
- self.assertOutput(out, SYSTEM_CHECK_MSG)
- def test_builtin_with_bad_settings(self):
- "multiple: manage.py builtin commands fail if settings file (from argument) doesn't exist"
- args = ['check', '--settings=bad_settings', 'admin_scripts']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_builtin_with_bad_environment(self):
- "multiple: manage.py builtin commands fail if settings file (from environment) doesn't exist"
- args = ['check', 'admin_scripts']
- out, err = self.run_manage(args, 'bad_settings')
- self.assertNoOutput(out)
- self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
- def test_custom_command(self):
- "multiple: manage.py can't execute user commands using default settings"
- args = ['noargs_command']
- out, err = self.run_manage(args)
- self.assertNoOutput(out)
- self.assertOutput(err, "Unknown command: 'noargs_command'")
- def test_custom_command_with_settings(self):
- "multiple: manage.py can execute user commands if settings are provided as argument"
- args = ['noargs_command', '--settings=alternate_settings']
- out, err = self.run_manage(args)
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- def test_custom_command_with_environment(self):
- "multiple: manage.py can execute user commands if settings are provided in environment"
- args = ['noargs_command']
- out, err = self.run_manage(args, 'alternate_settings')
- self.assertNoOutput(err)
- self.assertOutput(out, "EXECUTE: noargs_command")
- 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:
- 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')
- def test_import_error(self):
- """
- import error: manage.py builtin commands shows useful diagnostic info
- when settings wit