Coverage for src/governed_harness/configuration/policies.py: 94%
23 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-12 16:06 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-12 16:06 +0000
1"""Profile policies that take effect under ``governance.applyProfilePolicies`` (#51)."""
3from __future__ import annotations
5from collections.abc import Mapping
6from typing import Any
8from governed_harness.domain.errors import ConfigurationError
10UNAVAILABLE_STATUS_POLICIES = ("missingTestCommand", "missingTestScript")
11"""Status of an unavailable mandatory validator: a missing executable or Python module
12(``missingTestCommand``) or a missing package script (``missingTestScript``)."""
13UNAVAILABLE_STATUSES = ("BLOCKED", "FAILED")
16def coverage_minimum(policies: Mapping[str, Any]) -> float | None:
17 """The configured line-coverage minimum, ``None`` when ``coverage`` is absent or is not a
18 threshold (the Python profile's ``optional_for_research_prototype`` is a declaration)."""
19 value = policies.get("coverage")
20 if not isinstance(value, Mapping):
21 return None
22 minimum = value.get("minimumPercent")
23 if isinstance(minimum, bool) or not isinstance(minimum, int | float): 23 ↛ 24line 23 didn't jump to line 24 because the condition on line 23 was never true
24 raise ConfigurationError("policies.coverage.minimumPercent must be a number from 0 to 100")
25 if not 0 <= float(minimum) <= 100:
26 raise ConfigurationError("policies.coverage.minimumPercent must be from 0 to 100")
27 return float(minimum)
30def validate_profile_policies(policies: Mapping[str, Any]) -> None:
31 """Refuse values the harness cannot apply (configuration error, exit code 2)."""
32 for key in UNAVAILABLE_STATUS_POLICIES:
33 value = policies.get(key)
34 if value is not None and value not in UNAVAILABLE_STATUSES:
35 raise ConfigurationError(
36 f"policies.{key} must be one of {', '.join(UNAVAILABLE_STATUSES)}: {value!r}"
37 )
38 coverage_minimum(policies)