Skip to content

Commit b1a3d59

Browse files
committed
use pytest hook to redirect pytest warnings to stderr
1 parent c4adcff commit b1a3d59

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

colcon_core/command.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ def _main(*, command_name, argv):
177177
None, None)
178178
handler.handle(log_record)
179179

180+
# set an environment variable named after the command (if not already set)
181+
# which allows subprocesses to identify they are invoked by this command
182+
if command_name.upper() not in os.environ:
183+
os.environ[command_name.upper()] = '1'
184+
180185
# invoke verb
181186
return verb_main(context, colcon_logger)
182187

colcon_core/pytest/__init__.py

Whitespace-only changes.

colcon_core/pytest/hooks.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2020 Dirk Thomas
2+
# Licensed under the Apache License, Version 2.0
3+
4+
import os
5+
import sys
6+
import types
7+
8+
import pytest
9+
10+
11+
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
12+
def pytest_terminal_summary(terminalreporter, exitstatus, config):
13+
"""Redirect the summary warnings to stderr when run within colcon."""
14+
summary_warnings = terminalreporter.summary_warnings
15+
16+
def redirect_to_stderr(self):
17+
nonlocal summary_warnings
18+
tw = self._tw
19+
import _pytest.config
20+
self._tw = _pytest.config.create_terminal_writer(
21+
self.config, sys.stderr)
22+
summary_warnings()
23+
self._tw = tw
24+
25+
if 'COLCON' in os.environ:
26+
terminalreporter.summary_warnings = types.MethodType(
27+
redirect_to_stderr, terminalreporter)
28+
yield

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ colcon_core.verb =
126126
test = colcon_core.verb.test:TestVerb
127127
console_scripts =
128128
colcon = colcon_core.command:main
129+
pytest11 =
130+
colcon_core_warnings_stderr = colcon_core.pytest.hooks
129131

130132
[options.package_data]
131133
colcon_core.shell.template = *.em

0 commit comments

Comments
 (0)