Skip to content

Commit

Permalink
support pytest 5.0+ (#200)
Browse files Browse the repository at this point in the history
* use changed exit code constants from pytest 5.0

* update use of ExceptionInfo class to support pytest 5.x

* ignore deprecation warning about the imp module
  • Loading branch information
dirk-thomas committed Jul 16, 2019
1 parent 7f26625 commit 99ee2af
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 20 deletions.
20 changes: 16 additions & 4 deletions colcon_core/task/python/test/pytest.py
Expand Up @@ -154,13 +154,25 @@ async def step(self, context, env, setup_py_data): # noqa: D102
rc = await check_call(context, cmd, cwd=context.args.path, env=env)

# use local import to avoid a dependency on pytest
from _pytest.main import EXIT_TESTSFAILED
if rc and rc.returncode == EXIT_TESTSFAILED:
try:
from _pytest.main import ExitCode
EXIT_CODE_TESTS_FAILED = ExitCode.TESTS_FAILED # noqa: N806
except ImportError:
# support pytest < 5.0
from _pytest.main import EXIT_TESTSFAILED
EXIT_CODE_TESTS_FAILED = EXIT_TESTSFAILED # noqa: N806
if rc and rc.returncode == EXIT_CODE_TESTS_FAILED:
context.put_event_into_queue(
TestFailure(context.pkg.name))

from _pytest.main import EXIT_NOTESTSCOLLECTED
try:
from _pytest.main import ExitCode
EXIT_CODE_NO_TESTS = ExitCode.NO_TESTS_COLLECTED # noqa: N806
except ImportError:
# support pytest < 5.0
from _pytest.main import EXIT_NOTESTSCOLLECTED
EXIT_CODE_NO_TESTS = EXIT_NOTESTSCOLLECTED # noqa: N806
if rc and rc.returncode not in (
EXIT_NOTESTSCOLLECTED, EXIT_TESTSFAILED
EXIT_CODE_NO_TESTS, EXIT_CODE_TESTS_FAILED
):
return rc.returncode
2 changes: 2 additions & 0 deletions setup.cfg
Expand Up @@ -65,6 +65,8 @@ exclude =
[tool:pytest]
filterwarnings =
error
ignore:the imp module is deprecated in favour of importlib.*:DeprecationWarning
ignore:the imp module is deprecated in favour of importlib.*:PendingDeprecationWarning
junit_suite_name = colcon-core

[options.entry_points]
Expand Down
4 changes: 2 additions & 2 deletions test/test_entry_point.py
Expand Up @@ -117,7 +117,7 @@ def test_entry_point_blacklist():
with pytest.raises(RuntimeError) as e:
load_entry_point(entry_point)
assert 'The entry point group name is listed in the environment ' \
'variable' in str(e)
'variable' in str(e.value)
assert entry_point.load.call_count == 0

# entry point listed in the blacklist can't be loaded
Expand All @@ -127,7 +127,7 @@ def test_entry_point_blacklist():
with pytest.raises(RuntimeError) as e:
load_entry_point(entry_point)
assert 'The entry point name is listed in the environment variable' \
in str(e)
in str(e.value)
assert entry_point.load.call_count == 0


Expand Down
5 changes: 3 additions & 2 deletions test/test_executor.py
Expand Up @@ -148,13 +148,14 @@ def test_add_executor_arguments():
):
with pytest.raises(AssertionError) as e:
add_executor_arguments(parser)
assert 'Executor extensions must have unique priorities' in str(e)
assert 'Executor extensions must have unique priorities' in \
str(e.value)

# no extensions
with EntryPointContext():
with pytest.raises(AssertionError) as e:
add_executor_arguments(parser)
assert 'No executor extensions found' in str(e)
assert 'No executor extensions found' in str(e.value)

