Skip to content

Commit

Permalink
Better exception handling in tests
Browse files Browse the repository at this point in the history
1. Don't continue with testing if an extension crashes
2. Use logger.exception to format current traceback in an error
  • Loading branch information
rotu committed Mar 14, 2020
1 parent 9c2dfa7 commit 03c6107
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions colcon_core/task/python/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Licensed under the Apache License, Version 2.0

import re
import traceback

from colcon_core.logging import colcon_logger
from colcon_core.plugin_system import get_first_line_doc
Expand All @@ -27,7 +26,6 @@ def add_arguments(self, *, parser): # noqa: D102
add_python_testing_step_arguments(parser)

async def test(self, *, additional_hooks=None): # noqa: D102
pkg = self.context.pkg
args = self.context.args

logger.info(
Expand All @@ -36,8 +34,8 @@ async def test(self, *, additional_hooks=None): # noqa: D102
try:
env = await get_command_environment(
'setup_py', args.build_base, self.context.dependencies)
except RuntimeError as e:
logger.error(str(e))
except RuntimeError:
logger.exception('Failed to get command environment')
return 1
setup_py_data = get_setup_data(self.context.pkg, env)

Expand All @@ -52,15 +50,13 @@ async def test(self, *, additional_hooks=None): # noqa: D102
1, "test() by extension '{key}'".format_map(locals()))
try:
matched = extension.match(self.context, env, setup_py_data)
except Exception as e: # noqa: F841
except Exception:
# catch exceptions raised in python testing step extension
exc = traceback.format_exc()
logger.error(
logger.exception(
'Exception in Python testing step extension '
"'{extension.STEP_TYPE}': {e}\n{exc}"
"'{extension.STEP_TYPE}'"
.format_map(locals()))
# skip failing extension, continue with next one
continue
return 1
if matched:
break
else:
Expand All @@ -73,12 +69,11 @@ async def test(self, *, additional_hooks=None): # noqa: D102
1, "test.step() by extension '{key}'".format_map(locals()))
try:
return await extension.step(self.context, env, setup_py_data)
except Exception as e: # noqa: F841
except Exception:
# catch exceptions raised in python testing step extension
exc = traceback.format_exc()
logger.error(
logger.exception(
'Exception in Python testing step extension '
"'{extension.STEP_TYPE}': {e}\n{exc}".format_map(locals()))
"'{extension.STEP_TYPE}'".format_map(locals()))
return 1


Expand Down Expand Up @@ -176,12 +171,11 @@ def add_python_testing_step_arguments(parser):
try:
retval = extension.add_arguments(parser=parser)
assert retval is None, 'add_arguments() should return None'
except Exception as e: # noqa: F841
except Exception:
# catch exceptions raised in package selection extension
exc = traceback.format_exc()
logger.error(
logger.exception(
'Exception in Python testing step extension '
"'{extension.STEP_TYPE}': {e}\n{exc}".format_map(locals()))
"'{extension.STEP_TYPE}'".format_map(locals()))
# skip failing extension, continue with next one


Expand Down

0 comments on commit 03c6107

Please sign in to comment.