MCPcopy
hub / github.com/mitmproxy/mitmproxy / main

Function main

test/individual_coverage.py:15–114  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13
14
15async def main():
16 with open("pyproject.toml", "rb") as f:
17 data = tomllib.load(f)
18
19 exclude = re.compile(
20 "|".join(
21 f"({fnmatch.translate(x)})"
22 for x in data["tool"]["pytest"]["individual_coverage"]["exclude"]
23 )
24 )
25
26 sem = asyncio.Semaphore(os.cpu_count() or 1)
27
28 async def run_tests(f: Path, should_fail: bool) -> None:
29 if f.name == "__init__.py":
30 mod = ast.parse(f.read_text())
31 full_cov_on_import = all(
32 isinstance(stmt, (ast.ImportFrom, ast.Import, ast.Assign))
33 for stmt in mod.body
34 )
35 if full_cov_on_import:
36 if should_fail:
37 raise RuntimeError(
38 f"Remove {f} from tool.pytest.individual_coverage in pyproject.toml."
39 )
40 else:
41 print(f"{f}: skip __init__.py file without logic")
42 return
43
44 test_file = Path("test") / f.parent.with_name(f"test_{f.parent.name}.py")
45 else:
46 test_file = Path("test") / f.with_name(f"test_{f.name}")
47
48 coverage_file = f".coverage-{str(f).replace('/', '-')}"
49
50 async with sem:
51 try:
52 proc = await asyncio.create_subprocess_exec(
53 "pytest",
54 "-qq",
55 "--disable-pytest-warnings",
56 "--cov",
57 str(f.with_suffix("")).replace("/", "."),
58 "--cov-fail-under",
59 "100",
60 "--cov-report",
61 "term-missing:skip-covered",
62 test_file,
63 stdout=subprocess.PIPE,
64 stderr=subprocess.PIPE,
65 env={
66 "COVERAGE_FILE": coverage_file,
67 **os.environ,
68 },
69 )
70 stdout, stderr = await asyncio.wait_for(proc.communicate(), 60)
71 except TimeoutError:
72 raise RuntimeError(f"{f}: timeout")

Callers 1

Calls 6

run_testsFunction · 0.85
exitMethod · 0.80
loadMethod · 0.45
joinMethod · 0.45
appendMethod · 0.45
matchMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…