"""Benchmarks for coverage configuration parsing and threshold validation.""" from __future__ import annotations import ast import re from pathlib import Path NOXFILE_PATH = Path("noxfile.py") PYPROJECT_PATH = Path("pyproject.toml") class TimeCoverageConfigParsing: """Measure coverage configuration parsing performance.""" def setup(self) -> None: """Read source files once for reuse.""" if NOXFILE_PATH.exists(): self.noxfile_text = NOXFILE_PATH.read_text() else: self.noxfile_text = "" if PYPROJECT_PATH.exists(): self.pyproject_text = PYPROJECT_PATH.read_text() else: self.pyproject_text = "" def time_parse_noxfile_ast(self) -> None: """Benchmark AST parsing of noxfile.py.""" if self.noxfile_text: ast.parse(self.noxfile_text) def time_extract_threshold_constant(self) -> None: """Benchmark extracting COVERAGE_THRESHOLD from noxfile AST.""" if self.noxfile_text: tree = ast.parse(self.noxfile_text) for node in ast.walk(tree): if isinstance(node, ast.Assign): for target in node.targets: if ( isinstance(target, ast.Name) and target.id == "COVERAGE_THRESHOLD" and isinstance(node.value, ast.Constant) ): _ = node.value.value def time_parse_pyproject_toml(self) -> None: """Benchmark TOML parsing of pyproject.toml.""" if self.pyproject_text: import tomllib tomllib.loads(self.pyproject_text) def time_extract_coverage_config(self) -> None: """Benchmark extracting coverage config from pyproject.toml.""" if self.pyproject_text: import tomllib data = tomllib.loads(self.pyproject_text) _ = data.get("tool", {}).get("coverage", {}).get("run", {}) def time_parse_coverage_source(self) -> None: """Benchmark extracting coverage source list from pyproject.toml.""" if self.pyproject_text: re.findall(r"source\s*=\s*\[([^\]]+)\]", self.pyproject_text) def time_parse_fail_under(self) -> None: """Benchmark extracting fail-under threshold from noxfile.""" if self.noxfile_text: re.findall(r"--fail-under=(\d+)", self.noxfile_text)