| 1 |
""" |
| 2 |
A series of tests to establish that the command-line management tools work as |
| 3 |
advertised - especially with regards to the handling of the |
| 4 |
DJANGO_SETTINGS_MODULE and default settings.py files. |
| 5 |
""" |
| 6 |
|
| 7 |
import os |
| 8 |
import re |
| 9 |
import shutil |
| 10 |
import socket |
| 11 |
import stat |
| 12 |
import subprocess |
| 13 |
import sys |
| 14 |
import tempfile |
| 15 |
import unittest |
| 16 |
from io import StringIO |
| 17 |
from unittest import mock |
| 18 |
|
| 19 |
from user_commands.utils import AssertFormatterFailureCaughtContext |
| 20 |
|
| 21 |
from django import conf, get_version |
| 22 |
from django.conf import settings |
| 23 |
from django.core.checks import Error, Tags, register |
| 24 |
from django.core.checks.registry import registry |
| 25 |
from django.core.management import ( |
| 26 |
BaseCommand, |
| 27 |
CommandError, |
| 28 |
call_command, |
| 29 |
color, |
| 30 |
execute_from_command_line, |
| 31 |
) |
| 32 |
from django.core.management.base import LabelCommand, SystemCheckError |
| 33 |
from django.core.management.commands.loaddata import Command as LoaddataCommand |
| 34 |
from django.core.management.commands.runserver import Command as RunserverCommand |
| 35 |
from django.core.management.commands.testserver import Command as TestserverCommand |
| 36 |
from django.db import ConnectionHandler, connection |
| 37 |
from django.db.migrations.recorder import MigrationRecorder |
| 38 |
from django.test import LiveServerTestCase, SimpleTestCase, TestCase, override_settings |
| 39 |
from django.test.utils import captured_stderr, captured_stdout |
| 40 |
from django.urls import path |
| 41 |
from django.utils.version import PY313, get_docs_version |
| 42 |
from django.views.static import serve |
| 43 |
|
| 44 |
from . import urls |
| 45 |
|
| 46 |
custom_templates_dir = os.path.join(os.path.dirname(__file__), "custom_templates") |
| 47 |
|
| 48 |
SYSTEM_CHECK_MSG = "System check identified no issues" |
| 49 |
|
| 50 |
HAS_BLACK = shutil.which("black") |
| 51 |
|
| 52 |
|
| 53 |
class AdminScriptTestCase(SimpleTestCase): |
| 54 |
def setUp(self): |
| 55 |
tmpdir = tempfile.TemporaryDirectory() |
| 56 |
self.addCleanup(tmpdir.cleanup) |
| 57 |
# os.path.realpath() is required for temporary directories on macOS, |
| 58 |
# where `/var` is a symlink to `/private/var`. |
| 59 |
self.test_dir = os.path.realpath(os.path.join(tmpdir.name, "test_project")) |
| 60 |
os.mkdir(self.test_dir) |
| 61 |
|
| 62 |
def write_settings(self, filename, apps=None, is_dir=False, sdict=None, extra=None): |
| 63 |
if is_dir: |
| 64 |
settings_dir = os.path.join(self.test_dir, filename) |
| 65 |
os.mkdir(settings_dir) |
| 66 |
settings_file_path = os.path.join(settings_dir, "__init__.py") |
| 67 |
else: |
| 68 |
settings_file_path = os.path.join(self.test_dir, filename) |
| 69 |
|
| 70 |
with open(settings_file_path, "w") as settings_file: |
| 71 |
settings_file.write( |
| 72 |
"# Settings file automatically generated by admin_scripts test case\n" |
| 73 |
) |
| 74 |
if extra: |
| 75 |
settings_file.write("%s\n" % extra) |
| 76 |
exports = [ |
| 77 |
"DATABASES", |
| 78 |
"DEFAULT_AUTO_FIELD", |
| 79 |
"ROOT_URLCONF", |
| 80 |
"SECRET_KEY", |
| 81 |
"USE_TZ", |
| 82 |
] |
| 83 |
for s in exports: |
| 84 |
if hasattr(settings, s): |
| 85 |
o = getattr(settings, s) |
| 86 |
if not isinstance(o, (dict, tuple, list)): |
| 87 |
o = "'%s'" % o |
| 88 |
settings_file.write("%s = %s\n" % (s, o)) |
| 89 |
|
| 90 |
if apps is None: |
| 91 |
apps = [ |
| 92 |
"django.contrib.auth", |
| 93 |
"django.contrib.contenttypes", |
| 94 |
"admin_scripts", |
| 95 |
] |
| 96 |
|
| 97 |
settings_file.write("INSTALLED_APPS = %s\n" % apps) |
| 98 |
|
| 99 |
if sdict: |
| 100 |
for k, v in sdict.items(): |
| 101 |
settings_file.write("%s = %s\n" % (k, v)) |
| 102 |
|
| 103 |
def _ext_backend_paths(self): |
| 104 |
""" |
| 105 |
Returns the paths for any external backend packages. |
| 106 |
""" |
| 107 |
paths = [] |
| 108 |
for backend in settings.DATABASES.values(): |
| 109 |
package = backend["ENGINE"].split(".")[0] |
| 110 |
if package != "django": |
| 111 |
backend_pkg = __import__(package) |
| 112 |
backend_dir = os.path.dirname(backend_pkg.__file__) |
| 113 |
paths.append(os.path.dirname(backend_dir)) |
| 114 |
return paths |
| 115 |
|
| 116 |
def run_test(self, args, settings_file=None, apps=None, umask=-1): |
| 117 |
base_dir = os.path.dirname(self.test_dir) |
| 118 |
# The base dir for Django's tests is one level up. |
| 119 |
tests_dir = os.path.dirname(os.path.dirname(__file__)) |
| 120 |
# The base dir for Django is one level above the test dir. We don't use |
| 121 |
# `import django` to figure that out, so we don't pick up a Django |
| 122 |
# from site-packages or similar. |
| 123 |
django_dir = os.path.dirname(tests_dir) |
| 124 |
ext_backend_base_dirs = self._ext_backend_paths() |
| 125 |
|
| 126 |
# Define a temporary environment for the subprocess |
| 127 |
test_environ = os.environ.copy() |
| 128 |
|
| 129 |
# Set the test environment |
| 130 |
if settings_file: |
| 131 |
test_environ["DJANGO_SETTINGS_MODULE"] = settings_file |
| 132 |
elif "DJANGO_SETTINGS_MODULE" in test_environ: |
| 133 |
del test_environ["DJANGO_SETTINGS_MODULE"] |
| 134 |
python_path = [base_dir, django_dir, tests_dir] |
| 135 |
python_path.extend(ext_backend_base_dirs) |
| 136 |
test_environ["PYTHONPATH"] = os.pathsep.join(python_path) |
| 137 |
test_environ["PYTHONWARNINGS"] = "" |
| 138 |
|
| 139 |
p = subprocess.run( |
| 140 |
[sys.executable, *args], |
| 141 |
capture_output=True, |
| 142 |
cwd=self.test_dir, |
| 143 |
env=test_environ, |
| 144 |
text=True, |
| 145 |
umask=umask, |
| 146 |
) |
| 147 |
return p.stdout, p.stderr |
| 148 |
|
| 149 |
def run_django_admin(self, args, settings_file=None, umask=-1): |
| 150 |
return self.run_test(["-m", "django", *args], settings_file, umask=umask) |
| 151 |
|
| 152 |
def run_manage(self, args, settings_file=None, manage_py=None): |
| 153 |
template_manage_py = ( |
| 154 |
os.path.join(os.path.dirname(__file__), manage_py) |
| 155 |
if manage_py |
| 156 |
else os.path.join( |
| 157 |
os.path.dirname(conf.__file__), "project_template", "manage.py-tpl" |
| 158 |
) |
| 159 |
) |
| 160 |
test_manage_py = os.path.join(self.test_dir, "manage.py") |
| 161 |
shutil.copyfile(template_manage_py, test_manage_py) |
| 162 |
|
| 163 |
with open(test_manage_py) as fp: |
| 164 |
manage_py_contents = fp.read() |
| 165 |
manage_py_contents = manage_py_contents.replace( |
| 166 |
"{{ project_name }}", "test_project" |
| 167 |
) |
| 168 |
with open(test_manage_py, "w") as fp: |
| 169 |
fp.write(manage_py_contents) |
| 170 |
|
| 171 |
return self.run_test(["./manage.py", *args], settings_file) |
| 172 |
|
| 173 |
def assertNoOutput(self, stream): |
| 174 |
"Utility assertion: assert that the given stream is empty" |
| 175 |
self.assertEqual( |
| 176 |
len(stream), 0, "Stream should be empty: actually contains '%s'" % stream |
| 177 |
) |
| 178 |
|
| 179 |
def assertOutput(self, stream, msg, regex=False): |
| 180 |
"Utility assertion: assert that the given message exists in the output" |
| 181 |
if regex: |
| 182 |
self.assertIsNotNone( |
| 183 |
re.search(msg, stream), |
| 184 |
"'%s' does not match actual output text '%s'" % (msg, stream), |
| 185 |
) |
| 186 |
else: |
| 187 |
self.assertIn( |
| 188 |
msg, |
| 189 |
stream, |
| 190 |
"'%s' does not match actual output text '%s'" % (msg, stream), |
| 191 |
) |
| 192 |
|
| 193 |
def assertNotInOutput(self, stream, msg): |
| 194 |
"Utility assertion: assert that the given message doesn't exist in the output" |
| 195 |
self.assertNotIn( |
| 196 |
msg, stream, "'%s' matches actual output text '%s'" % (msg, stream) |
| 197 |
) |
| 198 |
|
| 199 |
|
| 200 |
########################################################################## |
| 201 |
# DJANGO ADMIN TESTS |
| 202 |
# This first series of test classes checks the environment processing |
| 203 |
# of the django-admin. |
| 204 |
########################################################################## |
| 205 |
|
| 206 |
|
| 207 |
class DjangoAdminNoSettings(AdminScriptTestCase): |
| 208 |
"A series of tests for django-admin when there is no settings.py file." |
| 209 |
|
| 210 |
def test_builtin_command(self): |
| 211 |
""" |
| 212 |
no settings: django-admin builtin commands fail with an error when no |
| 213 |
settings provided. |
| 214 |
""" |
| 215 |
args = ["check", "admin_scripts"] |
| 216 |
out, err = self.run_django_admin(args) |
| 217 |
self.assertNoOutput(out) |
| 218 |
self.assertOutput(err, "settings are not configured") |
| 219 |
|
| 220 |
def test_builtin_with_bad_settings(self): |
| 221 |
""" |
| 222 |
no settings: django-admin builtin commands fail if settings file (from |
| 223 |
argument) doesn't exist. |
| 224 |
""" |
| 225 |
args = ["check", "--settings=bad_settings", "admin_scripts"] |
| 226 |
out, err = self.run_django_admin(args) |
| 227 |
self.assertNoOutput(out) |
| 228 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 229 |
|
| 230 |
def test_builtin_with_bad_environment(self): |
| 231 |
""" |
| 232 |
no settings: django-admin builtin commands fail if settings file (from |
| 233 |
environment) doesn't exist. |
| 234 |
""" |
| 235 |
args = ["check", "admin_scripts"] |
| 236 |
out, err = self.run_django_admin(args, "bad_settings") |
| 237 |
self.assertNoOutput(out) |
| 238 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 239 |
|
| 240 |
def test_commands_with_invalid_settings(self): |
| 241 |
""" |
| 242 |
Commands that don't require settings succeed if the settings file |
| 243 |
doesn't exist. |
| 244 |
""" |
| 245 |
args = ["startproject"] |
| 246 |
out, err = self.run_django_admin(args, settings_file="bad_settings") |
| 247 |
self.assertNoOutput(out) |
| 248 |
self.assertOutput(err, "You must provide a project name", regex=True) |
| 249 |
|
| 250 |
|
| 251 |
class DjangoAdminDefaultSettings(AdminScriptTestCase): |
| 252 |
""" |
| 253 |
A series of tests for django-admin when using a settings.py file that |
| 254 |
contains the test application. |
| 255 |
""" |
| 256 |
|
| 257 |
def setUp(self): |
| 258 |
super().setUp() |
| 259 |
self.write_settings("settings.py") |
| 260 |
|
| 261 |
def test_builtin_command(self): |
| 262 |
""" |
| 263 |
default: django-admin builtin commands fail with an error when no |
| 264 |
settings provided. |
| 265 |
""" |
| 266 |
args = ["check", "admin_scripts"] |
| 267 |
out, err = self.run_django_admin(args) |
| 268 |
self.assertNoOutput(out) |
| 269 |
self.assertOutput(err, "settings are not configured") |
| 270 |
|
| 271 |
def test_builtin_with_settings(self): |
| 272 |
""" |
| 273 |
default: django-admin builtin commands succeed if settings are provided |
| 274 |
as argument. |
| 275 |
""" |
| 276 |
args = ["check", "--settings=test_project.settings", "admin_scripts"] |
| 277 |
out, err = self.run_django_admin(args) |
| 278 |
self.assertNoOutput(err) |
| 279 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 280 |
|
| 281 |
def test_builtin_with_environment(self): |
| 282 |
""" |
| 283 |
default: django-admin builtin commands succeed if settings are provided |
| 284 |
in the environment. |
| 285 |
""" |
| 286 |
args = ["check", "admin_scripts"] |
| 287 |
out, err = self.run_django_admin(args, "test_project.settings") |
| 288 |
self.assertNoOutput(err) |
| 289 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 290 |
|
| 291 |
def test_builtin_with_bad_settings(self): |
| 292 |
""" |
| 293 |
default: django-admin builtin commands fail if settings file (from |
| 294 |
argument) doesn't exist. |
| 295 |
""" |
| 296 |
args = ["check", "--settings=bad_settings", "admin_scripts"] |
| 297 |
out, err = self.run_django_admin(args) |
| 298 |
self.assertNoOutput(out) |
| 299 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 300 |
|
| 301 |
def test_builtin_with_bad_environment(self): |
| 302 |
""" |
| 303 |
default: django-admin builtin commands fail if settings file (from |
| 304 |
environment) doesn't exist. |
| 305 |
""" |
| 306 |
args = ["check", "admin_scripts"] |
| 307 |
out, err = self.run_django_admin(args, "bad_settings") |
| 308 |
self.assertNoOutput(out) |
| 309 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 310 |
|
| 311 |
def test_custom_command(self): |
| 312 |
""" |
| 313 |
default: django-admin can't execute user commands if it isn't provided |
| 314 |
settings. |
| 315 |
""" |
| 316 |
args = ["noargs_command"] |
| 317 |
out, err = self.run_django_admin(args) |
| 318 |
self.assertNoOutput(out) |
| 319 |
self.assertOutput(err, "No Django settings specified") |
| 320 |
self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 321 |
|
| 322 |
def test_custom_command_with_settings(self): |
| 323 |
""" |
| 324 |
default: django-admin can execute user commands if settings are |
| 325 |
provided as argument. |
| 326 |
""" |
| 327 |
args = ["noargs_command", "--settings=test_project.settings"] |
| 328 |
out, err = self.run_django_admin(args) |
| 329 |
self.assertNoOutput(err) |
| 330 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 331 |
|
| 332 |
def test_custom_command_with_environment(self): |
| 333 |
""" |
| 334 |
default: django-admin can execute user commands if settings are |
| 335 |
provided in environment. |
| 336 |
""" |
| 337 |
args = ["noargs_command"] |
| 338 |
out, err = self.run_django_admin(args, "test_project.settings") |
| 339 |
self.assertNoOutput(err) |
| 340 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 341 |
|
| 342 |
|
| 343 |
class DjangoAdminFullPathDefaultSettings(AdminScriptTestCase): |
| 344 |
""" |
| 345 |
A series of tests for django-admin when using a settings.py file that |
| 346 |
contains the test application specified using a full path. |
| 347 |
""" |
| 348 |
|
| 349 |
def setUp(self): |
| 350 |
super().setUp() |
| 351 |
self.write_settings( |
| 352 |
"settings.py", |
| 353 |
[ |
| 354 |
"django.contrib.auth", |
| 355 |
"django.contrib.contenttypes", |
| 356 |
"admin_scripts", |
| 357 |
"admin_scripts.complex_app", |
| 358 |
], |
| 359 |
) |
| 360 |
|
| 361 |
def test_builtin_command(self): |
| 362 |
""" |
| 363 |
fulldefault: django-admin builtin commands fail with an error when no |
| 364 |
settings provided. |
| 365 |
""" |
| 366 |
args = ["check", "admin_scripts"] |
| 367 |
out, err = self.run_django_admin(args) |
| 368 |
self.assertNoOutput(out) |
| 369 |
self.assertOutput(err, "settings are not configured") |
| 370 |
|
| 371 |
def test_builtin_with_settings(self): |
| 372 |
""" |
| 373 |
fulldefault: django-admin builtin commands succeed if a settings file |
| 374 |
is provided. |
| 375 |
""" |
| 376 |
args = ["check", "--settings=test_project.settings", "admin_scripts"] |
| 377 |
out, err = self.run_django_admin(args) |
| 378 |
self.assertNoOutput(err) |
| 379 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 380 |
|
| 381 |
def test_builtin_with_environment(self): |
| 382 |
""" |
| 383 |
fulldefault: django-admin builtin commands succeed if the environment |
| 384 |
contains settings. |
| 385 |
""" |
| 386 |
args = ["check", "admin_scripts"] |
| 387 |
out, err = self.run_django_admin(args, "test_project.settings") |
| 388 |
self.assertNoOutput(err) |
| 389 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 390 |
|
| 391 |
def test_builtin_with_bad_settings(self): |
| 392 |
""" |
| 393 |
fulldefault: django-admin builtin commands fail if settings file (from |
| 394 |
argument) doesn't exist. |
| 395 |
""" |
| 396 |
args = ["check", "--settings=bad_settings", "admin_scripts"] |
| 397 |
out, err = self.run_django_admin(args) |
| 398 |
self.assertNoOutput(out) |
| 399 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 400 |
|
| 401 |
def test_builtin_with_bad_environment(self): |
| 402 |
""" |
| 403 |
fulldefault: django-admin builtin commands fail if settings file (from |
| 404 |
environment) doesn't exist. |
| 405 |
""" |
| 406 |
args = ["check", "admin_scripts"] |
| 407 |
out, err = self.run_django_admin(args, "bad_settings") |
| 408 |
self.assertNoOutput(out) |
| 409 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 410 |
|
| 411 |
def test_custom_command(self): |
| 412 |
""" |
| 413 |
fulldefault: django-admin can't execute user commands unless settings |
| 414 |
are provided. |
| 415 |
""" |
| 416 |
args = ["noargs_command"] |
| 417 |
out, err = self.run_django_admin(args) |
| 418 |
self.assertNoOutput(out) |
| 419 |
self.assertOutput(err, "No Django settings specified") |
| 420 |
self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 421 |
|
| 422 |
def test_custom_command_with_settings(self): |
| 423 |
""" |
| 424 |
fulldefault: django-admin can execute user commands if settings are |
| 425 |
provided as argument. |
| 426 |
""" |
| 427 |
args = ["noargs_command", "--settings=test_project.settings"] |
| 428 |
out, err = self.run_django_admin(args) |
| 429 |
self.assertNoOutput(err) |
| 430 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 431 |
|
| 432 |
def test_custom_command_with_environment(self): |
| 433 |
""" |
| 434 |
fulldefault: django-admin can execute user commands if settings are |
| 435 |
provided in environment. |
| 436 |
""" |
| 437 |
args = ["noargs_command"] |
| 438 |
out, err = self.run_django_admin(args, "test_project.settings") |
| 439 |
self.assertNoOutput(err) |
| 440 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 441 |
|
| 442 |
|
| 443 |
class DjangoAdminMinimalSettings(AdminScriptTestCase): |
| 444 |
""" |
| 445 |
A series of tests for django-admin when using a settings.py file that |
| 446 |
doesn't contain the test application. |
| 447 |
""" |
| 448 |
|
| 449 |
def setUp(self): |
| 450 |
super().setUp() |
| 451 |
self.write_settings( |
| 452 |
"settings.py", apps=["django.contrib.auth", "django.contrib.contenttypes"] |
| 453 |
) |
| 454 |
|
| 455 |
def test_builtin_command(self): |
| 456 |
""" |
| 457 |
minimal: django-admin builtin commands fail with an error when no |
| 458 |
settings provided. |
| 459 |
""" |
| 460 |
args = ["check", "admin_scripts"] |
| 461 |
out, err = self.run_django_admin(args) |
| 462 |
self.assertNoOutput(out) |
| 463 |
self.assertOutput(err, "settings are not configured") |
| 464 |
|
| 465 |
def test_builtin_with_settings(self): |
| 466 |
""" |
| 467 |
minimal: django-admin builtin commands fail if settings are provided as |
| 468 |
argument. |
| 469 |
""" |
| 470 |
args = ["check", "--settings=test_project.settings", "admin_scripts"] |
| 471 |
out, err = self.run_django_admin(args) |
| 472 |
self.assertNoOutput(out) |
| 473 |
self.assertOutput(err, "No installed app with label 'admin_scripts'.") |
| 474 |
|
| 475 |
def test_builtin_with_environment(self): |
| 476 |
""" |
| 477 |
minimal: django-admin builtin commands fail if settings are provided in |
| 478 |
the environment. |
| 479 |
""" |
| 480 |
args = ["check", "admin_scripts"] |
| 481 |
out, err = self.run_django_admin(args, "test_project.settings") |
| 482 |
self.assertNoOutput(out) |
| 483 |
self.assertOutput(err, "No installed app with label 'admin_scripts'.") |
| 484 |
|
| 485 |
def test_builtin_with_bad_settings(self): |
| 486 |
""" |
| 487 |
minimal: django-admin builtin commands fail if settings file (from |
| 488 |
argument) doesn't exist. |
| 489 |
""" |
| 490 |
args = ["check", "--settings=bad_settings", "admin_scripts"] |
| 491 |
out, err = self.run_django_admin(args) |
| 492 |
self.assertNoOutput(out) |
| 493 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 494 |
|
| 495 |
def test_builtin_with_bad_environment(self): |
| 496 |
""" |
| 497 |
minimal: django-admin builtin commands fail if settings file (from |
| 498 |
environment) doesn't exist. |
| 499 |
""" |
| 500 |
args = ["check", "admin_scripts"] |
| 501 |
out, err = self.run_django_admin(args, "bad_settings") |
| 502 |
self.assertNoOutput(out) |
| 503 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 504 |
|
| 505 |
def test_custom_command(self): |
| 506 |
"minimal: django-admin can't execute user commands unless settings are provided" |
| 507 |
args = ["noargs_command"] |
| 508 |
out, err = self.run_django_admin(args) |
| 509 |
self.assertNoOutput(out) |
| 510 |
self.assertOutput(err, "No Django settings specified") |
| 511 |
self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 512 |
|
| 513 |
def test_custom_command_with_settings(self): |
| 514 |
""" |
| 515 |
minimal: django-admin can't execute user commands, even if settings are |
| 516 |
provided as argument. |
| 517 |
""" |
| 518 |
args = ["noargs_command", "--settings=test_project.settings"] |
| 519 |
out, err = self.run_django_admin(args) |
| 520 |
self.assertNoOutput(out) |
| 521 |
self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 522 |
|
| 523 |
def test_custom_command_with_environment(self): |
| 524 |
""" |
| 525 |
minimal: django-admin can't execute user commands, even if settings are |
| 526 |
provided in environment. |
| 527 |
""" |
| 528 |
args = ["noargs_command"] |
| 529 |
out, err = self.run_django_admin(args, "test_project.settings") |
| 530 |
self.assertNoOutput(out) |
| 531 |
self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 532 |
|
| 533 |
|
| 534 |
class DjangoAdminAlternateSettings(AdminScriptTestCase): |
| 535 |
""" |
| 536 |
A series of tests for django-admin when using a settings file with a name |
| 537 |
other than 'settings.py'. |
| 538 |
""" |
| 539 |
|
| 540 |
def setUp(self): |
| 541 |
super().setUp() |
| 542 |
self.write_settings("alternate_settings.py") |
| 543 |
|
| 544 |
def test_builtin_command(self): |
| 545 |
""" |
| 546 |
alternate: django-admin builtin commands fail with an error when no |
| 547 |
settings provided. |
| 548 |
""" |
| 549 |
args = ["check", "admin_scripts"] |
| 550 |
out, err = self.run_django_admin(args) |
| 551 |
self.assertNoOutput(out) |
| 552 |
self.assertOutput(err, "settings are not configured") |
| 553 |
|
| 554 |
def test_builtin_with_settings(self): |
| 555 |
""" |
| 556 |
alternate: django-admin builtin commands succeed if settings are |
| 557 |
provided as argument. |
| 558 |
""" |
| 559 |
args = ["check", "--settings=test_project.alternate_settings", "admin_scripts"] |
| 560 |
out, err = self.run_django_admin(args) |
| 561 |
self.assertNoOutput(err) |
| 562 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 563 |
|
| 564 |
def test_builtin_with_environment(self): |
| 565 |
""" |
| 566 |
alternate: django-admin builtin commands succeed if settings are |
| 567 |
provided in the environment. |
| 568 |
""" |
| 569 |
args = ["check", "admin_scripts"] |
| 570 |
out, err = self.run_django_admin(args, "test_project.alternate_settings") |
| 571 |
self.assertNoOutput(err) |
| 572 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 573 |
|
| 574 |
def test_builtin_with_bad_settings(self): |
| 575 |
""" |
| 576 |
alternate: django-admin builtin commands fail if settings file (from |
| 577 |
argument) doesn't exist. |
| 578 |
""" |
| 579 |
args = ["check", "--settings=bad_settings", "admin_scripts"] |
| 580 |
out, err = self.run_django_admin(args) |
| 581 |
self.assertNoOutput(out) |
| 582 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 583 |
|
| 584 |
def test_builtin_with_bad_environment(self): |
| 585 |
""" |
| 586 |
alternate: django-admin builtin commands fail if settings file (from |
| 587 |
environment) doesn't exist. |
| 588 |
""" |
| 589 |
args = ["check", "admin_scripts"] |
| 590 |
out, err = self.run_django_admin(args, "bad_settings") |
| 591 |
self.assertNoOutput(out) |
| 592 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 593 |
|
| 594 |
def test_custom_command(self): |
| 595 |
""" |
| 596 |
alternate: django-admin can't execute user commands unless settings |
| 597 |
are provided. |
| 598 |
""" |
| 599 |
args = ["noargs_command"] |
| 600 |
out, err = self.run_django_admin(args) |
| 601 |
self.assertNoOutput(out) |
| 602 |
self.assertOutput(err, "No Django settings specified") |
| 603 |
self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 604 |
|
| 605 |
def test_custom_command_with_settings(self): |
| 606 |
""" |
| 607 |
alternate: django-admin can execute user commands if settings are |
| 608 |
provided as argument. |
| 609 |
""" |
| 610 |
args = ["noargs_command", "--settings=test_project.alternate_settings"] |
| 611 |
out, err = self.run_django_admin(args) |
| 612 |
self.assertNoOutput(err) |
| 613 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 614 |
|
| 615 |
def test_custom_command_with_environment(self): |
| 616 |
""" |
| 617 |
alternate: django-admin can execute user commands if settings are |
| 618 |
provided in environment. |
| 619 |
""" |
| 620 |
args = ["noargs_command"] |
| 621 |
out, err = self.run_django_admin(args, "test_project.alternate_settings") |
| 622 |
self.assertNoOutput(err) |
| 623 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 624 |
|
| 625 |
|
| 626 |
class DjangoAdminMultipleSettings(AdminScriptTestCase): |
| 627 |
""" |
| 628 |
A series of tests for django-admin when multiple settings files |
| 629 |
(including the default 'settings.py') are available. The default settings |
| 630 |
file is insufficient for performing the operations described, so the |
| 631 |
alternate settings must be used by the running script. |
| 632 |
""" |
| 633 |
|
| 634 |
def setUp(self): |
| 635 |
super().setUp() |
| 636 |
self.write_settings( |
| 637 |
"settings.py", apps=["django.contrib.auth", "django.contrib.contenttypes"] |
| 638 |
) |
| 639 |
self.write_settings("alternate_settings.py") |
| 640 |
|
| 641 |
def test_builtin_command(self): |
| 642 |
""" |
| 643 |
alternate: django-admin builtin commands fail with an error when no |
| 644 |
settings provided. |
| 645 |
""" |
| 646 |
args = ["check", "admin_scripts"] |
| 647 |
out, err = self.run_django_admin(args) |
| 648 |
self.assertNoOutput(out) |
| 649 |
self.assertOutput(err, "settings are not configured") |
| 650 |
|
| 651 |
def test_builtin_with_settings(self): |
| 652 |
""" |
| 653 |
alternate: django-admin builtin commands succeed if settings are |
| 654 |
provided as argument. |
| 655 |
""" |
| 656 |
args = ["check", "--settings=test_project.alternate_settings", "admin_scripts"] |
| 657 |
out, err = self.run_django_admin(args) |
| 658 |
self.assertNoOutput(err) |
| 659 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 660 |
|
| 661 |
def test_builtin_with_environment(self): |
| 662 |
""" |
| 663 |
alternate: django-admin builtin commands succeed if settings are |
| 664 |
provided in the environment. |
| 665 |
""" |
| 666 |
args = ["check", "admin_scripts"] |
| 667 |
out, err = self.run_django_admin(args, "test_project.alternate_settings") |
| 668 |
self.assertNoOutput(err) |
| 669 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 670 |
|
| 671 |
def test_builtin_with_bad_settings(self): |
| 672 |
""" |
| 673 |
alternate: django-admin builtin commands fail if settings file (from |
| 674 |
argument) doesn't exist. |
| 675 |
""" |
| 676 |
args = ["check", "--settings=bad_settings", "admin_scripts"] |
| 677 |
out, err = self.run_django_admin(args) |
| 678 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 679 |
|
| 680 |
def test_builtin_with_bad_environment(self): |
| 681 |
""" |
| 682 |
alternate: django-admin builtin commands fail if settings file (from |
| 683 |
environment) doesn't exist. |
| 684 |
""" |
| 685 |
args = ["check", "admin_scripts"] |
| 686 |
out, err = self.run_django_admin(args, "bad_settings") |
| 687 |
self.assertNoOutput(out) |
| 688 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 689 |
|
| 690 |
def test_custom_command(self): |
| 691 |
""" |
| 692 |
alternate: django-admin can't execute user commands unless settings are |
| 693 |
provided. |
| 694 |
""" |
| 695 |
args = ["noargs_command"] |
| 696 |
out, err = self.run_django_admin(args) |
| 697 |
self.assertNoOutput(out) |
| 698 |
self.assertOutput(err, "No Django settings specified") |
| 699 |
self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 700 |
|
| 701 |
def test_custom_command_with_settings(self): |
| 702 |
""" |
| 703 |
alternate: django-admin can execute user commands if settings are |
| 704 |
provided as argument. |
| 705 |
""" |
| 706 |
args = ["noargs_command", "--settings=test_project.alternate_settings"] |
| 707 |
out, err = self.run_django_admin(args) |
| 708 |
self.assertNoOutput(err) |
| 709 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 710 |
|
| 711 |
def test_custom_command_with_environment(self): |
| 712 |
""" |
| 713 |
alternate: django-admin can execute user commands if settings are |
| 714 |
provided in environment. |
| 715 |
""" |
| 716 |
args = ["noargs_command"] |
| 717 |
out, err = self.run_django_admin(args, "test_project.alternate_settings") |
| 718 |
self.assertNoOutput(err) |
| 719 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 720 |
|
| 721 |
|
| 722 |
class DjangoAdminSettingsDirectory(AdminScriptTestCase): |
| 723 |
""" |
| 724 |
A series of tests for django-admin when the settings file is in a |
| 725 |
directory. (see #9751). |
| 726 |
""" |
| 727 |
|
| 728 |
def setUp(self): |
| 729 |
super().setUp() |
| 730 |
self.write_settings("settings", is_dir=True) |
| 731 |
|
| 732 |
def test_setup_environ(self): |
| 733 |
"directory: startapp creates the correct directory" |
| 734 |
args = ["startapp", "settings_test"] |
| 735 |
app_path = os.path.join(self.test_dir, "settings_test") |
| 736 |
out, err = self.run_django_admin(args, "test_project.settings") |
| 737 |
self.assertNoOutput(err) |
| 738 |
self.assertTrue(os.path.exists(app_path)) |
| 739 |
with open(os.path.join(app_path, "apps.py")) as f: |
| 740 |
content = f.read() |
| 741 |
self.assertIn("class SettingsTestConfig(AppConfig)", content) |
| 742 |
self.assertIn( |
| 743 |
'name = "settings_test"' if HAS_BLACK else "name = 'settings_test'", |
| 744 |
content, |
| 745 |
) |
| 746 |
|
| 747 |
def test_setup_environ_custom_template(self): |
| 748 |
"directory: startapp creates the correct directory with a custom template" |
| 749 |
template_path = os.path.join(custom_templates_dir, "app_template") |
| 750 |
args = ["startapp", "--template", template_path, "custom_settings_test"] |
| 751 |
app_path = os.path.join(self.test_dir, "custom_settings_test") |
| 752 |
out, err = self.run_django_admin(args, "test_project.settings") |
| 753 |
self.assertNoOutput(err) |
| 754 |
self.assertTrue(os.path.exists(app_path)) |
| 755 |
self.assertTrue(os.path.exists(os.path.join(app_path, "api.py"))) |
| 756 |
|
| 757 |
def test_startapp_unicode_name(self): |
| 758 |
"""startapp creates the correct directory with Unicode characters.""" |
| 759 |
args = ["startapp", "こんにちは"] |
| 760 |
app_path = os.path.join(self.test_dir, "こんにちは") |
| 761 |
out, err = self.run_django_admin(args, "test_project.settings") |
| 762 |
self.assertNoOutput(err) |
| 763 |
self.assertTrue(os.path.exists(app_path)) |
| 764 |
with open(os.path.join(app_path, "apps.py"), encoding="utf8") as f: |
| 765 |
content = f.read() |
| 766 |
self.assertIn("class こんにちはConfig(AppConfig)", content) |
| 767 |
self.assertIn( |
| 768 |
'name = "こんにちは"' if HAS_BLACK else "name = 'こんにちは'", content |
| 769 |
) |
| 770 |
|
| 771 |
def test_builtin_command(self): |
| 772 |
""" |
| 773 |
directory: django-admin builtin commands fail with an error when no |
| 774 |
settings provided. |
| 775 |
""" |
| 776 |
args = ["check", "admin_scripts"] |
| 777 |
out, err = self.run_django_admin(args) |
| 778 |
self.assertNoOutput(out) |
| 779 |
self.assertOutput(err, "settings are not configured") |
| 780 |
|
| 781 |
def test_builtin_with_bad_settings(self): |
| 782 |
""" |
| 783 |
directory: django-admin builtin commands fail if settings file (from |
| 784 |
argument) doesn't exist. |
| 785 |
""" |
| 786 |
args = ["check", "--settings=bad_settings", "admin_scripts"] |
| 787 |
out, err = self.run_django_admin(args) |
| 788 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 789 |
|
| 790 |
def test_builtin_with_bad_environment(self): |
| 791 |
""" |
| 792 |
directory: django-admin builtin commands fail if settings file (from |
| 793 |
environment) doesn't exist. |
| 794 |
""" |
| 795 |
args = ["check", "admin_scripts"] |
| 796 |
out, err = self.run_django_admin(args, "bad_settings") |
| 797 |
self.assertNoOutput(out) |
| 798 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 799 |
|
| 800 |
def test_custom_command(self): |
| 801 |
""" |
| 802 |
directory: django-admin can't execute user commands unless settings are |
| 803 |
provided. |
| 804 |
""" |
| 805 |
args = ["noargs_command"] |
| 806 |
out, err = self.run_django_admin(args) |
| 807 |
self.assertNoOutput(out) |
| 808 |
self.assertOutput(err, "No Django settings specified") |
| 809 |
self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 810 |
|
| 811 |
def test_builtin_with_settings(self): |
| 812 |
""" |
| 813 |
directory: django-admin builtin commands succeed if settings are |
| 814 |
provided as argument. |
| 815 |
""" |
| 816 |
args = ["check", "--settings=test_project.settings", "admin_scripts"] |
| 817 |
out, err = self.run_django_admin(args) |
| 818 |
self.assertNoOutput(err) |
| 819 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 820 |
|
| 821 |
def test_builtin_with_environment(self): |
| 822 |
""" |
| 823 |
directory: django-admin builtin commands succeed if settings are |
| 824 |
provided in the environment. |
| 825 |
""" |
| 826 |
args = ["check", "admin_scripts"] |
| 827 |
out, err = self.run_django_admin(args, "test_project.settings") |
| 828 |
self.assertNoOutput(err) |
| 829 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 830 |
|
| 831 |
|
| 832 |
########################################################################## |
| 833 |
# MANAGE.PY TESTS |
| 834 |
# This next series of test classes checks the environment processing |
| 835 |
# of the generated manage.py script |
| 836 |
########################################################################## |
| 837 |
|
| 838 |
|
| 839 |
class ManageManuallyConfiguredSettings(AdminScriptTestCase): |
| 840 |
"""Customized manage.py calling settings.configure().""" |
| 841 |
|
| 842 |
def test_non_existent_command_output(self): |
| 843 |
out, err = self.run_manage( |
| 844 |
["invalid_command"], manage_py="configured_settings_manage.py" |
| 845 |
) |
| 846 |
self.assertNoOutput(out) |
| 847 |
self.assertOutput(err, "Unknown command: 'invalid_command'") |
| 848 |
self.assertNotInOutput(err, "No Django settings specified") |
| 849 |
|
| 850 |
|
| 851 |
class ManageNoSettings(AdminScriptTestCase): |
| 852 |
"A series of tests for manage.py when there is no settings.py file." |
| 853 |
|
| 854 |
def test_builtin_command(self): |
| 855 |
""" |
| 856 |
no settings: manage.py builtin commands fail with an error when no |
| 857 |
settings provided. |
| 858 |
""" |
| 859 |
args = ["check", "admin_scripts"] |
| 860 |
out, err = self.run_manage(args) |
| 861 |
self.assertNoOutput(out) |
| 862 |
self.assertOutput( |
| 863 |
err, r"No module named '?(test_project\.)?settings'?", regex=True |
| 864 |
) |
| 865 |
|
| 866 |
def test_builtin_with_bad_settings(self): |
| 867 |
""" |
| 868 |
no settings: manage.py builtin commands fail if settings file (from |
| 869 |
argument) doesn't exist. |
| 870 |
""" |
| 871 |
args = ["check", "--settings=bad_settings", "admin_scripts"] |
| 872 |
out, err = self.run_manage(args) |
| 873 |
self.assertNoOutput(out) |
| 874 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 875 |
|
| 876 |
def test_builtin_with_bad_environment(self): |
| 877 |
""" |
| 878 |
no settings: manage.py builtin commands fail if settings file (from |
| 879 |
environment) doesn't exist. |
| 880 |
""" |
| 881 |
args = ["check", "admin_scripts"] |
| 882 |
out, err = self.run_manage(args, "bad_settings") |
| 883 |
self.assertNoOutput(out) |
| 884 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 885 |
|
| 886 |
|
| 887 |
class ManageDefaultSettings(AdminScriptTestCase): |
| 888 |
"""A series of tests for manage.py when using a settings.py file that |
| 889 |
contains the test application. |
| 890 |
""" |
| 891 |
|
| 892 |
def setUp(self): |
| 893 |
super().setUp() |
| 894 |
self.write_settings("settings.py") |
| 895 |
|
| 896 |
def test_builtin_command(self): |
| 897 |
""" |
| 898 |
default: manage.py builtin commands succeed when default settings are |
| 899 |
appropriate. |
| 900 |
""" |
| 901 |
args = ["check", "admin_scripts"] |
| 902 |
out, err = self.run_manage(args) |
| 903 |
self.assertNoOutput(err) |
| 904 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 905 |
|
| 906 |
def test_builtin_with_settings(self): |
| 907 |
""" |
| 908 |
default: manage.py builtin commands succeed if settings are provided as |
| 909 |
argument. |
| 910 |
""" |
| 911 |
args = ["check", "--settings=test_project.settings", "admin_scripts"] |
| 912 |
out, err = self.run_manage(args) |
| 913 |
self.assertNoOutput(err) |
| 914 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 915 |
|
| 916 |
def test_builtin_with_environment(self): |
| 917 |
""" |
| 918 |
default: manage.py builtin commands succeed if settings are provided in |
| 919 |
the environment. |
| 920 |
""" |
| 921 |
args = ["check", "admin_scripts"] |
| 922 |
out, err = self.run_manage(args, "test_project.settings") |
| 923 |
self.assertNoOutput(err) |
| 924 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 925 |
|
| 926 |
def test_builtin_with_bad_settings(self): |
| 927 |
""" |
| 928 |
default: manage.py builtin commands succeed if settings file (from |
| 929 |
argument) doesn't exist. |
| 930 |
""" |
| 931 |
args = ["check", "--settings=bad_settings", "admin_scripts"] |
| 932 |
out, err = self.run_manage(args) |
| 933 |
self.assertNoOutput(out) |
| 934 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 935 |
|
| 936 |
def test_builtin_with_bad_environment(self): |
| 937 |
""" |
| 938 |
default: manage.py builtin commands fail if settings file (from |
| 939 |
environment) doesn't exist. |
| 940 |
""" |
| 941 |
args = ["check", "admin_scripts"] |
| 942 |
out, err = self.run_manage(args, "bad_settings") |
| 943 |
self.assertNoOutput(out) |
| 944 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 945 |
|
| 946 |
def test_custom_command(self): |
| 947 |
""" |
| 948 |
default: manage.py can execute user commands when default settings are |
| 949 |
appropriate. |
| 950 |
""" |
| 951 |
args = ["noargs_command"] |
| 952 |
out, err = self.run_manage(args) |
| 953 |
self.assertNoOutput(err) |
| 954 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 955 |
|
| 956 |
def test_custom_command_with_settings(self): |
| 957 |
""" |
| 958 |
default: manage.py can execute user commands when settings are provided |
| 959 |
as argument. |
| 960 |
""" |
| 961 |
args = ["noargs_command", "--settings=test_project.settings"] |
| 962 |
out, err = self.run_manage(args) |
| 963 |
self.assertNoOutput(err) |
| 964 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 965 |
|
| 966 |
def test_custom_command_with_environment(self): |
| 967 |
""" |
| 968 |
default: manage.py can execute user commands when settings are provided |
| 969 |
in environment. |
| 970 |
""" |
| 971 |
args = ["noargs_command"] |
| 972 |
out, err = self.run_manage(args, "test_project.settings") |
| 973 |
self.assertNoOutput(err) |
| 974 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 975 |
|
| 976 |
|
| 977 |
class ManageFullPathDefaultSettings(AdminScriptTestCase): |
| 978 |
"""A series of tests for manage.py when using a settings.py file that |
| 979 |
contains the test application specified using a full path. |
| 980 |
""" |
| 981 |
|
| 982 |
def setUp(self): |
| 983 |
super().setUp() |
| 984 |
self.write_settings( |
| 985 |
"settings.py", |
| 986 |
["django.contrib.auth", "django.contrib.contenttypes", "admin_scripts"], |
| 987 |
) |
| 988 |
|
| 989 |
def test_builtin_command(self): |
| 990 |
""" |
| 991 |
fulldefault: manage.py builtin commands succeed when default settings |
| 992 |
are appropriate. |
| 993 |
""" |
| 994 |
args = ["check", "admin_scripts"] |
| 995 |
out, err = self.run_manage(args) |
| 996 |
self.assertNoOutput(err) |
| 997 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 998 |
|
| 999 |
def test_builtin_with_settings(self): |
| 1000 |
""" |
| 1001 |
fulldefault: manage.py builtin commands succeed if settings are |
| 1002 |
provided as argument. |
| 1003 |
""" |
| 1004 |
args = ["check", "--settings=test_project.settings", "admin_scripts"] |
| 1005 |
out, err = self.run_manage(args) |
| 1006 |
self.assertNoOutput(err) |
| 1007 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 1008 |
|
| 1009 |
def test_builtin_with_environment(self): |
| 1010 |
""" |
| 1011 |
fulldefault: manage.py builtin commands succeed if settings are |
| 1012 |
provided in the environment. |
| 1013 |
""" |
| 1014 |
args = ["check", "admin_scripts"] |
| 1015 |
out, err = self.run_manage(args, "test_project.settings") |
| 1016 |
self.assertNoOutput(err) |
| 1017 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 1018 |
|
| 1019 |
def test_builtin_with_bad_settings(self): |
| 1020 |
""" |
| 1021 |
fulldefault: manage.py builtin commands succeed if settings file (from |
| 1022 |
argument) doesn't exist. |
| 1023 |
""" |
| 1024 |
args = ["check", "--settings=bad_settings", "admin_scripts"] |
| 1025 |
out, err = self.run_manage(args) |
| 1026 |
self.assertNoOutput(out) |
| 1027 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 1028 |
|
| 1029 |
def test_builtin_with_bad_environment(self): |
| 1030 |
""" |
| 1031 |
fulldefault: manage.py builtin commands fail if settings file (from |
| 1032 |
environment) doesn't exist. |
| 1033 |
""" |
| 1034 |
args = ["check", "admin_scripts"] |
| 1035 |
out, err = self.run_manage(args, "bad_settings") |
| 1036 |
self.assertNoOutput(out) |
| 1037 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 1038 |
|
| 1039 |
def test_custom_command(self): |
| 1040 |
""" |
| 1041 |
fulldefault: manage.py can execute user commands when default settings |
| 1042 |
are appropriate. |
| 1043 |
""" |
| 1044 |
args = ["noargs_command"] |
| 1045 |
out, err = self.run_manage(args) |
| 1046 |
self.assertNoOutput(err) |
| 1047 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 1048 |
|
| 1049 |
def test_custom_command_with_settings(self): |
| 1050 |
""" |
| 1051 |
fulldefault: manage.py can execute user commands when settings are |
| 1052 |
provided as argument. |
| 1053 |
""" |
| 1054 |
args = ["noargs_command", "--settings=test_project.settings"] |
| 1055 |
out, err = self.run_manage(args) |
| 1056 |
self.assertNoOutput(err) |
| 1057 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 1058 |
|
| 1059 |
def test_custom_command_with_environment(self): |
| 1060 |
""" |
| 1061 |
fulldefault: manage.py can execute user commands when settings are |
| 1062 |
provided in environment. |
| 1063 |
""" |
| 1064 |
args = ["noargs_command"] |
| 1065 |
out, err = self.run_manage(args, "test_project.settings") |
| 1066 |
self.assertNoOutput(err) |
| 1067 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 1068 |
|
| 1069 |
|
| 1070 |
class ManageMinimalSettings(AdminScriptTestCase): |
| 1071 |
"""A series of tests for manage.py when using a settings.py file that |
| 1072 |
doesn't contain the test application. |
| 1073 |
""" |
| 1074 |
|
| 1075 |
def setUp(self): |
| 1076 |
super().setUp() |
| 1077 |
self.write_settings( |
| 1078 |
"settings.py", apps=["django.contrib.auth", "django.contrib.contenttypes"] |
| 1079 |
) |
| 1080 |
|
| 1081 |
def test_builtin_command(self): |
| 1082 |
""" |
| 1083 |
minimal: manage.py builtin commands fail with an error when no settings |
| 1084 |
provided. |
| 1085 |
""" |
| 1086 |
args = ["check", "admin_scripts"] |
| 1087 |
out, err = self.run_manage(args) |
| 1088 |
self.assertNoOutput(out) |
| 1089 |
self.assertOutput(err, "No installed app with label 'admin_scripts'.") |
| 1090 |
|
| 1091 |
def test_builtin_with_settings(self): |
| 1092 |
"minimal: manage.py builtin commands fail if settings are provided as argument" |
| 1093 |
args = ["check", "--settings=test_project.settings", "admin_scripts"] |
| 1094 |
out, err = self.run_manage(args) |
| 1095 |
self.assertNoOutput(out) |
| 1096 |
self.assertOutput(err, "No installed app with label 'admin_scripts'.") |
| 1097 |
|
| 1098 |
def test_builtin_with_environment(self): |
| 1099 |
""" |
| 1100 |
minimal: manage.py builtin commands fail if settings are provided in |
| 1101 |
the environment. |
| 1102 |
""" |
| 1103 |
args = ["check", "admin_scripts"] |
| 1104 |
out, err = self.run_manage(args, "test_project.settings") |
| 1105 |
self.assertNoOutput(out) |
| 1106 |
self.assertOutput(err, "No installed app with label 'admin_scripts'.") |
| 1107 |
|
| 1108 |
def test_builtin_with_bad_settings(self): |
| 1109 |
""" |
| 1110 |
minimal: manage.py builtin commands fail if settings file (from |
| 1111 |
argument) doesn't exist. |
| 1112 |
""" |
| 1113 |
args = ["check", "--settings=bad_settings", "admin_scripts"] |
| 1114 |
out, err = self.run_manage(args) |
| 1115 |
self.assertNoOutput(out) |
| 1116 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 1117 |
|
| 1118 |
def test_builtin_with_bad_environment(self): |
| 1119 |
""" |
| 1120 |
minimal: manage.py builtin commands fail if settings file (from |
| 1121 |
environment) doesn't exist. |
| 1122 |
""" |
| 1123 |
args = ["check", "admin_scripts"] |
| 1124 |
out, err = self.run_manage(args, "bad_settings") |
| 1125 |
self.assertNoOutput(out) |
| 1126 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 1127 |
|
| 1128 |
def test_custom_command(self): |
| 1129 |
"minimal: manage.py can't execute user commands without appropriate settings" |
| 1130 |
args = ["noargs_command"] |
| 1131 |
out, err = self.run_manage(args) |
| 1132 |
self.assertNoOutput(out) |
| 1133 |
self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 1134 |
|
| 1135 |
def test_custom_command_with_settings(self): |
| 1136 |
""" |
| 1137 |
minimal: manage.py can't execute user commands, even if settings are |
| 1138 |
provided as argument. |
| 1139 |
""" |
| 1140 |
args = ["noargs_command", "--settings=test_project.settings"] |
| 1141 |
out, err = self.run_manage(args) |
| 1142 |
self.assertNoOutput(out) |
| 1143 |
self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 1144 |
|
| 1145 |
def test_custom_command_with_environment(self): |
| 1146 |
""" |
| 1147 |
minimal: manage.py can't execute user commands, even if settings are |
| 1148 |
provided in environment. |
| 1149 |
""" |
| 1150 |
args = ["noargs_command"] |
| 1151 |
out, err = self.run_manage(args, "test_project.settings") |
| 1152 |
self.assertNoOutput(out) |
| 1153 |
self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 1154 |
|
| 1155 |
|
| 1156 |
class ManageAlternateSettings(AdminScriptTestCase): |
| 1157 |
"""A series of tests for manage.py when using a settings file |
| 1158 |
with a name other than 'settings.py'. |
| 1159 |
""" |
| 1160 |
|
| 1161 |
def setUp(self): |
| 1162 |
super().setUp() |
| 1163 |
self.write_settings("alternate_settings.py") |
| 1164 |
|
| 1165 |
def test_builtin_command(self): |
| 1166 |
""" |
| 1167 |
alternate: manage.py builtin commands fail with an error when no |
| 1168 |
default settings provided. |
| 1169 |
""" |
| 1170 |
args = ["check", "admin_scripts"] |
| 1171 |
out, err = self.run_manage(args) |
| 1172 |
self.assertNoOutput(out) |
| 1173 |
self.assertOutput( |
| 1174 |
err, r"No module named '?(test_project\.)?settings'?", regex=True |
| 1175 |
) |
| 1176 |
|
| 1177 |
def test_builtin_with_settings(self): |
| 1178 |
"alternate: manage.py builtin commands work with settings provided as argument" |
| 1179 |
args = ["check", "--settings=alternate_settings", "admin_scripts"] |
| 1180 |
out, err = self.run_manage(args) |
| 1181 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 1182 |
self.assertNoOutput(err) |
| 1183 |
|
| 1184 |
def test_builtin_with_environment(self): |
| 1185 |
""" |
| 1186 |
alternate: manage.py builtin commands work if settings are provided in |
| 1187 |
the environment |
| 1188 |
""" |
| 1189 |
args = ["check", "admin_scripts"] |
| 1190 |
out, err = self.run_manage(args, "alternate_settings") |
| 1191 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 1192 |
self.assertNoOutput(err) |
| 1193 |
|
| 1194 |
def test_builtin_with_bad_settings(self): |
| 1195 |
""" |
| 1196 |
alternate: manage.py builtin commands fail if settings file (from |
| 1197 |
argument) doesn't exist. |
| 1198 |
""" |
| 1199 |
args = ["check", "--settings=bad_settings", "admin_scripts"] |
| 1200 |
out, err = self.run_manage(args) |
| 1201 |
self.assertNoOutput(out) |
| 1202 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 1203 |
|
| 1204 |
def test_builtin_with_bad_environment(self): |
| 1205 |
""" |
| 1206 |
alternate: manage.py builtin commands fail if settings file (from |
| 1207 |
environment) doesn't exist |
| 1208 |
""" |
| 1209 |
args = ["check", "admin_scripts"] |
| 1210 |
out, err = self.run_manage(args, "bad_settings") |
| 1211 |
self.assertNoOutput(out) |
| 1212 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 1213 |
|
| 1214 |
def test_custom_command(self): |
| 1215 |
"alternate: manage.py can't execute user commands without settings" |
| 1216 |
args = ["noargs_command"] |
| 1217 |
out, err = self.run_manage(args) |
| 1218 |
self.assertNoOutput(out) |
| 1219 |
self.assertOutput( |
| 1220 |
err, r"No module named '?(test_project\.)?settings'?", regex=True |
| 1221 |
) |
| 1222 |
|
| 1223 |
def test_custom_command_with_settings(self): |
| 1224 |
""" |
| 1225 |
alternate: manage.py can execute user commands if settings are provided |
| 1226 |
as argument |
| 1227 |
""" |
| 1228 |
args = ["noargs_command", "--settings=alternate_settings"] |
| 1229 |
out, err = self.run_manage(args) |
| 1230 |
self.assertOutput( |
| 1231 |
out, |
| 1232 |
"EXECUTE: noargs_command options=[('force_color', False), " |
| 1233 |
"('no_color', False), ('pythonpath', None), ('settings', " |
| 1234 |
"'alternate_settings'), ('traceback', False), ('verbosity', 1)]", |
| 1235 |
) |
| 1236 |
self.assertNoOutput(err) |
| 1237 |
|
| 1238 |
def test_custom_command_with_environment(self): |
| 1239 |
""" |
| 1240 |
alternate: manage.py can execute user commands if settings are provided |
| 1241 |
in environment. |
| 1242 |
""" |
| 1243 |
args = ["noargs_command"] |
| 1244 |
out, err = self.run_manage(args, "alternate_settings") |
| 1245 |
self.assertOutput( |
| 1246 |
out, |
| 1247 |
"EXECUTE: noargs_command options=[('force_color', False), " |
| 1248 |
"('no_color', False), ('pythonpath', None), ('settings', None), " |
| 1249 |
"('traceback', False), ('verbosity', 1)]", |
| 1250 |
) |
| 1251 |
self.assertNoOutput(err) |
| 1252 |
|
| 1253 |
def test_custom_command_output_color(self): |
| 1254 |
""" |
| 1255 |
alternate: manage.py output syntax color can be deactivated with the |
| 1256 |
`--no-color` option. |
| 1257 |
""" |
| 1258 |
args = ["noargs_command", "--no-color", "--settings=alternate_settings"] |
| 1259 |
out, err = self.run_manage(args) |
| 1260 |
self.assertOutput( |
| 1261 |
out, |
| 1262 |
"EXECUTE: noargs_command options=[('force_color', False), " |
| 1263 |
"('no_color', True), ('pythonpath', None), ('settings', " |
| 1264 |
"'alternate_settings'), ('traceback', False), ('verbosity', 1)]", |
| 1265 |
) |
| 1266 |
self.assertNoOutput(err) |
| 1267 |
|
| 1268 |
|
| 1269 |
class ManageMultipleSettings(AdminScriptTestCase): |
| 1270 |
"""A series of tests for manage.py when multiple settings files |
| 1271 |
(including the default 'settings.py') are available. The default settings |
| 1272 |
file is insufficient for performing the operations described, so the |
| 1273 |
alternate settings must be used by the running script. |
| 1274 |
""" |
| 1275 |
|
| 1276 |
def setUp(self): |
| 1277 |
super().setUp() |
| 1278 |
self.write_settings( |
| 1279 |
"settings.py", apps=["django.contrib.auth", "django.contrib.contenttypes"] |
| 1280 |
) |
| 1281 |
self.write_settings("alternate_settings.py") |
| 1282 |
|
| 1283 |
def test_builtin_command(self): |
| 1284 |
""" |
| 1285 |
multiple: manage.py builtin commands fail with an error when no |
| 1286 |
settings provided. |
| 1287 |
""" |
| 1288 |
args = ["check", "admin_scripts"] |
| 1289 |
out, err = self.run_manage(args) |
| 1290 |
self.assertNoOutput(out) |
| 1291 |
self.assertOutput(err, "No installed app with label 'admin_scripts'.") |
| 1292 |
|
| 1293 |
def test_builtin_with_settings(self): |
| 1294 |
""" |
| 1295 |
multiple: manage.py builtin commands succeed if settings are provided |
| 1296 |
as argument. |
| 1297 |
""" |
| 1298 |
args = ["check", "--settings=alternate_settings", "admin_scripts"] |
| 1299 |
out, err = self.run_manage(args) |
| 1300 |
self.assertNoOutput(err) |
| 1301 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 1302 |
|
| 1303 |
def test_builtin_with_environment(self): |
| 1304 |
""" |
| 1305 |
multiple: manage.py can execute builtin commands if settings are |
| 1306 |
provided in the environment. |
| 1307 |
""" |
| 1308 |
args = ["check", "admin_scripts"] |
| 1309 |
out, err = self.run_manage(args, "alternate_settings") |
| 1310 |
self.assertNoOutput(err) |
| 1311 |
self.assertOutput(out, SYSTEM_CHECK_MSG) |
| 1312 |
|
| 1313 |
def test_builtin_with_bad_settings(self): |
| 1314 |
""" |
| 1315 |
multiple: manage.py builtin commands fail if settings file (from |
| 1316 |
argument) doesn't exist. |
| 1317 |
""" |
| 1318 |
args = ["check", "--settings=bad_settings", "admin_scripts"] |
| 1319 |
out, err = self.run_manage(args) |
| 1320 |
self.assertNoOutput(out) |
| 1321 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 1322 |
|
| 1323 |
def test_builtin_with_bad_environment(self): |
| 1324 |
""" |
| 1325 |
multiple: manage.py builtin commands fail if settings file (from |
| 1326 |
environment) doesn't exist. |
| 1327 |
""" |
| 1328 |
args = ["check", "admin_scripts"] |
| 1329 |
out, err = self.run_manage(args, "bad_settings") |
| 1330 |
self.assertNoOutput(out) |
| 1331 |
self.assertOutput(err, "No module named '?bad_settings'?", regex=True) |
| 1332 |
|
| 1333 |
def test_custom_command(self): |
| 1334 |
"multiple: manage.py can't execute user commands using default settings" |
| 1335 |
args = ["noargs_command"] |
| 1336 |
out, err = self.run_manage(args) |
| 1337 |
self.assertNoOutput(out) |
| 1338 |
self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 1339 |
|
| 1340 |
def test_custom_command_with_settings(self): |
| 1341 |
""" |
| 1342 |
multiple: manage.py can execute user commands if settings are provided |
| 1343 |
as argument. |
| 1344 |
""" |
| 1345 |
args = ["noargs_command", "--settings=alternate_settings"] |
| 1346 |
out, err = self.run_manage(args) |
| 1347 |
self.assertNoOutput(err) |
| 1348 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 1349 |
|
| 1350 |
def test_custom_command_with_environment(self): |
| 1351 |
""" |
| 1352 |
multiple: manage.py can execute user commands if settings are provided |
| 1353 |
in environment. |
| 1354 |
""" |
| 1355 |
args = ["noargs_command"] |
| 1356 |
out, err = self.run_manage(args, "alternate_settings") |
| 1357 |
self.assertNoOutput(err) |
| 1358 |
self.assertOutput(out, "EXECUTE: noargs_command") |
| 1359 |
|
| 1360 |
|
| 1361 |
class ManageSettingsWithSettingsErrors(AdminScriptTestCase): |
| 1362 |
""" |
| 1363 |
Tests for manage.py when using the default settings.py file containing |
| 1364 |
runtime errors. |
| 1365 |
""" |
| 1366 |
|
| 1367 |
def write_settings_with_import_error(self, filename): |
| 1368 |
settings_file_path = os.path.join(self.test_dir, filename) |
| 1369 |
with open(settings_file_path, "w") as settings_file: |
| 1370 |
settings_file.write( |
| 1371 |
"# Settings file automatically generated by admin_scripts test case\n" |
| 1372 |
) |
| 1373 |
settings_file.write( |
| 1374 |
"# The next line will cause an import error:\nimport foo42bar\n" |
| 1375 |
) |
| 1376 |
|
| 1377 |
def test_import_error(self): |
| 1378 |
""" |
| 1379 |
import error: manage.py builtin commands shows useful diagnostic info |
| 1380 |
when settings with import errors is provided (#14130). |
| 1381 |
""" |
| 1382 |
self.write_settings_with_import_error("settings.py") |
| 1383 |
args = ["check", "admin_scripts"] |
| 1384 |
out, err = self.run_manage(args) |
| 1385 |
self.assertNoOutput(out) |
| 1386 |
self.assertOutput(err, "No module named") |
| 1387 |
self.assertOutput(err, "foo42bar") |
| 1388 |
|
| 1389 |
def test_attribute_error(self): |
| 1390 |
""" |
| 1391 |
manage.py builtin commands does not swallow attribute error due to bad |
| 1392 |
settings (#18845). |
| 1393 |
""" |
| 1394 |
self.write_settings("settings.py", sdict={"BAD_VAR": "INSTALLED_APPS.crash"}) |
| 1395 |
args = ["collectstatic", "admin_scripts"] |
| 1396 |
out, err = self.run_manage(args) |
| 1397 |
self.assertNoOutput(out) |
| 1398 |
self.assertOutput(err, "AttributeError: 'list' object has no attribute 'crash'") |
| 1399 |
|
| 1400 |
def test_key_error(self): |
| 1401 |
self.write_settings("settings.py", sdict={"BAD_VAR": 'DATABASES["blah"]'}) |
| 1402 |
args = ["collectstatic", "admin_scripts"] |
| 1403 |
out, err = self.run_manage(args) |
| 1404 |
self.assertNoOutput(out) |
| 1405 |
self.assertOutput(err, "KeyError: 'blah'") |
| 1406 |
|
| 1407 |
def test_help(self): |
| 1408 |
""" |
| 1409 |
Test listing available commands output note when only core commands are |
| 1410 |
available. |
| 1411 |
""" |
| 1412 |
self.write_settings( |
| 1413 |
"settings.py", |
| 1414 |
extra="from django.core.exceptions import ImproperlyConfigured\n" |
| 1415 |
"raise ImproperlyConfigured()", |
| 1416 |
) |
| 1417 |
args = ["help"] |
| 1418 |
out, err = self.run_manage(args) |
| 1419 |
self.assertOutput(out, "only Django core commands are listed") |
| 1420 |
self.assertNoOutput(err) |
| 1421 |
|
| 1422 |
|
| 1423 |
class ManageCheck(AdminScriptTestCase): |
| 1424 |
def test_nonexistent_app(self): |
| 1425 |
"""check reports an error on a nonexistent app in INSTALLED_APPS.""" |
| 1426 |
self.write_settings( |
| 1427 |
"settings.py", |
| 1428 |
apps=["admin_scriptz.broken_app"], |
| 1429 |
sdict={"USE_I18N": False}, |
| 1430 |
) |
| 1431 |
args = ["check"] |
| 1432 |
out, err = self.run_manage(args) |
| 1433 |
self.assertNoOutput(out) |
| 1434 |
self.assertOutput(err, "ModuleNotFoundError") |
| 1435 |
self.assertOutput(err, "No module named") |
| 1436 |
self.assertOutput(err, "admin_scriptz") |
| 1437 |
|
| 1438 |
def test_broken_app(self): |
| 1439 |
"""manage.py check reports an ImportError if an app's models.py |
| 1440 |
raises one on import""" |
| 1441 |
|
| 1442 |
self.write_settings("settings.py", apps=["admin_scripts.broken_app"]) |
| 1443 |
args = ["check"] |
| 1444 |
out, err = self.run_manage(args) |
| 1445 |
self.assertNoOutput(out) |
| 1446 |
self.assertOutput(err, "ImportError") |
| 1447 |
|
| 1448 |
def test_complex_app(self): |
| 1449 |
"""manage.py check does not raise an ImportError validating a |
| 1450 |
complex app with nested calls to load_app""" |
| 1451 |
|
| 1452 |
self.write_settings( |
| 1453 |
"settings.py", |
| 1454 |
apps=[ |
| 1455 |
"admin_scripts.complex_app", |
| 1456 |
"admin_scripts.simple_app", |
| 1457 |
"django.contrib.admin.apps.SimpleAdminConfig", |
| 1458 |
"django.contrib.auth", |
| 1459 |
"django.contrib.contenttypes", |
| 1460 |
"django.contrib.messages", |
| 1461 |
], |
| 1462 |
sdict={ |
| 1463 |
"DEBUG": True, |
| 1464 |
"MIDDLEWARE": [ |
| 1465 |
"django.contrib.messages.middleware.MessageMiddleware", |
| 1466 |
"django.contrib.auth.middleware.AuthenticationMiddleware", |
| 1467 |
"django.contrib.sessions.middleware.SessionMiddleware", |
| 1468 |
], |
| 1469 |
"TEMPLATES": [ |
| 1470 |
{ |
| 1471 |
"BACKEND": "django.template.backends.django.DjangoTemplates", |
| 1472 |
"DIRS": [], |
| 1473 |
"APP_DIRS": True, |
| 1474 |
"OPTIONS": { |
| 1475 |
"context_processors": [ |
| 1476 |
"django.template.context_processors.request", |
| 1477 |
"django.contrib.auth.context_processors.auth", |
| 1478 |
"django.contrib.messages.context_processors.messages", |
| 1479 |
], |
| 1480 |
}, |
| 1481 |
}, |
| 1482 |
], |
| 1483 |
}, |
| 1484 |
) |
| 1485 |
args = ["check"] |
| 1486 |
out, err = self.run_manage(args) |
| 1487 |
self.assertNoOutput(err) |
| 1488 |
self.assertEqual(out, "System check identified no issues (0 silenced).\n") |
| 1489 |
|
| 1490 |
def test_app_with_import(self): |
| 1491 |
"""manage.py check does not raise errors when an app imports a base |
| 1492 |
class that itself has an abstract base.""" |
| 1493 |
|
| 1494 |
self.write_settings( |
| 1495 |
"settings.py", |
| 1496 |
apps=[ |
| 1497 |
"admin_scripts.app_with_import", |
| 1498 |
"django.contrib.auth", |
| 1499 |
"django.contrib.contenttypes", |
| 1500 |
"django.contrib.sites", |
| 1501 |
], |
| 1502 |
sdict={"DEBUG": True}, |
| 1503 |
) |
| 1504 |
args = ["check"] |
| 1505 |
out, err = self.run_manage(args) |
| 1506 |
self.assertNoOutput(err) |
| 1507 |
self.assertEqual(out, "System check identified no issues (0 silenced).\n") |
| 1508 |
|
| 1509 |
def test_output_format(self): |
| 1510 |
"""All errors/warnings should be sorted by level and by message.""" |
| 1511 |
|
| 1512 |
self.write_settings( |
| 1513 |
"settings.py", |
| 1514 |
apps=[ |
| 1515 |
"admin_scripts.app_raising_messages", |
| 1516 |
"django.contrib.auth", |
| 1517 |
"django.contrib.contenttypes", |
| 1518 |
], |
| 1519 |
sdict={"DEBUG": True}, |
| 1520 |
) |
| 1521 |
args = ["check"] |
| 1522 |
out, err = self.run_manage(args) |
| 1523 |
expected_err = ( |
| 1524 |
"SystemCheckError: System check identified some issues:\n" |
| 1525 |
"\n" |
| 1526 |
"ERRORS:\n" |
| 1527 |
"?: An error\n" |
| 1528 |
"\tHINT: Error hint\n" |
| 1529 |
"\n" |
| 1530 |
"WARNINGS:\n" |
| 1531 |
"a: Second warning\n" |
| 1532 |
"obj: First warning\n" |
| 1533 |
"\tHINT: Hint\n" |
| 1534 |
"\n" |
| 1535 |
"System check identified 3 issues (0 silenced).\n" |
| 1536 |
) |
| 1537 |
self.assertEqual(err, expected_err) |
| 1538 |
self.assertNoOutput(out) |
| 1539 |
|
| 1540 |
def test_warning_does_not_halt(self): |
| 1541 |
""" |
| 1542 |
When there are only warnings or less serious messages, then Django |
| 1543 |
should not prevent user from launching their project, so `check` |
| 1544 |
command should not raise `CommandError` exception. |
| 1545 |
|
| 1546 |
In this test we also test output format. |
| 1547 |
""" |
| 1548 |
|
| 1549 |
self.write_settings( |
| 1550 |
"settings.py", |
| 1551 |
apps=[ |
| 1552 |
"admin_scripts.app_raising_warning", |
| 1553 |
"django.contrib.auth", |
| 1554 |
"django.contrib.contenttypes", |
| 1555 |
], |
| 1556 |
sdict={"DEBUG": True}, |
| 1557 |
) |
| 1558 |
args = ["check"] |
| 1559 |
out, err = self.run_manage(args) |
| 1560 |
expected_err = ( |
| 1561 |
"System check identified some issues:\n" # No "CommandError: " part |
| 1562 |
"\n" |
| 1563 |
"WARNINGS:\n" |
| 1564 |
"?: A warning\n" |
| 1565 |
"\n" |
| 1566 |
"System check identified 1 issue (0 silenced).\n" |
| 1567 |
) |
| 1568 |
self.assertEqual(err, expected_err) |
| 1569 |
self.assertNoOutput(out) |
| 1570 |
|
| 1571 |
|
| 1572 |
class ManageRunserver(SimpleTestCase): |
| 1573 |
def setUp(self): |
| 1574 |
def monkey_run(*args, **options): |
| 1575 |
return |
| 1576 |
|
| 1577 |
self.output = StringIO() |
| 1578 |
self.cmd = RunserverCommand(stdout=self.output) |
| 1579 |
self.cmd.run = monkey_run |
| 1580 |
|
| 1581 |
def assertServerSettings(self, addr, port, ipv6=False, raw_ipv6=False): |
| 1582 |
self.assertEqual(self.cmd.addr, addr) |
| 1583 |
self.assertEqual(self.cmd.port, port) |
| 1584 |
self.assertEqual(self.cmd.use_ipv6, ipv6) |
| 1585 |
self.assertEqual(self.cmd._raw_ipv6, raw_ipv6) |
| 1586 |
|
| 1587 |
def test_runserver_addrport(self): |
| 1588 |
call_command(self.cmd) |
| 1589 |
self.assertServerSettings("127.0.0.1", "8000") |
| 1590 |
|
| 1591 |
call_command(self.cmd, addrport="1.2.3.4:8000") |
| 1592 |
self.assertServerSettings("1.2.3.4", "8000") |
| 1593 |
|
| 1594 |
call_command(self.cmd, addrport="7000") |
| 1595 |
self.assertServerSettings("127.0.0.1", "7000") |
| 1596 |
|
| 1597 |
def test_zero_ip_addr(self): |
| 1598 |
self.cmd.addr = "0" |
| 1599 |
self.cmd._raw_ipv6 = False |
| 1600 |
self.cmd.on_bind("8000") |
| 1601 |
self.assertIn( |
| 1602 |
"Starting development server at http://0.0.0.0:8000/", |
| 1603 |
self.output.getvalue(), |
| 1604 |
) |
| 1605 |
docs_version = get_docs_version() |
| 1606 |
self.assertIn( |
| 1607 |
"WARNING: This is a development server. Do not use it in a " |
| 1608 |
"production setting. Use a production WSGI or ASGI server instead." |
| 1609 |
"\nFor more information on production servers see: " |
| 1610 |
f"https://docs.djangoproject.com/en/{docs_version}/howto/" |
| 1611 |
"deployment/", |
| 1612 |
self.output.getvalue(), |
| 1613 |
) |
| 1614 |
|
| 1615 |
def test_on_bind(self): |
| 1616 |
self.cmd.addr = "127.0.0.1" |
| 1617 |
self.cmd._raw_ipv6 = False |
| 1618 |
self.cmd.on_bind("14437") |
| 1619 |
self.assertIn( |
| 1620 |
"Starting development server at http://127.0.0.1:14437/", |
| 1621 |
self.output.getvalue(), |
| 1622 |
) |
| 1623 |
docs_version = get_docs_version() |
| 1624 |
self.assertIn( |
| 1625 |
"WARNING: This is a development server. Do not use it in a " |
| 1626 |
"production setting. Use a production WSGI or ASGI server instead." |
| 1627 |
"\nFor more information on production servers see: " |
| 1628 |
f"https://docs.djangoproject.com/en/{docs_version}/howto/" |
| 1629 |
"deployment/", |
| 1630 |
self.output.getvalue(), |
| 1631 |
) |
| 1632 |
|
| 1633 |
@mock.patch.dict(os.environ, {"DJANGO_RUNSERVER_HIDE_WARNING": "true"}) |
| 1634 |
def test_hide_production_warning_with_environment_variable(self): |
| 1635 |
self.cmd.addr = "0" |
| 1636 |
self.cmd._raw_ipv6 = False |
| 1637 |
self.cmd.on_bind("8000") |
| 1638 |
self.assertIn( |
| 1639 |
"Starting development server at http://0.0.0.0:8000/", |
| 1640 |
self.output.getvalue(), |
| 1641 |
) |
| 1642 |
docs_version = get_docs_version() |
| 1643 |
self.assertNotIn( |
| 1644 |
"WARNING: This is a development server. Do not use it in a " |
| 1645 |
"production setting. Use a production WSGI or ASGI server instead." |
| 1646 |
"\nFor more information on production servers see: " |
| 1647 |
f"https://docs.djangoproject.com/en/{docs_version}/howto/" |
| 1648 |
"deployment/", |
| 1649 |
self.output.getvalue(), |
| 1650 |
) |
| 1651 |
|
| 1652 |
@unittest.skipUnless(socket.has_ipv6, "platform doesn't support IPv6") |
| 1653 |
def test_runner_addrport_ipv6(self): |
| 1654 |
call_command(self.cmd, addrport="", use_ipv6=True) |
| 1655 |
self.assertServerSettings("::1", "8000", ipv6=True, raw_ipv6=True) |
| 1656 |
|
| 1657 |
call_command(self.cmd, addrport="7000", use_ipv6=True) |
| 1658 |
self.assertServerSettings("::1", "7000", ipv6=True, raw_ipv6=True) |
| 1659 |
|
| 1660 |
call_command(self.cmd, addrport="[2001:0db8:1234:5678::9]:7000") |
| 1661 |
self.assertServerSettings( |
| 1662 |
"2001:0db8:1234:5678::9", "7000", ipv6=True, raw_ipv6=True |
| 1663 |
) |
| 1664 |
|
| 1665 |
def test_runner_hostname(self): |
| 1666 |
call_command(self.cmd, addrport="localhost:8000") |
| 1667 |
self.assertServerSettings("localhost", "8000") |
| 1668 |
|
| 1669 |
call_command(self.cmd, addrport="test.domain.local:7000") |
| 1670 |
self.assertServerSettings("test.domain.local", "7000") |
| 1671 |
|
| 1672 |
@unittest.skipUnless(socket.has_ipv6, "platform doesn't support IPv6") |
| 1673 |
def test_runner_hostname_ipv6(self): |
| 1674 |
call_command(self.cmd, addrport="test.domain.local:7000", use_ipv6=True) |
| 1675 |
self.assertServerSettings("test.domain.local", "7000", ipv6=True) |
| 1676 |
|
| 1677 |
def test_runner_custom_defaults(self): |
| 1678 |
self.cmd.default_addr = "0.0.0.0" |
| 1679 |
self.cmd.default_port = "5000" |
| 1680 |
call_command(self.cmd) |
| 1681 |
self.assertServerSettings("0.0.0.0", "5000") |
| 1682 |
|
| 1683 |
@unittest.skipUnless(socket.has_ipv6, "platform doesn't support IPv6") |
| 1684 |
def test_runner_custom_defaults_ipv6(self): |
| 1685 |
self.cmd.default_addr_ipv6 = "::" |
| 1686 |
call_command(self.cmd, use_ipv6=True) |
| 1687 |
self.assertServerSettings("::", "8000", ipv6=True, raw_ipv6=True) |
| 1688 |
|
| 1689 |
def test_runner_ambiguous(self): |
| 1690 |
# Only 4 characters, all of which could be in an ipv6 address |
| 1691 |
call_command(self.cmd, addrport="beef:7654") |
| 1692 |
self.assertServerSettings("beef", "7654") |
| 1693 |
|
| 1694 |
# Uses only characters that could be in an ipv6 address |
| 1695 |
call_command(self.cmd, addrport="deadbeef:7654") |
| 1696 |
self.assertServerSettings("deadbeef", "7654") |
| 1697 |
|
| 1698 |
def test_no_database(self): |
| 1699 |
""" |
| 1700 |
Ensure runserver.check_migrations doesn't choke on empty DATABASES. |
| 1701 |
""" |
| 1702 |
tested_connections = ConnectionHandler({}) |
| 1703 |
with mock.patch( |
| 1704 |
"django.core.management.base.connections", new=tested_connections |
| 1705 |
): |
| 1706 |
self.cmd.check_migrations() |
| 1707 |
|
| 1708 |
def test_readonly_database(self): |
| 1709 |
""" |
| 1710 |
runserver.check_migrations() doesn't choke when a database is read-only. |
| 1711 |
""" |
| 1712 |
with mock.patch.object(MigrationRecorder, "has_table", return_value=False): |
| 1713 |
self.cmd.check_migrations() |
| 1714 |
# You have # ... |
| 1715 |
self.assertIn("unapplied migration(s)", self.output.getvalue()) |
| 1716 |
|
| 1717 |
@mock.patch("django.core.management.commands.runserver.run") |
| 1718 |
@mock.patch("django.core.management.base.BaseCommand.check_migrations") |
| 1719 |
@mock.patch("django.core.management.base.BaseCommand.check") |
| 1720 |
def test_skip_checks(self, mocked_check, *mocked_objects): |
| 1721 |
call_command( |
| 1722 |
"runserver", |
| 1723 |
use_reloader=False, |
| 1724 |
skip_checks=True, |
| 1725 |
stdout=self.output, |
| 1726 |
) |
| 1727 |
self.assertNotIn("Performing system checks...", self.output.getvalue()) |
| 1728 |
mocked_check.assert_not_called() |
| 1729 |
|
| 1730 |
self.output.truncate(0) |
| 1731 |
call_command( |
| 1732 |
"runserver", |
| 1733 |
use_reloader=False, |
| 1734 |
skip_checks=False, |
| 1735 |
stdout=self.output, |
| 1736 |
) |
| 1737 |
self.assertIn("Performing system checks...", self.output.getvalue()) |
| 1738 |
mocked_check.assert_has_calls( |
| 1739 |
[mock.call(tags=set()), mock.call(display_num_errors=True)] |
| 1740 |
) |
| 1741 |
|
| 1742 |
def test_custom_system_checks(self): |
| 1743 |
original_checks = registry.registered_checks.copy() |
| 1744 |
|
| 1745 |
@register(Tags.signals) |
| 1746 |
def my_check(app_configs, **kwargs): |
| 1747 |
return [Error("my error")] |
| 1748 |
|
| 1749 |
class CustomException(Exception): |
| 1750 |
pass |
| 1751 |
|
| 1752 |
self.addCleanup(setattr, registry, "registered_checks", original_checks) |
| 1753 |
|
| 1754 |
class CustomRunserverCommand(RunserverCommand): |
| 1755 |
"""Rather than mock run(), raise immediately after system checks run.""" |
| 1756 |
|
| 1757 |
def check_migrations(self, *args, **kwargs): |
| 1758 |
raise CustomException |
| 1759 |
|
| 1760 |
class CustomRunserverCommandWithSignalsChecks(CustomRunserverCommand): |
| 1761 |
requires_system_checks = [Tags.signals] |
| 1762 |
|
| 1763 |
command = CustomRunserverCommandWithSignalsChecks() |
| 1764 |
with self.assertRaises(SystemCheckError): |
| 1765 |
call_command( |
| 1766 |
command, |
| 1767 |
use_reloader=False, |
| 1768 |
skip_checks=False, |
| 1769 |
stdout=StringIO(), |
| 1770 |
stderr=StringIO(), |
| 1771 |
) |
| 1772 |
|
| 1773 |
class CustomMigrateCommandWithSecurityChecks(CustomRunserverCommand): |
| 1774 |
requires_system_checks = [Tags.security] |
| 1775 |
|
| 1776 |
command = CustomMigrateCommandWithSecurityChecks() |
| 1777 |
with self.assertRaises(CustomException): |
| 1778 |
call_command( |
| 1779 |
command, |
| 1780 |
use_reloader=False, |
| 1781 |
skip_checks=False, |
| 1782 |
stdout=StringIO(), |
| 1783 |
stderr=StringIO(), |
| 1784 |
) |
| 1785 |
|
| 1786 |
|
| 1787 |
class ManageRunserverMigrationWarning(TestCase): |
| 1788 |
def setUp(self): |
| 1789 |
self.stdout = StringIO() |
| 1790 |
self.runserver_command = RunserverCommand(stdout=self.stdout) |
| 1791 |
|
| 1792 |
@override_settings(INSTALLED_APPS=["admin_scripts.app_waiting_migration"]) |
| 1793 |
def test_migration_warning_one_app(self): |
| 1794 |
self.runserver_command.check_migrations() |
| 1795 |
output = self.stdout.getvalue() |
| 1796 |
self.assertIn("You have 1 unapplied migration(s)", output) |
| 1797 |
self.assertIn("apply the migrations for app(s): app_waiting_migration.", output) |
| 1798 |
|
| 1799 |
@override_settings( |
| 1800 |
INSTALLED_APPS=[ |
| 1801 |
"admin_scripts.app_waiting_migration", |
| 1802 |
"admin_scripts.another_app_waiting_migration", |
| 1803 |
], |
| 1804 |
) |
| 1805 |
def test_migration_warning_multiple_apps(self): |
| 1806 |
self.runserver_command.check_migrations() |
| 1807 |
output = self.stdout.getvalue() |
| 1808 |
self.assertIn("You have 2 unapplied migration(s)", output) |
| 1809 |
self.assertIn( |
| 1810 |
"apply the migrations for app(s): another_app_waiting_migration, " |
| 1811 |
"app_waiting_migration.", |
| 1812 |
output, |
| 1813 |
) |
| 1814 |
|
| 1815 |
|
| 1816 |
class ManageRunserverEmptyAllowedHosts(AdminScriptTestCase): |
| 1817 |
def setUp(self): |
| 1818 |
super().setUp() |
| 1819 |
self.write_settings( |
| 1820 |
"settings.py", |
| 1821 |
sdict={ |
| 1822 |
"ALLOWED_HOSTS": [], |
| 1823 |
"DEBUG": False, |
| 1824 |
}, |
| 1825 |
) |
| 1826 |
|
| 1827 |
def test_empty_allowed_hosts_error(self): |
| 1828 |
out, err = self.run_manage(["runserver"]) |
| 1829 |
self.assertNoOutput(out) |
| 1830 |
self.assertOutput( |
| 1831 |
err, "CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False." |
| 1832 |
) |
| 1833 |
|
| 1834 |
|
| 1835 |
class ManageRunserverHelpOutput(AdminScriptTestCase): |
| 1836 |
def test_suppressed_options(self): |
| 1837 |
"""runserver doesn't support --verbosity and --trackback options.""" |
| 1838 |
out, err = self.run_manage(["runserver", "--help"]) |
| 1839 |
self.assertNotInOutput(out, "--verbosity") |
| 1840 |
self.assertNotInOutput(out, "--trackback") |
| 1841 |
self.assertOutput(out, "--settings") |
| 1842 |
|
| 1843 |
|
| 1844 |
class ManageTestserver(SimpleTestCase): |
| 1845 |
@mock.patch.object(TestserverCommand, "handle", return_value="") |
| 1846 |
def test_testserver_handle_params(self, mock_handle): |
| 1847 |
out = StringIO() |
| 1848 |
call_command("testserver", "blah.json", stdout=out) |
| 1849 |
mock_handle.assert_called_with( |
| 1850 |
"blah.json", |
| 1851 |
stdout=out, |
| 1852 |
settings=None, |
| 1853 |
pythonpath=None, |
| 1854 |
verbosity=1, |
| 1855 |
traceback=False, |
| 1856 |
addrport="", |
| 1857 |
no_color=False, |
| 1858 |
use_ipv6=False, |
| 1859 |
skip_checks=True, |
| 1860 |
interactive=True, |
| 1861 |
force_color=False, |
| 1862 |
) |
| 1863 |
|
| 1864 |
@mock.patch("django.db.connection.creation.create_test_db", return_value="test_db") |
| 1865 |
@mock.patch.object(LoaddataCommand, "handle", return_value="") |
| 1866 |
@mock.patch.object(RunserverCommand, "handle", return_value="") |
| 1867 |
def test_params_to_runserver( |
| 1868 |
self, mock_runserver_handle, mock_loaddata_handle, mock_create_test_db |
| 1869 |
): |
| 1870 |
call_command("testserver", "blah.json") |
| 1871 |
mock_runserver_handle.assert_called_with( |
| 1872 |
addrport="", |
| 1873 |
force_color=False, |
| 1874 |
insecure_serving=False, |
| 1875 |
no_color=False, |
| 1876 |
pythonpath=None, |
| 1877 |
settings=None, |
| 1878 |
shutdown_message=( |
| 1879 |
"\nServer stopped.\nNote that the test database, 'test_db', " |
| 1880 |
"has not been deleted. You can explore it on your own." |
| 1881 |
), |
| 1882 |
skip_checks=True, |
| 1883 |
traceback=False, |
| 1884 |
use_ipv6=False, |
| 1885 |
use_reloader=False, |
| 1886 |
use_static_handler=True, |
| 1887 |
use_threading=connection.features.test_db_allows_multiple_connections, |
| 1888 |
verbosity=1, |
| 1889 |
) |
| 1890 |
|
| 1891 |
|
| 1892 |
########################################################################## |
| 1893 |
# COMMAND PROCESSING TESTS |
| 1894 |
# user-space commands are correctly handled - in particular, arguments to |
| 1895 |
# the commands are correctly parsed and processed. |
| 1896 |
########################################################################## |
| 1897 |
class ColorCommand(BaseCommand): |
| 1898 |
requires_system_checks = [] |
| 1899 |
|
| 1900 |
def handle(self, *args, **options): |
| 1901 |
self.stdout.write("Hello, world!", self.style.ERROR) |
| 1902 |
self.stderr.write("Hello, world!", self.style.ERROR) |
| 1903 |
|
| 1904 |
|
| 1905 |
class CommandTypes(AdminScriptTestCase): |
| 1906 |
"Tests for the various types of base command types that can be defined." |
| 1907 |
|
| 1908 |
def setUp(self): |
| 1909 |
super().setUp() |
| 1910 |
self.write_settings("settings.py") |
| 1911 |
|
| 1912 |
def test_version(self): |
| 1913 |
"version is handled as a special case" |
| 1914 |
args = ["version"] |
| 1915 |
out, err = self.run_manage(args) |
| 1916 |
self.assertNoOutput(err) |
| 1917 |
self.assertOutput(out, get_version()) |
| 1918 |
|
| 1919 |
def test_version_alternative(self): |
| 1920 |
"--version is equivalent to version" |
| 1921 |
args1, args2 = ["version"], ["--version"] |
| 1922 |
# It's possible one outputs on stderr and the other on stdout, hence the set |
| 1923 |
self.assertEqual(set(self.run_manage(args1)), set(self.run_manage(args2))) |
| 1924 |
|
| 1925 |
def test_help(self): |
| 1926 |
"help is handled as a special case" |
| 1927 |
args = ["help"] |
| 1928 |
out, err = self.run_manage(args) |
| 1929 |
self.assertOutput( |
| 1930 |
out, "Type 'manage.py help <subcommand>' for help on a specific subcommand." |
| 1931 |
) |
| 1932 |
self.assertOutput(out, "[django]") |
| 1933 |
self.assertOutput(out, "startapp") |
| 1934 |
self.assertOutput(out, "startproject") |
| 1935 |
|
| 1936 |
def test_help_commands(self): |
| 1937 |
"help --commands shows the list of all available commands" |
| 1938 |
args = ["help", "--commands"] |
| 1939 |
out, err = self.run_manage(args) |
| 1940 |
self.assertNotInOutput(out, "usage:") |
| 1941 |
self.assertNotInOutput(out, "Options:") |
| 1942 |
self.assertNotInOutput(out, "[django]") |
| 1943 |
self.assertOutput(out, "startapp") |
| 1944 |
self.assertOutput(out, "startproject") |
| 1945 |
self.assertNotInOutput(out, "\n\n") |
| 1946 |
|
| 1947 |
def test_help_alternative(self): |
| 1948 |
"--help is equivalent to help" |
| 1949 |
args1, args2 = ["help"], ["--help"] |
| 1950 |
self.assertEqual(self.run_manage(args1), self.run_manage(args2)) |
| 1951 |
|
| 1952 |
def test_help_short_altert(self): |
| 1953 |
"-h is handled as a short form of --help" |
| 1954 |
args1, args2 = ["--help"], ["-h"] |
| 1955 |
self.assertEqual(self.run_manage(args1), self.run_manage(args2)) |
| 1956 |
|
| 1957 |
def test_specific_help(self): |
| 1958 |
"--help can be used on a specific command" |
| 1959 |
args = ["check", "--help"] |
| 1960 |
out, err = self.run_manage(args) |
| 1961 |
self.assertNoOutput(err) |
| 1962 |
# Command-specific options like --tag appear before options common to |
| 1963 |
# all commands like --version. |
| 1964 |
tag_location = out.find("--tag") |
| 1965 |
version_location = out.find("--version") |
| 1966 |
self.assertNotEqual(tag_location, -1) |
| 1967 |
self.assertNotEqual(version_location, -1) |
| 1968 |
self.assertLess(tag_location, version_location) |
| 1969 |
self.assertOutput( |
| 1970 |
out, "Checks the entire Django project for potential problems." |
| 1971 |
) |
| 1972 |
|
| 1973 |
def test_help_default_options_with_custom_arguments(self): |
| 1974 |
args = ["base_command", "--help"] |
| 1975 |
out, err = self.run_manage(args) |
| 1976 |
self.assertNoOutput(err) |
| 1977 |
expected_options = [ |
| 1978 |
"-h", |
| 1979 |
"--option_a OPTION_A", |
| 1980 |
"--option_b OPTION_B", |
| 1981 |
"--option_c OPTION_C", |
| 1982 |
"--version", |
| 1983 |
"-v {0,1,2,3}", |
| 1984 |
"--settings SETTINGS", |
| 1985 |
"--pythonpath PYTHONPATH", |
| 1986 |
"--traceback", |
| 1987 |
"--no-color", |
| 1988 |
"--force-color", |
| 1989 |
"args ...", |
| 1990 |
] |
| 1991 |
for option in expected_options: |
| 1992 |
self.assertOutput(out, f"[{option}]") |
| 1993 |
if PY313: |
| 1994 |
self.assertOutput(out, "--option_a, -a OPTION_A") |
| 1995 |
self.assertOutput(out, "--option_b, -b OPTION_B") |
| 1996 |
self.assertOutput(out, "--option_c, -c OPTION_C") |
| 1997 |
self.assertOutput(out, "-v, --verbosity {0,1,2,3}") |
| 1998 |
else: |
| 1999 |
self.assertOutput(out, "--option_a OPTION_A, -a OPTION_A") |
| 2000 |
self.assertOutput(out, "--option_b OPTION_B, -b OPTION_B") |
| 2001 |
self.assertOutput(out, "--option_c OPTION_C, -c OPTION_C") |
| 2002 |
self.assertOutput(out, "-v {0,1,2,3}, --verbosity {0,1,2,3}") |
| 2003 |
|
| 2004 |
def test_color_style(self): |
| 2005 |
style = color.no_style() |
| 2006 |
self.assertEqual(style.ERROR("Hello, world!"), "Hello, world!") |
| 2007 |
|
| 2008 |
style = color.make_style("nocolor") |
| 2009 |
self.assertEqual(style.ERROR("Hello, world!"), "Hello, world!") |
| 2010 |
|
| 2011 |
style = color.make_style("dark") |
| 2012 |
self.assertIn("Hello, world!", style.ERROR("Hello, world!")) |
| 2013 |
self.assertNotEqual(style.ERROR("Hello, world!"), "Hello, world!") |
| 2014 |
|
| 2015 |
# Default palette has color. |
| 2016 |
style = color.make_style("") |
| 2017 |
self.assertIn("Hello, world!", style.ERROR("Hello, world!")) |
| 2018 |
self.assertNotEqual(style.ERROR("Hello, world!"), "Hello, world!") |
| 2019 |
|
| 2020 |
def test_command_color(self): |
| 2021 |
out = StringIO() |
| 2022 |
err = StringIO() |
| 2023 |
command = ColorCommand(stdout=out, stderr=err) |
| 2024 |
call_command(command) |
| 2025 |
if color.supports_color(): |
| 2026 |
self.assertIn("Hello, world!\n", out.getvalue()) |
| 2027 |
self.assertIn("Hello, world!\n", err.getvalue()) |
| 2028 |
self.assertNotEqual(out.getvalue(), "Hello, world!\n") |
| 2029 |
self.assertNotEqual(err.getvalue(), "Hello, world!\n") |
| 2030 |
else: |
| 2031 |
self.assertEqual(out.getvalue(), "Hello, world!\n") |
| 2032 |
self.assertEqual(err.getvalue(), "Hello, world!\n") |
| 2033 |
|
| 2034 |
def test_command_no_color(self): |
| 2035 |
"--no-color prevent colorization of the output" |
| 2036 |
out = StringIO() |
| 2037 |
err = StringIO() |
| 2038 |
command = ColorCommand(stdout=out, stderr=err, no_color=True) |
| 2039 |
call_command(command) |
| 2040 |
self.assertEqual(out.getvalue(), "Hello, world!\n") |
| 2041 |
self.assertEqual(err.getvalue(), "Hello, world!\n") |
| 2042 |
|
| 2043 |
out = StringIO() |
| 2044 |
err = StringIO() |
| 2045 |
command = ColorCommand(stdout=out, stderr=err) |
| 2046 |
call_command(command, no_color=True) |
| 2047 |
self.assertEqual(out.getvalue(), "Hello, world!\n") |
| 2048 |
self.assertEqual(err.getvalue(), "Hello, world!\n") |
| 2049 |
|
| 2050 |
def test_force_color_execute(self): |
| 2051 |
out = StringIO() |
| 2052 |
err = StringIO() |
| 2053 |
with mock.patch.object(sys.stdout, "isatty", lambda: False): |
| 2054 |
command = ColorCommand(stdout=out, stderr=err) |
| 2055 |
call_command(command, force_color=True) |
| 2056 |
self.assertEqual(out.getvalue(), "\x1b[31;1mHello, world!\n\x1b[0m") |
| 2057 |
self.assertEqual(err.getvalue(), "\x1b[31;1mHello, world!\n\x1b[0m") |
| 2058 |
|
| 2059 |
def test_force_color_command_init(self): |
| 2060 |
out = StringIO() |
| 2061 |
err = StringIO() |
| 2062 |
with mock.patch.object(sys.stdout, "isatty", lambda: False): |
| 2063 |
command = ColorCommand(stdout=out, stderr=err, force_color=True) |
| 2064 |
call_command(command) |
| 2065 |
self.assertEqual(out.getvalue(), "\x1b[31;1mHello, world!\n\x1b[0m") |
| 2066 |
self.assertEqual(err.getvalue(), "\x1b[31;1mHello, world!\n\x1b[0m") |
| 2067 |
|
| 2068 |
def test_no_color_force_color_mutually_exclusive_execute(self): |
| 2069 |
msg = "The --no-color and --force-color options can't be used together." |
| 2070 |
with self.assertRaisesMessage(CommandError, msg): |
| 2071 |
call_command(BaseCommand(), no_color=True, force_color=True) |
| 2072 |
|
| 2073 |
def test_no_color_force_color_mutually_exclusive_command_init(self): |
| 2074 |
msg = "'no_color' and 'force_color' can't be used together." |
| 2075 |
with self.assertRaisesMessage(CommandError, msg): |
| 2076 |
call_command(BaseCommand(no_color=True, force_color=True)) |
| 2077 |
|
| 2078 |
def test_custom_stdout(self): |
| 2079 |
class Command(BaseCommand): |
| 2080 |
requires_system_checks = [] |
| 2081 |
|
| 2082 |
def handle(self, *args, **options): |
| 2083 |
self.stdout.write("Hello, World!") |
| 2084 |
|
| 2085 |
out = StringIO() |
| 2086 |
command = Command(stdout=out) |
| 2087 |
call_command(command) |
| 2088 |
self.assertEqual(out.getvalue(), "Hello, World!\n") |
| 2089 |
out.truncate(0) |
| 2090 |
new_out = StringIO() |
| 2091 |
call_command(command, stdout=new_out) |
| 2092 |
self.assertEqual(out.getvalue(), "") |
| 2093 |
self.assertEqual(new_out.getvalue(), "Hello, World!\n") |
| 2094 |
|
| 2095 |
def test_custom_stderr(self): |
| 2096 |
class Command(BaseCommand): |
| 2097 |
requires_system_checks = [] |
| 2098 |
|
| 2099 |
def handle(self, *args, **options): |
| 2100 |
self.stderr.write("Hello, World!") |
| 2101 |
|
| 2102 |
err = StringIO() |
| 2103 |
command = Command(stderr=err) |
| 2104 |
call_command(command) |
| 2105 |
self.assertEqual(err.getvalue(), "Hello, World!\n") |
| 2106 |
err.truncate(0) |
| 2107 |
new_err = StringIO() |
| 2108 |
call_command(command, stderr=new_err) |
| 2109 |
self.assertEqual(err.getvalue(), "") |
| 2110 |
self.assertEqual(new_err.getvalue(), "Hello, World!\n") |
| 2111 |
|
| 2112 |
def test_base_command(self): |
| 2113 |
"User BaseCommands can execute when a label is provided" |
| 2114 |
args = ["base_command", "testlabel"] |
| 2115 |
expected_labels = "('testlabel',)" |
| 2116 |
self._test_base_command(args, expected_labels) |
| 2117 |
|
| 2118 |
def test_base_command_no_label(self): |
| 2119 |
"User BaseCommands can execute when no labels are provided" |
| 2120 |
args = ["base_command"] |
| 2121 |
expected_labels = "()" |
| 2122 |
self._test_base_command(args, expected_labels) |
| 2123 |
|
| 2124 |
def test_base_command_multiple_label(self): |
| 2125 |
"User BaseCommands can execute when no labels are provided" |
| 2126 |
args = ["base_command", "testlabel", "anotherlabel"] |
| 2127 |
expected_labels = "('testlabel', 'anotherlabel')" |
| 2128 |
self._test_base_command(args, expected_labels) |
| 2129 |
|
| 2130 |
def test_base_command_with_option(self): |
| 2131 |
"User BaseCommands can execute with options when a label is provided" |
| 2132 |
args = ["base_command", "testlabel", "--option_a=x"] |
| 2133 |
expected_labels = "('testlabel',)" |
| 2134 |
self._test_base_command(args, expected_labels, option_a="'x'") |
| 2135 |
|
| 2136 |
def test_base_command_with_options(self): |
| 2137 |
"User BaseCommands can execute with multiple options when a label is provided" |
| 2138 |
args = ["base_command", "testlabel", "-a", "x", "--option_b=y"] |
| 2139 |
expected_labels = "('testlabel',)" |
| 2140 |
self._test_base_command(args, expected_labels, option_a="'x'", option_b="'y'") |
| 2141 |
|
| 2142 |
def test_base_command_with_wrong_option(self): |
| 2143 |
"User BaseCommands outputs command usage when wrong option is specified" |
| 2144 |
args = ["base_command", "--invalid"] |
| 2145 |
out, err = self.run_manage(args) |
| 2146 |
self.assertNoOutput(out) |
| 2147 |
self.assertOutput(err, "usage: manage.py base_command") |
| 2148 |
self.assertOutput(err, "error: unrecognized arguments: --invalid") |
| 2149 |
|
| 2150 |
def _test_base_command(self, args, labels, option_a="'1'", option_b="'2'"): |
| 2151 |
out, err = self.run_manage(args) |
| 2152 |
|
| 2153 |
expected_out = ( |
| 2154 |
"EXECUTE:BaseCommand labels=%s, " |
| 2155 |
"options=[('force_color', False), ('no_color', False), " |
| 2156 |
"('option_a', %s), ('option_b', %s), ('option_c', '3'), " |
| 2157 |
"('pythonpath', None), ('settings', None), ('traceback', False), " |
| 2158 |
"('verbosity', 1)]" |
| 2159 |
) % (labels, option_a, option_b) |
| 2160 |
self.assertNoOutput(err) |
| 2161 |
self.assertOutput(out, expected_out) |
| 2162 |
|
| 2163 |
def test_base_run_from_argv(self): |
| 2164 |
""" |
| 2165 |
Test run_from_argv properly terminates even with custom execute() (#19665) |
| 2166 |
Also test proper traceback display. |
| 2167 |
""" |
| 2168 |
err = StringIO() |
| 2169 |
command = BaseCommand(stderr=err) |
| 2170 |
|
| 2171 |
def raise_command_error(*args, **kwargs): |
| 2172 |
raise CommandError("Custom error") |
| 2173 |
|
| 2174 |
command.execute = lambda args: args # This will trigger TypeError |
| 2175 |
|
| 2176 |
# If the Exception is not CommandError it should always |
| 2177 |
# raise the original exception. |
| 2178 |
with self.assertRaises(TypeError): |
| 2179 |
command.run_from_argv(["", ""]) |
| 2180 |
|
| 2181 |
# If the Exception is CommandError and --traceback is not present |
| 2182 |
# this command should raise a SystemExit and don't print any |
| 2183 |
# traceback to the stderr. |
| 2184 |
command.execute = raise_command_error |
| 2185 |
err.truncate(0) |
| 2186 |
with self.assertRaises(SystemExit): |
| 2187 |
command.run_from_argv(["", ""]) |
| 2188 |
err_message = err.getvalue() |
| 2189 |
self.assertNotIn("Traceback", err_message) |
| 2190 |
self.assertIn("CommandError", err_message) |
| 2191 |
|
| 2192 |
# If the Exception is CommandError and --traceback is present |
| 2193 |
# this command should raise the original CommandError as if it |
| 2194 |
# were not a CommandError. |
| 2195 |
err.truncate(0) |
| 2196 |
with self.assertRaises(CommandError): |
| 2197 |
command.run_from_argv(["", "", "--traceback"]) |
| 2198 |
|
| 2199 |
def test_run_from_argv_non_ascii_error(self): |
| 2200 |
""" |
| 2201 |
Non-ASCII message of CommandError does not raise any |
| 2202 |
UnicodeDecodeError in run_from_argv. |
| 2203 |
""" |
| 2204 |
|
| 2205 |
def raise_command_error(*args, **kwargs): |
| 2206 |
raise CommandError("Erreur personnalisée") |
| 2207 |
|
| 2208 |
command = BaseCommand(stderr=StringIO()) |
| 2209 |
command.execute = raise_command_error |
| 2210 |
|
| 2211 |
with self.assertRaises(SystemExit): |
| 2212 |
command.run_from_argv(["", ""]) |
| 2213 |
|
| 2214 |
def test_run_from_argv_closes_connections(self): |
| 2215 |
""" |
| 2216 |
A command called from the command line should close connections after |
| 2217 |
being executed (#21255). |
| 2218 |
""" |
| 2219 |
command = BaseCommand() |
| 2220 |
command.check = lambda: [] |
| 2221 |
command.handle = lambda *args, **kwargs: args |
| 2222 |
with mock.patch("django.core.management.base.connections") as mock_connections: |
| 2223 |
command.run_from_argv(["", ""]) |
| 2224 |
# Test connections have been closed |
| 2225 |
self.assertTrue(mock_connections.close_all.called) |
| 2226 |
|
| 2227 |
def test_noargs(self): |
| 2228 |
"NoArg Commands can be executed" |
| 2229 |
args = ["noargs_command"] |
| 2230 |
out, err = self.run_manage(args) |
| 2231 |
self.assertNoOutput(err) |
| 2232 |
self.assertOutput( |
| 2233 |
out, |
| 2234 |
"EXECUTE: noargs_command options=[('force_color', False), " |
| 2235 |
"('no_color', False), ('pythonpath', None), ('settings', None), " |
| 2236 |
"('traceback', False), ('verbosity', 1)]", |
| 2237 |
) |
| 2238 |
|
| 2239 |
def test_noargs_with_args(self): |
| 2240 |
"NoArg Commands raise an error if an argument is provided" |
| 2241 |
args = ["noargs_command", "argument"] |
| 2242 |
out, err = self.run_manage(args) |
| 2243 |
self.assertOutput(err, "error: unrecognized arguments: argument") |
| 2244 |
|
| 2245 |
def test_app_command(self): |
| 2246 |
"User AppCommands can execute when a single app name is provided" |
| 2247 |
args = ["app_command", "auth"] |
| 2248 |
out, err = self.run_manage(args) |
| 2249 |
self.assertNoOutput(err) |
| 2250 |
self.assertOutput(out, "EXECUTE:AppCommand name=django.contrib.auth, options=") |
| 2251 |
self.assertOutput( |
| 2252 |
out, |
| 2253 |
", options=[('force_color', False), ('no_color', False), " |
| 2254 |
"('pythonpath', None), ('settings', None), ('traceback', False), " |
| 2255 |
"('verbosity', 1)]", |
| 2256 |
) |
| 2257 |
|
| 2258 |
def test_app_command_no_apps(self): |
| 2259 |
"User AppCommands raise an error when no app name is provided" |
| 2260 |
args = ["app_command"] |
| 2261 |
out, err = self.run_manage(args) |
| 2262 |
self.assertOutput(err, "error: Enter at least one application label.") |
| 2263 |
|
| 2264 |
def test_app_command_multiple_apps(self): |
| 2265 |
"User AppCommands raise an error when multiple app names are provided" |
| 2266 |
args = ["app_command", "auth", "contenttypes"] |
| 2267 |
out, err = self.run_manage(args) |
| 2268 |
self.assertNoOutput(err) |
| 2269 |
self.assertOutput(out, "EXECUTE:AppCommand name=django.contrib.auth, options=") |
| 2270 |
self.assertOutput( |
| 2271 |
out, |
| 2272 |
", options=[('force_color', False), ('no_color', False), " |
| 2273 |
"('pythonpath', None), ('settings', None), ('traceback', False), " |
| 2274 |
"('verbosity', 1)]", |
| 2275 |
) |
| 2276 |
self.assertOutput( |
| 2277 |
out, "EXECUTE:AppCommand name=django.contrib.contenttypes, options=" |
| 2278 |
) |
| 2279 |
self.assertOutput( |
| 2280 |
out, |
| 2281 |
", options=[('force_color', False), ('no_color', False), " |
| 2282 |
"('pythonpath', None), ('settings', None), ('traceback', False), " |
| 2283 |
"('verbosity', 1)]", |
| 2284 |
) |
| 2285 |
|
| 2286 |
def test_app_command_invalid_app_label(self): |
| 2287 |
"User AppCommands can execute when a single app name is provided" |
| 2288 |
args = ["app_command", "NOT_AN_APP"] |
| 2289 |
out, err = self.run_manage(args) |
| 2290 |
self.assertOutput(err, "No installed app with label 'NOT_AN_APP'.") |
| 2291 |
|
| 2292 |
def test_app_command_some_invalid_app_labels(self): |
| 2293 |
"User AppCommands can execute when some of the provided app names are invalid" |
| 2294 |
args = ["app_command", "auth", "NOT_AN_APP"] |
| 2295 |
out, err = self.run_manage(args) |
| 2296 |
self.assertOutput(err, "No installed app with label 'NOT_AN_APP'.") |
| 2297 |
|
| 2298 |
def test_label_command(self): |
| 2299 |
"User LabelCommands can execute when a label is provided" |
| 2300 |
args = ["label_command", "testlabel"] |
| 2301 |
out, err = self.run_manage(args) |
| 2302 |
self.assertNoOutput(err) |
| 2303 |
self.assertOutput( |
| 2304 |
out, |
| 2305 |
"EXECUTE:LabelCommand label=testlabel, options=[('force_color', " |
| 2306 |
"False), ('no_color', False), ('pythonpath', None), ('settings', " |
| 2307 |
"None), ('traceback', False), ('verbosity', 1)]", |
| 2308 |
) |
| 2309 |
|
| 2310 |
def test_label_command_no_label(self): |
| 2311 |
"User LabelCommands raise an error if no label is provided" |
| 2312 |
args = ["label_command"] |
| 2313 |
out, err = self.run_manage(args) |
| 2314 |
self.assertOutput(err, "Enter at least one label") |
| 2315 |
|
| 2316 |
def test_label_command_multiple_label(self): |
| 2317 |
"User LabelCommands are executed multiple times if multiple labels are provided" |
| 2318 |
args = ["label_command", "testlabel", "anotherlabel"] |
| 2319 |
out, err = self.run_manage(args) |
| 2320 |
self.assertNoOutput(err) |
| 2321 |
self.assertOutput( |
| 2322 |
out, |
| 2323 |
"EXECUTE:LabelCommand label=testlabel, options=[('force_color', " |
| 2324 |
"False), ('no_color', False), ('pythonpath', None), " |
| 2325 |
"('settings', None), ('traceback', False), ('verbosity', 1)]", |
| 2326 |
) |
| 2327 |
self.assertOutput( |
| 2328 |
out, |
| 2329 |
"EXECUTE:LabelCommand label=anotherlabel, options=[('force_color', " |
| 2330 |
"False), ('no_color', False), ('pythonpath', None), " |
| 2331 |
"('settings', None), ('traceback', False), ('verbosity', 1)]", |
| 2332 |
) |
| 2333 |
|
| 2334 |
def test_custom_label_command_custom_missing_args_message(self): |
| 2335 |
class Command(LabelCommand): |
| 2336 |
missing_args_message = "Missing argument." |
| 2337 |
|
| 2338 |
with self.assertRaisesMessage(CommandError, "Error: Missing argument."): |
| 2339 |
call_command(Command()) |
| 2340 |
|
| 2341 |
def test_custom_label_command_none_missing_args_message(self): |
| 2342 |
class Command(LabelCommand): |
| 2343 |
missing_args_message = None |
| 2344 |
|
| 2345 |
with self.assertRaisesMessage(CommandError, ""): |
| 2346 |
call_command(Command()) |
| 2347 |
|
| 2348 |
def test_suppress_base_options_command_help(self): |
| 2349 |
args = ["suppress_base_options_command", "--help"] |
| 2350 |
out, err = self.run_manage(args) |
| 2351 |
self.assertNoOutput(err) |
| 2352 |
self.assertOutput(out, "Test suppress base options command.") |
| 2353 |
self.assertNotInOutput(out, "input file") |
| 2354 |
self.assertOutput(out, "-h, --help") |
| 2355 |
self.assertNotInOutput(out, "--version") |
| 2356 |
self.assertNotInOutput(out, "--verbosity") |
| 2357 |
self.assertNotInOutput(out, "-v {0,1,2,3}") |
| 2358 |
self.assertNotInOutput(out, "--settings") |
| 2359 |
self.assertNotInOutput(out, "--pythonpath") |
| 2360 |
self.assertNotInOutput(out, "--traceback") |
| 2361 |
self.assertNotInOutput(out, "--no-color") |
| 2362 |
self.assertNotInOutput(out, "--force-color") |
| 2363 |
|
| 2364 |
def test_suppress_base_options_command_defaults(self): |
| 2365 |
args = ["suppress_base_options_command"] |
| 2366 |
out, err = self.run_manage(args) |
| 2367 |
self.assertNoOutput(err) |
| 2368 |
self.assertOutput( |
| 2369 |
out, |
| 2370 |
"EXECUTE:SuppressBaseOptionsCommand options=[('file', None), " |
| 2371 |
"('force_color', False), ('no_color', False), " |
| 2372 |
"('pythonpath', None), ('settings', None), " |
| 2373 |
"('traceback', False), ('verbosity', 1)]", |
| 2374 |
) |
| 2375 |
|
| 2376 |
|
| 2377 |
class Discovery(SimpleTestCase): |
| 2378 |
def test_precedence(self): |
| 2379 |
""" |
| 2380 |
Apps listed first in INSTALLED_APPS have precedence. |
| 2381 |
""" |
| 2382 |
with self.settings( |
| 2383 |
INSTALLED_APPS=[ |
| 2384 |
"admin_scripts.complex_app", |
| 2385 |
"admin_scripts.simple_app", |
| 2386 |
"django.contrib.auth", |
| 2387 |
"django.contrib.contenttypes", |
| 2388 |
] |
| 2389 |
): |
| 2390 |
out = StringIO() |
| 2391 |
call_command("duplicate", stdout=out) |
| 2392 |
self.assertEqual(out.getvalue().strip(), "complex_app") |
| 2393 |
with self.settings( |
| 2394 |
INSTALLED_APPS=[ |
| 2395 |
"admin_scripts.simple_app", |
| 2396 |
"admin_scripts.complex_app", |
| 2397 |
"django.contrib.auth", |
| 2398 |
"django.contrib.contenttypes", |
| 2399 |
] |
| 2400 |
): |
| 2401 |
out = StringIO() |
| 2402 |
call_command("duplicate", stdout=out) |
| 2403 |
self.assertEqual(out.getvalue().strip(), "simple_app") |
| 2404 |
|
| 2405 |
|
| 2406 |
class CommandDBOptionChoiceTests(SimpleTestCase): |
| 2407 |
def test_invalid_choice_db_option(self): |
| 2408 |
expected_error = ( |
| 2409 |
r"Error: argument --database: invalid choice: 'deflaut' " |
| 2410 |
r"\(choose from '?default'?, '?other'?\)" |
| 2411 |
) |
| 2412 |
args = [ |
| 2413 |
"changepassword", |
| 2414 |
"createsuperuser", |
| 2415 |
"remove_stale_contenttypes", |
| 2416 |
"check", |
| 2417 |
"createcachetable", |
| 2418 |
"dbshell", |
| 2419 |
"flush", |
| 2420 |
"dumpdata", |
| 2421 |
"inspectdb", |
| 2422 |
"loaddata", |
| 2423 |
"showmigrations", |
| 2424 |
"sqlflush", |
| 2425 |
"sqlmigrate", |
| 2426 |
"sqlsequencereset", |
| 2427 |
"migrate", |
| 2428 |
] |
| 2429 |
|
| 2430 |
for arg in args: |
| 2431 |
with self.assertRaisesRegex(CommandError, expected_error): |
| 2432 |
call_command(arg, "--database", "deflaut", verbosity=0) |
| 2433 |
|
| 2434 |
|
| 2435 |
class ArgumentOrder(AdminScriptTestCase): |
| 2436 |
"""Tests for 2-stage argument parsing scheme. |
| 2437 |
|
| 2438 |
django-admin command arguments are parsed in 2 parts; the core arguments |
| 2439 |
(--settings, --traceback and --pythonpath) are parsed using a basic parser, |
| 2440 |
ignoring any unknown options. Then the full settings are |
| 2441 |
passed to the command parser, which extracts commands of interest to the |
| 2442 |
individual command. |
| 2443 |
""" |
| 2444 |
|
| 2445 |
def setUp(self): |
| 2446 |
super().setUp() |
| 2447 |
self.write_settings( |
| 2448 |
"settings.py", apps=["django.contrib.auth", "django.contrib.contenttypes"] |
| 2449 |
) |
| 2450 |
self.write_settings("alternate_settings.py") |
| 2451 |
|
| 2452 |
def test_setting_then_option(self): |
| 2453 |
"""Options passed after settings are correctly handled.""" |
| 2454 |
args = [ |
| 2455 |
"base_command", |
| 2456 |
"testlabel", |
| 2457 |
"--settings=alternate_settings", |
| 2458 |
"--option_a=x", |
| 2459 |
] |
| 2460 |
self._test(args) |
| 2461 |
|
| 2462 |
def test_setting_then_short_option(self): |
| 2463 |
"""Short options passed after settings are correctly handled.""" |
| 2464 |
args = ["base_command", "testlabel", "--settings=alternate_settings", "-a", "x"] |
| 2465 |
self._test(args) |
| 2466 |
|
| 2467 |
def test_option_then_setting(self): |
| 2468 |
"""Options passed before settings are correctly handled.""" |
| 2469 |
args = [ |
| 2470 |
"base_command", |
| 2471 |
"testlabel", |
| 2472 |
"--option_a=x", |
| 2473 |
"--settings=alternate_settings", |
| 2474 |
] |
| 2475 |
self._test(args) |
| 2476 |
|
| 2477 |
def test_short_option_then_setting(self): |
| 2478 |
"""Short options passed before settings are correctly handled.""" |
| 2479 |
args = ["base_command", "testlabel", "-a", "x", "--settings=alternate_settings"] |
| 2480 |
self._test(args) |
| 2481 |
|
| 2482 |
def test_option_then_setting_then_option(self): |
| 2483 |
"""Options are correctly handled when they are passed before and after |
| 2484 |
a setting.""" |
| 2485 |
args = [ |
| 2486 |
"base_command", |
| 2487 |
"testlabel", |
| 2488 |
"--option_a=x", |
| 2489 |
"--settings=alternate_settings", |
| 2490 |
"--option_b=y", |
| 2491 |
] |
| 2492 |
self._test(args, option_b="'y'") |
| 2493 |
|
| 2494 |
def _test(self, args, option_b="'2'"): |
| 2495 |
out, err = self.run_manage(args) |
| 2496 |
self.assertNoOutput(err) |
| 2497 |
self.assertOutput( |
| 2498 |
out, |
| 2499 |
"EXECUTE:BaseCommand labels=('testlabel',), options=[" |
| 2500 |
"('force_color', False), ('no_color', False), ('option_a', 'x'), " |
| 2501 |
"('option_b', %s), ('option_c', '3'), ('pythonpath', None), " |
| 2502 |
"('settings', 'alternate_settings'), ('traceback', False), " |
| 2503 |
"('verbosity', 1)]" % option_b, |
| 2504 |
) |
| 2505 |
|
| 2506 |
|
| 2507 |
class ExecuteFromCommandLine(SimpleTestCase): |
| 2508 |
def test_program_name_from_argv(self): |
| 2509 |
""" |
| 2510 |
Program name is computed from the execute_from_command_line()'s argv |
| 2511 |
argument, not sys.argv. |
| 2512 |
""" |
| 2513 |
args = ["help", "shell"] |
| 2514 |
with captured_stdout() as out, captured_stderr() as err: |
| 2515 |
with mock.patch("sys.argv", [None] + args): |
| 2516 |
execute_from_command_line(["django-admin"] + args) |
| 2517 |
self.assertIn("usage: django-admin shell", out.getvalue()) |
| 2518 |
self.assertEqual(err.getvalue(), "") |
| 2519 |
|
| 2520 |
|
| 2521 |
@override_settings(ROOT_URLCONF="admin_scripts.urls") |
| 2522 |
class StartProject(LiveServerTestCase, AdminScriptTestCase): |
| 2523 |
available_apps = [ |
| 2524 |
"admin_scripts", |
| 2525 |
"django.contrib.auth", |
| 2526 |
"django.contrib.contenttypes", |
| 2527 |
"django.contrib.sessions", |
| 2528 |
] |
| 2529 |
|
| 2530 |
def test_wrong_args(self): |
| 2531 |
""" |
| 2532 |
Passing the wrong kinds of arguments outputs an error and prints usage. |
| 2533 |
""" |
| 2534 |
out, err = self.run_django_admin(["startproject"]) |
| 2535 |
self.assertNoOutput(out) |
| 2536 |
self.assertOutput(err, "usage:") |
| 2537 |
self.assertOutput(err, "You must provide a project name.") |
| 2538 |
|
| 2539 |
def test_simple_project(self): |
| 2540 |
"Make sure the startproject management command creates a project" |
| 2541 |
args = ["startproject", "testproject"] |
| 2542 |
testproject_dir = os.path.join(self.test_dir, "testproject") |
| 2543 |
|
| 2544 |
out, err = self.run_django_admin(args) |
| 2545 |
self.assertNoOutput(err) |
| 2546 |
self.assertTrue(os.path.isdir(testproject_dir)) |
| 2547 |
|
| 2548 |
# running again.. |
| 2549 |
out, err = self.run_django_admin(args) |
| 2550 |
self.assertNoOutput(out) |
| 2551 |
self.assertOutput( |
| 2552 |
err, |
| 2553 |
"CommandError: 'testproject' conflicts with the name of an " |
| 2554 |
"existing Python module and cannot be used as a project name. " |
| 2555 |
"Please try another name.", |
| 2556 |
) |
| 2557 |
|
| 2558 |
def test_invalid_project_name(self): |
| 2559 |
"Make sure the startproject management command validates a project name" |
| 2560 |
for bad_name in ("7testproject", "../testproject"): |
| 2561 |
with self.subTest(project_name=bad_name): |
| 2562 |
args = ["startproject", bad_name] |
| 2563 |
testproject_dir = os.path.join(self.test_dir, bad_name) |
| 2564 |
|
| 2565 |
out, err = self.run_django_admin(args) |
| 2566 |
self.assertOutput( |
| 2567 |
err, |
| 2568 |
"Error: '%s' is not a valid project name. Please make " |
| 2569 |
"sure the name is a valid identifier." % bad_name, |
| 2570 |
) |
| 2571 |
self.assertFalse(os.path.exists(testproject_dir)) |
| 2572 |
|
| 2573 |
def test_importable_project_name(self): |
| 2574 |
""" |
| 2575 |
startproject validates that project name doesn't clash with existing |
| 2576 |
Python modules. |
| 2577 |
""" |
| 2578 |
bad_name = "os" |
| 2579 |
args = ["startproject", bad_name] |
| 2580 |
testproject_dir = os.path.join(self.test_dir, bad_name) |
| 2581 |
|
| 2582 |
out, err = self.run_django_admin(args) |
| 2583 |
self.assertOutput( |
| 2584 |
err, |
| 2585 |
"CommandError: 'os' conflicts with the name of an existing " |
| 2586 |
"Python module and cannot be used as a project name. Please try " |
| 2587 |
"another name.", |
| 2588 |
) |
| 2589 |
self.assertFalse(os.path.exists(testproject_dir)) |
| 2590 |
|
| 2591 |
def test_command_does_not_import(self): |
| 2592 |
""" |
| 2593 |
startproject doesn't import modules (and cannot be fooled by a module |
| 2594 |
raising ImportError). |
| 2595 |
""" |
| 2596 |
bad_name = "raises_import_error" |
| 2597 |
args = ["startproject", bad_name] |
| 2598 |
testproject_dir = os.path.join(self.test_dir, bad_name) |
| 2599 |
|
| 2600 |
with open(os.path.join(self.test_dir, "raises_import_error.py"), "w") as f: |
| 2601 |
f.write("raise ImportError") |
| 2602 |
|
| 2603 |
out, err = self.run_django_admin(args) |
| 2604 |
self.assertOutput( |
| 2605 |
err, |
| 2606 |
"CommandError: 'raises_import_error' conflicts with the name of an " |
| 2607 |
"existing Python module and cannot be used as a project name. Please try " |
| 2608 |
"another name.", |
| 2609 |
) |
| 2610 |
self.assertNoOutput(out) |
| 2611 |
self.assertFalse(os.path.exists(testproject_dir)) |
| 2612 |
|
| 2613 |
def test_simple_project_different_directory(self): |
| 2614 |
""" |
| 2615 |
The startproject management command creates a project in a specific |
| 2616 |
directory. |
| 2617 |
""" |
| 2618 |
args = ["startproject", "testproject", "othertestproject"] |
| 2619 |
testproject_dir = os.path.join(self.test_dir, "othertestproject") |
| 2620 |
os.mkdir(testproject_dir) |
| 2621 |
|
| 2622 |
out, err = self.run_django_admin(args) |
| 2623 |
self.assertNoOutput(err) |
| 2624 |
self.assertTrue(os.path.exists(os.path.join(testproject_dir, "manage.py"))) |
| 2625 |
|
| 2626 |
# running again.. |
| 2627 |
out, err = self.run_django_admin(args) |
| 2628 |
self.assertNoOutput(out) |
| 2629 |
self.assertOutput( |
| 2630 |
err, |
| 2631 |
"already exists. Overlaying a project into an existing directory " |
| 2632 |
"won't replace conflicting files.", |
| 2633 |
) |
| 2634 |
|
| 2635 |
def test_custom_project_template(self): |
| 2636 |
""" |
| 2637 |
The startproject management command is able to use a different project |
| 2638 |
template. |
| 2639 |
""" |
| 2640 |
template_path = os.path.join(custom_templates_dir, "project_template") |
| 2641 |
args = ["startproject", "--template", template_path, "customtestproject"] |
| 2642 |
testproject_dir = os.path.join(self.test_dir, "customtestproject") |
| 2643 |
|
| 2644 |
out, err = self.run_django_admin(args) |
| 2645 |
self.assertNoOutput(err) |
| 2646 |
self.assertTrue(os.path.isdir(testproject_dir)) |
| 2647 |
self.assertTrue(os.path.exists(os.path.join(testproject_dir, "additional_dir"))) |
| 2648 |
|
| 2649 |
def test_custom_project_template_non_python_files_not_formatted(self): |
| 2650 |
template_path = os.path.join(custom_templates_dir, "project_template") |
| 2651 |
args = ["startproject", "--template", template_path, "customtestproject"] |
| 2652 |
testproject_dir = os.path.join(self.test_dir, "customtestproject") |
| 2653 |
|
| 2654 |
_, err = self.run_django_admin(args) |
| 2655 |
self.assertNoOutput(err) |
| 2656 |
with open( |
| 2657 |
os.path.join(template_path, "additional_dir", "requirements.in") |
| 2658 |
) as f: |
| 2659 |
expected = f.read() |
| 2660 |
with open( |
| 2661 |
os.path.join(testproject_dir, "additional_dir", "requirements.in") |
| 2662 |
) as f: |
| 2663 |
result = f.read() |
| 2664 |
self.assertEqual(expected, result) |
| 2665 |
|
| 2666 |
def test_template_dir_with_trailing_slash(self): |
| 2667 |
"Ticket 17475: Template dir passed has a trailing path separator" |
| 2668 |
template_path = os.path.join(custom_templates_dir, "project_template" + os.sep) |
| 2669 |
args = ["startproject", "--template", template_path, "customtestproject"] |
| 2670 |
testproject_dir = os.path.join(self.test_dir, "customtestproject") |
| 2671 |
|
| 2672 |
out, err = self.run_django_admin(args) |
| 2673 |
self.assertNoOutput(err) |
| 2674 |
self.assertTrue(os.path.isdir(testproject_dir)) |
| 2675 |
self.assertTrue(os.path.exists(os.path.join(testproject_dir, "additional_dir"))) |
| 2676 |
|
| 2677 |
def test_custom_project_template_from_tarball_by_path(self): |
| 2678 |
""" |
| 2679 |
The startproject management command is able to use a different project |
| 2680 |
template from a tarball. |
| 2681 |
""" |
| 2682 |
template_path = os.path.join(custom_templates_dir, "project_template.tgz") |
| 2683 |
args = ["startproject", "--template", template_path, "tarballtestproject"] |
| 2684 |
testproject_dir = os.path.join(self.test_dir, "tarballtestproject") |
| 2685 |
|
| 2686 |
out, err = self.run_django_admin(args) |
| 2687 |
self.assertNoOutput(err) |
| 2688 |
self.assertTrue(os.path.isdir(testproject_dir)) |
| 2689 |
self.assertTrue(os.path.exists(os.path.join(testproject_dir, "run.py"))) |
| 2690 |
|
| 2691 |
def test_custom_project_template_from_tarball_to_alternative_location(self): |
| 2692 |
""" |
| 2693 |
Startproject can use a project template from a tarball and create it in |
| 2694 |
a specified location. |
| 2695 |
""" |
| 2696 |
template_path = os.path.join(custom_templates_dir, "project_template.tgz") |
| 2697 |
args = [ |
| 2698 |
"startproject", |
| 2699 |
"--template", |
| 2700 |
template_path, |
| 2701 |
"tarballtestproject", |
| 2702 |
"altlocation", |
| 2703 |
] |
| 2704 |
testproject_dir = os.path.join(self.test_dir, "altlocation") |
| 2705 |
os.mkdir(testproject_dir) |
| 2706 |
|
| 2707 |
out, err = self.run_django_admin(args) |
| 2708 |
self.assertNoOutput(err) |
| 2709 |
self.assertTrue(os.path.isdir(testproject_dir)) |
| 2710 |
self.assertTrue(os.path.exists(os.path.join(testproject_dir, "run.py"))) |
| 2711 |
|
| 2712 |
def test_custom_project_template_from_tarball_by_url(self): |
| 2713 |
""" |
| 2714 |
The startproject management command is able to use a different project |
| 2715 |
template from a tarball via a URL. |
| 2716 |
""" |
| 2717 |
template_url = "%s/custom_templates/project_template.tgz" % self.live_server_url |
| 2718 |
|
| 2719 |
args = ["startproject", "--template", template_url, "urltestproject"] |
| 2720 |
testproject_dir = os.path.join(self.test_dir, "urltestproject") |
| 2721 |
|
| 2722 |
out, err = self.run_django_admin(args) |
| 2723 |
self.assertNoOutput(err) |
| 2724 |
self.assertTrue(os.path.isdir(testproject_dir)) |
| 2725 |
self.assertTrue(os.path.exists(os.path.join(testproject_dir, "run.py"))) |
| 2726 |
|
| 2727 |
def test_custom_project_template_from_tarball_by_url_django_user_agent(self): |
| 2728 |
user_agent = None |
| 2729 |
|
| 2730 |
def serve_template(request, *args, **kwargs): |
| 2731 |
nonlocal user_agent |
| 2732 |
user_agent = request.headers["User-Agent"] |
| 2733 |
return serve(request, *args, **kwargs) |
| 2734 |
|
| 2735 |
old_urlpatterns = urls.urlpatterns[:] |
| 2736 |
try: |
| 2737 |
urls.urlpatterns += [ |
| 2738 |
path( |
| 2739 |
"user_agent_check/<path:path>", |
| 2740 |
serve_template, |
| 2741 |
{"document_root": os.path.join(urls.here, "custom_templates")}, |
| 2742 |
), |
| 2743 |
] |
| 2744 |
|
| 2745 |
template_url = ( |
| 2746 |
f"{self.live_server_url}/user_agent_check/project_template.tgz" |
| 2747 |
) |
| 2748 |
args = ["startproject", "--template", template_url, "urltestproject"] |
| 2749 |
_, err = self.run_django_admin(args) |
| 2750 |
|
| 2751 |
self.assertNoOutput(err) |
| 2752 |
self.assertIn("Django/%s" % get_version(), user_agent) |
| 2753 |
finally: |
| 2754 |
urls.urlpatterns = old_urlpatterns |
| 2755 |
|
| 2756 |
def test_project_template_tarball_url(self): |
| 2757 |
""" |
| 2758 |
Startproject management command handles project template tar/zip balls |
| 2759 |
from non-canonical urls. |
| 2760 |
""" |
| 2761 |
template_url = ( |
| 2762 |
"%s/custom_templates/project_template.tgz/" % self.live_server_url |
| 2763 |
) |
| 2764 |
|
| 2765 |
args = ["startproject", "--template", template_url, "urltestproject"] |
| 2766 |
testproject_dir = os.path.join(self.test_dir, "urltestproject") |
| 2767 |
|
| 2768 |
out, err = self.run_django_admin(args) |
| 2769 |
self.assertNoOutput(err) |
| 2770 |
self.assertTrue(os.path.isdir(testproject_dir)) |
| 2771 |
self.assertTrue(os.path.exists(os.path.join(testproject_dir, "run.py"))) |
| 2772 |
|
| 2773 |
def test_file_without_extension(self): |
| 2774 |
"Make sure the startproject management command is able to render custom files" |
| 2775 |
template_path = os.path.join(custom_templates_dir, "project_template") |
| 2776 |
args = [ |
| 2777 |
"startproject", |
| 2778 |
"--template", |
| 2779 |
template_path, |
| 2780 |
"customtestproject", |
| 2781 |
"-e", |
| 2782 |
"txt", |
| 2783 |
"-n", |
| 2784 |
"Procfile", |
| 2785 |
] |
| 2786 |
testproject_dir = os.path.join(self.test_dir, "customtestproject") |
| 2787 |
|
| 2788 |
out, err = self.run_django_admin(args) |
| 2789 |
self.assertNoOutput(err) |
| 2790 |
self.assertTrue(os.path.isdir(testproject_dir)) |
| 2791 |
self.assertTrue(os.path.exists(os.path.join(testproject_dir, "additional_dir"))) |
| 2792 |
base_path = os.path.join(testproject_dir, "additional_dir") |
| 2793 |
for f in ("Procfile", "additional_file.py", "requirements.txt"): |
| 2794 |
self.assertTrue(os.path.exists(os.path.join(base_path, f))) |
| 2795 |
with open(os.path.join(base_path, f)) as fh: |
| 2796 |
self.assertEqual( |
| 2797 |
fh.read().strip(), "# some file for customtestproject test project" |
| 2798 |
) |
| 2799 |
|
| 2800 |
def test_custom_project_template_context_variables(self): |
| 2801 |
"Make sure template context variables are rendered with proper values" |
| 2802 |
template_path = os.path.join(custom_templates_dir, "project_template") |
| 2803 |
args = [ |
| 2804 |
"startproject", |
| 2805 |
"--template", |
| 2806 |
template_path, |
| 2807 |
"another_project", |
| 2808 |
"project_dir", |
| 2809 |
] |
| 2810 |
testproject_dir = os.path.join(self.test_dir, "project_dir") |
| 2811 |
os.mkdir(testproject_dir) |
| 2812 |
out, err = self.run_django_admin(args) |
| 2813 |
self.assertNoOutput(err) |
| 2814 |
test_manage_py = os.path.join(testproject_dir, "manage.py") |
| 2815 |
with open(test_manage_py) as fp: |
| 2816 |
content = fp.read() |
| 2817 |
self.assertIn('project_name = "another_project"', content) |
| 2818 |
self.assertIn('project_directory = "%s"' % testproject_dir, content) |
| 2819 |
|
| 2820 |
def test_no_escaping_of_project_variables(self): |
| 2821 |
"Make sure template context variables are not html escaped" |
| 2822 |
# We're using a custom command so we need the alternate settings |
| 2823 |
self.write_settings("alternate_settings.py") |
| 2824 |
template_path = os.path.join(custom_templates_dir, "project_template") |
| 2825 |
args = [ |
| 2826 |
"custom_startproject", |
| 2827 |
"--template", |
| 2828 |
template_path, |
| 2829 |
"another_project", |
| 2830 |
"project_dir", |
| 2831 |
"--extra", |
| 2832 |
"<&>", |
| 2833 |
"--settings=alternate_settings", |
| 2834 |
] |
| 2835 |
testproject_dir = os.path.join(self.test_dir, "project_dir") |
| 2836 |
os.mkdir(testproject_dir) |
| 2837 |
out, err = self.run_manage(args) |
| 2838 |
self.assertNoOutput(err) |
| 2839 |
test_manage_py = os.path.join(testproject_dir, "additional_dir", "extra.py") |
| 2840 |
with open(test_manage_py) as fp: |
| 2841 |
content = fp.read() |
| 2842 |
self.assertIn("<&>", content) |
| 2843 |
|
| 2844 |
def test_custom_project_destination_missing(self): |
| 2845 |
""" |
| 2846 |
Make sure an exception is raised when the provided |
| 2847 |
destination directory doesn't exist |
| 2848 |
""" |
| 2849 |
template_path = os.path.join(custom_templates_dir, "project_template") |
| 2850 |
args = [ |
| 2851 |
"startproject", |
| 2852 |
"--template", |
| 2853 |
template_path, |
| 2854 |
"yet_another_project", |
| 2855 |
"project_dir2", |
| 2856 |
] |
| 2857 |
testproject_dir = os.path.join(self.test_dir, "project_dir2") |
| 2858 |
out, err = self.run_django_admin(args) |
| 2859 |
self.assertNoOutput(out) |
| 2860 |
self.assertOutput( |
| 2861 |
err, |
| 2862 |
"Destination directory '%s' does not exist, please create it first." |
| 2863 |
% testproject_dir, |
| 2864 |
) |
| 2865 |
self.assertFalse(os.path.exists(testproject_dir)) |
| 2866 |
|
| 2867 |
def test_custom_project_template_with_non_ascii_templates(self): |
| 2868 |
""" |
| 2869 |
The startproject management command is able to render templates with |
| 2870 |
non-ASCII content. |
| 2871 |
""" |
| 2872 |
template_path = os.path.join(custom_templates_dir, "project_template") |
| 2873 |
args = [ |
| 2874 |
"startproject", |
| 2875 |
"--template", |
| 2876 |
template_path, |
| 2877 |
"--extension=txt", |
| 2878 |
"customtestproject", |
| 2879 |
] |
| 2880 |
testproject_dir = os.path.join(self.test_dir, "customtestproject") |
| 2881 |
|
| 2882 |
out, err = self.run_django_admin(args) |
| 2883 |
self.assertNoOutput(err) |
| 2884 |
self.assertTrue(os.path.isdir(testproject_dir)) |
| 2885 |
path = os.path.join(testproject_dir, "ticket-18091-non-ascii-template.txt") |
| 2886 |
with open(path, encoding="utf-8") as f: |
| 2887 |
self.assertEqual( |
| 2888 |
f.read().splitlines(False), |
| 2889 |
["Some non-ASCII text for testing ticket #18091:", "üäö €"], |
| 2890 |
) |
| 2891 |
|
| 2892 |
def test_custom_project_template_hidden_directory_default_excluded(self): |
| 2893 |
"""Hidden directories are excluded by default.""" |
| 2894 |
template_path = os.path.join(custom_templates_dir, "project_template") |
| 2895 |
args = [ |
| 2896 |
"startproject", |
| 2897 |
"--template", |
| 2898 |
template_path, |
| 2899 |
"custom_project_template_hidden_directories", |
| 2900 |
"project_dir", |
| 2901 |
] |
| 2902 |
testproject_dir = os.path.join(self.test_dir, "project_dir") |
| 2903 |
os.mkdir(testproject_dir) |
| 2904 |
|
| 2905 |
_, err = self.run_django_admin(args) |
| 2906 |
self.assertNoOutput(err) |
| 2907 |
hidden_dir = os.path.join(testproject_dir, ".hidden") |
| 2908 |
self.assertIs(os.path.exists(hidden_dir), False) |
| 2909 |
|
| 2910 |
def test_custom_project_template_hidden_directory_included(self): |
| 2911 |
""" |
| 2912 |
Template context variables in hidden directories are rendered, if not |
| 2913 |
excluded. |
| 2914 |
""" |
| 2915 |
template_path = os.path.join(custom_templates_dir, "project_template") |
| 2916 |
project_name = "custom_project_template_hidden_directories_included" |
| 2917 |
args = [ |
| 2918 |
"startproject", |
| 2919 |
"--template", |
| 2920 |
template_path, |
| 2921 |
project_name, |
| 2922 |
"project_dir", |
| 2923 |
"--exclude", |
| 2924 |
] |
| 2925 |
testproject_dir = os.path.join(self.test_dir, "project_dir") |
| 2926 |
os.mkdir(testproject_dir) |
| 2927 |
|
| 2928 |
_, err = self.run_django_admin(args) |
| 2929 |
self.assertNoOutput(err) |
| 2930 |
render_py_path = os.path.join(testproject_dir, ".hidden", "render.py") |
| 2931 |
with open(render_py_path) as fp: |
| 2932 |
self.assertIn( |
| 2933 |
f"# The {project_name} should be rendered.", |
| 2934 |
fp.read(), |
| 2935 |
) |
| 2936 |
|
| 2937 |
def test_custom_project_template_exclude_directory(self): |
| 2938 |
""" |
| 2939 |
Excluded directories (in addition to .git and __pycache__) are not |
| 2940 |
included in the project. |
| 2941 |
""" |
| 2942 |
template_path = os.path.join(custom_templates_dir, "project_template") |
| 2943 |
project_name = "custom_project_with_excluded_directories" |
| 2944 |
args = [ |
| 2945 |
"startproject", |
| 2946 |
"--template", |
| 2947 |
template_path, |
| 2948 |
project_name, |
| 2949 |
"project_dir", |
| 2950 |
"--exclude", |
| 2951 |
"additional_dir", |
| 2952 |
"-x", |
| 2953 |
".hidden", |
| 2954 |
] |
| 2955 |
testproject_dir = os.path.join(self.test_dir, "project_dir") |
| 2956 |
os.mkdir(testproject_dir) |
| 2957 |
|
| 2958 |
_, err = self.run_django_admin(args) |
| 2959 |
self.assertNoOutput(err) |
| 2960 |
excluded_directories = [ |
| 2961 |
".hidden", |
| 2962 |
"additional_dir", |
| 2963 |
".git", |
| 2964 |
"__pycache__", |
| 2965 |
] |
| 2966 |
for directory in excluded_directories: |
| 2967 |
self.assertIs( |
| 2968 |
os.path.exists(os.path.join(testproject_dir, directory)), |
| 2969 |
False, |
| 2970 |
) |
| 2971 |
not_excluded = os.path.join(testproject_dir, project_name) |
| 2972 |
self.assertIs(os.path.exists(not_excluded), True) |
| 2973 |
|
| 2974 |
@unittest.skipIf( |
| 2975 |
sys.platform == "win32", |
| 2976 |
"Windows only partially supports umasks and chmod.", |
| 2977 |
) |
| 2978 |
def test_honor_umask(self): |
| 2979 |
_, err = self.run_django_admin(["startproject", "testproject"], umask=0o077) |
| 2980 |
self.assertNoOutput(err) |
| 2981 |
testproject_dir = os.path.join(self.test_dir, "testproject") |
| 2982 |
self.assertIs(os.path.isdir(testproject_dir), True) |
| 2983 |
tests = [ |
| 2984 |
(["manage.py"], 0o700), |
| 2985 |
(["testproject"], 0o700), |
| 2986 |
(["testproject", "settings.py"], 0o600), |
| 2987 |
] |
| 2988 |
for paths, expected_mode in tests: |
| 2989 |
file_path = os.path.join(testproject_dir, *paths) |
| 2990 |
with self.subTest(paths[-1]): |
| 2991 |
self.assertEqual( |
| 2992 |
stat.S_IMODE(os.stat(file_path).st_mode), |
| 2993 |
expected_mode, |
| 2994 |
) |
| 2995 |
|
| 2996 |
def test_failure_to_format_code(self): |
| 2997 |
with AssertFormatterFailureCaughtContext(self) as ctx: |
| 2998 |
call_command( |
| 2999 |
"startapp", |
| 3000 |
"mynewapp", |
| 3001 |
directory=self.test_dir, |
| 3002 |
stdout=ctx.stdout, |
| 3003 |
stderr=ctx.stderr, |
| 3004 |
) |
| 3005 |
|
| 3006 |
|
| 3007 |
class StartApp(AdminScriptTestCase): |
| 3008 |
def test_invalid_name(self): |
| 3009 |
"""startapp validates that app name is a valid Python identifier.""" |
| 3010 |
for bad_name in ("7testproject", "../testproject"): |
| 3011 |
with self.subTest(app_name=bad_name): |
| 3012 |
args = ["startapp", bad_name] |
| 3013 |
testproject_dir = os.path.join(self.test_dir, bad_name) |
| 3014 |
|
| 3015 |
out, err = self.run_django_admin(args) |
| 3016 |
self.assertOutput( |
| 3017 |
err, |
| 3018 |
"CommandError: '{}' is not a valid app name. Please make " |
| 3019 |
"sure the name is a valid identifier.".format(bad_name), |
| 3020 |
) |
| 3021 |
self.assertFalse(os.path.exists(testproject_dir)) |
| 3022 |
|
| 3023 |
def test_importable_name(self): |
| 3024 |
""" |
| 3025 |
startapp validates that app name doesn't clash with existing Python |
| 3026 |
modules. |
| 3027 |
""" |
| 3028 |
bad_name = "os" |
| 3029 |
args = ["startapp", bad_name] |
| 3030 |
testproject_dir = os.path.join(self.test_dir, bad_name) |
| 3031 |
|
| 3032 |
out, err = self.run_django_admin(args) |
| 3033 |
self.assertOutput( |
| 3034 |
err, |
| 3035 |
"CommandError: 'os' conflicts with the name of an existing " |
| 3036 |
"Python module and cannot be used as an app name. Please try " |
| 3037 |
"another name.", |
| 3038 |
) |
| 3039 |
self.assertFalse(os.path.exists(testproject_dir)) |
| 3040 |
|
| 3041 |
def test_invalid_target_name(self): |
| 3042 |
for bad_target in ( |
| 3043 |
"invalid.dir_name", |
| 3044 |
"7invalid_dir_name", |
| 3045 |
".invalid_dir_name", |
| 3046 |
): |
| 3047 |
with self.subTest(bad_target): |
| 3048 |
_, err = self.run_django_admin(["startapp", "app", bad_target]) |
| 3049 |
self.assertOutput( |
| 3050 |
err, |
| 3051 |
"CommandError: '%s' is not a valid app directory. Please " |
| 3052 |
"make sure the directory is a valid identifier." % bad_target, |
| 3053 |
) |
| 3054 |
|
| 3055 |
def test_importable_target_name(self): |
| 3056 |
_, err = self.run_django_admin(["startapp", "app", "os"]) |
| 3057 |
self.assertOutput( |
| 3058 |
err, |
| 3059 |
"CommandError: 'os' conflicts with the name of an existing Python " |
| 3060 |
"module and cannot be used as an app directory. Please try " |
| 3061 |
"another directory.", |
| 3062 |
) |
| 3063 |
|
| 3064 |
def test_trailing_slash_in_target_app_directory_name(self): |
| 3065 |
app_dir = os.path.join(self.test_dir, "apps", "app1") |
| 3066 |
os.makedirs(app_dir) |
| 3067 |
_, err = self.run_django_admin( |
| 3068 |
["startapp", "app", os.path.join("apps", "app1", "")] |
| 3069 |
) |
| 3070 |
self.assertNoOutput(err) |
| 3071 |
self.assertIs(os.path.exists(os.path.join(app_dir, "apps.py")), True) |
| 3072 |
|
| 3073 |
def test_overlaying_app(self): |
| 3074 |
# Use a subdirectory so it is outside the PYTHONPATH. |
| 3075 |
os.makedirs(os.path.join(self.test_dir, "apps/app1")) |
| 3076 |
self.run_django_admin(["startapp", "app1", "apps/app1"]) |
| 3077 |
out, err = self.run_django_admin(["startapp", "app2", "apps/app1"]) |
| 3078 |
self.assertOutput( |
| 3079 |
err, |
| 3080 |
"already exists. Overlaying an app into an existing directory " |
| 3081 |
"won't replace conflicting files.", |
| 3082 |
) |
| 3083 |
|
| 3084 |
def test_template(self): |
| 3085 |
out, err = self.run_django_admin(["startapp", "new_app"]) |
| 3086 |
self.assertNoOutput(err) |
| 3087 |
app_path = os.path.join(self.test_dir, "new_app") |
| 3088 |
self.assertIs(os.path.exists(app_path), True) |
| 3089 |
with open(os.path.join(app_path, "apps.py")) as f: |
| 3090 |
content = f.read() |
| 3091 |
self.assertIn("class NewAppConfig(AppConfig)", content) |
| 3092 |
if HAS_BLACK: |
| 3093 |
test_str = 'default_auto_field = "django.db.models.BigAutoField"' |
| 3094 |
else: |
| 3095 |
test_str = "default_auto_field = 'django.db.models.BigAutoField'" |
| 3096 |
self.assertIn(test_str, content) |
| 3097 |
self.assertIn( |
| 3098 |
'name = "new_app"' if HAS_BLACK else "name = 'new_app'", |
| 3099 |
content, |
| 3100 |
) |
| 3101 |
|
| 3102 |
|
| 3103 |
class DiffSettings(AdminScriptTestCase): |
| 3104 |
"""Tests for diffsettings management command.""" |
| 3105 |
|
| 3106 |
def test_basic(self): |
| 3107 |
"""Runs without error and emits settings diff.""" |
| 3108 |
self.write_settings("settings_to_diff.py", sdict={"FOO": '"bar"'}) |
| 3109 |
args = ["diffsettings", "--settings=settings_to_diff"] |
| 3110 |
out, err = self.run_manage(args) |
| 3111 |
self.assertNoOutput(err) |
| 3112 |
self.assertOutput(out, "FOO = 'bar' ###") |
| 3113 |
# Attributes from django.conf.Settings don't appear. |
| 3114 |
self.assertNotInOutput(out, "is_overridden = ") |
| 3115 |
|
| 3116 |
def test_settings_configured(self): |
| 3117 |
out, err = self.run_manage( |
| 3118 |
["diffsettings"], manage_py="configured_settings_manage.py" |
| 3119 |
) |
| 3120 |
self.assertNoOutput(err) |
| 3121 |
self.assertOutput(out, "CUSTOM = 1 ###\nDEBUG = True") |
| 3122 |
# Attributes from django.conf.UserSettingsHolder don't appear. |
| 3123 |
self.assertNotInOutput(out, "default_settings = ") |
| 3124 |
|
| 3125 |
def test_dynamic_settings_configured(self): |
| 3126 |
# Custom default settings appear. |
| 3127 |
out, err = self.run_manage( |
| 3128 |
["diffsettings"], manage_py="configured_dynamic_settings_manage.py" |
| 3129 |
) |
| 3130 |
self.assertNoOutput(err) |
| 3131 |
self.assertOutput(out, "FOO = 'bar' ###") |
| 3132 |
|
| 3133 |
def test_all(self): |
| 3134 |
"""The all option also shows settings with the default value.""" |
| 3135 |
self.write_settings("settings_to_diff.py", sdict={"STATIC_URL": "None"}) |
| 3136 |
args = ["diffsettings", "--settings=settings_to_diff", "--all"] |
| 3137 |
out, err = self.run_manage(args) |
| 3138 |
self.assertNoOutput(err) |
| 3139 |
self.assertOutput(out, "### STATIC_URL = None") |
| 3140 |
|
| 3141 |
def test_custom_default(self): |
| 3142 |
""" |
| 3143 |
The --default option specifies an alternate settings module for |
| 3144 |
comparison. |
| 3145 |
""" |
| 3146 |
self.write_settings( |
| 3147 |
"settings_default.py", sdict={"FOO": '"foo"', "BAR": '"bar1"'} |
| 3148 |
) |
| 3149 |
self.write_settings( |
| 3150 |
"settings_to_diff.py", sdict={"FOO": '"foo"', "BAR": '"bar2"'} |
| 3151 |
) |
| 3152 |
out, err = self.run_manage( |
| 3153 |
[ |
| 3154 |
"diffsettings", |
| 3155 |
"--settings=settings_to_diff", |
| 3156 |
"--default=settings_default", |
| 3157 |
] |
| 3158 |
) |
| 3159 |
self.assertNoOutput(err) |
| 3160 |
self.assertNotInOutput(out, "FOO") |
| 3161 |
self.assertOutput(out, "BAR = 'bar2'") |
| 3162 |
|
| 3163 |
def test_unified(self): |
| 3164 |
"""--output=unified emits settings diff in unified mode.""" |
| 3165 |
self.write_settings("settings_to_diff.py", sdict={"FOO": '"bar"'}) |
| 3166 |
args = ["diffsettings", "--settings=settings_to_diff", "--output=unified"] |
| 3167 |
out, err = self.run_manage(args) |
| 3168 |
self.assertNoOutput(err) |
| 3169 |
self.assertOutput(out, "+ FOO = 'bar'") |
| 3170 |
self.assertOutput(out, "- SECRET_KEY = ''") |
| 3171 |
self.assertOutput(out, "+ SECRET_KEY = 'django_tests_secret_key'") |
| 3172 |
self.assertNotInOutput(out, " APPEND_SLASH = True") |
| 3173 |
|
| 3174 |
def test_unified_all(self): |
| 3175 |
""" |
| 3176 |
--output=unified --all emits settings diff in unified mode and includes |
| 3177 |
settings with the default value. |
| 3178 |
""" |
| 3179 |
self.write_settings("settings_to_diff.py", sdict={"FOO": '"bar"'}) |
| 3180 |
args = [ |
| 3181 |
"diffsettings", |
| 3182 |
"--settings=settings_to_diff", |
| 3183 |
"--output=unified", |
| 3184 |
"--all", |
| 3185 |
] |
| 3186 |
out, err = self.run_manage(args) |
| 3187 |
self.assertNoOutput(err) |
| 3188 |
self.assertOutput(out, " APPEND_SLASH = True") |
| 3189 |
self.assertOutput(out, "+ FOO = 'bar'") |
| 3190 |
self.assertOutput(out, "- SECRET_KEY = ''") |
| 3191 |
|
| 3192 |
|
| 3193 |
class Dumpdata(AdminScriptTestCase): |
| 3194 |
"""Tests for dumpdata management command.""" |
| 3195 |
|
| 3196 |
def setUp(self): |
| 3197 |
super().setUp() |
| 3198 |
self.write_settings("settings.py") |
| 3199 |
|
| 3200 |
def test_pks_parsing(self): |
| 3201 |
"""Regression for #20509 |
| 3202 |
|
| 3203 |
Test would raise an exception rather than printing an error message. |
| 3204 |
""" |
| 3205 |
args = ["dumpdata", "--pks=1"] |
| 3206 |
out, err = self.run_manage(args) |
| 3207 |
self.assertOutput(err, "You can only use --pks option with one model") |
| 3208 |
self.assertNoOutput(out) |
| 3209 |
|
| 3210 |
|
| 3211 |
class MainModule(AdminScriptTestCase): |
| 3212 |
"""python -m django works like django-admin.""" |
| 3213 |
|
| 3214 |
def test_program_name_in_help(self): |
| 3215 |
out, err = self.run_test(["-m", "django", "help"]) |
| 3216 |
self.assertOutput( |
| 3217 |
out, |
| 3218 |
"Type 'python -m django help <subcommand>' for help on a specific " |
| 3219 |
"subcommand.", |
| 3220 |
) |
| 3221 |
|
| 3222 |
|
| 3223 |
class DjangoAdminSuggestions(AdminScriptTestCase): |
| 3224 |
def setUp(self): |
| 3225 |
super().setUp() |
| 3226 |
self.write_settings("settings.py") |
| 3227 |
|
| 3228 |
def test_suggestions(self): |
| 3229 |
args = ["rnserver", "--settings=test_project.settings"] |
| 3230 |
out, err = self.run_django_admin(args) |
| 3231 |
self.assertNoOutput(out) |
| 3232 |
self.assertOutput(err, "Unknown command: 'rnserver'. Did you mean runserver?") |
| 3233 |
|
| 3234 |
def test_no_suggestions(self): |
| 3235 |
args = ["abcdef", "--settings=test_project.settings"] |
| 3236 |
out, err = self.run_django_admin(args) |
| 3237 |
self.assertNoOutput(out) |
| 3238 |
self.assertNotInOutput(err, "Did you mean") |