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 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) (limited to 'tests/admin_scripts') 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): -- cgit v1.3