-
Notifications
You must be signed in to change notification settings - Fork 48
/
pytest_wrapper.py.tpl
66 lines (49 loc) · 1.94 KB
/
pytest_wrapper.py.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Adapted from https://gist.github.com/betaboon/c1dd785b5ba468b4df4e382eafff969a
import contextlib
import os
import pathlib
import sys
import coverage
import domain_coordinator
import pytest
{ament_setup}
def start_coverage_session() -> coverage.Coverage:
coverage_dir = pathlib.Path(os.getenv('COVERAGE_DIR'))
coverage_file = coverage_dir / '.coverage'
coverage_manifest = pathlib.Path(os.getenv('COVERAGE_MANIFEST'))
coverage_sources = coverage_manifest.read_text().splitlines()
coverage_session = coverage.Coverage(
data_file=str(coverage_file),
include=coverage_sources,
)
coverage_session.start()
return coverage_session
def finalize_coverage_session(coverage_session: coverage.Coverage) -> None:
coverage_session.stop()
coverage_session.save()
output_file = pathlib.Path(os.getenv('COVERAGE_OUTPUT_FILE'))
coverage_session.lcov_report(outfile=output_file)
def main() -> None:
test_outputs_dir = os.environ.get('TEST_UNDECLARED_OUTPUTS_DIR')
if test_outputs_dir:
os.environ['ROS_HOME'] = test_outputs_dir
os.environ['ROS_LOG_DIR'] = test_outputs_dir
with contextlib.ExitStack() as stack:
if 'ROS_DOMAIN_ID' not in os.environ:
domain_id = stack.enter_context(domain_coordinator.domain_id())
os.environ['ROS_DOMAIN_ID'] = str(domain_id)
print(f'Running with ROS_DOMAIN_ID {os.environ["ROS_DOMAIN_ID"]}')
bazel_coverage = os.getenv('COVERAGE') == '1'
coverage_session = None
if bazel_coverage:
coverage_session = start_coverage_session()
args = [
'-ra', '-vv', '-p', 'launch_pytest.plugin',
f'--junitxml={os.environ["XML_OUTPUT_FILE"]}'
] + sys.argv[1:]
pytest_exit_code = pytest.main(args)
if coverage_session:
finalize_coverage_session(coverage_session)
sys.exit(pytest_exit_code)
if __name__ == '__main__':
main()