'Test run_from_argv properly terminates even with custom execute() (#19665)
Also test proper traceback display.'

def test_base_run_from_argv(self):

    command = BaseCommand()
    def raise_command_error(*args, **kwargs):
        raise CommandError(u'Custom    error')
    old_stderr = sys.stderr
    sys.stderr = err = StringIO()
    try:
        command.execute = (lambda args: args)
        with self.assertRaises(TypeError):
            command.run_from_argv([u'', u''])
        command.execute = raise_command_error
        err.truncate(0)
        with self.assertRaises(SystemExit):
            command.run_from_argv([u'', u''])
        err_message = err.getvalue()
        self.assertNotIn(u'Traceback', err_message)
        self.assertIn(u'CommandError', err_message)
        err.truncate(0)
        with self.assertRaises(CommandError):
            command.run_from_argv([u'', u'', u'--traceback'])
    finally:
        sys.stderr = old_stderr
