summaryrefslogtreecommitdiff
path: root/tests/user_commands
diff options
context:
space:
mode:
authorRaffaele Salmaso <raffaele@salmaso.org>2014-08-18 00:29:49 +0200
committerClaude Paroz <claude@2xlibre.net>2014-08-18 17:54:24 +0200
commite0a98e2374b1d7660ef3841a5bb2626f1c69970a (patch)
tree2e2550c9961cf60bde41a05f28bb7f752f4adb0c /tests/user_commands
parent7ed3d0bb61f46f56b8a001b7b2d983eaf9a65466 (diff)
downloaddjango-e0a98e2374b1d7660ef3841a5bb2626f1c69970a.tar.gz
Fixed #23309 -- Fixed call_command to parse args correctly
Diffstat (limited to 'tests/user_commands')
-rw-r--r--tests/user_commands/management/commands/hal.py28
-rw-r--r--tests/user_commands/tests.py26
2 files changed, 54 insertions, 0 deletions
diff --git a/tests/user_commands/management/commands/hal.py b/tests/user_commands/management/commands/hal.py
new file mode 100644
index 0000000000..c7ea51c9e5
--- /dev/null
+++ b/tests/user_commands/management/commands/hal.py
@@ -0,0 +1,28 @@
+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.")
diff --git a/tests/user_commands/tests.py b/tests/user_commands/tests.py
index 398e01eb6b..19383e6af4 100644
--- a/tests/user_commands/tests.py
+++ b/tests/user_commands/tests.py
@@ -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):