diff options
author | Hasan Ramezani <hasan.r67@gmail.com> | 2020-05-14 00:00:41 +0200 |
---|---|---|
committer | Carlton Gibson <carlton.gibson@noumenal.es> | 2020-05-21 12:34:54 +0200 |
commit | c60524c658f197f645b638f9bcc553103bfe2630 (patch) | |
tree | 46c3f2c654626caddbd2764b195adf2fcb4a21fa /tests/user_commands | |
parent | a4e6030904df63b3f10aa0729b86dc6942b0458e (diff) | |
download | django-c60524c658f197f645b638f9bcc553103bfe2630.tar.gz |
Fixed #31546 -- Allowed specifying list of tags in Command.requires_system_checks.
Diffstat (limited to 'tests/user_commands')
4 files changed, 85 insertions, 4 deletions
diff --git a/tests/user_commands/management/commands/dance.py b/tests/user_commands/management/commands/dance.py index 82cfc3338a..efa1bc0d8a 100644 --- a/tests/user_commands/management/commands/dance.py +++ b/tests/user_commands/management/commands/dance.py @@ -4,7 +4,7 @@ from django.core.management.base import BaseCommand, CommandError class Command(BaseCommand): help = "Dance around like a madman." args = '' - requires_system_checks = True + requires_system_checks = '__all__' def add_arguments(self, parser): parser.add_argument("integer", nargs='?', type=int, default=0) diff --git a/tests/user_commands/management/commands/no_system_checks.py b/tests/user_commands/management/commands/no_system_checks.py new file mode 100644 index 0000000000..40c6051c53 --- /dev/null +++ b/tests/user_commands/management/commands/no_system_checks.py @@ -0,0 +1,8 @@ +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + requires_system_checks = [] + + def handle(self, *args, **options): + pass diff --git a/tests/user_commands/management/commands/specific_system_checks.py b/tests/user_commands/management/commands/specific_system_checks.py new file mode 100644 index 0000000000..5551b2ab36 --- /dev/null +++ b/tests/user_commands/management/commands/specific_system_checks.py @@ -0,0 +1,9 @@ +from django.core.checks import Tags +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + requires_system_checks = [Tags.staticfiles, Tags.models] + + def handle(self, *args, **options): + pass diff --git a/tests/user_commands/tests.py b/tests/user_commands/tests.py index 0a3b6ae77e..f0627e8ae7 100644 --- a/tests/user_commands/tests.py +++ b/tests/user_commands/tests.py @@ -6,6 +6,7 @@ from admin_scripts.tests import AdminScriptTestCase from django.apps import apps from django.core import management +from django.core.checks import Tags from django.core.management import BaseCommand, CommandError, find_commands from django.core.management.utils import ( find_command, get_random_secret_key, is_ignored_path, @@ -13,8 +14,9 @@ from django.core.management.utils import ( ) from django.db import connection from django.test import SimpleTestCase, override_settings -from django.test.utils import captured_stderr, extend_sys_path +from django.test.utils import captured_stderr, extend_sys_path, ignore_warnings from django.utils import translation +from django.utils.deprecation import RemovedInDjango41Warning from django.utils.version import PY37 from .management.commands import dance @@ -59,13 +61,13 @@ class CommandTests(SimpleTestCase): with self.assertRaises(CommandError) as cm: management.call_command('dance', example="raise") self.assertEqual(cm.exception.returncode, 3) - dance.Command.requires_system_checks = False + dance.Command.requires_system_checks = [] try: with captured_stderr() as stderr, self.assertRaises(SystemExit) as cm: management.ManagementUtility(['manage.py', 'dance', '--example=raise']).execute() self.assertEqual(cm.exception.code, 3) finally: - dance.Command.requires_system_checks = True + dance.Command.requires_system_checks = '__all__' self.assertIn("CommandError", stderr.getvalue()) def test_no_translations_deactivate_translations(self): @@ -155,6 +157,7 @@ class CommandTests(SimpleTestCase): def patched_check(self_, **kwargs): self.counter += 1 + self.kwargs = kwargs saved_check = BaseCommand.check BaseCommand.check = patched_check @@ -163,9 +166,28 @@ class CommandTests(SimpleTestCase): self.assertEqual(self.counter, 0) management.call_command("dance", verbosity=0, skip_checks=False) self.assertEqual(self.counter, 1) + self.assertEqual(self.kwargs, {}) finally: BaseCommand.check = saved_check + def test_requires_system_checks_empty(self): + with mock.patch('django.core.management.base.BaseCommand.check') as mocked_check: + management.call_command('no_system_checks') + self.assertIs(mocked_check.called, False) + + def test_requires_system_checks_specific(self): + with mock.patch('django.core.management.base.BaseCommand.check') as mocked_check: + management.call_command('specific_system_checks') + mocked_check.called_once_with(tags=[Tags.staticfiles, Tags.models]) + + def test_requires_system_checks_invalid(self): + class Command(BaseCommand): + requires_system_checks = 'x' + + msg = 'requires_system_checks must be a list or tuple.' + with self.assertRaisesMessage(TypeError, msg): + Command() + def test_check_migrations(self): requires_migrations_checks = dance.Command.requires_migrations_checks self.assertIs(requires_migrations_checks, False) @@ -334,3 +356,45 @@ class UtilsTests(SimpleTestCase): def test_normalize_path_patterns_truncates_wildcard_base(self): expected = [os.path.normcase(p) for p in ['foo/bar', 'bar/*/']] self.assertEqual(normalize_path_patterns(['foo/bar/*', 'bar/*/']), expected) + + +class DeprecationTests(SimpleTestCase): + def test_requires_system_checks_warning(self): + class Command(BaseCommand): + pass + + msg = ( + "Using a boolean value for requires_system_checks is deprecated. " + "Use '__all__' instead of True, and [] (an empty list) instead of " + "False." + ) + for value in [False, True]: + Command.requires_system_checks = value + with self.assertRaisesMessage(RemovedInDjango41Warning, msg): + Command() + + @ignore_warnings(category=RemovedInDjango41Warning) + def test_requires_system_checks_true(self): + class Command(BaseCommand): + requires_system_checks = True + + def handle(self, *args, **options): + pass + + command = Command() + with mock.patch('django.core.management.base.BaseCommand.check') as mocked_check: + management.call_command(command, skip_checks=False) + mocked_check.assert_called_once_with() + + @ignore_warnings(category=RemovedInDjango41Warning) + def test_requires_system_checks_false(self): + class Command(BaseCommand): + requires_system_checks = False + + def handle(self, *args, **options): + pass + + command = Command() + with mock.patch('django.core.management.base.BaseCommand.check') as mocked_check: + management.call_command(command) + self.assertIs(mocked_check.called, False) |