Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/coverage_threshold/cli/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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


Expand All @@ -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),
)
2 changes: 1 addition & 1 deletion src/coverage_threshold/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/coverage_threshold/cli/read_report.py
Original file line number Diff line number Diff line change
@@ -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()))
6 changes: 3 additions & 3 deletions src/coverage_threshold/lib/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

Expand Down
2 changes: 2 additions & 0 deletions src/coverage_threshold/model/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -46,6 +47,7 @@ def parse(obj: Any) -> Config:
if "modules" in obj
else None
),
coverage_json=parse_option_field(obj, str, "coverage_json"),
)


Expand Down
23 changes: 23 additions & 0 deletions tests/integration/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pathlib
import subprocess
import textwrap

import pytest

Expand Down Expand Up @@ -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")
1 change: 1 addition & 0 deletions tests/unit/config/complex.pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/unit/config/test_read_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/lib/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))


Expand Down