diff options
| author | Hasan Ramezani <hasan.r67@gmail.com> | 2021-10-22 16:38:14 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-10-25 10:09:06 +0200 |
| commit | c9ebe4ca4e3f5d5d76bfbdae489e3f44e32416e5 (patch) | |
| tree | 108af2ab73ab082133dd937c937198e038fc5f3e /tests/user_commands | |
| parent | ac815f6ea83a66122ee434f773f8c923f4cd3125 (diff) | |
[4.0.x] Fixed #33205 -- Made call_command() raise TypeError when dest with multiple arguments is passed.
Backport of c1e4111c74ee9d9f48cbee5a5b7c40289203c93d from main
Diffstat (limited to 'tests/user_commands')
| -rw-r--r-- | tests/user_commands/management/commands/mutually_exclusive_required_with_same_dest.py | 13 | ||||
| -rw-r--r-- | tests/user_commands/tests.py | 35 |
2 files changed, 48 insertions, 0 deletions
diff --git a/tests/user_commands/management/commands/mutually_exclusive_required_with_same_dest.py b/tests/user_commands/management/commands/mutually_exclusive_required_with_same_dest.py new file mode 100644 index 0000000000..1a9ab5576d --- /dev/null +++ b/tests/user_commands/management/commands/mutually_exclusive_required_with_same_dest.py @@ -0,0 +1,13 @@ +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + def add_arguments(self, parser): + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument('--for', dest='until', action='store') + group.add_argument('--until', action='store') + + def handle(self, *args, **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 60cfe86b3d..a727e56efb 100644 --- a/tests/user_commands/tests.py +++ b/tests/user_commands/tests.py @@ -275,6 +275,41 @@ class CommandTests(SimpleTestCase): ) self.assertIn(expected_output, out.getvalue()) + def test_mutually_exclusive_group_required_with_same_dest_options(self): + tests = [ + {'until': '2'}, + {'for': '1', 'until': '2'}, + ] + msg = ( + "Cannot pass the dest 'until' that matches multiple arguments via " + "**options." + ) + for options in tests: + with self.subTest(options=options): + with self.assertRaisesMessage(TypeError, msg): + management.call_command( + 'mutually_exclusive_required_with_same_dest', + **options, + ) + + def test_mutually_exclusive_group_required_with_same_dest_args(self): + tests = [ + ('--until=1',), + ('--until', 1), + ('--for=1',), + ('--for', 1), + ] + for args in tests: + out = StringIO() + with self.subTest(options=args): + management.call_command( + 'mutually_exclusive_required_with_same_dest', + *args, + stdout=out, + ) + output = out.getvalue() + self.assertIn('until=1', output) + def test_required_list_option(self): tests = [ (('--foo-list', [1, 2]), {}), |
