mitmproxy/test/conftest.py

151 lines
4.5 KiB
Python
Raw Normal View History

2017-01-23 21:53:56 +00:00
import os
2016-12-01 09:36:18 +00:00
import pytest
import OpenSSL
import functools
2017-02-06 16:48:44 +00:00
from contextlib import contextmanager
2017-01-23 21:53:56 +00:00
2016-12-01 09:36:18 +00:00
import mitmproxy.net.tcp
requires_alpn = pytest.mark.skipif(
not mitmproxy.net.tcp.HAS_ALPN,
reason='requires OpenSSL with ALPN support')
2017-02-01 15:17:22 +00:00
skip_windows = pytest.mark.skipif(
os.name == "nt",
reason='Skipping due to Windows'
)
skip_not_windows = pytest.mark.skipif(
os.name != "nt",
reason='Skipping due to not Windows'
)
skip_appveyor = pytest.mark.skipif(
"APPVEYOR" in os.environ,
reason='Skipping due to Appveyor'
)
2016-12-01 09:36:18 +00:00
original_pytest_raises = pytest.raises
2017-02-06 16:48:44 +00:00
# TODO: remove this wrapper when pytest 3.1.0 is released
@contextmanager
2017-02-01 15:48:46 +00:00
@functools.wraps(original_pytest_raises)
def raises(exc, *args, **kwargs):
2017-02-06 16:48:44 +00:00
with original_pytest_raises(exc, *args, **kwargs) as exc_info:
yield
if 'match' in kwargs:
assert exc_info.match(kwargs['match'])
pytest.raises = raises
2016-12-01 09:36:18 +00:00
@pytest.fixture()
def disable_alpn(monkeypatch):
monkeypatch.setattr(mitmproxy.net.tcp, 'HAS_ALPN', False)
monkeypatch.setattr(OpenSSL.SSL._lib, 'Cryptography_HAS_ALPN', False)
2017-01-23 21:53:56 +00:00
enable_coverage = False
coverage_values = []
coverage_passed = False
def pytest_addoption(parser):
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")
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")
def pytest_configure(config):
global enable_coverage
enable_coverage = (
len(config.getoption('file_or_dir')) == 0 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
)
@pytest.hookimpl(hookwrapper=True)
def pytest_runtestloop(session):
global enable_coverage
global coverage_values
global coverage_passed
if not enable_coverage:
yield
return
cov = pytest.config.pluginmanager.getplugin("_cov").cov_controller.cov
if os.name == 'nt':
cov.exclude('pragma: windows no cover')
yield
coverage_values = dict([(name, 0) for name in pytest.config.option.full_cov])
prefix = os.getcwd()
excluded_files = [os.path.normpath(f) for f in pytest.config.option.no_full_cov]
measured_files = [os.path.normpath(os.path.relpath(f, prefix)) for f in cov.get_data().measured_files()]
2017-02-02 13:20:56 +00:00
measured_files = [f for f in measured_files if not any(f.startswith(excluded_f) for excluded_f in excluded_files)]
2017-01-23 21:53:56 +00:00
2017-02-09 10:20:53 +00:00
for name in coverage_values.keys():
2017-01-23 21:53:56 +00:00
files = [f for f in measured_files if f.startswith(os.path.normpath(name))]
try:
with open(os.devnull, 'w') as null:
coverage_values[name] = cov.report(files, ignore_errors=True, file=null)
except:
pass
if any(v < 100 for v in coverage_values.values()):
# make sure we get the EXIT_TESTSFAILED exit code
session.testsfailed += 1
else:
coverage_passed = True
def pytest_terminal_summary(terminalreporter, exitstatus):
global enable_coverage
global coverage_values
global coverage_passed
if not enable_coverage:
return
terminalreporter.write('\n')
if not coverage_passed:
markup = {'red': True, 'bold': True}
msg = "FAIL: Full test coverage not reached!\n"
terminalreporter.write(msg, **markup)
2017-02-09 10:20:53 +00:00
for name in sorted(coverage_values.keys()):
if coverage_values[name] < 100:
2017-01-23 21:53:56 +00:00
markup = {'red': True, 'bold': True}
else:
markup = {'green': True}
2017-02-09 10:20:53 +00:00
msg = 'Coverage for {}: {:.2f}%\n'.format(name, coverage_values[name])
2017-01-23 21:53:56 +00:00
terminalreporter.write(msg, **markup)
else:
markup = {'green': True}
msg = 'SUCCESS: Full test coverage reached in modules and files:\n'
msg += '{}\n\n'.format('\n'.join(pytest.config.option.full_cov))
terminalreporter.write(msg, **markup)
msg = 'Excluded files:\n'
2017-02-09 10:20:53 +00:00
msg += '{}\n'.format('\n'.join(sorted(pytest.config.option.no_full_cov)))
2017-01-23 21:53:56 +00:00
terminalreporter.write(msg)