| 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") |
| 73 | finally: |
| 74 | Path(coverage_file).unlink(missing_ok=True) |
| 75 | |
| 76 | if should_fail: |
| 77 | if proc.returncode != 0: |
| 78 | print(f"{f}: excluded") |
| 79 | else: |
| 80 | raise RuntimeError( |
| 81 | f"{f} is now fully covered by {test_file}. Remove it from tool.pytest.individual_coverage in pyproject.toml." |
| 82 | ) |
| 83 | else: |
| 84 | if proc.returncode == 0: |
| 85 | print(f"{f}: ok") |