diff options
| author | Hasan Ramezani <hasan.r67@gmail.com> | 2020-09-29 15:51:42 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-09-30 20:10:38 +0200 |
| commit | 6eb3f53bdd5089e3ab229780fe339fecfc30a7ee (patch) | |
| tree | 3e8eb9ba5b5f25f2dddde27dcfd85c50bf28274f /tests/user_commands | |
| parent | 11c4a4412b74bb1dfe52d706a58f230066821c33 (diff) | |
Fixed #32047 -- Fixed call_command() crash if a constant option from required mutually exclusive group is passed in options.
Diffstat (limited to 'tests/user_commands')
| -rw-r--r-- | tests/user_commands/management/commands/mutually_exclusive_required.py | 9 | ||||
| -rw-r--r-- | tests/user_commands/tests.py | 30 |
2 files changed, 37 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 e5df17edb0..3fbf514c4d 100644 --- a/tests/user_commands/management/commands/mutually_exclusive_required.py +++ b/tests/user_commands/management/commands/mutually_exclusive_required.py @@ -7,6 +7,13 @@ 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('--append_const', action='append_const', const=42) + group.add_argument('--const', action='store_const', const=31) + group.add_argument('--count', action='count') + group.add_argument('--flag_false', action='store_false') + group.add_argument('--flag_true', action='store_true') def handle(self, *args, **options): - self.stdout.write(','.join(options)) + for option, value in options.items(): + if value is not None: + self.stdout.write('%s=%s' % (option, value)) diff --git a/tests/user_commands/tests.py b/tests/user_commands/tests.py index f0627e8ae7..fe61a23ccd 100644 --- a/tests/user_commands/tests.py +++ b/tests/user_commands/tests.py @@ -243,10 +243,38 @@ class CommandTests(SimpleTestCase): self.assertIn('foo_id', out.getvalue()) 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 is required' + msg = ( + 'Error: one of the arguments --foo-id --foo-name --append_const ' + '--const --count --flag_false --flag_true is required' + ) with self.assertRaisesMessage(CommandError, msg): management.call_command('mutually_exclusive_required', stdout=out) + def test_mutually_exclusive_group_required_const_options(self): + tests = [ + ('append_const', [42]), + ('const', 31), + ('count', 1), + ('flag_false', False), + ('flag_true', True), + ] + for arg, value in tests: + out = StringIO() + expected_output = '%s=%s' % (arg, value) + with self.subTest(arg=arg): + management.call_command( + 'mutually_exclusive_required', + '--%s' % arg, + stdout=out, + ) + self.assertIn(expected_output, out.getvalue()) + out.truncate(0) + management.call_command( + 'mutually_exclusive_required', + **{arg: value, 'stdout': out}, + ) + self.assertIn(expected_output, out.getvalue()) + def test_subparser(self): out = StringIO() management.call_command('subparser', 'foo', 12, stdout=out) |
