Coverage for src/governed_harness/validators/coverage.py: 93%

28 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-12 16:06 +0000

1"""Line coverage threshold of a Python project (``policies.coverage.minimumPercent`` under 

2``governance.applyProfilePolicies``). 

3 

4The Python profile declares ``coverage: optional_for_research_prototype``, which no component 

5read. A project that sets ``coverage: {minimumPercent: N}`` gets a mandatory validator, 

6``python.coverage``, that runs the test suite under ``coverage.py`` and fails when the total line 

7coverage is below ``N`` percent. It runs the tests a second time (after ``python.pytest``) and 

8needs the ``coverage`` package in the project's interpreter; without it the validator is 

9``BLOCKED`` (or the ``missingTestCommand`` status). 

10""" 

11 

12from __future__ import annotations 

13 

14from dataclasses import replace 

15from pathlib import Path 

16 

17from governed_harness.configuration.models import ValidatorDefinition 

18from governed_harness.configuration.policies import coverage_minimum 

19from governed_harness.validators.base import ValidationContext, ValidatorOutput 

20from governed_harness.validators.command import CommandValidator 

21 

22COVERAGE_VALIDATOR_ID = "python.coverage" 

23COVERAGE_TIMEOUT_SECONDS = 900 

24 

25 

26__all__ = ["COVERAGE_VALIDATOR_ID", "CoverageValidator", "coverage_minimum"] 

27 

28 

29class CoverageValidator: 

30 validator_id = COVERAGE_VALIDATOR_ID 

31 

32 def __init__(self, minimum: float) -> None: 

33 self.minimum = minimum 

34 

35 def definition(self) -> ValidatorDefinition: 

36 return ValidatorDefinition( 

37 id=self.validator_id, 

38 command=("python", "-m", "coverage", "run", "-m", "pytest", "-q"), 

39 mandatory=True, 

40 timeoutSeconds=COVERAGE_TIMEOUT_SECONDS, 

41 ) 

42 

43 def execute(self, context: ValidationContext, *, data_file: Path) -> ValidatorOutput: 

44 """Run the tests under coverage, then the report with ``--fail-under``. The result is 

45 the report's (or the first step's when the tests themselves did not pass).""" 

46 data = str(data_file) 

47 run_definition = context.definition.model_copy( 

48 update={ 

49 "command": ( 

50 "python", 

51 "-m", 

52 "coverage", 

53 "run", 

54 f"--data-file={data}", 

55 "-m", 

56 "pytest", 

57 "-q", 

58 ) 

59 } 

60 ) 

61 # dataclasses.replace returns the type it receives; Sonar types it as DataclassInstance. 

62 run_context: ValidationContext = replace( # NOSONAR(S5890) 

63 context, definition=run_definition 

64 ) 

65 first = CommandValidator(self.validator_id).execute(run_context) # NOSONAR(S5655) 

66 if not first.result.status.successful: 66 ↛ 67line 66 didn't jump to line 67 because the condition on line 66 was never true

67 return first 

68 report_definition = context.definition.model_copy( 

69 update={ 

70 "command": ( 

71 "python", 

72 "-m", 

73 "coverage", 

74 "report", 

75 f"--data-file={data}", 

76 f"--fail-under={self.minimum:g}", 

77 ) 

78 } 

79 ) 

80 report_context: ValidationContext = replace( # NOSONAR(S5890) 

81 context, definition=report_definition 

82 ) 

83 second = CommandValidator(self.validator_id).execute(report_context) # NOSONAR(S5655) 

84 result = second.result.model_copy( 

85 update={ 

86 "summary": ( 

87 f"line coverage reached {self.minimum:g}%" 

88 if second.result.status.successful 

89 else f"line coverage below {self.minimum:g}% ({second.result.summary})" 

90 ), 

91 "evidence_refs": first.result.evidence_refs + second.result.evidence_refs, 

92 "tool_invocation_ids": first.result.tool_invocation_ids 

93 + second.result.tool_invocation_ids, 

94 "started_at": first.result.started_at, 

95 } 

96 ) 

97 return ValidatorOutput( 

98 result, second.findings, first.tool_invocations + second.tool_invocations 

99 )