Commit e0a98e23 authored by Raffaele Salmaso's avatar Raffaele Salmaso Committed by Claude Paroz
Browse files

Fixed #23309 -- Fixed call_command to parse args correctly

parent 7ed3d0bb
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -108,6 +108,8 @@ def call_command(name, *args, **options):
        arg_options = dict((opt_mapping.get(key, key), value) for key, value in options.items())
        defaults = parser.parse_args(args=args)
        defaults = dict(defaults._get_kwargs(), **arg_options)
        # Move positional args out of options to mimic legacy optparse
        args = defaults.pop('args', ())
    else:
        # Legacy optparse method
        defaults, _ = parser.parse_args(args=[])
+1 −5
Original line number Diff line number Diff line
@@ -350,11 +350,7 @@ class BaseCommand(object):
            options = parser.parse_args(argv[2:])
            cmd_options = vars(options)
            # Move positional args out of options to mimic legacy optparse
            if 'args' in options:
                args = options.args
                del cmd_options['args']
            else:
                args = ()
            args = cmd_options.pop('args', ())
        else:
            options, args = parser.parse_args(argv[2:])
            cmd_options = vars(options)
+28 −0
Original line number Diff line number Diff line
from django.core.management.base import BaseCommand, CommandError


class Command(BaseCommand):
    help = "Useless command."

    def add_arguments(self, parser):
        parser.add_argument('args', metavar='app_label', nargs='*',
            help='Specify the app label(s) to works on.')
        parser.add_argument('--empty', action='store_true', dest='empty', default=False,
            help="Do nothing.")

    def handle(self, *app_labels, **options):
        app_labels = set(app_labels)

        if options['empty']:
            self.stdout.write("Dave, I can't do that.")
            return

        if not app_labels:
            raise CommandError("I'm sorry Dave, I'm afraid I can't do that.")

        # raise an error if some --parameter is flowing from options to args
        for app_label in app_labels:
            if app_label.startswith('--'):
                raise CommandError("Sorry, Dave, I can't let you do that.")

        self.stdout.write("Dave, my mind is going. I can feel it. I can feel it.")
+26 −0
Original line number Diff line number Diff line
@@ -108,6 +108,32 @@ class CommandTests(SimpleTestCase):
            sys.stdout, sys.stderr = old_stdout, old_stderr
        self.assertEqual(output, "All right, let's dance Rock'n'Roll.\n")

    def test_calling_an_help_command_should_exit_with_systemexit_exception(self):
        out = StringIO()
        with self.assertRaises(SystemExit):
            management.call_command('hal', "--help", stdout=out)
        self.assertIn("", out.getvalue())

    def test_calling_a_command_with_only_empty_parameter_should_ends_gracefully(self):
        out = StringIO()
        management.call_command('hal', "--empty", stdout=out)
        self.assertIn("Dave, I can't do that.\n", out.getvalue())

    def test_calling_command_with_app_labels_and_parameters_should_be_ok(self):
        out = StringIO()
        management.call_command('hal', 'myapp', "--verbosity", "3", stdout=out)
        self.assertIn("Dave, my mind is going. I can feel it. I can feel it.\n", out.getvalue())

    def test_calling_command_with_parameters_and_app_labels_at_the_end_should_be_ok(self):
        out = StringIO()
        management.call_command('hal', "--verbosity", "3", "myapp", stdout=out)
        self.assertIn("Dave, my mind is going. I can feel it. I can feel it.\n", out.getvalue())

    def test_calling_a_command_with_no_app_labels_and_parameters_should_raise_a_command_error(self):
        out = StringIO()
        with self.assertRaises(CommandError):
            management.call_command('hal', stdout=out)


class UtilsTests(SimpleTestCase):