2018-03-31 15:25:21 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2017-02-15 17:52:32 +00:00
|
|
|
import io
|
|
|
|
import contextlib
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import glob
|
|
|
|
import multiprocessing
|
|
|
|
import configparser
|
|
|
|
import itertools
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
def run_tests(src, test, fail):
|
|
|
|
stderr = io.StringIO()
|
|
|
|
stdout = io.StringIO()
|
|
|
|
with contextlib.redirect_stderr(stderr):
|
|
|
|
with contextlib.redirect_stdout(stdout):
|
2022-04-26 11:53:35 +00:00
|
|
|
e = pytest.main(
|
|
|
|
[
|
|
|
|
"-qq",
|
|
|
|
"--disable-pytest-warnings",
|
|
|
|
"--cov",
|
|
|
|
src.replace(".py", "").replace("/", "."),
|
|
|
|
"--cov-fail-under",
|
|
|
|
"100",
|
|
|
|
"--cov-report",
|
|
|
|
"term-missing:skip-covered",
|
|
|
|
"-o",
|
|
|
|
"faulthandler_timeout=0",
|
|
|
|
test,
|
|
|
|
]
|
|
|
|
)
|
2017-02-15 17:52:32 +00:00
|
|
|
|
2020-10-31 16:30:15 +00:00
|
|
|
if e == 0:
|
|
|
|
if fail:
|
2022-04-26 11:53:35 +00:00
|
|
|
print(
|
|
|
|
"FAIL DUE TO UNEXPECTED SUCCESS:",
|
|
|
|
src,
|
|
|
|
"Please remove this file from setup.cfg tool:individual_coverage/exclude.",
|
|
|
|
)
|
2017-02-15 17:52:32 +00:00
|
|
|
e = 42
|
2020-10-31 16:30:15 +00:00
|
|
|
else:
|
|
|
|
print(".")
|
2017-02-15 17:52:32 +00:00
|
|
|
else:
|
|
|
|
if fail:
|
2019-09-28 17:52:18 +00:00
|
|
|
print("Ignoring allowed fail:", src)
|
2017-02-15 17:52:32 +00:00
|
|
|
e = 0
|
|
|
|
else:
|
2022-04-26 11:53:35 +00:00
|
|
|
cov = [
|
|
|
|
l
|
|
|
|
for l in stdout.getvalue().split("\n")
|
|
|
|
if (src in l) or ("was never imported" in l)
|
|
|
|
]
|
2017-02-15 17:52:32 +00:00
|
|
|
if len(cov) == 1:
|
2019-09-28 17:52:18 +00:00
|
|
|
print("FAIL:", cov[0])
|
2017-02-15 17:52:32 +00:00
|
|
|
else:
|
2019-09-28 17:52:18 +00:00
|
|
|
print("FAIL:", src, test, stdout.getvalue(), stdout.getvalue())
|
2017-02-15 17:52:32 +00:00
|
|
|
print(stderr.getvalue())
|
|
|
|
print(stdout.getvalue())
|
|
|
|
|
|
|
|
sys.exit(e)
|
|
|
|
|
|
|
|
|
|
|
|
def start_pytest(src, test, fail):
|
|
|
|
# run pytest in a new process, otherwise imports and modules might conflict
|
|
|
|
proc = multiprocessing.Process(target=run_tests, args=(src, test, fail))
|
|
|
|
proc.start()
|
|
|
|
proc.join()
|
|
|
|
return (src, test, proc.exitcode)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
c = configparser.ConfigParser()
|
2022-04-26 11:53:35 +00:00
|
|
|
c.read("setup.cfg")
|
|
|
|
fs = c["tool:individual_coverage"]["exclude"].strip().split("\n")
|
2017-02-15 17:52:32 +00:00
|
|
|
no_individual_cov = [f.strip() for f in fs]
|
|
|
|
|
2022-04-26 11:53:35 +00:00
|
|
|
excluded = [
|
|
|
|
"mitmproxy/contrib/",
|
|
|
|
"mitmproxy/test/",
|
|
|
|
"mitmproxy/tools/",
|
|
|
|
"mitmproxy/platform/",
|
|
|
|
]
|
|
|
|
src_files = glob.glob("mitmproxy/**/*.py", recursive=True)
|
|
|
|
src_files = [f for f in src_files if os.path.basename(f) != "__init__.py"]
|
|
|
|
src_files = [
|
|
|
|
f for f in src_files if not any(os.path.normpath(p) in f for p in excluded)
|
|
|
|
]
|
2022-07-20 15:23:43 +00:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
src_files = [f for f in src_files if sys.argv[1] in str(f)]
|
2017-02-15 17:52:32 +00:00
|
|
|
|
|
|
|
ps = []
|
|
|
|
for src in sorted(src_files):
|
2022-04-26 11:53:35 +00:00
|
|
|
test = os.path.join(
|
|
|
|
"test", os.path.dirname(src), "test_" + os.path.basename(src)
|
|
|
|
)
|
2017-02-15 17:52:32 +00:00
|
|
|
if os.path.isfile(test):
|
|
|
|
ps.append((src, test, src in no_individual_cov))
|
|
|
|
|
|
|
|
result = list(itertools.starmap(start_pytest, ps))
|
|
|
|
|
|
|
|
if any(e != 0 for _, _, e in result):
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2022-04-26 11:53:35 +00:00
|
|
|
if __name__ == "__main__":
|
2017-02-15 17:52:32 +00:00
|
|
|
main()
|