summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2012-05-26 20:50:44 +0200
committerClaude Paroz <claude@2xlibre.net>2012-05-27 20:38:47 +0200
commitf2b6763ad7cb281ca8699a9c3d532a82f965be4f (patch)
tree7fbbb6d1bd00aa9b29b5b8f42e49c3e50cbdc5a0 /tests
parent4423757c0c50afbe2470434778c8d5e5b4a70925 (diff)
Fixed #18387 -- Do not call sys.exit during call_command.
Moved sys.exit(1) so as failing management commands reach it only when running from command line.
Diffstat (limited to 'tests')
-rw-r--r--tests/modeltests/fixtures/tests.py4
-rw-r--r--tests/modeltests/user_commands/management/commands/dance.py8
-rw-r--r--tests/modeltests/user_commands/tests.py17
3 files changed, 24 insertions, 5 deletions
diff --git a/tests/modeltests/fixtures/tests.py b/tests/modeltests/fixtures/tests.py
index 48a5fe7f16..478bbe9129 100644
--- a/tests/modeltests/fixtures/tests.py
+++ b/tests/modeltests/fixtures/tests.py
@@ -185,14 +185,14 @@ class FixtureLoadingTests(TestCase):
exclude_list=['fixtures.Article', 'fixtures.Book', 'sites'])
# Excluding a bogus app should throw an error
- self.assertRaises(SystemExit,
+ self.assertRaises(management.CommandError,
self._dumpdata_assert,
['fixtures', 'sites'],
'',
exclude_list=['foo_app'])
# Excluding a bogus model should throw an error
- self.assertRaises(SystemExit,
+ self.assertRaises(management.CommandError,
self._dumpdata_assert,
['fixtures', 'sites'],
'',
diff --git a/tests/modeltests/user_commands/management/commands/dance.py b/tests/modeltests/user_commands/management/commands/dance.py
index 4ad5579e1e..911530d223 100644
--- a/tests/modeltests/user_commands/management/commands/dance.py
+++ b/tests/modeltests/user_commands/management/commands/dance.py
@@ -1,6 +1,6 @@
from optparse import make_option
-from django.core.management.base import BaseCommand
+from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
@@ -8,11 +8,13 @@ class Command(BaseCommand):
args = ''
requires_model_validation = True
- option_list =[
+ option_list = BaseCommand.option_list + (
make_option("-s", "--style", default="Rock'n'Roll"),
make_option("-x", "--example")
- ]
+ )
def handle(self, *args, **options):
example = options["example"]
+ if example == "raise":
+ raise CommandError()
self.stdout.write("I don't feel like dancing %s." % options["style"])
diff --git a/tests/modeltests/user_commands/tests.py b/tests/modeltests/user_commands/tests.py
index c1e2bf9a74..509f13f62f 100644
--- a/tests/modeltests/user_commands/tests.py
+++ b/tests/modeltests/user_commands/tests.py
@@ -1,3 +1,4 @@
+import sys
from StringIO import StringIO
from django.core import management
@@ -26,4 +27,20 @@ class CommandTests(TestCase):
self.assertEqual(translation.get_language(), 'fr')
def test_explode(self):
+ """ Test that an unknown command raises CommandError """
self.assertRaises(CommandError, management.call_command, ('explode',))
+
+ def test_system_exit(self):
+ """ Exception raised in a command should raise CommandError with
+ call_command, but SystemExit when run from command line
+ """
+ with self.assertRaises(CommandError):
+ management.call_command('dance', example="raise")
+ old_stderr = sys.stderr
+ sys.stderr = err = StringIO()
+ try:
+ with self.assertRaises(SystemExit):
+ management.ManagementUtility(['manage.py', 'dance', '--example=raise']).execute()
+ finally:
+ sys.stderr = old_stderr
+ self.assertIn("CommandError", err.getvalue())