MIL-010 task 6: the coverage report reaches the workflow as an artifact (ADR-0010, RC-073)

Task: MIL-010#6
Refs #160
This commit is contained in:
2026-10-14 13:54:01 +08:00
parent 4fc0a8cab3
commit 0dc5132a2e
8 changed files with 142 additions and 24 deletions
+9 -9
View File
@@ -35,21 +35,21 @@ class ConfigurationError(Exception):
def read_coverage(path: Path) -> int:
"""Return the line coverage of a Cobertura XML report as a whole percent, half up."""
if not path.is_file():
raise ConfigurationError(f"coverage-report '{path}' does not exist.")
raise ConfigurationError(f"coverage-report '{path.as_posix()}' does not exist.")
data = path.read_bytes()
if b"<!ENTITY" in data:
raise ConfigurationError(
f"coverage-report '{path}' declares entities, which are refused."
f"coverage-report '{path.as_posix()}' declares entities, which are refused."
)
try:
root = ElementTree.fromstring(data)
except ElementTree.ParseError as error:
raise ConfigurationError(
f"coverage-report '{path}' is not well-formed XML: {error}."
f"coverage-report '{path.as_posix()}' is not well-formed XML: {error}."
) from error
if root.tag != "coverage":
raise ConfigurationError(
f"coverage-report '{path}' is not a Cobertura report (root element '{root.tag}')."
f"coverage-report '{path.as_posix()}' is not a Cobertura report (root element '{root.tag}')."
)
covered, valid = root.get("lines-covered"), root.get("lines-valid")
try:
@@ -57,27 +57,27 @@ def read_coverage(path: Path) -> int:
covered_lines, valid_lines = int(covered), int(valid)
if valid_lines <= 0:
raise ConfigurationError(
f"coverage-report '{path}' has no valid lines."
f"coverage-report '{path.as_posix()}' has no valid lines."
)
if not 0 <= covered_lines <= valid_lines:
raise ConfigurationError(
f"coverage-report '{path}' has lines-covered outside 0..lines-valid."
f"coverage-report '{path.as_posix()}' has lines-covered outside 0..lines-valid."
)
fraction = Decimal(covered_lines) / Decimal(valid_lines)
else:
rate = root.get("line-rate")
if rate is None:
raise ConfigurationError(
f"coverage-report '{path}' has neither lines-valid and lines-covered nor line-rate."
f"coverage-report '{path.as_posix()}' has neither lines-valid and lines-covered nor line-rate."
)
fraction = Decimal(rate)
if not Decimal(0) <= fraction <= Decimal(1):
raise ConfigurationError(
f"coverage-report '{path}' has a line-rate outside 0..1."
f"coverage-report '{path.as_posix()}' has a line-rate outside 0..1."
)
except (ValueError, InvalidOperation) as error:
raise ConfigurationError(
f"coverage-report '{path}' has a value that is not a number."
f"coverage-report '{path.as_posix()}' has a value that is not a number."
) from error
return int((fraction * 100).quantize(Decimal(1), rounding=ROUND_HALF_UP))
+10 -3
View File
@@ -10,8 +10,8 @@
# and nothing when none of them changed
#
# Settings come from the environment, never from interpolated expressions:
# WORKING_DIRECTORY, EXCLUDE, COVERAGE_REPORT, COVERAGE_WARN, COVERAGE_GOOD,
# BADGES_BRANCH, BADGES_OUT (the directory of the badge files; a temporary
# WORKING_DIRECTORY, EXCLUDE, COVERAGE_REPORT, COVERAGE_ARTIFACT,
# COVERAGE_WARN, COVERAGE_GOOD, BADGES_BRANCH, BADGES_OUT (the directory of the badge files; a temporary
# directory under RUNNER_TEMP by default).
# Every failure is printed as ::error::[category] message.
@@ -41,6 +41,9 @@ validate_inputs() {
relative_path "$COVERAGE_REPORT" ||
fail "coverage-report must be a relative path inside the repository."
fi
if [ -n "$COVERAGE_ARTIFACT" ] && [ -z "$COVERAGE_REPORT" ]; then
fail "coverage-artifact '$COVERAGE_ARTIFACT' needs coverage-report, the file inside the artifact."
fi
case "$BADGES_BRANCH" in
main | master | HEAD | "") fail "badges-branch '$BADGES_BRANCH' must not be a default branch." ;;
esac
@@ -55,9 +58,13 @@ measure() {
python3 "$TOOLS_DIR/count_loc.py" \
--directory "$WORKING_DIRECTORY" --exclude "$EXCLUDE" --output "$metrics"
rm -rf "$OUT_DIR"
report="$COVERAGE_REPORT"
if [ -n "$COVERAGE_ARTIFACT" ]; then
report="coverage-artifact/$COVERAGE_REPORT"
fi
python3 "$TOOLS_DIR/badges.py" \
--metrics "$metrics" --output-dir "$OUT_DIR" \
--coverage-report "$COVERAGE_REPORT" \
--coverage-report "$report" \
--coverage-warn "$COVERAGE_WARN" --coverage-good "$COVERAGE_GOOD"
rm -f "$metrics"
}