2017-02-24 01:58:57 +01:00
|
|
|
"""Monkey patch os._exit when running under coverage so we don't lose coverage data in forks, such as with `pytest --boxed`."""
|
2019-07-10 17:25:05 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
2017-02-24 01:58:57 +01:00
|
|
|
|
|
|
|
|
2019-07-10 17:25:05 +02:00
|
|
|
def pytest_configure():
|
|
|
|
try:
|
|
|
|
import coverage
|
|
|
|
except ImportError:
|
|
|
|
coverage = None
|
2017-04-27 05:21:11 +02:00
|
|
|
|
2019-07-10 17:25:05 +02:00
|
|
|
try:
|
|
|
|
test = coverage.Coverage
|
|
|
|
except AttributeError:
|
|
|
|
coverage = None
|
2017-02-24 01:58:57 +01:00
|
|
|
|
|
|
|
if not coverage:
|
|
|
|
return
|
|
|
|
|
2019-07-10 17:25:05 +02:00
|
|
|
import gc
|
|
|
|
import os
|
|
|
|
|
2017-02-24 01:58:57 +01:00
|
|
|
coverage_instances = []
|
|
|
|
|
|
|
|
for obj in gc.get_objects():
|
|
|
|
if isinstance(obj, coverage.Coverage):
|
|
|
|
coverage_instances.append(obj)
|
|
|
|
|
|
|
|
if not coverage_instances:
|
2019-03-22 15:59:29 +01:00
|
|
|
coverage_config = os.environ.get('COVERAGE_CONF')
|
2018-09-18 22:58:22 +02:00
|
|
|
|
|
|
|
if not coverage_config:
|
|
|
|
return
|
|
|
|
|
2019-03-22 15:59:29 +01:00
|
|
|
coverage_output = os.environ.get('COVERAGE_FILE')
|
2019-03-11 23:13:09 +01:00
|
|
|
|
|
|
|
if not coverage_output:
|
|
|
|
return
|
|
|
|
|
2018-09-18 22:58:22 +02:00
|
|
|
cov = coverage.Coverage(config_file=coverage_config)
|
|
|
|
coverage_instances.append(cov)
|
|
|
|
else:
|
|
|
|
cov = None
|
2017-02-24 01:58:57 +01:00
|
|
|
|
|
|
|
os_exit = os._exit
|
|
|
|
|
|
|
|
def coverage_exit(*args, **kwargs):
|
|
|
|
for instance in coverage_instances:
|
|
|
|
instance.stop()
|
|
|
|
instance.save()
|
|
|
|
|
|
|
|
os_exit(*args, **kwargs)
|
|
|
|
|
|
|
|
os._exit = coverage_exit
|
2018-09-18 22:58:22 +02:00
|
|
|
|
|
|
|
if cov:
|
|
|
|
cov.start()
|