Commit 74bb91fd authored by Raphaël Hertzog's avatar Raphaël Hertzog

core: fix error handling in ./manage.py tracker_run_task

parent 74051ae1
......@@ -12,7 +12,6 @@ Implements a command to start a number of available Distro Tracker tasks.
A task is a subclass of :class:`distro_tracker.core.tasks.BaseTask`.
"""
import logging
import traceback
from django.core.management.base import BaseCommand
......@@ -52,7 +51,6 @@ class Command(BaseCommand):
)
def handle(self, *args, **kwargs):
verbose = int(kwargs.get('verbosity', 1)) > 0
params = {}
if kwargs['force_update']:
params['force_update'] = True
......@@ -61,12 +59,6 @@ class Command(BaseCommand):
for task_name in kwargs['tasks']:
if isinstance(task_name, bytes):
task_name = task_name.decode('utf-8')
logger.info("Starting task %s (from ./manage.py tracker_run_task)",
task_name)
try:
run_task(task_name, **params)
except Exception:
logger.exception("Task %s failed:", task_name)
if verbose:
self.stdout.write('Task {} failed:\n'.format(task_name))
traceback.print_exc(file=self.stdout)
logger.info("./manage.py tracker_run_task %s", task_name)
if not run_task(task_name, **params):
self.stderr.write('Task {} failed to run.\n'.format(task_name))
......@@ -14,6 +14,7 @@
Tests for the Distro Tracker core management commands.
"""
import io
from unittest import mock
from django.core.management import call_command
......@@ -45,6 +46,7 @@ class RunTaskManagementCommandTest(SimpleTestCase):
:func:`run_task <distro_tracker.core.tasks.run_task>` function for each
given task name.
"""
mock_run_task.return_value = True
self.run_command(['TaskName1', 'TaskName2'])
# The run task was called only for the given commands
......@@ -58,6 +60,7 @@ class RunTaskManagementCommandTest(SimpleTestCase):
Tests that the management command passes the force flag to the task
invocations when it is given.
"""
mock_run_task.return_value = True
self.run_command(['TaskName1'], force_update=True)
mock_run_task.assert_called_once_with('TaskName1', force_update=True)
......@@ -67,10 +70,20 @@ class RunTaskManagementCommandTest(SimpleTestCase):
Tests that the management command passes the fake_update flag to the
task invocations when it is given.
"""
mock_run_task.return_value = True
self.run_command(['TaskName1'], fake_update=True)
mock_run_task.assert_called_once_with('TaskName1', fake_update=True)
def test_outputs_to_stderr_when_fails(self, mock_run_task):
mock_run_task.return_value = False
stderr = io.StringIO()
self.run_command(['TaskName1'], stderr=stderr)
self.assertEqual(stderr.getvalue(),
'Task TaskName1 failed to run.\n')
@mock.patch('distro_tracker.core.management.commands.'
'tracker_run_all_tasks.run_all_tasks')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment