Fixed #35689 -- Handled custom labels in LabelCommand.missing_args_message.

This commit is contained in:
Giovanni Fabbretti 2024-08-23 15:23:51 +02:00 committed by Sarah Boyce
parent 47b921391f
commit f72bbd4480
3 changed files with 27 additions and 1 deletions

View File

@ -672,7 +672,13 @@ class LabelCommand(BaseCommand):
""" """
label = "label" 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): def add_arguments(self, parser):
parser.add_argument("args", metavar=self.label, nargs="+") parser.add_argument("args", metavar=self.label, nargs="+")

View File

@ -25,6 +25,7 @@ from django.core.management import (
color, color,
execute_from_command_line, 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.loaddata import Command as LoaddataCommand
from django.core.management.commands.runserver import Command as RunserverCommand from django.core.management.commands.runserver import Command as RunserverCommand
from django.core.management.commands.testserver import Command as TestserverCommand from django.core.management.commands.testserver import Command as TestserverCommand
@ -2280,6 +2281,20 @@ class CommandTypes(AdminScriptTestCase):
"('settings', None), ('traceback', False), ('verbosity', 1)]", "('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): def test_suppress_base_options_command_help(self):
args = ["suppress_base_options_command", "--help"] args = ["suppress_base_options_command", "--help"]
out, err = self.run_manage(args) out, err = self.run_manage(args)

View File

@ -124,6 +124,11 @@ class TestFindStatic(TestDefaults, CollectionTestCase):
searched_locations, 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): class TestConfiguration(StaticFilesTestCase):
def test_location_empty(self): def test_location_empty(self):