From f72bbd44808452f4a70be5f7b9d35e46dee32e2d Mon Sep 17 00:00:00 2001 From: Giovanni Fabbretti Date: Fri, 23 Aug 2024 15:23:51 +0200 Subject: [PATCH] Fixed #35689 -- Handled custom labels in LabelCommand.missing_args_message. --- django/core/management/base.py | 8 +++++++- tests/admin_scripts/tests.py | 15 +++++++++++++++ tests/staticfiles_tests/test_management.py | 5 +++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/django/core/management/base.py b/django/core/management/base.py index 41b17a24c8..6232b42bd4 100644 --- a/django/core/management/base.py +++ b/django/core/management/base.py @@ -672,7 +672,13 @@ class LabelCommand(BaseCommand): """ label = "label" - missing_args_message = "Enter at least one %s." % label + missing_args_message = "Enter at least one %s." + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + if self.missing_args_message == LabelCommand.missing_args_message: + self.missing_args_message = self.missing_args_message % self.label def add_arguments(self, parser): parser.add_argument("args", metavar=self.label, nargs="+") diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index 5ee3eeb803..29023b74c3 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -25,6 +25,7 @@ from django.core.management import ( color, execute_from_command_line, ) +from django.core.management.base import LabelCommand 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 @@ -2280,6 +2281,20 @@ class CommandTypes(AdminScriptTestCase): "('settings', None), ('traceback', False), ('verbosity', 1)]", ) + def test_custom_label_command_custom_missing_args_message(self): + class Command(LabelCommand): + missing_args_message = "Missing argument." + + with self.assertRaisesMessage(CommandError, "Error: Missing argument."): + call_command(Command()) + + def test_custom_label_command_none_missing_args_message(self): + class Command(LabelCommand): + missing_args_message = None + + with self.assertRaisesMessage(CommandError, ""): + call_command(Command()) + def test_suppress_base_options_command_help(self): args = ["suppress_base_options_command", "--help"] out, err = self.run_manage(args) diff --git a/tests/staticfiles_tests/test_management.py b/tests/staticfiles_tests/test_management.py index c0d3817383..1b9179af49 100644 --- a/tests/staticfiles_tests/test_management.py +++ b/tests/staticfiles_tests/test_management.py @@ -124,6 +124,11 @@ class TestFindStatic(TestDefaults, CollectionTestCase): searched_locations, ) + def test_missing_args_message(self): + msg = "Enter at least one staticfile." + with self.assertRaisesMessage(CommandError, msg): + call_command("findstatic") + class TestConfiguration(StaticFilesTestCase): def test_location_empty(self):