From 2ce4545de1791d6ed2405cb0657401e179bc5357 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sat, 23 Nov 2024 11:41:14 -0500 Subject: Fixed #35920 -- Observed requires_system_checks in migrate and runserver. Before, the full suite of system checks was run by these commands regardless if requires_system_checks had been overridden. Co-authored-by: Simon Charette --- tests/admin_scripts/tests.py | 52 +++++++++++++++++++++++++++++++++++++-- tests/migrations/test_commands.py | 28 ++++++++++++++++++++- 2 files changed, 77 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index 3145723920..5d8a5ec97e 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -20,6 +20,8 @@ from user_commands.utils import AssertFormatterFailureCaughtContext from django import conf, get_version from django.conf import settings +from django.core.checks import Error, Tags, register +from django.core.checks.registry import registry from django.core.management import ( BaseCommand, CommandError, @@ -27,7 +29,7 @@ from django.core.management import ( color, execute_from_command_line, ) -from django.core.management.base import LabelCommand +from django.core.management.base import LabelCommand, SystemCheckError from django.core.management.commands.loaddata import Command as LoaddataCommand from django.core.management.commands.runserver import Command as RunserverCommand from django.core.management.commands.testserver import Command as TestserverCommand @@ -1733,7 +1735,53 @@ class ManageRunserver(SimpleTestCase): stdout=self.output, ) self.assertIn("Performing system checks...", self.output.getvalue()) - mocked_check.assert_called() + mocked_check.assert_has_calls( + [mock.call(tags=set()), mock.call(display_num_errors=True)] + ) + + def test_custom_system_checks(self): + original_checks = registry.registered_checks.copy() + + @register(Tags.signals) + def my_check(app_configs, **kwargs): + return [Error("my error")] + + class CustomException(Exception): + pass + + self.addCleanup(setattr, registry, "registered_checks", original_checks) + + class CustomRunserverCommand(RunserverCommand): + """Rather than mock run(), raise immediately after system checks run.""" + + def check_migrations(self, *args, **kwargs): + raise CustomException + + class CustomRunserverCommandWithSignalsChecks(CustomRunserverCommand): + requires_system_checks = [Tags.signals] + + command = CustomRunserverCommandWithSignalsChecks() + with self.assertRaises(SystemCheckError): + call_command( + command, + use_reloader=False, + skip_checks=False, + stdout=StringIO(), + stderr=StringIO(), + ) + + class CustomMigrateCommandWithSecurityChecks(CustomRunserverCommand): + requires_system_checks = [Tags.security] + + command = CustomMigrateCommandWithSecurityChecks() + with self.assertRaises(CustomException): + call_command( + command, + use_reloader=False, + skip_checks=False, + stdout=StringIO(), + stderr=StringIO(), + ) class ManageRunserverMigrationWarning(TestCase): diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index 18f7e1e157..5ff5cd4b26 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -8,6 +8,8 @@ from pathlib import Path from unittest import mock from django.apps import apps +from django.core.checks import Error, Tags, register +from django.core.checks.registry import registry from django.core.management import CommandError, call_command from django.core.management.base import SystemCheckError from django.core.management.commands.makemigrations import ( @@ -96,6 +98,7 @@ class MigrateTests(MigrationTestBase): self.assertTableNotExists("migrations_tribble") self.assertTableNotExists("migrations_book") + @mock.patch("django.core.management.base.BaseCommand.check") @override_settings( INSTALLED_APPS=[ "django.contrib.auth", @@ -103,10 +106,33 @@ class MigrateTests(MigrationTestBase): "migrations.migrations_test_apps.migrated_app", ] ) - def test_migrate_with_system_checks(self): + def test_migrate_with_system_checks(self, mocked_check): out = io.StringIO() call_command("migrate", skip_checks=False, no_color=True, stdout=out) self.assertIn("Apply all migrations: migrated_app", out.getvalue()) + mocked_check.assert_called_once() + + def test_migrate_with_custom_system_checks(self): + original_checks = registry.registered_checks.copy() + + @register(Tags.signals) + def my_check(app_configs, **kwargs): + return [Error("my error")] + + self.addCleanup(setattr, registry, "registered_checks", original_checks) + + class CustomMigrateCommandWithSignalsChecks(MigrateCommand): + requires_system_checks = [Tags.signals] + + command = CustomMigrateCommandWithSignalsChecks() + with self.assertRaises(SystemCheckError): + call_command(command, skip_checks=False, stderr=io.StringIO()) + + class CustomMigrateCommandWithSecurityChecks(MigrateCommand): + requires_system_checks = [Tags.security] + + command = CustomMigrateCommandWithSecurityChecks() + call_command(command, skip_checks=False, stdout=io.StringIO()) @override_settings( INSTALLED_APPS=[ -- cgit v1.3