mitmproxy/test/full_coverage_plugin.py

144 lines
4.2 KiB
Python
Raw Normal View History

2017-02-14 23:27:14 +00:00
import os
import configparser
import pytest
2017-05-17 15:27:57 +00:00
import sys
2017-02-14 23:27:14 +00:00
here = os.path.abspath(os.path.dirname(__file__))
2017-02-14 23:27:14 +00:00
enable_coverage = False
coverage_values = []
coverage_passed = True
no_full_cov = []
def pytest_addoption(parser):
2022-04-26 11:53:35 +00:00
parser.addoption(
"--full-cov",
action="append",
dest="full_cov",
default=[],
help="Require full test coverage of 100%% for this module/path/filename (multi-allowed). Default: none",
)
2017-02-14 23:27:14 +00:00
2022-04-26 11:53:35 +00:00
parser.addoption(
"--no-full-cov",
action="append",
dest="no_full_cov",
default=[],
help="Exclude file from a parent 100%% coverage requirement (multi-allowed). Default: none",
)
2017-02-14 23:27:14 +00:00
def pytest_configure(config):
global enable_coverage
global no_full_cov
enable_coverage = (
2022-04-26 11:53:35 +00:00
config.getoption("file_or_dir")
and len(config.getoption("file_or_dir")) == 0
and config.getoption("full_cov")
and len(config.getoption("full_cov")) > 0
and config.pluginmanager.getplugin("_cov") is not None
and config.pluginmanager.getplugin("_cov").cov_controller is not None
and config.pluginmanager.getplugin("_cov").cov_controller.cov is not None
2017-02-14 23:27:14 +00:00
)
c = configparser.ConfigParser()
c.read(os.path.join(here, "..", "setup.cfg"))
2022-04-26 11:53:35 +00:00
fs = c["tool:full_coverage"]["exclude"].split("\n")
2017-02-14 23:27:14 +00:00
no_full_cov = config.option.no_full_cov + [f.strip() for f in fs]
@pytest.hookimpl(hookwrapper=True)
def pytest_runtestloop(session):
global enable_coverage
global coverage_values
global coverage_passed
global no_full_cov
if not enable_coverage:
yield
return
2019-09-28 14:53:24 +00:00
cov = session.config.pluginmanager.getplugin("_cov").cov_controller.cov
2017-02-14 23:27:14 +00:00
2022-04-26 11:53:35 +00:00
if os.name == "nt":
cov.exclude("pragma: windows no cover")
2017-02-14 23:27:14 +00:00
2022-04-26 11:53:35 +00:00
if sys.platform == "darwin":
cov.exclude("pragma: osx no cover")
2017-05-17 15:27:57 +00:00
if os.environ.get("OPENSSL") == "old":
2022-04-26 11:53:35 +00:00
cov.exclude("pragma: openssl-old no cover")
2017-05-17 15:27:57 +00:00
2017-02-14 23:27:14 +00:00
yield
coverage_values = {name: 0 for name in session.config.option.full_cov}
2017-02-14 23:27:14 +00:00
prefix = os.getcwd()
excluded_files = [os.path.normpath(f) for f in no_full_cov]
2022-04-26 11:53:35 +00:00
measured_files = [
os.path.normpath(os.path.relpath(f, prefix))
for f in cov.get_data().measured_files()
]
measured_files = [
f
for f in measured_files
if not any(f.startswith(excluded_f) for excluded_f in excluded_files)
]
2017-02-14 23:27:14 +00:00
for name in coverage_values.keys():
files = [f for f in measured_files if f.startswith(os.path.normpath(name))]
try:
2022-04-26 11:53:35 +00:00
with open(os.devnull, "w") as null:
2017-02-14 23:27:14 +00:00
overall = cov.report(files, ignore_errors=True, file=null)
2022-04-26 11:53:35 +00:00
singles = [
(s, cov.report(s, ignore_errors=True, file=null)) for s in files
]
2017-02-14 23:27:14 +00:00
coverage_values[name] = (overall, singles)
except:
pass
if any(v < 100 for v, _ in coverage_values.values()):
# make sure we get the EXIT_TESTSFAILED exit code
session.testsfailed += 1
coverage_passed = False
2019-09-28 14:53:24 +00:00
def pytest_terminal_summary(terminalreporter, exitstatus, config):
2017-02-14 23:27:14 +00:00
global enable_coverage
global coverage_values
global coverage_passed
global no_full_cov
if not enable_coverage:
return
2022-04-26 11:53:35 +00:00
terminalreporter.write("\n")
2017-02-14 23:27:14 +00:00
if not coverage_passed:
2022-04-26 11:53:35 +00:00
markup = {"red": True, "bold": True}
2017-02-14 23:27:14 +00:00
msg = "FAIL: Full test coverage not reached!\n"
terminalreporter.write(msg, **markup)
for name in sorted(coverage_values.keys()):
2022-04-26 11:53:35 +00:00
msg = f"Coverage for {name}: {coverage_values[name][0]:.2f}%\n"
2017-02-14 23:27:14 +00:00
if coverage_values[name][0] < 100:
2022-04-26 11:53:35 +00:00
markup = {"red": True, "bold": True}
2017-02-14 23:27:14 +00:00
for s, v in sorted(coverage_values[name][1]):
if v < 100:
2022-04-26 11:53:35 +00:00
msg += f" {s}: {v:.2f}%\n"
2017-02-14 23:27:14 +00:00
else:
2022-04-26 11:53:35 +00:00
markup = {"green": True}
2017-02-14 23:27:14 +00:00
terminalreporter.write(msg, **markup)
else:
2022-04-26 11:53:35 +00:00
msg = "SUCCESS: Full test coverage reached in modules and files:\n"
msg += "{}\n\n".format("\n".join(config.option.full_cov))
2017-02-14 23:27:14 +00:00
terminalreporter.write(msg, green=True)
2022-04-26 11:53:35 +00:00
msg = "\nExcluded files:\n"
2017-02-14 23:27:14 +00:00
for s in sorted(no_full_cov):
msg += f" {s}\n"
2017-02-14 23:27:14 +00:00
terminalreporter.write(msg)