diff options
author | Hasan Ramezani <hasan.r67@gmail.com> | 2020-10-29 19:30:43 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-10-30 12:01:33 +0100 |
commit | f06beea92999407cc8dad3c47f006b7c727095a6 (patch) | |
tree | 94ee9bc71037cd30cf3f49207f84901ffc8203af /tests/user_commands | |
parent | 966b5b49b6521483f1c90b4499c4c80e80136de3 (diff) | |
download | django-f06beea92999407cc8dad3c47f006b7c727095a6.tar.gz |
Fixed #32153 -- Fixed management commands when using required list options.
Thanks Mark Gajdosik for the report and initial patch.
Diffstat (limited to 'tests/user_commands')
-rw-r--r-- | tests/user_commands/management/commands/mutually_exclusive_required.py | 1 | ||||
-rw-r--r-- | tests/user_commands/management/commands/required_list_option.py | 10 | ||||
-rw-r--r-- | tests/user_commands/tests.py | 21 |
3 files changed, 30 insertions, 2 deletions
diff --git a/tests/user_commands/management/commands/mutually_exclusive_required.py b/tests/user_commands/management/commands/mutually_exclusive_required.py index 3fbf514c4d..8e48f12658 100644 --- a/tests/user_commands/management/commands/mutually_exclusive_required.py +++ b/tests/user_commands/management/commands/mutually_exclusive_required.py @@ -7,6 +7,7 @@ class Command(BaseCommand): group = parser.add_mutually_exclusive_group(required=True) group.add_argument('--foo-id', type=int, nargs='?', default=None) group.add_argument('--foo-name', type=str, nargs='?', default=None) + group.add_argument('--foo-list', type=int, nargs='+') group.add_argument('--append_const', action='append_const', const=42) group.add_argument('--const', action='store_const', const=31) group.add_argument('--count', action='count') diff --git a/tests/user_commands/management/commands/required_list_option.py b/tests/user_commands/management/commands/required_list_option.py new file mode 100644 index 0000000000..84f39f0142 --- /dev/null +++ b/tests/user_commands/management/commands/required_list_option.py @@ -0,0 +1,10 @@ +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument('--foo-list', nargs='+', type=int, required=True) + + def handle(self, *args, **options): + for option, value in options.items(): + self.stdout.write('%s=%s' % (option, value)) diff --git a/tests/user_commands/tests.py b/tests/user_commands/tests.py index eb2d38fbfb..9262e2717a 100644 --- a/tests/user_commands/tests.py +++ b/tests/user_commands/tests.py @@ -244,8 +244,9 @@ class CommandTests(SimpleTestCase): management.call_command('mutually_exclusive_required', foo_name='foo', stdout=out) self.assertIn('foo_name', out.getvalue()) msg = ( - 'Error: one of the arguments --foo-id --foo-name --append_const ' - '--const --count --flag_false --flag_true is required' + 'Error: one of the arguments --foo-id --foo-name --foo-list ' + '--append_const --const --count --flag_false --flag_true is ' + 'required' ) with self.assertRaisesMessage(CommandError, msg): management.call_command('mutually_exclusive_required', stdout=out) @@ -275,6 +276,22 @@ class CommandTests(SimpleTestCase): ) self.assertIn(expected_output, out.getvalue()) + def test_required_list_option(self): + tests = [ + (('--foo-list', [1, 2]), {}), + ((), {'foo_list': [1, 2]}), + ] + for command in ['mutually_exclusive_required', 'required_list_option']: + for args, kwargs in tests: + with self.subTest(command=command, args=args, kwargs=kwargs): + out = StringIO() + management.call_command( + command, + *args, + **{**kwargs, 'stdout': out}, + ) + self.assertIn('foo_list=[1, 2]', out.getvalue()) + def test_required_const_options(self): args = { 'append_const': [42], |