Skip to content

Commit 7fb6872

Browse files
committed
[Testing] Add in Coverage Reporting to tests
Initial point to add in coverage reporting and updating the PR on workflow execution.
1 parent f0b130d commit 7fb6872

3 files changed

Lines changed: 143 additions & 3 deletions

File tree

.github/workflows/pr_check.yml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,34 @@ jobs:
3939
4040
- name: Test
4141
run: |
42-
pytest
42+
pytest --cov --cov-report=term --cov-report=markdown:coverage.md
43+
44+
- name: Upload coverage report
45+
if: always()
46+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
47+
with:
48+
name: coverage-report
49+
path: coverage.md
50+
retention-days: 30
51+
52+
- name: Save PR Metadata
53+
if: github.event_name == 'pull_request'
54+
env:
55+
PR_NUMBER: ${{ github.event.pull_request.number }}
56+
run: |
57+
mkdir -p pr_metadata
58+
echo "$PR_NUMBER" > pr_metadata/pr_number.txt
59+
60+
- name: Checksum PR Metadata
61+
if: github.event_name == 'pull_request'
62+
run: |
63+
cd pr_metadata && sha256sum *.txt > checksums.txt
64+
65+
- name: Upload PR metadata
66+
if: github.event_name == 'pull_request'
67+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
68+
with:
69+
name: pr-metadata
70+
path: pr_metadata/
71+
retention-days: 30
72+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: PR Checks - Post Coverage
2+
3+
on:
4+
workflow_run:
5+
workflows: ["PR Pre Checks"]
6+
types:
7+
- completed
8+
workflow_dispatch:
9+
inputs:
10+
run_id:
11+
description: "Workflow run ID to fetch artifacts from"
12+
required: True
13+
type: string
14+
15+
permissions:
16+
actions: read
17+
contents: read
18+
pull-requests: write
19+
20+
jobs:
21+
post-coverage:
22+
runs-on: ubuntu-latest
23+
if: >
24+
github.event_name == 'workflow_dispatch' ||
25+
(github.event.workflow_run.event == 'pull_request' &&
26+
github.event.workflow_run.conclusion == 'success')
27+
28+
steps:
29+
- name: Download coverage artifact
30+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
31+
with:
32+
name: coverage-report
33+
github-token: ${{ secrets.GITHUB_TOKEN }}
34+
run-id: ${{ github.event_name == 'workflow_dispatch' && inputs.run_id || github.event.workflow_run.id }}
35+
36+
- name: Download PR metadata
37+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
38+
with:
39+
name: pr-metadata
40+
github-token: ${{ secrets.GITHUB_TOKEN }}
41+
run-id: ${{ github.event_name == 'workflow_dispatch' && inputs.run_id || github.event.workflow_run.id }}
42+
43+
- name: Verify artifact integrity
44+
run: |
45+
if [ ! -f pr_metadata/checksums.txt ]; then
46+
echo "Warning: No checksums file found. Skipping integrity check."
47+
else
48+
cd pr_metadata
49+
if sha256sum -c checksums.txt --quiet; then
50+
echo "Artifact integrity verified."
51+
else
52+
echo "Error: Artifact integrity check failed."
53+
exit 1
54+
fi
55+
fi
56+
57+
- name: Read and Validate PR number
58+
id: pr_number
59+
env:
60+
PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}
61+
run: |
62+
if [ -z "$PR_NUMBER" ] || ! [[ "$PR_NUMBER" =~ ^[0-9]+$ ]]; then
63+
echo "Error: Could not determine PR number from workflow_run payload: $PR_NUMBER"
64+
exit 1
65+
fi
66+
echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_OUTPUT"
67+
- name: Validate coverage report
68+
run: |
69+
if [ ! -f coverage.md ]; then
70+
echo "Error: coverage.md file not found."
71+
exit 1
72+
fi
73+
FILE_SIZE=$(stat -c%s coverage.md)
74+
if [ "$FILE_SIZE" -gt 65536 ]; then
75+
echo "Error: coverage.md file size exceeds 64KB."
76+
exit 1
77+
fi
78+
79+
- name: Update PR body with coverage
80+
env:
81+
GH_TOKEN: ${{ github.token }}
82+
PR_NUMBER: ${{ steps.pr_number.outputs.PR_NUMBER }}
83+
REPOSITORY: ${{ github.repository }}
84+
run: |
85+
# Fetch the current PR body
86+
CURRENT_BODY=$(gh pr view "$PR_NUMBER" --repo "$REPOSITORY" --json body --jq '.body')
87+
88+
# Strip previous coverage section if it exists
89+
CLEAN_BODY=$(printf '%s\n' "$CURRENT_BODY" | sed '/<!-- coverage-report -->/,$d')
90+
91+
# Build New PR body with coverage appended at the bottom
92+
{
93+
echo "$CLEAN_BODY"
94+
echo "<!-- coverage-report -->"
95+
echo "## Coverage Report"
96+
echo ""
97+
cat coverage.md
98+
} > new_body.md
99+
100+
gh pr edit "$PR_NUMBER" --repo "$REPOSITORY" --body-file new_body.md

pyproject.toml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ dependencies = [
2020
dev = [
2121
"pre-commit",
2222
"ruff",
23-
"pytest",
24-
]
23+
"pytest",
24+
"pytest-cov",
25+
]
2526

2627
[tool.ruff]
2728
line-length = 120
@@ -47,3 +48,12 @@ kt = "kt.kt:main"
4748
[tool.pytest.ini_options]
4849
testpaths = ["tests"]
4950
addopts = "-ra -q"
51+
52+
[tool.coverage.run]
53+
source = ["."]
54+
omit = ["tests/*"]
55+
branch = true
56+
57+
[tool.coverage.report]
58+
show_missing = true
59+
skip_empty = true

0 commit comments

Comments
 (0)