2008-07-10 20:12:41 +08:00
|
|
|
"""
|
2014-03-02 22:25:53 +08:00
|
|
|
A series of tests to establish that the command-line management tools work as
|
2015-01-28 20:35:27 +08:00
|
|
|
advertised - especially with regards to the handling of the
|
|
|
|
DJANGO_SETTINGS_MODULE and default settings.py files.
|
2008-07-10 20:12:41 +08:00
|
|
|
"""
|
2013-07-01 20:22:27 +08:00
|
|
|
import codecs
|
2008-07-10 20:12:41 +08:00
|
|
|
import os
|
2011-12-23 06:38:02 +08:00
|
|
|
import re
|
2008-07-10 20:12:41 +08:00
|
|
|
import shutil
|
2011-12-10 05:50:28 +08:00
|
|
|
import socket
|
2011-12-27 22:54:00 +08:00
|
|
|
import subprocess
|
2008-07-23 03:48:36 +08:00
|
|
|
import sys
|
2015-02-22 01:56:36 +08:00
|
|
|
import tempfile
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2017-01-07 19:11:46 +08:00
|
|
|
from io import StringIO
|
2017-01-20 01:16:04 +08:00
|
|
|
from unittest import mock
|
2008-07-10 20:12:41 +08:00
|
|
|
|
2013-05-01 22:40:49 +08:00
|
|
|
import django
|
|
|
|
from django import conf, get_version
|
2008-07-10 20:12:41 +08:00
|
|
|
from django.conf import settings
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.management import (
|
|
|
|
BaseCommand, CommandError, call_command, color,
|
|
|
|
)
|
2017-12-21 02:08:45 +08:00
|
|
|
from django.core.management.commands.loaddata import Command as LoaddataCommand
|
2017-12-21 03:47:17 +08:00
|
|
|
from django.core.management.commands.runserver import (
|
|
|
|
Command as RunserverCommand,
|
|
|
|
)
|
|
|
|
from django.core.management.commands.testserver import (
|
|
|
|
Command as TestserverCommand,
|
|
|
|
)
|
2017-12-21 02:08:45 +08:00
|
|
|
from django.db import ConnectionHandler, connection
|
2015-05-09 23:01:40 +08:00
|
|
|
from django.db.migrations.recorder import MigrationRecorder
|
2015-04-18 05:38:20 +08:00
|
|
|
from django.test import (
|
2017-01-20 01:16:04 +08:00
|
|
|
LiveServerTestCase, SimpleTestCase, TestCase, override_settings,
|
2015-04-18 05:38:20 +08:00
|
|
|
)
|
2017-10-12 23:08:05 +08:00
|
|
|
from django.utils.version import PY36
|
2013-02-26 20:19:18 +08:00
|
|
|
|
2017-01-20 21:01:02 +08:00
|
|
|
custom_templates_dir = os.path.join(os.path.dirname(__file__), 'custom_templates')
|
2015-02-06 04:29:11 +08:00
|
|
|
|
2014-12-27 07:23:00 +08:00
|
|
|
SYSTEM_CHECK_MSG = 'System check identified no issues'
|
2013-02-26 20:19:18 +08:00
|
|
|
|
2010-12-04 15:28:12 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
class AdminScriptTestCase(unittest.TestCase):
|
2015-02-06 04:29:11 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUpClass()
|
2015-02-06 04:29:11 +08:00
|
|
|
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)
|
2017-01-21 21:13:44 +08:00
|
|
|
super().tearDownClass()
|
2015-02-06 04:29:11 +08:00
|
|
|
|
2014-02-18 17:19:04 +08:00
|
|
|
def write_settings(self, filename, apps=None, is_dir=False, sdict=None, extra=None):
|
2009-05-13 05:45:03 +08:00
|
|
|
if is_dir:
|
2015-02-06 04:29:11 +08:00
|
|
|
settings_dir = os.path.join(self.test_dir, filename)
|
2009-05-13 05:45:03 +08:00
|
|
|
os.mkdir(settings_dir)
|
2012-05-05 20:01:38 +08:00
|
|
|
settings_file_path = os.path.join(settings_dir, '__init__.py')
|
2009-05-13 05:45:03 +08:00
|
|
|
else:
|
2015-02-06 04:29:11 +08:00
|
|
|
settings_file_path = os.path.join(self.test_dir, filename)
|
2012-05-05 20:01:38 +08:00
|
|
|
|
|
|
|
with open(settings_file_path, 'w') as settings_file:
|
2013-02-26 20:19:18 +08:00
|
|
|
settings_file.write('# Settings file automatically generated by admin_scripts test case\n')
|
2014-02-18 17:19:04 +08:00
|
|
|
if extra:
|
|
|
|
settings_file.write("%s\n" % extra)
|
2012-05-05 20:01:38 +08:00
|
|
|
exports = [
|
|
|
|
'DATABASES',
|
|
|
|
'ROOT_URLCONF',
|
|
|
|
'SECRET_KEY',
|
|
|
|
]
|
|
|
|
for s in exports:
|
|
|
|
if hasattr(settings, s):
|
|
|
|
o = getattr(settings, s)
|
2014-04-20 20:58:29 +08:00
|
|
|
if not isinstance(o, (dict, tuple, list)):
|
2012-05-05 20:01:38 +08:00
|
|
|
o = "'%s'" % o
|
|
|
|
settings_file.write("%s = %s\n" % (s, o))
|
|
|
|
|
|
|
|
if apps is None:
|
2013-02-26 20:19:18 +08:00
|
|
|
apps = ['django.contrib.auth', 'django.contrib.contenttypes', 'admin_scripts']
|
2012-05-05 20:01:38 +08:00
|
|
|
|
|
|
|
settings_file.write("INSTALLED_APPS = %s\n" % apps)
|
|
|
|
|
|
|
|
if sdict:
|
|
|
|
for k, v in sdict.items():
|
|
|
|
settings_file.write("%s = %s\n" % (k, v))
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2009-05-13 05:45:03 +08:00
|
|
|
def remove_settings(self, filename, is_dir=False):
|
2015-02-06 04:29:11 +08:00
|
|
|
full_name = os.path.join(self.test_dir, filename)
|
2009-05-13 05:45:03 +08:00
|
|
|
if is_dir:
|
|
|
|
shutil.rmtree(full_name)
|
|
|
|
else:
|
|
|
|
os.remove(full_name)
|
2008-08-16 10:29:10 +08:00
|
|
|
|
2017-12-21 23:31:59 +08:00
|
|
|
# Also remove a __pycache__ directory, if it exists; it could
|
2008-07-10 20:12:41 +08:00
|
|
|
# mess up later tests that depend upon the .py file not existing
|
2015-02-06 04:29:11 +08:00
|
|
|
cache_name = os.path.join(self.test_dir, '__pycache__')
|
2012-08-13 16:58:21 +08:00
|
|
|
if os.path.isdir(cache_name):
|
|
|
|
shutil.rmtree(cache_name)
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _ext_backend_paths(self):
|
2008-08-09 02:48:18 +08:00
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
Returns the paths for any external backend packages.
|
2008-08-09 02:48:18 +08:00
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
paths = []
|
|
|
|
for backend in settings.DATABASES.values():
|
2017-05-10 21:47:20 +08:00
|
|
|
package = backend['ENGINE'].split('.')[0]
|
|
|
|
if package != 'django':
|
|
|
|
backend_pkg = __import__(package)
|
2009-12-22 23:18:51 +08:00
|
|
|
backend_dir = os.path.dirname(backend_pkg.__file__)
|
|
|
|
paths.append(os.path.dirname(backend_dir))
|
|
|
|
return paths
|
2008-08-16 10:29:10 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def run_test(self, script, args, settings_file=None, apps=None):
|
2015-02-06 04:29:11 +08:00
|
|
|
base_dir = os.path.dirname(self.test_dir)
|
2013-02-27 04:04:32 +08:00
|
|
|
# The base dir for Django's tests is one level up.
|
2017-01-20 21:01:02 +08:00
|
|
|
tests_dir = os.path.dirname(os.path.dirname(__file__))
|
2013-02-27 04:04:32 +08:00
|
|
|
# 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)
|
2009-12-22 23:18:51 +08:00
|
|
|
ext_backend_base_dirs = self._ext_backend_paths()
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2014-03-08 19:32:57 +08:00
|
|
|
# Define a temporary environment for the subprocess
|
|
|
|
test_environ = os.environ.copy()
|
2008-07-10 21:53:45 +08:00
|
|
|
old_cwd = os.getcwd()
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
# Set the test environment
|
|
|
|
if settings_file:
|
2017-01-20 17:20:53 +08:00
|
|
|
test_environ['DJANGO_SETTINGS_MODULE'] = settings_file
|
2014-03-08 19:32:57 +08:00
|
|
|
elif 'DJANGO_SETTINGS_MODULE' in test_environ:
|
|
|
|
del test_environ['DJANGO_SETTINGS_MODULE']
|
2013-02-26 22:38:11 +08:00
|
|
|
python_path = [base_dir, django_dir, tests_dir]
|
2009-12-22 23:18:51 +08:00
|
|
|
python_path.extend(ext_backend_base_dirs)
|
2017-12-21 23:31:59 +08:00
|
|
|
test_environ['PYTHONPATH'] = os.pathsep.join(python_path)
|
2017-01-20 17:20:53 +08:00
|
|
|
test_environ['PYTHONWARNINGS'] = ''
|
2008-07-11 21:04:17 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
# Move to the test directory and run
|
2015-02-06 04:29:11 +08:00
|
|
|
os.chdir(self.test_dir)
|
2016-04-08 10:04:45 +08:00
|
|
|
out, err = subprocess.Popen(
|
|
|
|
[sys.executable, script] + args,
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
|
|
env=test_environ, universal_newlines=True,
|
|
|
|
).communicate()
|
2008-07-10 21:53:45 +08:00
|
|
|
# Move back to the old working directory
|
|
|
|
os.chdir(old_cwd)
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
return out, err
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def run_django_admin(self, args, settings_file=None):
|
2017-01-20 21:01:02 +08:00
|
|
|
script_dir = os.path.abspath(os.path.join(os.path.dirname(django.__file__), 'bin'))
|
2013-05-01 22:40:49 +08:00
|
|
|
return self.run_test(os.path.join(script_dir, 'django-admin.py'), args, settings_file)
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def run_manage(self, args, settings_file=None):
|
2012-10-30 02:08:07 +08:00
|
|
|
def safe_remove(path):
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2012-10-30 02:08:07 +08:00
|
|
|
os.remove(path)
|
2017-09-07 20:16:21 +08:00
|
|
|
except OSError:
|
|
|
|
pass
|
2012-10-30 02:08:07 +08:00
|
|
|
|
2017-01-20 21:01:02 +08:00
|
|
|
conf_dir = os.path.dirname(conf.__file__)
|
2016-01-24 17:06:01 +08:00
|
|
|
template_manage_py = os.path.join(conf_dir, 'project_template', 'manage.py-tpl')
|
2008-07-10 20:12:41 +08:00
|
|
|
|
2015-02-06 04:29:11 +08:00
|
|
|
test_manage_py = os.path.join(self.test_dir, 'manage.py')
|
2008-07-10 20:12:41 +08:00
|
|
|
shutil.copyfile(template_manage_py, test_manage_py)
|
|
|
|
|
2011-10-13 13:56:15 +08:00
|
|
|
with open(test_manage_py, 'r') as fp:
|
|
|
|
manage_py_contents = fp.read()
|
|
|
|
manage_py_contents = manage_py_contents.replace(
|
2013-02-26 20:19:18 +08:00
|
|
|
"{{ project_name }}", "test_project")
|
2011-10-13 13:56:15 +08:00
|
|
|
with open(test_manage_py, 'w') as fp:
|
|
|
|
fp.write(manage_py_contents)
|
2012-10-30 02:08:07 +08:00
|
|
|
self.addCleanup(safe_remove, test_manage_py)
|
2011-10-13 13:56:15 +08:00
|
|
|
|
2012-10-30 02:08:07 +08:00
|
|
|
return self.run_test('./manage.py', args, settings_file)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def assertNoOutput(self, stream):
|
|
|
|
"Utility assertion: assert that the given stream is empty"
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(len(stream), 0, "Stream should be empty: actually contains '%s'" % stream)
|
2011-05-24 02:54:02 +08:00
|
|
|
|
2014-09-17 16:43:45 +08:00
|
|
|
def assertOutput(self, stream, msg, regex=False):
|
2008-07-10 20:12:41 +08:00
|
|
|
"Utility assertion: assert that the given message exists in the output"
|
2014-09-17 16:43:45 +08:00
|
|
|
if regex:
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertIsNotNone(
|
|
|
|
re.search(msg, stream),
|
|
|
|
"'%s' does not match actual output text '%s'" % (msg, stream)
|
|
|
|
)
|
2014-09-17 16:43:45 +08:00
|
|
|
else:
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn(msg, stream, "'%s' does not match actual output text '%s'" % (msg, stream))
|
2008-07-10 20:12:41 +08:00
|
|
|
|
2012-02-08 02:46:29 +08:00
|
|
|
def assertNotInOutput(self, stream, msg):
|
|
|
|
"Utility assertion: assert that the given message doesn't exist in the output"
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertNotIn(msg, stream, "'%s' matches actual output text '%s'" % (msg, stream))
|
2012-02-08 02:46:29 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
##########################################################################
|
|
|
|
# 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."
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_command(self):
|
2012-09-09 04:12:18 +08:00
|
|
|
"no settings: django-admin builtin commands fail with an error when no settings provided"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2012-09-09 04:12:18 +08:00
|
|
|
self.assertOutput(err, 'settings are not configured')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_with_bad_settings(self):
|
|
|
|
"no settings: django-admin builtin commands fail if settings file (from argument) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=bad_settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_environment(self):
|
|
|
|
"no settings: django-admin builtin commands fail if settings file (from environment) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_django_admin(args, 'bad_settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
2017-11-20 15:02:14 +08:00
|
|
|
def test_commands_with_invalid_settings(self):
|
|
|
|
""""
|
|
|
|
Commands that don't require settings succeed if the settings file
|
|
|
|
doesn't exist.
|
|
|
|
"""
|
|
|
|
args = ['startproject']
|
|
|
|
out, err = self.run_django_admin(args, settings_file='bad_settings')
|
|
|
|
self.assertNoOutput(out)
|
|
|
|
self.assertOutput(err, "You must provide a project name", regex=True)
|
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
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')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_command(self):
|
2012-09-09 04:12:18 +08:00
|
|
|
"default: django-admin builtin commands fail with an error when no settings provided"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2012-09-09 04:12:18 +08:00
|
|
|
self.assertOutput(err, 'settings are not configured')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_with_settings(self):
|
|
|
|
"default: django-admin builtin commands succeed if settings are provided as argument"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=test_project.settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_environment(self):
|
|
|
|
"default: django-admin builtin commands succeed if settings are provided in the environment"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_django_admin(args, 'test_project.settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_settings(self):
|
|
|
|
"default: django-admin builtin commands fail if settings file (from argument) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=bad_settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_environment(self):
|
|
|
|
"default: django-admin builtin commands fail if settings file (from environment) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_django_admin(args, 'bad_settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_custom_command(self):
|
2008-08-10 16:42:49 +08:00
|
|
|
"default: django-admin can't execute user commands if it isn't provided settings"
|
2008-07-10 20:12:41 +08:00
|
|
|
args = ['noargs_command']
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2015-08-19 17:58:16 +08:00
|
|
|
self.assertOutput(err, "No Django settings specified")
|
|
|
|
self.assertOutput(err, "Unknown command: 'noargs_command'")
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_custom_command_with_settings(self):
|
2008-08-10 16:42:49 +08:00
|
|
|
"default: django-admin can execute user commands if settings are provided as argument"
|
2013-02-26 20:19:18 +08:00
|
|
|
args = ['noargs_command', '--settings=test_project.settings']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
2008-08-10 16:42:49 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_custom_command_with_environment(self):
|
2008-08-10 16:42:49 +08:00
|
|
|
"default: django-admin can execute user commands if settings are provided in environment"
|
2008-07-10 20:12:41 +08:00
|
|
|
args = ['noargs_command']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_django_admin(args, 'test_project.settings')
|
2008-08-10 16:42:49 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2008-08-08 20:27:40 +08:00
|
|
|
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):
|
2014-08-05 08:30:00 +08:00
|
|
|
self.write_settings('settings.py', ['django.contrib.auth', 'django.contrib.contenttypes',
|
|
|
|
'admin_scripts', 'admin_scripts.complex_app'])
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
|
|
|
|
|
|
|
def test_builtin_command(self):
|
2012-09-09 04:12:18 +08:00
|
|
|
"fulldefault: django-admin builtin commands fail with an error when no settings provided"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2008-08-08 20:27:40 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2012-09-09 04:12:18 +08:00
|
|
|
self.assertOutput(err, 'settings are not configured')
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def test_builtin_with_settings(self):
|
2008-08-10 16:42:49 +08:00
|
|
|
"fulldefault: django-admin builtin commands succeed if a settings file is provided"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=test_project.settings', 'admin_scripts']
|
2008-08-08 20:27:40 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
2008-08-10 16:42:49 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def test_builtin_with_environment(self):
|
2008-08-10 16:42:49 +08:00
|
|
|
"fulldefault: django-admin builtin commands succeed if the environment contains settings"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_django_admin(args, 'test_project.settings')
|
2008-08-10 16:42:49 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_settings(self):
|
|
|
|
"fulldefault: django-admin builtin commands fail if settings file (from argument) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=bad_settings', 'admin_scripts']
|
2008-08-08 20:27:40 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_environment(self):
|
|
|
|
"fulldefault: django-admin builtin commands fail if settings file (from environment) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_django_admin(args, 'bad_settings')
|
2008-08-08 20:27:40 +08:00
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def test_custom_command(self):
|
2008-08-10 16:42:49 +08:00
|
|
|
"fulldefault: django-admin can't execute user commands unless settings are provided"
|
2008-08-08 20:27:40 +08:00
|
|
|
args = ['noargs_command']
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2015-08-19 17:58:16 +08:00
|
|
|
self.assertOutput(err, "No Django settings specified")
|
|
|
|
self.assertOutput(err, "Unknown command: 'noargs_command'")
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def test_custom_command_with_settings(self):
|
2008-08-10 16:42:49 +08:00
|
|
|
"fulldefault: django-admin can execute user commands if settings are provided as argument"
|
2013-02-26 20:19:18 +08:00
|
|
|
args = ['noargs_command', '--settings=test_project.settings']
|
2008-08-08 20:27:40 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
2008-08-10 16:42:49 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def test_custom_command_with_environment(self):
|
2008-08-10 16:42:49 +08:00
|
|
|
"fulldefault: django-admin can execute user commands if settings are provided in environment"
|
2008-08-08 20:27:40 +08:00
|
|
|
args = ['noargs_command']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_django_admin(args, 'test_project.settings')
|
2008-08-10 16:42:49 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-08-08 20:27:40 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
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):
|
2011-12-23 06:38:02 +08:00
|
|
|
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_command(self):
|
2012-09-09 04:12:18 +08:00
|
|
|
"minimal: django-admin builtin commands fail with an error when no settings provided"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2012-09-09 04:12:18 +08:00
|
|
|
self.assertOutput(err, 'settings are not configured')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_with_settings(self):
|
|
|
|
"minimal: django-admin builtin commands fail if settings are provided as argument"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=test_project.settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2013-12-18 20:10:23 +08:00
|
|
|
self.assertOutput(err, "No installed app with label 'admin_scripts'.")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_environment(self):
|
|
|
|
"minimal: django-admin builtin commands fail if settings are provided in the environment"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_django_admin(args, 'test_project.settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(out)
|
2013-12-18 20:10:23 +08:00
|
|
|
self.assertOutput(err, "No installed app with label 'admin_scripts'.")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_settings(self):
|
|
|
|
"minimal: django-admin builtin commands fail if settings file (from argument) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=bad_settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_environment(self):
|
|
|
|
"minimal: django-admin builtin commands fail if settings file (from environment) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_django_admin(args, 'bad_settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_custom_command(self):
|
2008-08-10 16:42:49 +08:00
|
|
|
"minimal: django-admin can't execute user commands unless settings are provided"
|
2008-07-10 20:12:41 +08:00
|
|
|
args = ['noargs_command']
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2015-08-19 17:58:16 +08:00
|
|
|
self.assertOutput(err, "No Django settings specified")
|
|
|
|
self.assertOutput(err, "Unknown command: 'noargs_command'")
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_custom_command_with_settings(self):
|
|
|
|
"minimal: django-admin can't execute user commands, even if settings are provided as argument"
|
2013-02-26 20:19:18 +08:00
|
|
|
args = ['noargs_command', '--settings=test_project.settings']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
|
|
|
self.assertOutput(err, "Unknown command: 'noargs_command'")
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
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']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_django_admin(args, 'test_project.settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(out)
|
|
|
|
self.assertOutput(err, "Unknown command: 'noargs_command'")
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
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')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('alternate_settings.py')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_command(self):
|
2012-09-09 04:12:18 +08:00
|
|
|
"alternate: django-admin builtin commands fail with an error when no settings provided"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2012-09-09 04:12:18 +08:00
|
|
|
self.assertOutput(err, 'settings are not configured')
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_settings(self):
|
|
|
|
"alternate: django-admin builtin commands succeed if settings are provided as argument"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=test_project.alternate_settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_environment(self):
|
|
|
|
"alternate: django-admin builtin commands succeed if settings are provided in the environment"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_django_admin(args, 'test_project.alternate_settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_settings(self):
|
|
|
|
"alternate: django-admin builtin commands fail if settings file (from argument) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=bad_settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_environment(self):
|
|
|
|
"alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_django_admin(args, 'bad_settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
2008-08-16 10:29:10 +08:00
|
|
|
def test_custom_command(self):
|
2008-08-10 16:42:49 +08:00
|
|
|
"alternate: django-admin can't execute user commands unless settings are provided"
|
2008-07-10 20:12:41 +08:00
|
|
|
args = ['noargs_command']
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2015-08-19 17:58:16 +08:00
|
|
|
self.assertOutput(err, "No Django settings specified")
|
|
|
|
self.assertOutput(err, "Unknown command: 'noargs_command'")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_custom_command_with_settings(self):
|
2008-08-10 16:42:49 +08:00
|
|
|
"alternate: django-admin can execute user commands if settings are provided as argument"
|
2013-02-26 20:19:18 +08:00
|
|
|
args = ['noargs_command', '--settings=test_project.alternate_settings']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
2008-08-10 16:42:49 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_custom_command_with_environment(self):
|
2008-08-10 16:42:49 +08:00
|
|
|
"alternate: django-admin can execute user commands if settings are provided in environment"
|
2008-07-10 20:12:41 +08:00
|
|
|
args = ['noargs_command']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_django_admin(args, 'test_project.alternate_settings')
|
2008-08-10 16:42:49 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2008-07-28 02:25:06 +08:00
|
|
|
file is insufficient for performing the operations described, so the
|
2008-07-10 20:12:41 +08:00
|
|
|
alternate settings must be used by the running script.
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
2011-12-23 06:38:02 +08:00
|
|
|
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
|
2008-07-10 20:12:41 +08:00
|
|
|
self.write_settings('alternate_settings.py')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
|
|
|
self.remove_settings('alternate_settings.py')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_command(self):
|
2012-09-09 04:12:18 +08:00
|
|
|
"alternate: django-admin builtin commands fail with an error when no settings provided"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2012-09-09 04:12:18 +08:00
|
|
|
self.assertOutput(err, 'settings are not configured')
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_settings(self):
|
|
|
|
"alternate: django-admin builtin commands succeed if settings are provided as argument"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=test_project.alternate_settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_environment(self):
|
|
|
|
"alternate: django-admin builtin commands succeed if settings are provided in the environment"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_django_admin(args, 'test_project.alternate_settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_settings(self):
|
|
|
|
"alternate: django-admin builtin commands fail if settings file (from argument) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=bad_settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_environment(self):
|
|
|
|
"alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_django_admin(args, 'bad_settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
2008-08-16 10:29:10 +08:00
|
|
|
def test_custom_command(self):
|
2008-08-10 16:42:49 +08:00
|
|
|
"alternate: django-admin can't execute user commands unless settings are provided"
|
2008-07-10 20:12:41 +08:00
|
|
|
args = ['noargs_command']
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2015-08-19 17:58:16 +08:00
|
|
|
self.assertOutput(err, "No Django settings specified")
|
|
|
|
self.assertOutput(err, "Unknown command: 'noargs_command'")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_custom_command_with_settings(self):
|
2011-10-13 13:56:15 +08:00
|
|
|
"alternate: django-admin can execute user commands if settings are provided as argument"
|
2013-02-26 20:19:18 +08:00
|
|
|
args = ['noargs_command', '--settings=test_project.alternate_settings']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
2008-08-10 16:42:49 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_custom_command_with_environment(self):
|
2011-10-13 13:56:15 +08:00
|
|
|
"alternate: django-admin can execute user commands if settings are provided in environment"
|
2008-07-10 20:12:41 +08:00
|
|
|
args = ['noargs_command']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_django_admin(args, 'test_project.alternate_settings')
|
2008-08-10 16:42:49 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2009-05-13 05:45:03 +08:00
|
|
|
|
|
|
|
class DjangoAdminSettingsDirectory(AdminScriptTestCase):
|
|
|
|
"""
|
|
|
|
A series of tests for django-admin.py when the settings file is in a
|
|
|
|
directory. (see #9751).
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2009-05-13 05:45:03 +08:00
|
|
|
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"
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['startapp', 'settings_test']
|
2015-02-06 04:29:11 +08:00
|
|
|
app_path = os.path.join(self.test_dir, 'settings_test')
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_django_admin(args, 'test_project.settings')
|
2011-12-23 06:38:02 +08:00
|
|
|
self.addCleanup(shutil.rmtree, app_path)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertTrue(os.path.exists(app_path))
|
2015-06-13 21:41:56 +08:00
|
|
|
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)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
def test_setup_environ_custom_template(self):
|
|
|
|
"directory: startapp creates the correct directory with a custom template"
|
2013-02-26 20:19:18 +08:00
|
|
|
template_path = os.path.join(custom_templates_dir, 'app_template')
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['startapp', '--template', template_path, 'custom_settings_test']
|
2015-02-06 04:29:11 +08:00
|
|
|
app_path = os.path.join(self.test_dir, 'custom_settings_test')
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_django_admin(args, 'test_project.settings')
|
2011-05-24 02:54:02 +08:00
|
|
|
self.addCleanup(shutil.rmtree, app_path)
|
2009-05-13 05:45:03 +08:00
|
|
|
self.assertNoOutput(err)
|
2011-05-24 02:54:02 +08:00
|
|
|
self.assertTrue(os.path.exists(app_path))
|
2011-12-23 06:38:02 +08:00
|
|
|
self.assertTrue(os.path.exists(os.path.join(app_path, 'api.py')))
|
2009-05-13 05:45:03 +08:00
|
|
|
|
2015-10-22 19:33:59 +08:00
|
|
|
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))
|
2015-10-23 07:37:03 +08:00
|
|
|
with open(os.path.join(app_path, 'apps.py'), 'r', encoding='utf8') as f:
|
2015-10-22 19:33:59 +08:00
|
|
|
content = f.read()
|
|
|
|
self.assertIn("class こんにちはConfig(AppConfig)", content)
|
|
|
|
self.assertIn("name = 'こんにちは'", content)
|
|
|
|
|
2009-05-13 05:45:03 +08:00
|
|
|
def test_builtin_command(self):
|
2012-09-09 04:12:18 +08:00
|
|
|
"directory: django-admin builtin commands fail with an error when no settings provided"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2009-05-13 05:45:03 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2012-09-09 04:12:18 +08:00
|
|
|
self.assertOutput(err, 'settings are not configured')
|
2009-05-13 05:45:03 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_settings(self):
|
|
|
|
"directory: django-admin builtin commands fail if settings file (from argument) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=bad_settings', 'admin_scripts']
|
2009-05-13 05:45:03 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2009-05-13 05:45:03 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_environment(self):
|
|
|
|
"directory: django-admin builtin commands fail if settings file (from environment) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_django_admin(args, 'bad_settings')
|
2009-05-13 05:45:03 +08:00
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2009-05-13 05:45:03 +08:00
|
|
|
|
|
|
|
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)
|
2015-08-19 17:58:16 +08:00
|
|
|
self.assertOutput(err, "No Django settings specified")
|
|
|
|
self.assertOutput(err, "Unknown command: 'noargs_command'")
|
2009-05-13 05:45:03 +08:00
|
|
|
|
|
|
|
def test_builtin_with_settings(self):
|
|
|
|
"directory: django-admin builtin commands succeed if settings are provided as argument"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=test_project.settings', 'admin_scripts']
|
2009-05-13 05:45:03 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2009-05-13 05:45:03 +08:00
|
|
|
|
|
|
|
def test_builtin_with_environment(self):
|
|
|
|
"directory: django-admin builtin commands succeed if settings are provided in the environment"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_django_admin(args, 'test_project.settings')
|
2009-05-13 05:45:03 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2009-05-13 05:45:03 +08:00
|
|
|
|
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
##########################################################################
|
|
|
|
# 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."
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_command(self):
|
2012-09-09 04:12:18 +08:00
|
|
|
"no settings: manage.py builtin commands fail with an error when no settings provided"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
2016-09-17 00:15:00 +08:00
|
|
|
self.assertOutput(err, r"No module named '?(test_project\.)?settings'?", regex=True)
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_with_bad_settings(self):
|
|
|
|
"no settings: manage.py builtin commands fail if settings file (from argument) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=bad_settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_environment(self):
|
|
|
|
"no settings: manage.py builtin commands fail if settings file (from environment) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_manage(args, 'bad_settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
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')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_command(self):
|
|
|
|
"default: manage.py builtin commands succeed when default settings are appropriate"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_with_settings(self):
|
|
|
|
"default: manage.py builtin commands succeed if settings are provided as argument"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=test_project.settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_environment(self):
|
|
|
|
"default: manage.py builtin commands succeed if settings are provided in the environment"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_manage(args, 'test_project.settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_settings(self):
|
|
|
|
"default: manage.py builtin commands succeed if settings file (from argument) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=bad_settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_environment(self):
|
|
|
|
"default: manage.py builtin commands fail if settings file (from environment) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_manage(args, 'bad_settings')
|
2011-05-13 20:15:52 +08:00
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
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)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_custom_command_with_settings(self):
|
|
|
|
"default: manage.py can execute user commands when settings are provided as argument"
|
2013-02-26 20:19:18 +08:00
|
|
|
args = ['noargs_command', '--settings=test_project.settings']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_custom_command_with_environment(self):
|
|
|
|
"default: manage.py can execute user commands when settings are provided in environment"
|
|
|
|
args = ['noargs_command']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_manage(args, 'test_project.settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
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):
|
2013-02-26 20:19:18 +08:00
|
|
|
self.write_settings('settings.py', ['django.contrib.auth', 'django.contrib.contenttypes', 'admin_scripts'])
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
|
|
|
|
|
|
|
def test_builtin_command(self):
|
|
|
|
"fulldefault: manage.py builtin commands succeed when default settings are appropriate"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2008-08-08 20:27:40 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def test_builtin_with_settings(self):
|
|
|
|
"fulldefault: manage.py builtin commands succeed if settings are provided as argument"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=test_project.settings', 'admin_scripts']
|
2008-08-08 20:27:40 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def test_builtin_with_environment(self):
|
|
|
|
"fulldefault: manage.py builtin commands succeed if settings are provided in the environment"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_manage(args, 'test_project.settings')
|
2008-08-08 20:27:40 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_settings(self):
|
|
|
|
"fulldefault: manage.py builtin commands succeed if settings file (from argument) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=bad_settings', 'admin_scripts']
|
2008-08-08 20:27:40 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_environment(self):
|
|
|
|
"fulldefault: manage.py builtin commands fail if settings file (from environment) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_manage(args, 'bad_settings')
|
2011-05-13 20:15:52 +08:00
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
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)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def test_custom_command_with_settings(self):
|
|
|
|
"fulldefault: manage.py can execute user commands when settings are provided as argument"
|
2013-02-26 20:19:18 +08:00
|
|
|
args = ['noargs_command', '--settings=test_project.settings']
|
2008-08-08 20:27:40 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
def test_custom_command_with_environment(self):
|
|
|
|
"fulldefault: manage.py can execute user commands when settings are provided in environment"
|
|
|
|
args = ['noargs_command']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_manage(args, 'test_project.settings')
|
2008-08-08 20:27:40 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-08-08 20:27:40 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
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):
|
2011-12-23 06:38:02 +08:00
|
|
|
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_command(self):
|
2012-09-09 04:12:18 +08:00
|
|
|
"minimal: manage.py builtin commands fail with an error when no settings provided"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
2013-12-18 20:10:23 +08:00
|
|
|
self.assertOutput(err, "No installed app with label 'admin_scripts'.")
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_with_settings(self):
|
|
|
|
"minimal: manage.py builtin commands fail if settings are provided as argument"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=test_project.settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
2013-12-18 20:10:23 +08:00
|
|
|
self.assertOutput(err, "No installed app with label 'admin_scripts'.")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_environment(self):
|
|
|
|
"minimal: manage.py builtin commands fail if settings are provided in the environment"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_manage(args, 'test_project.settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(out)
|
2013-12-18 20:10:23 +08:00
|
|
|
self.assertOutput(err, "No installed app with label 'admin_scripts'.")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_settings(self):
|
|
|
|
"minimal: manage.py builtin commands fail if settings file (from argument) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=bad_settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_environment(self):
|
|
|
|
"minimal: manage.py builtin commands fail if settings file (from environment) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_manage(args, 'bad_settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
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'")
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_custom_command_with_settings(self):
|
|
|
|
"minimal: manage.py can't execute user commands, even if settings are provided as argument"
|
2013-02-26 20:19:18 +08:00
|
|
|
args = ['noargs_command', '--settings=test_project.settings']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
|
|
|
self.assertOutput(err, "Unknown command: 'noargs_command'")
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
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']
|
2013-02-26 20:19:18 +08:00
|
|
|
out, err = self.run_manage(args, 'test_project.settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(out)
|
|
|
|
self.assertOutput(err, "Unknown command: 'noargs_command'")
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
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')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('alternate_settings.py')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_command(self):
|
2012-09-09 04:12:18 +08:00
|
|
|
"alternate: manage.py builtin commands fail with an error when no default settings provided"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
2016-09-17 00:15:00 +08:00
|
|
|
self.assertOutput(err, r"No module named '?(test_project\.)?settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_settings(self):
|
2011-10-13 13:56:15 +08:00
|
|
|
"alternate: manage.py builtin commands work with settings provided as argument"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=alternate_settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2011-10-13 13:56:15 +08:00
|
|
|
self.assertNoOutput(err)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_environment(self):
|
2011-10-13 13:56:15 +08:00
|
|
|
"alternate: manage.py builtin commands work if settings are provided in the environment"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_manage(args, 'alternate_settings')
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2011-10-13 13:56:15 +08:00
|
|
|
self.assertNoOutput(err)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_settings(self):
|
|
|
|
"alternate: manage.py builtin commands fail if settings file (from argument) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=bad_settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_environment(self):
|
|
|
|
"alternate: manage.py builtin commands fail if settings file (from environment) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_manage(args, 'bad_settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_custom_command(self):
|
2011-10-13 13:56:15 +08:00
|
|
|
"alternate: manage.py can't execute user commands without settings"
|
2008-07-10 20:12:41 +08:00
|
|
|
args = ['noargs_command']
|
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
2016-09-17 00:15:00 +08:00
|
|
|
self.assertOutput(err, r"No module named '?(test_project\.)?settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_custom_command_with_settings(self):
|
2011-10-13 13:56:15 +08:00
|
|
|
"alternate: manage.py can execute user commands if settings are provided as argument"
|
2008-07-10 20:12:41 +08:00
|
|
|
args = ['noargs_command', '--settings=alternate_settings']
|
|
|
|
out, err = self.run_manage(args)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertOutput(
|
|
|
|
out,
|
|
|
|
"EXECUTE: noargs_command options=[('no_color', False), "
|
|
|
|
"('pythonpath', None), ('settings', 'alternate_settings'), "
|
|
|
|
"('traceback', False), ('verbosity', 1)]"
|
|
|
|
)
|
2011-10-13 13:56:15 +08:00
|
|
|
self.assertNoOutput(err)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_custom_command_with_environment(self):
|
2011-10-13 13:56:15 +08:00
|
|
|
"alternate: manage.py can execute user commands if settings are provided in environment"
|
2008-07-10 20:12:41 +08:00
|
|
|
args = ['noargs_command']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_manage(args, 'alternate_settings')
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertOutput(
|
|
|
|
out,
|
|
|
|
"EXECUTE: noargs_command options=[('no_color', False), "
|
|
|
|
"('pythonpath', None), ('settings', None), ('traceback', False), "
|
|
|
|
"('verbosity', 1)]"
|
|
|
|
)
|
2013-02-23 23:28:45 +08:00
|
|
|
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)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertOutput(
|
|
|
|
out,
|
|
|
|
"EXECUTE: noargs_command options=[('no_color', True), "
|
|
|
|
"('pythonpath', None), ('settings', 'alternate_settings'), "
|
|
|
|
"('traceback', False), ('verbosity', 1)]"
|
|
|
|
)
|
2011-10-13 13:56:15 +08:00
|
|
|
self.assertNoOutput(err)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ManageMultipleSettings(AdminScriptTestCase):
|
|
|
|
"""A series of tests for manage.py when multiple settings files
|
|
|
|
(including the default 'settings.py') are available. The default settings
|
2008-07-28 02:25:06 +08:00
|
|
|
file is insufficient for performing the operations described, so the
|
2008-07-10 20:12:41 +08:00
|
|
|
alternate settings must be used by the running script.
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
2011-12-23 06:38:02 +08:00
|
|
|
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
|
2008-07-10 20:12:41 +08:00
|
|
|
self.write_settings('alternate_settings.py')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
|
|
|
self.remove_settings('alternate_settings.py')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_builtin_command(self):
|
2012-09-09 04:12:18 +08:00
|
|
|
"multiple: manage.py builtin commands fail with an error when no settings provided"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
2013-12-18 20:10:23 +08:00
|
|
|
self.assertOutput(err, "No installed app with label 'admin_scripts'.")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_settings(self):
|
|
|
|
"multiple: manage.py builtin commands succeed if settings are provided as argument"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=alternate_settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_environment(self):
|
2011-05-13 20:15:52 +08:00
|
|
|
"multiple: manage.py can execute builtin commands if settings are provided in the environment"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_manage(args, 'alternate_settings')
|
2011-05-13 20:15:52 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, SYSTEM_CHECK_MSG)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_settings(self):
|
|
|
|
"multiple: manage.py builtin commands fail if settings file (from argument) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--settings=bad_settings', 'admin_scripts']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_builtin_with_bad_environment(self):
|
|
|
|
"multiple: manage.py builtin commands fail if settings file (from environment) doesn't exist"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_manage(args, 'bad_settings')
|
2008-07-10 20:12:41 +08:00
|
|
|
self.assertNoOutput(out)
|
2014-09-17 16:43:45 +08:00
|
|
|
self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
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)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_custom_command_with_environment(self):
|
|
|
|
"multiple: manage.py can execute user commands if settings are provided in environment"
|
|
|
|
args = ['noargs_command']
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_manage(args, 'alternate_settings')
|
2011-05-13 20:15:52 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-06-18 07:07:54 +08:00
|
|
|
self.assertOutput(out, "EXECUTE: noargs_command")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
2012-09-26 21:07:11 +08:00
|
|
|
|
2013-10-18 00:57:44 +08:00
|
|
|
class ManageSettingsWithSettingsErrors(AdminScriptTestCase):
|
|
|
|
"""
|
|
|
|
Tests for manage.py when using the default settings.py file containing
|
|
|
|
runtime errors.
|
2011-02-13 09:56:12 +08:00
|
|
|
"""
|
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
|
|
|
|
2013-10-18 00:57:44 +08:00
|
|
|
def write_settings_with_import_error(self, filename):
|
2015-02-06 04:29:11 +08:00
|
|
|
settings_file_path = os.path.join(self.test_dir, filename)
|
2012-05-05 20:01:38 +08:00
|
|
|
with open(settings_file_path, 'w') as settings_file:
|
2013-02-26 20:19:18 +08:00
|
|
|
settings_file.write('# Settings file automatically generated by admin_scripts test case\n')
|
2012-05-05 20:01:38 +08:00
|
|
|
settings_file.write('# The next line will cause an import error:\nimport foo42bar\n')
|
2011-02-13 09:56:12 +08:00
|
|
|
|
2013-10-18 00:57:44 +08:00
|
|
|
def test_import_error(self):
|
2012-09-26 21:07:11 +08:00
|
|
|
"""
|
|
|
|
import error: manage.py builtin commands shows useful diagnostic info
|
2013-10-18 00:57:44 +08:00
|
|
|
when settings with import errors is provided (#14130).
|
2012-09-26 21:07:11 +08:00
|
|
|
"""
|
|
|
|
self.write_settings_with_import_error('settings.py')
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', 'admin_scripts']
|
2011-02-13 09:56:12 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
2012-09-29 01:17:40 +08:00
|
|
|
self.assertOutput(err, "No module named")
|
|
|
|
self.assertOutput(err, "foo42bar")
|
2010-04-12 20:39:18 +08:00
|
|
|
|
2013-10-18 00:57:44 +08:00
|
|
|
def test_attribute_error(self):
|
2012-09-26 21:07:11 +08:00
|
|
|
"""
|
2013-10-18 00:57:44 +08:00
|
|
|
manage.py builtin commands does not swallow attribute error due to bad
|
|
|
|
settings (#18845).
|
2012-09-26 21:07:11 +08:00
|
|
|
"""
|
|
|
|
self.write_settings('settings.py', sdict={'BAD_VAR': 'INSTALLED_APPS.crash'})
|
|
|
|
args = ['collectstatic', 'admin_scripts']
|
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
|
|
|
self.assertOutput(err, "AttributeError: 'list' object has no attribute 'crash'")
|
|
|
|
|
2013-10-18 00:57:44 +08:00
|
|
|
def test_key_error(self):
|
|
|
|
self.write_settings('settings.py', sdict={'BAD_VAR': 'DATABASES["blah"]'})
|
|
|
|
args = ['collectstatic', 'admin_scripts']
|
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
|
|
|
self.assertOutput(err, "KeyError: 'blah'")
|
|
|
|
|
2013-10-22 03:21:49 +08:00
|
|
|
def test_help(self):
|
|
|
|
"""
|
|
|
|
Test listing available commands output note when only core commands are
|
|
|
|
available.
|
|
|
|
"""
|
2016-12-23 23:55:00 +08:00
|
|
|
self.write_settings(
|
|
|
|
'settings.py',
|
|
|
|
extra='from django.core.exceptions import ImproperlyConfigured\n'
|
|
|
|
'raise ImproperlyConfigured()',
|
|
|
|
)
|
2013-10-22 03:21:49 +08:00
|
|
|
args = ['help']
|
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertOutput(out, 'only Django core commands are listed')
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
|
2012-09-26 21:07:11 +08:00
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
class ManageCheck(AdminScriptTestCase):
|
2010-04-12 20:39:18 +08:00
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
|
|
|
|
|
|
|
def test_nonexistent_app(self):
|
2017-02-03 09:43:21 +08:00
|
|
|
"""check reports an error on a nonexistent app in INSTALLED_APPS."""
|
2016-04-08 10:04:45 +08:00
|
|
|
self.write_settings(
|
|
|
|
'settings.py',
|
2014-01-20 10:45:21 +08:00
|
|
|
apps=['admin_scriptz.broken_app'],
|
2016-04-08 10:04:45 +08:00
|
|
|
sdict={'USE_I18N': False},
|
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
args = ['check']
|
2010-04-12 20:39:18 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
2016-09-15 23:54:22 +08:00
|
|
|
self.assertOutput(err, 'ModuleNotFoundError' if PY36 else 'ImportError')
|
2012-09-29 01:17:40 +08:00
|
|
|
self.assertOutput(err, 'No module named')
|
|
|
|
self.assertOutput(err, 'admin_scriptz')
|
2010-04-12 20:39:18 +08:00
|
|
|
|
|
|
|
def test_broken_app(self):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" manage.py check reports an ImportError if an app's models.py
|
|
|
|
raises one on import """
|
|
|
|
|
2010-04-12 20:39:18 +08:00
|
|
|
self.write_settings('settings.py', apps=['admin_scripts.broken_app'])
|
2014-01-20 10:45:21 +08:00
|
|
|
args = ['check']
|
2010-04-12 20:39:18 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
|
|
|
self.assertOutput(err, 'ImportError')
|
2010-04-15 03:10:27 +08:00
|
|
|
|
|
|
|
def test_complex_app(self):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" manage.py check does not raise an ImportError validating a
|
|
|
|
complex app with nested calls to load_app """
|
|
|
|
|
|
|
|
self.write_settings(
|
|
|
|
'settings.py',
|
|
|
|
apps=[
|
|
|
|
'admin_scripts.complex_app',
|
|
|
|
'admin_scripts.simple_app',
|
2014-01-25 05:43:00 +08:00
|
|
|
'django.contrib.admin.apps.SimpleAdminConfig',
|
2014-01-20 10:45:21 +08:00
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes',
|
|
|
|
],
|
|
|
|
sdict={
|
|
|
|
'DEBUG': True
|
|
|
|
}
|
|
|
|
)
|
|
|
|
args = ['check']
|
2010-04-15 03:10:27 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2014-01-23 03:09:02 +08:00
|
|
|
self.assertEqual(out, 'System check identified no issues (0 silenced).\n')
|
2010-04-15 03:10:27 +08:00
|
|
|
|
2010-04-20 22:39:46 +08:00
|
|
|
def test_app_with_import(self):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" manage.py check does not raise errors when an app imports a base
|
|
|
|
class that itself has an abstract base. """
|
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
self.write_settings(
|
|
|
|
'settings.py',
|
|
|
|
apps=[
|
|
|
|
'admin_scripts.app_with_import',
|
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes',
|
|
|
|
'django.contrib.sites',
|
|
|
|
],
|
|
|
|
sdict={'DEBUG': True},
|
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
args = ['check']
|
2010-04-20 22:39:46 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2014-01-23 03:09:02 +08:00
|
|
|
self.assertEqual(out, 'System check identified no issues (0 silenced).\n')
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_output_format(self):
|
|
|
|
""" All errors/warnings should be sorted by level and by message. """
|
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
self.write_settings(
|
|
|
|
'settings.py',
|
|
|
|
apps=[
|
|
|
|
'admin_scripts.app_raising_messages',
|
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes',
|
|
|
|
],
|
|
|
|
sdict={'DEBUG': True},
|
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
args = ['check']
|
|
|
|
out, err = self.run_manage(args)
|
|
|
|
expected_err = (
|
2014-10-20 19:10:53 +08:00
|
|
|
"SystemCheckError: System check identified some issues:\n"
|
2014-01-20 10:45:21 +08:00
|
|
|
"\n"
|
|
|
|
"ERRORS:\n"
|
|
|
|
"?: An error\n"
|
|
|
|
"\tHINT: Error hint\n"
|
|
|
|
"\n"
|
|
|
|
"WARNINGS:\n"
|
|
|
|
"a: Second warning\n"
|
|
|
|
"obj: First warning\n"
|
|
|
|
"\tHINT: Hint\n"
|
|
|
|
"\n"
|
2014-01-23 03:09:02 +08:00
|
|
|
"System check identified 3 issues (0 silenced).\n"
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(err, expected_err)
|
|
|
|
self.assertNoOutput(out)
|
|
|
|
|
|
|
|
def test_warning_does_not_halt(self):
|
|
|
|
"""
|
|
|
|
When there are only warnings or less serious messages, then Django
|
|
|
|
should not prevent user from launching their project, so `check`
|
|
|
|
command should not raise `CommandError` exception.
|
|
|
|
|
|
|
|
In this test we also test output format.
|
|
|
|
"""
|
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
self.write_settings(
|
|
|
|
'settings.py',
|
|
|
|
apps=[
|
|
|
|
'admin_scripts.app_raising_warning',
|
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes',
|
|
|
|
],
|
|
|
|
sdict={'DEBUG': True},
|
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
args = ['check']
|
|
|
|
out, err = self.run_manage(args)
|
|
|
|
expected_err = (
|
|
|
|
"System check identified some issues:\n" # No "CommandError: " part
|
|
|
|
"\n"
|
|
|
|
"WARNINGS:\n"
|
|
|
|
"?: A warning\n"
|
|
|
|
"\n"
|
2014-01-23 03:09:02 +08:00
|
|
|
"System check identified 1 issue (0 silenced).\n"
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(err, expected_err)
|
|
|
|
self.assertNoOutput(out)
|
2010-04-12 20:39:18 +08:00
|
|
|
|
2011-12-10 05:50:28 +08:00
|
|
|
|
2011-01-15 14:31:38 +08:00
|
|
|
class ManageRunserver(AdminScriptTestCase):
|
|
|
|
def setUp(self):
|
2011-12-10 05:50:28 +08:00
|
|
|
def monkey_run(*args, **options):
|
|
|
|
return
|
2011-01-15 14:31:38 +08:00
|
|
|
|
2015-05-09 23:01:40 +08:00
|
|
|
self.output = StringIO()
|
2017-12-21 03:47:17 +08:00
|
|
|
self.cmd = RunserverCommand(stdout=self.output)
|
2011-01-15 14:31:38 +08:00
|
|
|
self.cmd.run = monkey_run
|
|
|
|
|
2016-03-03 09:12:56 +08:00
|
|
|
def assertServerSettings(self, addr, port, ipv6=False, raw_ipv6=False):
|
2011-01-15 14:31:38 +08:00
|
|
|
self.assertEqual(self.cmd.addr, addr)
|
|
|
|
self.assertEqual(self.cmd.port, port)
|
|
|
|
self.assertEqual(self.cmd.use_ipv6, ipv6)
|
|
|
|
self.assertEqual(self.cmd._raw_ipv6, raw_ipv6)
|
|
|
|
|
|
|
|
def test_runserver_addrport(self):
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(self.cmd)
|
2011-01-15 14:31:38 +08:00
|
|
|
self.assertServerSettings('127.0.0.1', '8000')
|
|
|
|
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(self.cmd, addrport="1.2.3.4:8000")
|
2011-01-15 14:31:38 +08:00
|
|
|
self.assertServerSettings('1.2.3.4', '8000')
|
|
|
|
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(self.cmd, addrport="7000")
|
2011-01-15 14:31:38 +08:00
|
|
|
self.assertServerSettings('127.0.0.1', '7000')
|
|
|
|
|
2011-12-10 05:53:21 +08:00
|
|
|
@unittest.skipUnless(socket.has_ipv6, "platform doesn't support IPv6")
|
2011-12-10 05:50:28 +08:00
|
|
|
def test_runner_addrport_ipv6(self):
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(self.cmd, addrport="", use_ipv6=True)
|
2011-01-27 11:15:12 +08:00
|
|
|
self.assertServerSettings('::1', '8000', ipv6=True, raw_ipv6=True)
|
2011-01-15 14:31:38 +08:00
|
|
|
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(self.cmd, addrport="7000", use_ipv6=True)
|
2011-01-27 11:15:12 +08:00
|
|
|
self.assertServerSettings('::1', '7000', ipv6=True, raw_ipv6=True)
|
2011-01-15 14:31:38 +08:00
|
|
|
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(self.cmd, addrport="[2001:0db8:1234:5678::9]:7000")
|
2011-01-15 14:31:38 +08:00
|
|
|
self.assertServerSettings('2001:0db8:1234:5678::9', '7000', ipv6=True, raw_ipv6=True)
|
|
|
|
|
2011-12-10 05:50:28 +08:00
|
|
|
def test_runner_hostname(self):
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(self.cmd, addrport="localhost:8000")
|
2011-01-15 14:31:38 +08:00
|
|
|
self.assertServerSettings('localhost', '8000')
|
|
|
|
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(self.cmd, addrport="test.domain.local:7000")
|
2011-01-15 14:31:38 +08:00
|
|
|
self.assertServerSettings('test.domain.local', '7000')
|
|
|
|
|
2011-12-10 05:53:21 +08:00
|
|
|
@unittest.skipUnless(socket.has_ipv6, "platform doesn't support IPv6")
|
2011-12-10 05:50:28 +08:00
|
|
|
def test_runner_hostname_ipv6(self):
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(self.cmd, addrport="test.domain.local:7000", use_ipv6=True)
|
2011-01-15 14:31:38 +08:00
|
|
|
self.assertServerSettings('test.domain.local', '7000', ipv6=True)
|
|
|
|
|
2017-01-28 02:30:27 +08:00
|
|
|
def test_runner_custom_defaults(self):
|
|
|
|
self.cmd.default_addr = '0.0.0.0'
|
|
|
|
self.cmd.default_port = '5000'
|
|
|
|
call_command(self.cmd)
|
|
|
|
self.assertServerSettings('0.0.0.0', '5000')
|
|
|
|
|
|
|
|
@unittest.skipUnless(socket.has_ipv6, "platform doesn't support IPv6")
|
|
|
|
def test_runner_custom_defaults_ipv6(self):
|
|
|
|
self.cmd.default_addr_ipv6 = '::'
|
|
|
|
call_command(self.cmd, use_ipv6=True)
|
|
|
|
self.assertServerSettings('::', '8000', ipv6=True, raw_ipv6=True)
|
|
|
|
|
2011-12-10 05:50:28 +08:00
|
|
|
def test_runner_ambiguous(self):
|
2011-01-27 11:15:12 +08:00
|
|
|
# Only 4 characters, all of which could be in an ipv6 address
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(self.cmd, addrport="beef:7654")
|
2011-01-15 14:31:38 +08:00
|
|
|
self.assertServerSettings('beef', '7654')
|
|
|
|
|
|
|
|
# Uses only characters that could be in an ipv6 address
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(self.cmd, addrport="deadbeef:7654")
|
2011-01-15 14:31:38 +08:00
|
|
|
self.assertServerSettings('deadbeef', '7654')
|
|
|
|
|
2015-05-09 23:01:40 +08:00
|
|
|
def test_no_database(self):
|
|
|
|
"""
|
|
|
|
Ensure runserver.check_migrations doesn't choke on empty DATABASES.
|
|
|
|
"""
|
|
|
|
tested_connections = ConnectionHandler({})
|
2016-02-12 18:02:36 +08:00
|
|
|
with mock.patch('django.core.management.base.connections', new=tested_connections):
|
2015-05-09 23:01:40 +08:00
|
|
|
self.cmd.check_migrations()
|
|
|
|
|
|
|
|
def test_readonly_database(self):
|
|
|
|
"""
|
2016-11-05 16:16:23 +08:00
|
|
|
runserver.check_migrations() doesn't choke when a database is read-only.
|
2015-05-09 23:01:40 +08:00
|
|
|
"""
|
2016-11-05 16:16:23 +08:00
|
|
|
with mock.patch.object(MigrationRecorder, 'has_table', return_value=False):
|
2015-05-09 23:01:40 +08:00
|
|
|
self.cmd.check_migrations()
|
2016-11-05 16:16:23 +08:00
|
|
|
# You have # ...
|
|
|
|
self.assertIn('unapplied migration(s)', self.output.getvalue())
|
2015-05-09 23:01:40 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2015-12-04 00:51:39 +08:00
|
|
|
class ManageRunserverMigrationWarning(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.stdout = StringIO()
|
2017-12-21 03:47:17 +08:00
|
|
|
self.runserver_command = RunserverCommand(stdout=self.stdout)
|
2015-12-04 00:51:39 +08:00
|
|
|
|
|
|
|
@override_settings(INSTALLED_APPS=["admin_scripts.app_waiting_migration"])
|
|
|
|
def test_migration_warning_one_app(self):
|
|
|
|
self.runserver_command.check_migrations()
|
|
|
|
output = self.stdout.getvalue()
|
|
|
|
self.assertIn('You have 1 unapplied migration(s)', output)
|
|
|
|
self.assertIn('apply the migrations for app(s): app_waiting_migration.', output)
|
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
INSTALLED_APPS=[
|
|
|
|
"admin_scripts.app_waiting_migration",
|
|
|
|
"admin_scripts.another_app_waiting_migration",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_migration_warning_multiple_apps(self):
|
|
|
|
self.runserver_command.check_migrations()
|
|
|
|
output = self.stdout.getvalue()
|
|
|
|
self.assertIn('You have 2 unapplied migration(s)', output)
|
|
|
|
self.assertIn(
|
|
|
|
'apply the migrations for app(s): another_app_waiting_migration, '
|
|
|
|
'app_waiting_migration.', output
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-06-10 03:07:23 +08:00
|
|
|
class ManageRunserverEmptyAllowedHosts(AdminScriptTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
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.')
|
|
|
|
|
2011-01-15 14:31:38 +08:00
|
|
|
|
2015-04-03 17:26:20 +08:00
|
|
|
class ManageTestserver(AdminScriptTestCase):
|
|
|
|
|
2017-01-12 06:17:25 +08:00
|
|
|
@mock.patch.object(TestserverCommand, 'handle', return_value='')
|
2015-04-03 17:26:20 +08:00
|
|
|
def test_testserver_handle_params(self, mock_handle):
|
|
|
|
out = StringIO()
|
|
|
|
call_command('testserver', 'blah.json', stdout=out)
|
|
|
|
mock_handle.assert_called_with(
|
|
|
|
'blah.json',
|
|
|
|
stdout=out, settings=None, pythonpath=None, verbosity=1,
|
|
|
|
traceback=False, addrport='', no_color=False, use_ipv6=False,
|
|
|
|
skip_checks=True, interactive=True,
|
|
|
|
)
|
|
|
|
|
2017-12-21 02:08:45 +08:00
|
|
|
@mock.patch('django.db.connection.creation.create_test_db', return_value='test_db')
|
|
|
|
@mock.patch.object(LoaddataCommand, 'handle', return_value='')
|
|
|
|
@mock.patch.object(RunserverCommand, 'handle', return_value='')
|
|
|
|
def test_params_to_runserver(self, mock_runserver_handle, mock_loaddata_handle, mock_create_test_db):
|
|
|
|
out = StringIO()
|
|
|
|
call_command('testserver', 'blah.json', stdout=out)
|
|
|
|
mock_runserver_handle.assert_called_with(
|
|
|
|
addrport='',
|
|
|
|
insecure_serving=False,
|
|
|
|
no_color=False,
|
|
|
|
pythonpath=None,
|
|
|
|
settings=None,
|
|
|
|
shutdown_message=(
|
|
|
|
"\nServer stopped.\nNote that the test database, 'test_db', "
|
|
|
|
"has not been deleted. You can explore it on your own."
|
|
|
|
),
|
|
|
|
skip_checks=True,
|
|
|
|
traceback=False,
|
|
|
|
use_ipv6=False,
|
|
|
|
use_reloader=False,
|
|
|
|
use_static_handler=True,
|
|
|
|
use_threading=connection.features.test_db_allows_multiple_connections,
|
|
|
|
verbosity=1,
|
|
|
|
)
|
|
|
|
|
2015-04-03 17:26:20 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
##########################################################################
|
|
|
|
# COMMAND PROCESSING TESTS
|
2016-10-27 15:53:39 +08:00
|
|
|
# user-space commands are correctly handled - in particular, arguments to
|
|
|
|
# the commands are correctly parsed and processed.
|
2008-07-10 20:12:41 +08:00
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
class CommandTypes(AdminScriptTestCase):
|
|
|
|
"Tests for the various types of base command types that can be defined."
|
|
|
|
def setUp(self):
|
|
|
|
self.write_settings('settings.py')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-11 21:18:19 +08:00
|
|
|
def test_version(self):
|
2012-02-08 02:46:29 +08:00
|
|
|
"version is handled as a special case"
|
|
|
|
args = ['version']
|
2008-07-11 21:18:19 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2012-01-08 23:05:15 +08:00
|
|
|
self.assertOutput(out, get_version())
|
2008-07-11 21:18:19 +08:00
|
|
|
|
2012-02-08 02:46:29 +08:00
|
|
|
def test_version_alternative(self):
|
|
|
|
"--version is equivalent to version"
|
|
|
|
args1, args2 = ['version'], ['--version']
|
2013-10-16 22:24:59 +08:00
|
|
|
# It's possible one outputs on stderr and the other on stdout, hence the set
|
|
|
|
self.assertEqual(set(self.run_manage(args1)), set(self.run_manage(args2)))
|
2012-02-08 02:46:29 +08:00
|
|
|
|
2008-07-11 21:18:19 +08:00
|
|
|
def test_help(self):
|
2012-02-08 02:46:29 +08:00
|
|
|
"help is handled as a special case"
|
|
|
|
args = ['help']
|
2008-07-11 21:18:19 +08:00
|
|
|
out, err = self.run_manage(args)
|
2011-03-27 01:45:42 +08:00
|
|
|
self.assertOutput(out, "Type 'manage.py help <subcommand>' for help on a specific subcommand.")
|
2012-02-08 02:46:29 +08:00
|
|
|
self.assertOutput(out, '[django]')
|
|
|
|
self.assertOutput(out, 'startapp')
|
|
|
|
self.assertOutput(out, 'startproject')
|
2011-02-21 21:45:57 +08:00
|
|
|
|
2012-02-08 02:46:29 +08:00
|
|
|
def test_help_commands(self):
|
|
|
|
"help --commands shows the list of all available commands"
|
|
|
|
args = ['help', '--commands']
|
2011-02-21 21:45:57 +08:00
|
|
|
out, err = self.run_manage(args)
|
2013-10-16 22:24:59 +08:00
|
|
|
self.assertNotInOutput(out, 'usage:')
|
2012-02-08 02:46:29 +08:00
|
|
|
self.assertNotInOutput(out, 'Options:')
|
|
|
|
self.assertNotInOutput(out, '[django]')
|
|
|
|
self.assertOutput(out, 'startapp')
|
|
|
|
self.assertOutput(out, 'startproject')
|
|
|
|
self.assertNotInOutput(out, '\n\n')
|
|
|
|
|
|
|
|
def test_help_alternative(self):
|
|
|
|
"--help is equivalent to help"
|
|
|
|
args1, args2 = ['help'], ['--help']
|
|
|
|
self.assertEqual(self.run_manage(args1), self.run_manage(args2))
|
|
|
|
|
|
|
|
def test_help_short_altert(self):
|
|
|
|
"-h is handled as a short form of --help"
|
|
|
|
args1, args2 = ['--help'], ['-h']
|
|
|
|
self.assertEqual(self.run_manage(args1), self.run_manage(args2))
|
2008-07-11 21:18:19 +08:00
|
|
|
|
|
|
|
def test_specific_help(self):
|
|
|
|
"--help can be used on a specific command"
|
2014-12-27 07:23:00 +08:00
|
|
|
args = ['check', '--help']
|
2008-07-11 21:18:19 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2014-12-27 07:23:00 +08:00
|
|
|
self.assertOutput(out, "Checks the entire Django project for potential problems.")
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2014-10-21 16:31:24 +08:00
|
|
|
def test_color_style(self):
|
|
|
|
style = color.no_style()
|
|
|
|
self.assertEqual(style.ERROR('Hello, world!'), 'Hello, world!')
|
|
|
|
|
|
|
|
style = color.make_style('nocolor')
|
|
|
|
self.assertEqual(style.ERROR('Hello, world!'), 'Hello, world!')
|
|
|
|
|
|
|
|
style = color.make_style('dark')
|
|
|
|
self.assertIn('Hello, world!', style.ERROR('Hello, world!'))
|
|
|
|
self.assertNotEqual(style.ERROR('Hello, world!'), 'Hello, world!')
|
|
|
|
|
|
|
|
# Default palette has color.
|
|
|
|
style = color.make_style('')
|
|
|
|
self.assertIn('Hello, world!', style.ERROR('Hello, world!'))
|
|
|
|
self.assertNotEqual(style.ERROR('Hello, world!'), 'Hello, world!')
|
|
|
|
|
2014-10-22 02:11:31 +08:00
|
|
|
def test_command_color(self):
|
|
|
|
class Command(BaseCommand):
|
|
|
|
requires_system_checks = False
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
self.stdout.write('Hello, world!', self.style.ERROR)
|
|
|
|
self.stderr.write('Hello, world!', self.style.ERROR)
|
|
|
|
|
|
|
|
out = StringIO()
|
|
|
|
err = StringIO()
|
|
|
|
command = Command(stdout=out, stderr=err)
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(command)
|
2014-10-22 02:11:31 +08:00
|
|
|
if color.supports_color():
|
|
|
|
self.assertIn('Hello, world!\n', out.getvalue())
|
|
|
|
self.assertIn('Hello, world!\n', err.getvalue())
|
|
|
|
self.assertNotEqual(out.getvalue(), 'Hello, world!\n')
|
|
|
|
self.assertNotEqual(err.getvalue(), 'Hello, world!\n')
|
|
|
|
else:
|
|
|
|
self.assertEqual(out.getvalue(), 'Hello, world!\n')
|
|
|
|
self.assertEqual(err.getvalue(), 'Hello, world!\n')
|
|
|
|
|
|
|
|
def test_command_no_color(self):
|
2013-02-23 23:28:45 +08:00
|
|
|
"--no-color prevent colorization of the output"
|
2014-10-22 02:11:31 +08:00
|
|
|
class Command(BaseCommand):
|
|
|
|
requires_system_checks = False
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
self.stdout.write('Hello, world!', self.style.ERROR)
|
|
|
|
self.stderr.write('Hello, world!', self.style.ERROR)
|
|
|
|
|
|
|
|
out = StringIO()
|
|
|
|
err = StringIO()
|
|
|
|
command = Command(stdout=out, stderr=err, no_color=True)
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(command)
|
2014-10-22 02:11:31 +08:00
|
|
|
self.assertEqual(out.getvalue(), 'Hello, world!\n')
|
|
|
|
self.assertEqual(err.getvalue(), 'Hello, world!\n')
|
|
|
|
|
2013-02-23 23:28:45 +08:00
|
|
|
out = StringIO()
|
2014-10-22 02:11:31 +08:00
|
|
|
err = StringIO()
|
|
|
|
command = Command(stdout=out, stderr=err)
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(command, no_color=True)
|
2014-10-22 02:11:31 +08:00
|
|
|
self.assertEqual(out.getvalue(), 'Hello, world!\n')
|
|
|
|
self.assertEqual(err.getvalue(), 'Hello, world!\n')
|
2013-07-30 23:30:20 +08:00
|
|
|
|
2014-10-22 02:11:31 +08:00
|
|
|
def test_custom_stdout(self):
|
|
|
|
class Command(BaseCommand):
|
|
|
|
requires_system_checks = False
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
self.stdout.write("Hello, World!")
|
|
|
|
|
|
|
|
out = StringIO()
|
|
|
|
command = Command(stdout=out)
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(command)
|
2014-10-22 02:11:31 +08:00
|
|
|
self.assertEqual(out.getvalue(), "Hello, World!\n")
|
|
|
|
out.truncate(0)
|
|
|
|
new_out = StringIO()
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(command, stdout=new_out)
|
2014-10-22 02:11:31 +08:00
|
|
|
self.assertEqual(out.getvalue(), "")
|
|
|
|
self.assertEqual(new_out.getvalue(), "Hello, World!\n")
|
|
|
|
|
|
|
|
def test_custom_stderr(self):
|
|
|
|
class Command(BaseCommand):
|
|
|
|
requires_system_checks = False
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
self.stderr.write("Hello, World!")
|
|
|
|
|
|
|
|
err = StringIO()
|
|
|
|
command = Command(stderr=err)
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(command)
|
2014-10-22 02:11:31 +08:00
|
|
|
self.assertEqual(err.getvalue(), "Hello, World!\n")
|
|
|
|
err.truncate(0)
|
|
|
|
new_err = StringIO()
|
2016-03-03 09:12:56 +08:00
|
|
|
call_command(command, stderr=new_err)
|
2014-10-22 02:11:31 +08:00
|
|
|
self.assertEqual(err.getvalue(), "")
|
|
|
|
self.assertEqual(new_err.getvalue(), "Hello, World!\n")
|
2013-07-30 23:30:20 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_base_command(self):
|
|
|
|
"User BaseCommands can execute when a label is provided"
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['base_command', 'testlabel']
|
2014-01-20 10:45:21 +08:00
|
|
|
expected_labels = "('testlabel',)"
|
|
|
|
self._test_base_command(args, expected_labels)
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_base_command_no_label(self):
|
|
|
|
"User BaseCommands can execute when no labels are provided"
|
|
|
|
args = ['base_command']
|
2014-01-20 10:45:21 +08:00
|
|
|
expected_labels = "()"
|
|
|
|
self._test_base_command(args, expected_labels)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_base_command_multiple_label(self):
|
|
|
|
"User BaseCommands can execute when no labels are provided"
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['base_command', 'testlabel', 'anotherlabel']
|
2014-01-20 10:45:21 +08:00
|
|
|
expected_labels = "('testlabel', 'anotherlabel')"
|
|
|
|
self._test_base_command(args, expected_labels)
|
2008-07-11 21:18:19 +08:00
|
|
|
|
|
|
|
def test_base_command_with_option(self):
|
|
|
|
"User BaseCommands can execute with options when a label is provided"
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['base_command', 'testlabel', '--option_a=x']
|
2014-01-20 10:45:21 +08:00
|
|
|
expected_labels = "('testlabel',)"
|
|
|
|
self._test_base_command(args, expected_labels, option_a="'x'")
|
2008-07-11 21:18:19 +08:00
|
|
|
|
|
|
|
def test_base_command_with_options(self):
|
|
|
|
"User BaseCommands can execute with multiple options when a label is provided"
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['base_command', 'testlabel', '-a', 'x', '--option_b=y']
|
2014-01-20 10:45:21 +08:00
|
|
|
expected_labels = "('testlabel',)"
|
|
|
|
self._test_base_command(args, expected_labels, option_a="'x'", option_b="'y'")
|
|
|
|
|
2014-06-15 02:01:02 +08:00
|
|
|
def test_base_command_with_wrong_option(self):
|
|
|
|
"User BaseCommands outputs command usage when wrong option is specified"
|
|
|
|
args = ['base_command', '--invalid']
|
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(out)
|
|
|
|
self.assertOutput(err, "usage: manage.py base_command")
|
|
|
|
self.assertOutput(err, "error: unrecognized arguments: --invalid")
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def _test_base_command(self, args, labels, option_a="'1'", option_b="'2'"):
|
2008-07-11 21:18:19 +08:00
|
|
|
out, err = self.run_manage(args)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2014-06-07 04:39:33 +08:00
|
|
|
expected_out = (
|
|
|
|
"EXECUTE:BaseCommand labels=%s, "
|
|
|
|
"options=[('no_color', False), ('option_a', %s), ('option_b', %s), "
|
|
|
|
"('option_c', '3'), ('pythonpath', None), ('settings', None), "
|
|
|
|
"('traceback', False), ('verbosity', 1)]") % (labels, option_a, option_b)
|
2008-07-11 21:18:19 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertOutput(out, expected_out)
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2013-01-25 21:52:24 +08:00
|
|
|
def test_base_run_from_argv(self):
|
|
|
|
"""
|
|
|
|
Test run_from_argv properly terminates even with custom execute() (#19665)
|
|
|
|
Also test proper traceback display.
|
|
|
|
"""
|
2014-10-22 01:15:10 +08:00
|
|
|
err = StringIO()
|
|
|
|
command = BaseCommand(stderr=err)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-03-09 19:38:45 +08:00
|
|
|
def raise_command_error(*args, **kwargs):
|
|
|
|
raise CommandError("Custom error")
|
2013-01-25 21:52:24 +08:00
|
|
|
|
2014-10-22 01:15:10 +08:00
|
|
|
command.execute = lambda args: args # This will trigger TypeError
|
|
|
|
|
|
|
|
# If the Exception is not CommandError it should always
|
|
|
|
# raise the original exception.
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
command.run_from_argv(['', ''])
|
|
|
|
|
|
|
|
# If the Exception is CommandError and --traceback is not present
|
|
|
|
# this command should raise a SystemExit and don't print any
|
|
|
|
# traceback to the stderr.
|
|
|
|
command.execute = raise_command_error
|
|
|
|
err.truncate(0)
|
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
command.run_from_argv(['', ''])
|
|
|
|
err_message = err.getvalue()
|
|
|
|
self.assertNotIn("Traceback", err_message)
|
|
|
|
self.assertIn("CommandError", err_message)
|
|
|
|
|
|
|
|
# If the Exception is CommandError and --traceback is present
|
|
|
|
# this command should raise the original CommandError as if it
|
|
|
|
# were not a CommandError.
|
|
|
|
err.truncate(0)
|
|
|
|
with self.assertRaises(CommandError):
|
|
|
|
command.run_from_argv(['', '', '--traceback'])
|
2013-01-25 21:52:24 +08:00
|
|
|
|
2013-10-15 02:24:57 +08:00
|
|
|
def test_run_from_argv_non_ascii_error(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Non-ASCII message of CommandError does not raise any
|
2013-10-15 02:24:57 +08:00
|
|
|
UnicodeDecodeError in run_from_argv.
|
|
|
|
"""
|
|
|
|
def raise_command_error(*args, **kwargs):
|
|
|
|
raise CommandError("Erreur personnalisée")
|
|
|
|
|
2014-10-22 01:15:10 +08:00
|
|
|
command = BaseCommand(stderr=StringIO())
|
2013-10-15 02:24:57 +08:00
|
|
|
command.execute = raise_command_error
|
|
|
|
|
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
command.run_from_argv(['', ''])
|
|
|
|
|
2014-12-29 01:10:12 +08:00
|
|
|
def test_run_from_argv_closes_connections(self):
|
|
|
|
"""
|
|
|
|
A command called from the command line should close connections after
|
|
|
|
being executed (#21255).
|
|
|
|
"""
|
|
|
|
command = BaseCommand(stderr=StringIO())
|
2015-02-11 01:02:47 +08:00
|
|
|
command.check = lambda: []
|
2014-12-29 01:10:12 +08:00
|
|
|
command.handle = lambda *args, **kwargs: args
|
|
|
|
with mock.patch('django.core.management.base.connections') as mock_connections:
|
|
|
|
command.run_from_argv(['', ''])
|
|
|
|
# Test connections have been closed
|
|
|
|
self.assertTrue(mock_connections.close_all.called)
|
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_noargs(self):
|
|
|
|
"NoArg Commands can be executed"
|
|
|
|
args = ['noargs_command']
|
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertOutput(
|
|
|
|
out,
|
|
|
|
"EXECUTE: noargs_command options=[('no_color', False), "
|
|
|
|
"('pythonpath', None), ('settings', None), ('traceback', False), "
|
|
|
|
"('verbosity', 1)]"
|
|
|
|
)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_noargs_with_args(self):
|
|
|
|
"NoArg Commands raise an error if an argument is provided"
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['noargs_command', 'argument']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
2014-06-15 00:12:43 +08:00
|
|
|
self.assertOutput(err, "error: unrecognized arguments: argument")
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_app_command(self):
|
|
|
|
"User AppCommands can execute when a single app name is provided"
|
|
|
|
args = ['app_command', 'auth']
|
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2013-12-27 22:17:56 +08:00
|
|
|
self.assertOutput(out, "EXECUTE:AppCommand name=django.contrib.auth, options=")
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertOutput(
|
|
|
|
out,
|
|
|
|
", options=[('no_color', False), ('pythonpath', None), "
|
|
|
|
"('settings', None), ('traceback', False), ('verbosity', 1)]"
|
|
|
|
)
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_app_command_no_apps(self):
|
|
|
|
"User AppCommands raise an error when no app name is provided"
|
|
|
|
args = ['app_command']
|
|
|
|
out, err = self.run_manage(args)
|
2014-06-15 00:12:43 +08:00
|
|
|
self.assertOutput(err, 'error: Enter at least one application label.')
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_app_command_multiple_apps(self):
|
|
|
|
"User AppCommands raise an error when multiple app names are provided"
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['app_command', 'auth', 'contenttypes']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2013-12-27 22:17:56 +08:00
|
|
|
self.assertOutput(out, "EXECUTE:AppCommand name=django.contrib.auth, options=")
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertOutput(
|
|
|
|
out,
|
|
|
|
", options=[('no_color', False), ('pythonpath', None), "
|
|
|
|
"('settings', None), ('traceback', False), ('verbosity', 1)]"
|
|
|
|
)
|
2013-12-27 22:17:56 +08:00
|
|
|
self.assertOutput(out, "EXECUTE:AppCommand name=django.contrib.contenttypes, options=")
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertOutput(
|
|
|
|
out,
|
|
|
|
", options=[('no_color', False), ('pythonpath', None), "
|
|
|
|
"('settings', None), ('traceback', False), ('verbosity', 1)]"
|
|
|
|
)
|
2008-07-10 20:12:41 +08:00
|
|
|
|
2013-12-28 16:53:02 +08:00
|
|
|
def test_app_command_invalid_app_label(self):
|
2008-07-10 20:12:41 +08:00
|
|
|
"User AppCommands can execute when a single app name is provided"
|
|
|
|
args = ['app_command', 'NOT_AN_APP']
|
|
|
|
out, err = self.run_manage(args)
|
2013-12-18 20:10:23 +08:00
|
|
|
self.assertOutput(err, "No installed app with label 'NOT_AN_APP'.")
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2013-12-28 16:53:02 +08:00
|
|
|
def test_app_command_some_invalid_app_labels(self):
|
2008-07-10 20:12:41 +08:00
|
|
|
"User AppCommands can execute when some of the provided app names are invalid"
|
|
|
|
args = ['app_command', 'auth', 'NOT_AN_APP']
|
|
|
|
out, err = self.run_manage(args)
|
2013-12-18 20:10:23 +08:00
|
|
|
self.assertOutput(err, "No installed app with label 'NOT_AN_APP'.")
|
2008-07-10 20:12:41 +08:00
|
|
|
|
|
|
|
def test_label_command(self):
|
|
|
|
"User LabelCommands can execute when a label is provided"
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['label_command', 'testlabel']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertOutput(
|
|
|
|
out,
|
|
|
|
"EXECUTE:LabelCommand label=testlabel, options=[('no_color', False), "
|
|
|
|
"('pythonpath', None), ('settings', None), ('traceback', False), ('verbosity', 1)]"
|
|
|
|
)
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-10 20:12:41 +08:00
|
|
|
def test_label_command_no_label(self):
|
|
|
|
"User LabelCommands raise an error if no label is provided"
|
|
|
|
args = ['label_command']
|
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertOutput(err, 'Enter at least one label')
|
|
|
|
|
|
|
|
def test_label_command_multiple_label(self):
|
|
|
|
"User LabelCommands are executed multiple times if multiple labels are provided"
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['label_command', 'testlabel', 'anotherlabel']
|
2008-07-10 20:12:41 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertOutput(
|
|
|
|
out,
|
|
|
|
"EXECUTE:LabelCommand label=testlabel, options=[('no_color', False), "
|
|
|
|
"('pythonpath', None), ('settings', None), ('traceback', False), ('verbosity', 1)]"
|
|
|
|
)
|
|
|
|
self.assertOutput(
|
|
|
|
out,
|
|
|
|
"EXECUTE:LabelCommand label=anotherlabel, options=[('no_color', False), "
|
|
|
|
"('pythonpath', None), ('settings', None), ('traceback', False), ('verbosity', 1)]"
|
|
|
|
)
|
2008-07-11 21:18:19 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class Discovery(SimpleTestCase):
|
2014-01-02 01:05:12 +08:00
|
|
|
|
|
|
|
def test_precedence(self):
|
|
|
|
"""
|
2014-04-27 01:18:45 +08:00
|
|
|
Apps listed first in INSTALLED_APPS have precedence.
|
2014-01-02 01:05:12 +08:00
|
|
|
"""
|
|
|
|
with self.settings(INSTALLED_APPS=['admin_scripts.complex_app',
|
2014-01-20 10:45:21 +08:00
|
|
|
'admin_scripts.simple_app',
|
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes']):
|
2014-01-02 01:05:12 +08:00
|
|
|
out = StringIO()
|
|
|
|
call_command('duplicate', stdout=out)
|
|
|
|
self.assertEqual(out.getvalue().strip(), 'complex_app')
|
|
|
|
with self.settings(INSTALLED_APPS=['admin_scripts.simple_app',
|
2014-01-20 10:45:21 +08:00
|
|
|
'admin_scripts.complex_app',
|
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes']):
|
2014-01-02 01:05:12 +08:00
|
|
|
out = StringIO()
|
|
|
|
call_command('duplicate', stdout=out)
|
|
|
|
self.assertEqual(out.getvalue().strip(), 'simple_app')
|
|
|
|
|
|
|
|
|
2008-07-11 21:18:19 +08:00
|
|
|
class ArgumentOrder(AdminScriptTestCase):
|
|
|
|
"""Tests for 2-stage argument parsing scheme.
|
|
|
|
|
|
|
|
django-admin command arguments are parsed in 2 parts; the core arguments
|
2013-10-16 22:24:59 +08:00
|
|
|
(--settings, --traceback and --pythonpath) are parsed using a basic parser,
|
|
|
|
ignoring any unknown options. Then the full settings are
|
2008-07-11 21:18:19 +08:00
|
|
|
passed to the command parser, which extracts commands of interest to the
|
2008-07-28 02:25:06 +08:00
|
|
|
individual command.
|
2008-07-11 21:18:19 +08:00
|
|
|
"""
|
|
|
|
def setUp(self):
|
2011-12-23 06:38:02 +08:00
|
|
|
self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
|
2008-07-11 21:18:19 +08:00
|
|
|
self.write_settings('alternate_settings.py')
|
2008-07-28 02:25:06 +08:00
|
|
|
|
2008-07-11 21:18:19 +08:00
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
|
|
|
self.remove_settings('alternate_settings.py')
|
|
|
|
|
|
|
|
def test_setting_then_option(self):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Options passed after settings are correctly handled. """
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['base_command', 'testlabel', '--settings=alternate_settings', '--option_a=x']
|
2014-01-20 10:45:21 +08:00
|
|
|
self._test(args)
|
2008-07-11 21:18:19 +08:00
|
|
|
|
|
|
|
def test_setting_then_short_option(self):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Short options passed after settings are correctly handled. """
|
|
|
|
args = ['base_command', 'testlabel', '--settings=alternate_settings', '-a', 'x']
|
|
|
|
self._test(args)
|
2008-07-11 21:18:19 +08:00
|
|
|
|
|
|
|
def test_option_then_setting(self):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Options passed before settings are correctly handled. """
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['base_command', 'testlabel', '--option_a=x', '--settings=alternate_settings']
|
2014-01-20 10:45:21 +08:00
|
|
|
self._test(args)
|
2008-07-11 21:18:19 +08:00
|
|
|
|
|
|
|
def test_short_option_then_setting(self):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Short options passed before settings are correctly handled. """
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['base_command', 'testlabel', '-a', 'x', '--settings=alternate_settings']
|
2014-01-20 10:45:21 +08:00
|
|
|
self._test(args)
|
2008-07-11 21:18:19 +08:00
|
|
|
|
|
|
|
def test_option_then_setting_then_option(self):
|
2014-01-20 10:45:21 +08:00
|
|
|
""" Options are correctly handled when they are passed before and after
|
|
|
|
a setting. """
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['base_command', 'testlabel', '--option_a=x', '--settings=alternate_settings', '--option_b=y']
|
2014-01-20 10:45:21 +08:00
|
|
|
self._test(args, option_b="'y'")
|
|
|
|
|
|
|
|
def _test(self, args, option_b="'2'"):
|
2008-07-11 21:18:19 +08:00
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertOutput(
|
|
|
|
out,
|
|
|
|
"EXECUTE:BaseCommand labels=('testlabel',), options=[('no_color', False), "
|
|
|
|
"('option_a', 'x'), ('option_b', %s), ('option_c', '3'), "
|
|
|
|
"('pythonpath', None), ('settings', 'alternate_settings'), "
|
|
|
|
"('traceback', False), ('verbosity', 1)]" % option_b
|
|
|
|
)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='admin_scripts.urls')
|
2011-12-23 06:38:02 +08:00
|
|
|
class StartProject(LiveServerTestCase, AdminScriptTestCase):
|
|
|
|
|
2013-06-04 14:09:29 +08:00
|
|
|
available_apps = [
|
|
|
|
'admin_scripts',
|
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes',
|
|
|
|
'django.contrib.sessions',
|
|
|
|
]
|
|
|
|
|
2011-12-23 06:38:02 +08:00
|
|
|
def test_wrong_args(self):
|
2014-06-15 00:12:43 +08:00
|
|
|
"Make sure passing the wrong kinds of arguments outputs an error and prints usage"
|
2011-12-23 06:38:02 +08:00
|
|
|
out, err = self.run_django_admin(['startproject'])
|
|
|
|
self.assertNoOutput(out)
|
2014-06-15 00:12:43 +08:00
|
|
|
self.assertOutput(err, "usage:")
|
2014-06-07 04:39:33 +08:00
|
|
|
self.assertOutput(err, "You must provide a project name.")
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
def test_simple_project(self):
|
|
|
|
"Make sure the startproject management command creates a project"
|
|
|
|
args = ['startproject', 'testproject']
|
2015-02-06 04:29:11 +08:00
|
|
|
testproject_dir = os.path.join(self.test_dir, 'testproject')
|
2012-10-30 02:08:07 +08:00
|
|
|
self.addCleanup(shutil.rmtree, testproject_dir, True)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertTrue(os.path.isdir(testproject_dir))
|
|
|
|
|
|
|
|
# running again..
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2012-01-16 00:06:56 +08:00
|
|
|
self.assertOutput(err, "already exists")
|
2011-12-23 06:38:02 +08:00
|
|
|
|
2011-12-30 03:06:57 +08:00
|
|
|
def test_invalid_project_name(self):
|
2012-01-02 01:45:40 +08:00
|
|
|
"Make sure the startproject management command validates a project name"
|
2012-10-13 22:05:34 +08:00
|
|
|
for bad_name in ('7testproject', '../testproject'):
|
|
|
|
args = ['startproject', bad_name]
|
2015-02-06 04:29:11 +08:00
|
|
|
testproject_dir = os.path.join(self.test_dir, bad_name)
|
2012-10-13 22:05:34 +08:00
|
|
|
self.addCleanup(shutil.rmtree, testproject_dir, True)
|
|
|
|
|
|
|
|
out, err = self.run_django_admin(args)
|
2016-12-01 18:38:01 +08:00
|
|
|
self.assertOutput(
|
|
|
|
err,
|
|
|
|
"Error: '%s' is not a valid project name. Please make "
|
|
|
|
"sure the name is a valid identifier." % bad_name
|
|
|
|
)
|
2012-10-13 22:05:34 +08:00
|
|
|
self.assertFalse(os.path.exists(testproject_dir))
|
2011-12-30 03:06:57 +08:00
|
|
|
|
2017-05-28 18:52:49 +08:00
|
|
|
def test_importable_project_name(self):
|
|
|
|
"""
|
|
|
|
startproject validates that project name doesn't clash with existing
|
|
|
|
Python modules.
|
|
|
|
"""
|
|
|
|
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(
|
|
|
|
err,
|
|
|
|
"CommandError: 'os' conflicts with the name of an existing "
|
|
|
|
"Python module and cannot be used as a project name. Please try "
|
|
|
|
"another name."
|
|
|
|
)
|
|
|
|
self.assertFalse(os.path.exists(testproject_dir))
|
|
|
|
|
2011-12-23 06:38:02 +08:00
|
|
|
def test_simple_project_different_directory(self):
|
|
|
|
"Make sure the startproject management command creates a project in a specific directory"
|
|
|
|
args = ['startproject', 'testproject', 'othertestproject']
|
2015-02-06 04:29:11 +08:00
|
|
|
testproject_dir = os.path.join(self.test_dir, 'othertestproject')
|
2012-01-05 07:55:34 +08:00
|
|
|
os.mkdir(testproject_dir)
|
2012-10-30 02:08:07 +08:00
|
|
|
self.addCleanup(shutil.rmtree, testproject_dir)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
2012-01-05 07:55:34 +08:00
|
|
|
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'manage.py')))
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
# running again..
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
2012-01-05 07:55:34 +08:00
|
|
|
self.assertOutput(err, "already exists")
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
def test_custom_project_template(self):
|
|
|
|
"Make sure the startproject management command is able to use a different project template"
|
2013-02-26 20:19:18 +08:00
|
|
|
template_path = os.path.join(custom_templates_dir, 'project_template')
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['startproject', '--template', template_path, 'customtestproject']
|
2015-02-06 04:29:11 +08:00
|
|
|
testproject_dir = os.path.join(self.test_dir, 'customtestproject')
|
2012-10-30 02:08:07 +08:00
|
|
|
self.addCleanup(shutil.rmtree, testproject_dir, True)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertTrue(os.path.isdir(testproject_dir))
|
|
|
|
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'additional_dir')))
|
|
|
|
|
2011-12-30 01:03:38 +08:00
|
|
|
def test_template_dir_with_trailing_slash(self):
|
|
|
|
"Ticket 17475: Template dir passed has a trailing path separator"
|
2013-02-26 20:19:18 +08:00
|
|
|
template_path = os.path.join(custom_templates_dir, 'project_template' + os.sep)
|
2011-12-30 01:03:38 +08:00
|
|
|
args = ['startproject', '--template', template_path, 'customtestproject']
|
2015-02-06 04:29:11 +08:00
|
|
|
testproject_dir = os.path.join(self.test_dir, 'customtestproject')
|
2012-10-30 02:08:07 +08:00
|
|
|
self.addCleanup(shutil.rmtree, testproject_dir, True)
|
2011-12-30 01:03:38 +08:00
|
|
|
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertTrue(os.path.isdir(testproject_dir))
|
|
|
|
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'additional_dir')))
|
|
|
|
|
2011-12-23 06:38:02 +08:00
|
|
|
def test_custom_project_template_from_tarball_by_path(self):
|
|
|
|
"Make sure the startproject management command is able to use a different project template from a tarball"
|
2013-02-26 20:19:18 +08:00
|
|
|
template_path = os.path.join(custom_templates_dir, 'project_template.tgz')
|
2011-12-23 06:38:02 +08:00
|
|
|
args = ['startproject', '--template', template_path, 'tarballtestproject']
|
2015-02-06 04:29:11 +08:00
|
|
|
testproject_dir = os.path.join(self.test_dir, 'tarballtestproject')
|
2012-10-30 02:08:07 +08:00
|
|
|
self.addCleanup(shutil.rmtree, testproject_dir, True)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertTrue(os.path.isdir(testproject_dir))
|
|
|
|
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'run.py')))
|
|
|
|
|
2012-01-05 07:55:34 +08:00
|
|
|
def test_custom_project_template_from_tarball_to_alternative_location(self):
|
|
|
|
"Startproject can use a project template from a tarball and create it in a specified location"
|
2013-02-26 20:19:18 +08:00
|
|
|
template_path = os.path.join(custom_templates_dir, 'project_template.tgz')
|
2012-01-05 07:55:34 +08:00
|
|
|
args = ['startproject', '--template', template_path, 'tarballtestproject', 'altlocation']
|
2015-02-06 04:29:11 +08:00
|
|
|
testproject_dir = os.path.join(self.test_dir, 'altlocation')
|
2012-01-05 07:55:34 +08:00
|
|
|
os.mkdir(testproject_dir)
|
2012-10-30 02:08:07 +08:00
|
|
|
self.addCleanup(shutil.rmtree, testproject_dir)
|
2012-01-05 07:55:34 +08:00
|
|
|
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertTrue(os.path.isdir(testproject_dir))
|
|
|
|
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'run.py')))
|
|
|
|
|
2011-12-23 06:38:02 +08:00
|
|
|
def test_custom_project_template_from_tarball_by_url(self):
|
2015-09-12 07:33:12 +08:00
|
|
|
"""
|
|
|
|
The startproject management command is able to use a different project
|
|
|
|
template from a tarball via a URL.
|
|
|
|
"""
|
2014-01-14 23:43:27 +08:00
|
|
|
template_url = '%s/custom_templates/project_template.tgz' % self.live_server_url
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
args = ['startproject', '--template', template_url, 'urltestproject']
|
2015-02-06 04:29:11 +08:00
|
|
|
testproject_dir = os.path.join(self.test_dir, 'urltestproject')
|
2012-10-30 02:08:07 +08:00
|
|
|
self.addCleanup(shutil.rmtree, testproject_dir, True)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertTrue(os.path.isdir(testproject_dir))
|
|
|
|
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'run.py')))
|
2012-01-02 01:45:40 +08:00
|
|
|
|
|
|
|
def test_project_template_tarball_url(self):
|
|
|
|
"Startproject management command handles project template tar/zip balls from non-canonical urls"
|
2014-01-14 23:43:27 +08:00
|
|
|
template_url = '%s/custom_templates/project_template.tgz/' % self.live_server_url
|
2012-01-02 01:45:40 +08:00
|
|
|
|
|
|
|
args = ['startproject', '--template', template_url, 'urltestproject']
|
2015-02-06 04:29:11 +08:00
|
|
|
testproject_dir = os.path.join(self.test_dir, 'urltestproject')
|
2012-10-30 02:08:07 +08:00
|
|
|
self.addCleanup(shutil.rmtree, testproject_dir, True)
|
2012-01-02 01:45:40 +08:00
|
|
|
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertTrue(os.path.isdir(testproject_dir))
|
|
|
|
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'run.py')))
|
2012-02-04 21:01:30 +08:00
|
|
|
|
|
|
|
def test_file_without_extension(self):
|
|
|
|
"Make sure the startproject management command is able to render custom files"
|
2013-02-26 20:19:18 +08:00
|
|
|
template_path = os.path.join(custom_templates_dir, 'project_template')
|
2012-02-04 21:01:30 +08:00
|
|
|
args = ['startproject', '--template', template_path, 'customtestproject', '-e', 'txt', '-n', 'Procfile']
|
2015-02-06 04:29:11 +08:00
|
|
|
testproject_dir = os.path.join(self.test_dir, 'customtestproject')
|
2012-10-30 02:08:07 +08:00
|
|
|
self.addCleanup(shutil.rmtree, testproject_dir, True)
|
2012-02-04 21:01:30 +08:00
|
|
|
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertTrue(os.path.isdir(testproject_dir))
|
|
|
|
self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'additional_dir')))
|
|
|
|
base_path = os.path.join(testproject_dir, 'additional_dir')
|
|
|
|
for f in ('Procfile', 'additional_file.py', 'requirements.txt'):
|
|
|
|
self.assertTrue(os.path.exists(os.path.join(base_path, f)))
|
|
|
|
with open(os.path.join(base_path, f)) as fh:
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertEqual(fh.read().strip(), '# some file for customtestproject test project')
|
2012-03-22 06:29:32 +08:00
|
|
|
|
|
|
|
def test_custom_project_template_context_variables(self):
|
|
|
|
"Make sure template context variables are rendered with proper values"
|
2013-02-26 20:19:18 +08:00
|
|
|
template_path = os.path.join(custom_templates_dir, 'project_template')
|
2012-03-22 06:29:32 +08:00
|
|
|
args = ['startproject', '--template', template_path, 'another_project', 'project_dir']
|
2015-02-06 04:29:11 +08:00
|
|
|
testproject_dir = os.path.join(self.test_dir, 'project_dir')
|
2012-03-22 06:29:32 +08:00
|
|
|
os.mkdir(testproject_dir)
|
|
|
|
self.addCleanup(shutil.rmtree, testproject_dir)
|
2012-10-30 02:08:07 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
2012-03-22 06:29:32 +08:00
|
|
|
self.assertNoOutput(err)
|
|
|
|
test_manage_py = os.path.join(testproject_dir, 'manage.py')
|
|
|
|
with open(test_manage_py, 'r') as fp:
|
2017-01-24 19:22:42 +08:00
|
|
|
content = fp.read()
|
2012-03-22 06:29:32 +08:00
|
|
|
self.assertIn("project_name = 'another_project'", content)
|
|
|
|
self.assertIn("project_directory = '%s'" % testproject_dir, content)
|
|
|
|
|
2012-07-17 06:26:31 +08:00
|
|
|
def test_no_escaping_of_project_variables(self):
|
|
|
|
"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')
|
2012-10-30 02:08:07 +08:00
|
|
|
self.addCleanup(self.remove_settings, 'alternate_settings.py')
|
2013-02-26 20:19:18 +08:00
|
|
|
template_path = os.path.join(custom_templates_dir, 'project_template')
|
2015-09-12 07:33:12 +08:00
|
|
|
args = [
|
|
|
|
'custom_startproject', '--template', template_path,
|
|
|
|
'another_project', 'project_dir', '--extra', '<&>',
|
|
|
|
'--settings=alternate_settings',
|
|
|
|
]
|
2015-02-06 04:29:11 +08:00
|
|
|
testproject_dir = os.path.join(self.test_dir, 'project_dir')
|
2012-07-17 06:26:31 +08:00
|
|
|
os.mkdir(testproject_dir)
|
|
|
|
self.addCleanup(shutil.rmtree, testproject_dir)
|
2012-10-30 02:08:07 +08:00
|
|
|
out, err = self.run_manage(args)
|
2012-07-17 06:26:31 +08:00
|
|
|
self.assertNoOutput(err)
|
|
|
|
test_manage_py = os.path.join(testproject_dir, 'additional_dir', 'extra.py')
|
|
|
|
with open(test_manage_py, 'r') as fp:
|
|
|
|
content = fp.read()
|
|
|
|
self.assertIn("<&>", content)
|
|
|
|
|
2012-03-22 06:29:32 +08:00
|
|
|
def test_custom_project_destination_missing(self):
|
|
|
|
"""
|
|
|
|
Make sure an exception is raised when the provided
|
|
|
|
destination directory doesn't exist
|
|
|
|
"""
|
2013-02-26 20:19:18 +08:00
|
|
|
template_path = os.path.join(custom_templates_dir, 'project_template')
|
2012-03-22 06:29:32 +08:00
|
|
|
args = ['startproject', '--template', template_path, 'yet_another_project', 'project_dir2']
|
2015-02-06 04:29:11 +08:00
|
|
|
testproject_dir = os.path.join(self.test_dir, 'project_dir2')
|
2012-03-22 06:29:32 +08:00
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(out)
|
|
|
|
self.assertOutput(err, "Destination directory '%s' does not exist, please create it first." % testproject_dir)
|
|
|
|
self.assertFalse(os.path.exists(testproject_dir))
|
|
|
|
|
2012-08-29 05:22:06 +08:00
|
|
|
def test_custom_project_template_with_non_ascii_templates(self):
|
2015-09-12 07:33:12 +08:00
|
|
|
"""
|
|
|
|
The startproject management command is able to render templates with
|
|
|
|
non-ASCII content.
|
|
|
|
"""
|
2013-02-26 20:19:18 +08:00
|
|
|
template_path = os.path.join(custom_templates_dir, 'project_template')
|
2012-08-29 05:22:06 +08:00
|
|
|
args = ['startproject', '--template', template_path, '--extension=txt', 'customtestproject']
|
2015-02-06 04:29:11 +08:00
|
|
|
testproject_dir = os.path.join(self.test_dir, 'customtestproject')
|
2012-10-30 02:08:07 +08:00
|
|
|
self.addCleanup(shutil.rmtree, testproject_dir, True)
|
2012-08-29 05:22:06 +08:00
|
|
|
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertTrue(os.path.isdir(testproject_dir))
|
2012-08-29 06:02:53 +08:00
|
|
|
path = os.path.join(testproject_dir, 'ticket-18091-non-ascii-template.txt')
|
2013-09-30 01:26:03 +08:00
|
|
|
with codecs.open(path, 'r', encoding='utf-8') as f:
|
|
|
|
self.assertEqual(f.read().splitlines(False), [
|
|
|
|
'Some non-ASCII text for testing ticket #18091:',
|
|
|
|
'üäö €'])
|
2012-10-23 08:13:59 +08:00
|
|
|
|
|
|
|
|
2017-05-28 18:52:49 +08:00
|
|
|
class StartApp(AdminScriptTestCase):
|
|
|
|
|
|
|
|
def test_invalid_name(self):
|
|
|
|
"""startapp validates that app name is a valid Python identifier."""
|
|
|
|
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(
|
|
|
|
err,
|
|
|
|
"CommandError: '{}' is not a valid app name. Please make "
|
|
|
|
"sure the name is a valid identifier.".format(bad_name)
|
|
|
|
)
|
|
|
|
self.assertFalse(os.path.exists(testproject_dir))
|
|
|
|
|
|
|
|
def test_importable_name(self):
|
|
|
|
"""
|
|
|
|
startapp validates that app name doesn't clash with existing Python
|
|
|
|
modules.
|
|
|
|
"""
|
|
|
|
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(
|
|
|
|
err,
|
|
|
|
"CommandError: 'os' conflicts with the name of an existing "
|
|
|
|
"Python module and cannot be used as an app name. Please try "
|
|
|
|
"another name."
|
|
|
|
)
|
|
|
|
self.assertFalse(os.path.exists(testproject_dir))
|
|
|
|
|
|
|
|
|
2012-10-23 08:13:59 +08:00
|
|
|
class DiffSettings(AdminScriptTestCase):
|
|
|
|
"""Tests for diffsettings management command."""
|
2013-03-18 03:48:30 +08:00
|
|
|
|
2012-10-23 08:13:59 +08:00
|
|
|
def test_basic(self):
|
2013-03-18 03:48:30 +08:00
|
|
|
"""Runs without error and emits settings diff."""
|
2012-10-23 08:13:59 +08:00
|
|
|
self.write_settings('settings_to_diff.py', sdict={'FOO': '"bar"'})
|
2012-10-30 02:08:07 +08:00
|
|
|
self.addCleanup(self.remove_settings, 'settings_to_diff.py')
|
2012-10-23 08:13:59 +08:00
|
|
|
args = ['diffsettings', '--settings=settings_to_diff']
|
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, "FOO = 'bar' ###")
|
2013-03-18 03:48:30 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
self.assertOutput(out, "### STATIC_URL = None")
|
2013-05-30 01:47:49 +08:00
|
|
|
|
2016-11-05 19:31:37 +08:00
|
|
|
def test_custom_default(self):
|
|
|
|
"""
|
|
|
|
The --default option specifies an alternate settings module for
|
|
|
|
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")
|
|
|
|
self.assertOutput(out, "BAR = 'bar2'")
|
|
|
|
|
2017-03-08 16:54:17 +08:00
|
|
|
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)
|
|
|
|
self.assertOutput(out, "+ FOO = 'bar'")
|
|
|
|
self.assertOutput(out, "- SECRET_KEY = ''")
|
|
|
|
self.assertOutput(out, "+ SECRET_KEY = 'django_tests_secret_key'")
|
|
|
|
self.assertNotInOutput(out, " APPEND_SLASH = True")
|
|
|
|
|
|
|
|
def test_unified_all(self):
|
|
|
|
"""
|
|
|
|
--output=unified --all emits settings diff in unified mode and includes
|
|
|
|
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)
|
|
|
|
self.assertOutput(out, " APPEND_SLASH = True")
|
|
|
|
self.assertOutput(out, "+ FOO = 'bar'")
|
|
|
|
self.assertOutput(out, "- SECRET_KEY = ''")
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-05-30 01:47:49 +08:00
|
|
|
class Dumpdata(AdminScriptTestCase):
|
|
|
|
"""Tests for dumpdata management command."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.write_settings('settings.py')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
|
|
|
|
|
|
|
def test_pks_parsing(self):
|
|
|
|
"""Regression for #20509
|
|
|
|
|
|
|
|
Test would raise an exception rather than printing an error message.
|
|
|
|
"""
|
|
|
|
args = ['dumpdata', '--pks=1']
|
|
|
|
out, err = self.run_manage(args)
|
|
|
|
self.assertOutput(err, "You can only use --pks option with one model")
|
|
|
|
self.assertNoOutput(out)
|
2015-04-30 23:54:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
class MainModule(AdminScriptTestCase):
|
|
|
|
"""python -m django works like django-admin."""
|
|
|
|
|
|
|
|
def test_runs_django_admin(self):
|
|
|
|
cmd_out, _ = self.run_django_admin(['--version'])
|
|
|
|
mod_out, _ = self.run_test('-m', ['django', '--version'])
|
|
|
|
self.assertEqual(mod_out, cmd_out)
|
2017-08-31 21:22:25 +08:00
|
|
|
|
|
|
|
def test_program_name_in_help(self):
|
|
|
|
out, err = self.run_test('-m', ['django', 'help'])
|
|
|
|
self.assertOutput(out, "Type 'python -m django help <subcommand>' for help on a specific subcommand.")
|