# choose executor by environment variable
with EntryPointContext(extension1=Extension1, extension2=Extension2):
Expand Down
4 changes: 2 additions & 2 deletions test/test_logging.py
Expand Up @@ -48,11 +48,11 @@ def test_get_numeric_log_level():
# invalid string
with pytest.raises(ValueError) as e:
get_numeric_log_level('invalid')
assert str(e).endswith(
assert str(e.value).endswith(
'valid names are: CRITICAL, ERROR, WARNING, INFO, DEBUG '
'(case-insensitive)')

# negative numeric
with pytest.raises(ValueError) as e:
get_numeric_log_level('-1')
assert str(e).endswith('numeric log levels must be positive')
assert str(e.value).endswith('numeric log levels must be positive')
2 changes: 1 addition & 1 deletion test/test_package_descriptor.py
Expand Up @@ -45,7 +45,7 @@ def test_get_dependencies():

with pytest.raises(AssertionError) as e:
d1.get_dependencies()
assert "'self'" in str(e)
assert "'self'" in str(e.value)


def test_get_recursive_dependencies():
Expand Down
3 changes: 2 additions & 1 deletion test/test_package_identification_python.py
Expand Up @@ -47,7 +47,8 @@ def test_identify():
desc.name = 'other-name'
with pytest.raises(RuntimeError) as e:
extension.identify(desc)
assert str(e).endswith('Package name already set to different value')
assert str(e.value).endswith(
'Package name already set to different value')

(basepath / 'setup.cfg').write_text(
'[metadata]\n'
Expand Down
6 changes: 3 additions & 3 deletions test/test_plugin_system.py
Expand Up @@ -155,13 +155,13 @@ def test_satisfies_version():

with pytest.raises(RuntimeError) as e:
satisfies_version('1.0.3', '^1.1')
assert 'too old' in str(e)
assert 'too old' in str(e.value)

with pytest.raises(RuntimeError) as e:
satisfies_version('2.0.0', '^1.2')
assert 'newer' in str(e)
assert 'newer' in str(e.value)

# different semantic for version numbers before 1.0
with pytest.raises(RuntimeError) as e:
satisfies_version('0.2.3', '^0.1')
assert 'newer' in str(e)
assert 'newer' in str(e.value)
6 changes: 3 additions & 3 deletions test/test_shell.py
Expand Up @@ -78,7 +78,7 @@ def test_get_command_environment():
with pytest.raises(RuntimeError) as e:
run_until_complete(coroutine)
assert 'Could not find a shell extension for the command environment' \
in str(e)
in str(e.value)
assert extensions[90]['extension1'].generate_command_environment \
.call_count == 1
# the raised exceptions are catched and result in a debug/info message
Expand All @@ -98,7 +98,7 @@ def test_get_command_environment():
coroutine = get_command_environment(None, '/build/base', None)
with pytest.raises(RuntimeError) as e:
run_until_complete(coroutine)
assert str(e).endswith(': custom exception')
assert str(e.value) == 'custom exception'
assert extensions[90]['extension1'].generate_command_environment \
.call_count == 0

Expand Down Expand Up @@ -167,7 +167,7 @@ def test_create_environment_hook():
# no primary shell extension
with pytest.raises(RuntimeError) as e:
create_environment_hook(None, None, None, None, None)
assert str(e).endswith(
assert str(e.value).endswith(
'Could not find a primary shell extension for creating an '
'environment hook')

Expand Down
2 changes: 1 addition & 1 deletion test/test_shell_bat.py
Expand Up @@ -63,7 +63,7 @@ def _test_extension(prefix_path):
coroutine = extension.generate_command_environment(
'task_name', prefix_path, {})
run_until_complete(coroutine)
assert str(e).endswith('Not usable on non-Windows systems')
assert str(e.value).endswith('Not usable on non-Windows systems')
else:
# dependency script missing
with pytest.raises(RuntimeError) as e:
Expand Down
2 changes: 1 addition & 1 deletion test/test_shell_sh.py
Expand Up @@ -64,7 +64,7 @@ def _test_extension(prefix_path):
coroutine = extension.generate_command_environment(
'task_name', prefix_path, {})
run_until_complete(coroutine)
assert str(e).endswith('Not usable on Windows systems')
assert str(e.value).endswith('Not usable on Windows systems')
else:
# dependency script missing
with pytest.raises(RuntimeError) as e:
Expand Down

0 comments on commit 99ee2af

Please sign in to comment.