File: individual_coverage.py

package info (click to toggle)
mitmproxy 8.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,952 kB
  • sloc: python: 53,389; javascript: 1,603; xml: 186; sh: 105; ansic: 68; makefile: 13
file content (106 lines) | stat: -rwxr-xr-x 2,925 bytes parent folder | download | duplicates (2)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3

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):
            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,
                ]
            )

    if e == 0:
        if fail:
            print(
                "FAIL DUE TO UNEXPECTED SUCCESS:",
                src,
                "Please remove this file from setup.cfg tool:individual_coverage/exclude.",
            )
            e = 42
        else:
            print(".")
    else:
        if fail:
            print("Ignoring allowed fail:", src)
            e = 0
        else:
            cov = [
                l
                for l in stdout.getvalue().split("\n")
                if (src in l) or ("was never imported" in l)
            ]
            if len(cov) == 1:
                print("FAIL:", cov[0])
            else:
                print("FAIL:", src, test, stdout.getvalue(), stdout.getvalue())
                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()
    c.read("setup.cfg")
    fs = c["tool:individual_coverage"]["exclude"].strip().split("\n")
    no_individual_cov = [f.strip() for f in fs]

    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)
    ]

    ps = []
    for src in sorted(src_files):
        test = os.path.join(
            "test", os.path.dirname(src), "test_" + os.path.basename(src)
        )
        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)


if __name__ == "__main__":
    main()