From 551c1877a252af624a0a5ba3fe817b9803080372 Mon Sep 17 00:00:00 2001 From: Jonathan Sharpe Date: Fri, 27 Nov 2026 11:29:56 +0000 Subject: [PATCH 1/2] Fix typo --- src/coverage_threshold/lib/_common.py | 6 +++--- tests/unit/lib/test_common.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/coverage_threshold/lib/_common.py b/src/coverage_threshold/lib/_common.py index 3034e47..c0577e1 100644 --- a/src/coverage_threshold/lib/_common.py +++ b/src/coverage_threshold/lib/_common.py @@ -22,11 +22,11 @@ def __call__( ) -> CheckResult: ... -def _safe_percent(numerator: int, denomenator: int) -> Decimal: - if denomenator == 0: +def _safe_percent(numerator: int, denominator: int, /) -> Decimal: + if denominator == 0: return Decimal(100) else: - return ((Decimal(numerator) / Decimal(denomenator)) * Decimal(100)).quantize( + return ((Decimal(numerator) / Decimal(denominator)) * Decimal(100)).quantize( Decimal("0.0001") ) diff --git a/tests/unit/lib/test_common.py b/tests/unit/lib/test_common.py index 3ce4ee3..a031f7d 100644 --- a/tests/unit/lib/test_common.py +++ b/tests/unit/lib/test_common.py @@ -15,10 +15,10 @@ def test_safe_percent__100_percent_when_denomenator_is_zero(numerator: int) -> N @given(integers(min_value=0, max_value=10**10), integers(min_value=1, max_value=10**10)) def test_safe_percent__regular_fraction_otherwise( - numerator: int, denomenator: int + numerator: int, denominator: int ) -> None: - assert _safe_percent(numerator, denomenator) == ( - Decimal(numerator) / Decimal(denomenator) * Decimal(100) + assert _safe_percent(numerator, denominator) == ( + Decimal(numerator) / Decimal(denominator) * Decimal(100) ).quantize(Decimal("0.0001")) From 4f6c49fba1c21cd8320dfffd8420f139441fc785 Mon Sep 17 00:00:00 2001 From: Jonathan Sharpe Date: Fri, 27 Nov 2026 12:01:47 +0000 Subject: [PATCH 2/2] Allow config file to set coverage file path --- src/coverage_threshold/cli/args.py | 5 +++-- src/coverage_threshold/cli/main.py | 2 +- src/coverage_threshold/cli/read_report.py | 5 +++-- src/coverage_threshold/model/config.py | 2 ++ tests/integration/test_cli.py | 23 +++++++++++++++++++++++ tests/unit/config/complex.pyproject.toml | 1 + tests/unit/config/test_read_config.py | 1 + 7 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/coverage_threshold/cli/args.py b/src/coverage_threshold/cli/args.py index ebdcb18..08349a8 100644 --- a/src/coverage_threshold/cli/args.py +++ b/src/coverage_threshold/cli/args.py @@ -56,7 +56,7 @@ parser.add_argument( "--coverage-json", type=str, - default="./coverage.json", + default=None, help="path to coverage json (default: ./coverage.json)", ) parser.add_argument( @@ -75,7 +75,7 @@ class ArgsNamespace(argparse.Namespace): file_line_coverage_min: Optional[Decimal] file_branch_coverage_min: Optional[Decimal] file_combined_coverage_min: Optional[Decimal] - coverage_json: str + coverage_json: Optional[str] config: str @@ -101,4 +101,5 @@ def combine_config_with_args(args: ArgsNamespace, config: Config) -> Config: args.file_combined_coverage_min, config.file_combined_coverage_min ), modules=config.modules, + coverage_json=fallback(args.coverage_json, config.coverage_json), ) diff --git a/src/coverage_threshold/cli/main.py b/src/coverage_threshold/cli/main.py index 2ed4bd5..dfe0c33 100644 --- a/src/coverage_threshold/cli/main.py +++ b/src/coverage_threshold/cli/main.py @@ -11,9 +11,9 @@ def bool_to_return_status(x: bool) -> int: def main() -> int: args = parser.parse_args(namespace=ArgsNamespace()) - report = read_report(args.coverage_json) config_from_file = read_config(args.config) config = combine_config_with_args(args, config_from_file) + report = read_report(config.coverage_json) all_checks = check_all(report, config) if all_checks.result: print(colors.OKGREEN + "Success!" + colors.ENDC) diff --git a/src/coverage_threshold/cli/read_report.py b/src/coverage_threshold/cli/read_report.py index 660cde5..056cd82 100644 --- a/src/coverage_threshold/cli/read_report.py +++ b/src/coverage_threshold/cli/read_report.py @@ -1,8 +1,9 @@ import json +from typing import Optional from coverage_threshold.model.report import ReportModel -def read_report(coverage_json_filename: str) -> ReportModel: - with open(coverage_json_filename) as coverage_json_file: +def read_report(coverage_json_filename: Optional[str]) -> ReportModel: + with open(coverage_json_filename or "./coverage.json") as coverage_json_file: return ReportModel.parse(json.loads(coverage_json_file.read())) diff --git a/src/coverage_threshold/model/config.py b/src/coverage_threshold/model/config.py index bb5733d..bb5ac8d 100644 --- a/src/coverage_threshold/model/config.py +++ b/src/coverage_threshold/model/config.py @@ -17,6 +17,7 @@ class Config: file_branch_coverage_min: Optional[Decimal] = None file_combined_coverage_min: Optional[Decimal] = None modules: Optional[Mapping[str, ModuleConfig]] = None + coverage_json: Optional[str] = None @staticmethod def parse(obj: Any) -> Config: @@ -46,6 +47,7 @@ def parse(obj: Any) -> Config: if "modules" in obj else None ), + coverage_json=parse_option_field(obj, str, "coverage_json"), ) diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py index cd391f8..9adae07 100644 --- a/tests/integration/test_cli.py +++ b/tests/integration/test_cli.py @@ -1,4 +1,6 @@ +import pathlib import subprocess +import textwrap import pytest @@ -43,3 +45,24 @@ def test_cli_fails() -> None: + f"{colors.FAIL}Total line coverage metric failed" + f", expected 100.0, was 75.0000{colors.ENDC}\n" ).encode("utf-8") + + +def test_cli_accepts_coverage_file_in_config(tmp_path: pathlib.Path) -> None: + config_file = tmp_path / "pyproject.toml" + config_file.write_text( + textwrap.dedent( + f""" + [tool.coverage-threshold] + coverage_json = "{EXAMPLE_PROJECT_PATH}/coverage.json" + line_coverage_min = 75.0 + """ + ) + ) + process = subprocess.run( + ["coverage-threshold", "--config", str(config_file)], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + assert process.returncode == 0 + assert process.stderr == b"" + assert process.stdout == SUCCESS_MESSAGE.encode("utf-8") diff --git a/tests/unit/config/complex.pyproject.toml b/tests/unit/config/complex.pyproject.toml index 7d1f290..24a309e 100644 --- a/tests/unit/config/complex.pyproject.toml +++ b/tests/unit/config/complex.pyproject.toml @@ -2,6 +2,7 @@ line_coverage_min = 95 file_line_coverage_min = 95 branch_coverage_min = 50 +coverage_json = "path/to/coverage.json" [tool.coverage-threshold.modules."src/cli/"] file_line_coverage_min = 40 diff --git a/tests/unit/config/test_read_config.py b/tests/unit/config/test_read_config.py index 1985fea..86d4308 100644 --- a/tests/unit/config/test_read_config.py +++ b/tests/unit/config/test_read_config.py @@ -22,6 +22,7 @@ def test_read_config_parses_complex_pyproject_format() -> None: assert config.line_coverage_min == Decimal("95.0") assert config.file_line_coverage_min == Decimal("95.0") assert config.branch_coverage_min == Decimal("50.0") + assert config.coverage_json == "path/to/coverage.json" assert config.modules is not None assert config.modules["src/cli/"].file_line_coverage_min == Decimal("40.0") assert config.modules["src/cli/my_command.py"].file_line_coverage_min == Decimal(