Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use pytest hook to redirect pytest warnings to stderr #338

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions colcon_core/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ def _main(*, command_name, argv):
None, None)
handler.handle(log_record)

# set an environment variable named after the command (if not already set)
# which allows subprocesses to identify they are invoked by this command
if command_name.upper() not in os.environ:
os.environ[command_name.upper()] = '1'
dirk-thomas marked this conversation as resolved.
Show resolved Hide resolved

# invoke verb
return verb_main(context, colcon_logger)

Expand Down
Empty file added colcon_core/pytest/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions colcon_core/pytest/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2020 Dirk Thomas
# Licensed under the Apache License, Version 2.0

import os
import sys
import types

import pytest


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_terminal_summary(terminalreporter, exitstatus, config):
"""Redirect the summary warnings to stderr when run within colcon."""
summary_warnings = terminalreporter.summary_warnings

def redirect_to_stderr(self):
nonlocal summary_warnings
tw = self._tw
import _pytest.config
self._tw = _pytest.config.create_terminal_writer(
self.config, sys.stderr)
summary_warnings()
self._tw = tw

if 'COLCON' in os.environ:
terminalreporter.summary_warnings = types.MethodType(
redirect_to_stderr, terminalreporter)
yield
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ colcon_core.verb =
test = colcon_core.verb.test:TestVerb
console_scripts =
colcon = colcon_core.command:main
pytest11 =
colcon_core_warnings_stderr = colcon_core.pytest.hooks

[options.package_data]
colcon_core.shell.template = *.em
Expand Down