Commit b46c0ea6 authored by Claude Paroz's avatar Claude Paroz
Browse files

Fixed #26190 -- Returned handle() result from call_command

Thanks Tim Graham for the review.
parent 47b5a6a4
Loading
Loading
Loading
Loading
+6 −7
Original line number Diff line number Diff line
@@ -348,18 +348,17 @@ class BaseCommand(object):
            output = self.handle(*args, **options)
            if output:
                if self.output_transaction:
                    # This needs to be imported here, because it relies on
                    # settings.
                    from django.db import connections, DEFAULT_DB_ALIAS
                    connection = connections[options.get('database', DEFAULT_DB_ALIAS)]
                    if connection.ops.start_transaction_sql():
                        self.stdout.write(self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()))
                    output = '%s\n%s\n%s' % (
                        self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()),
                        output,
                        self.style.SQL_KEYWORD(connection.ops.end_transaction_sql()),
                    )
                self.stdout.write(output)
                if self.output_transaction:
                    self.stdout.write('\n' + self.style.SQL_KEYWORD(connection.ops.end_transaction_sql()))
        finally:
            if saved_locale is not None:
                translation.activate(saved_locale)
        return output

    def check(self, app_configs=None, tags=None, display_num_errors=False,
              include_deployment_checks=False, fail_level=checks.ERROR):
+8 −0
Original line number Diff line number Diff line
@@ -1793,6 +1793,14 @@ Command options which take multiple options are passed a list::

      management.call_command('dumpdata', exclude=['contenttypes', 'auth'])

The return value of the ``call_command()`` function is the same as the return
value of the ``handle()`` method of the command.

.. versionchanged:: 1.10

    ``call_command()`` now returns the value received from the
    ``command.handle()`` method.

Output redirection
==================

+3 −0
Original line number Diff line number Diff line
@@ -230,6 +230,9 @@ Internationalization
Management Commands
~~~~~~~~~~~~~~~~~~~

* :func:`~django.core.management.call_command` now returns the value returned
  from the ``command.handle()`` method.

* The new :option:`check --fail-level` option allows specifying the message
  level that will cause the command to exit with a non-zero status.

+8 −13
Original line number Diff line number Diff line
@@ -61,17 +61,15 @@ class CommandTests(SimpleTestCase):

    def test_deactivate_locale_set(self):
        # Deactivate translation when set to true
        out = StringIO()
        with translation.override('pl'):
            management.call_command('leave_locale_alone_false', stdout=out)
            self.assertEqual(out.getvalue(), "")
            result = management.call_command('leave_locale_alone_false', stdout=StringIO())
            self.assertIsNone(result)

    def test_configured_locale_preserved(self):
        # Leaves locale from settings when set to false
        out = StringIO()
        with translation.override('pl'):
            management.call_command('leave_locale_alone_true', stdout=out)
            self.assertEqual(out.getvalue(), "pl\n")
            result = management.call_command('leave_locale_alone_true', stdout=StringIO())
            self.assertEqual(result, "pl")

    def test_find_command_without_PATH(self):
        """
@@ -132,16 +130,13 @@ class CommandTests(SimpleTestCase):
        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)
            management.call_command('hal', stdout=StringIO())

    def test_output_transaction(self):
        out = StringIO()
        management.call_command('transaction', stdout=out, no_color=True)
        output = out.getvalue().strip()
        self.assertTrue(output.startswith(connection.ops.start_transaction_sql()))
        self.assertTrue(output.endswith(connection.ops.end_transaction_sql()))
        output = management.call_command('transaction', stdout=StringIO(), no_color=True)
        self.assertTrue(output.strip().startswith(connection.ops.start_transaction_sql()))
        self.assertTrue(output.strip().endswith(connection.ops.end_transaction_sql()))

    def test_call_command_no_checks(self):
        """