Skip to content

Commit 163501a

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 58ab2ff commit 163501a

3 files changed

Lines changed: 104 additions & 3 deletions

File tree

.github/workflows/pr_check.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,12 @@ 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
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
pr_number:
15+
description: "PR number to update"
16+
required: true
17+
type: string
18+
19+
permissions:
20+
actions: read
21+
contents: read
22+
pull-requests: write
23+
24+
jobs:
25+
post-coverage:
26+
runs-on: ubuntu-latest
27+
if: >
28+
github.event_name == 'workflow_dispatch' ||
29+
(github.event.workflow_run.event == 'pull_request' &&
30+
github.event.workflow_run.conclusion == 'success')
31+
32+
steps:
33+
- name: Download coverage artifact
34+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
35+
with:
36+
name: coverage-report
37+
github-token: ${{ secrets.GITHUB_TOKEN }}
38+
run-id: ${{ github.event_name == 'workflow_dispatch' && inputs.run_id || github.event.workflow_run.id }}
39+
40+
- name: Read and Validate PR number
41+
id: pr_number
42+
env:
43+
PR_NUMBER: ${{ github.event_name == 'workflow_dispatch' && inputs.pr_number || github.event.workflow_run.pull_requests[0].number }}
44+
run: |
45+
if [ -z "$PR_NUMBER" ] || ! [[ "$PR_NUMBER" =~ ^[0-9]+$ ]]; then
46+
echo "Error: Could not determine PR number: $PR_NUMBER"
47+
exit 1
48+
fi
49+
echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_OUTPUT"
50+
- name: Validate coverage report
51+
run: |
52+
if [ ! -f coverage.md ]; then
53+
echo "Error: coverage.md file not found."
54+
exit 1
55+
fi
56+
FILE_SIZE=$(stat -c%s coverage.md)
57+
if [ "$FILE_SIZE" -gt 65536 ]; then
58+
echo "Error: coverage.md file size exceeds 64KB."
59+
exit 1
60+
fi
61+
62+
- name: Update PR body with coverage
63+
env:
64+
GH_TOKEN: ${{ github.token }}
65+
PR_NUMBER: ${{ steps.pr_number.outputs.PR_NUMBER }}
66+
REPOSITORY: ${{ github.repository }}
67+
run: |
68+
# Fetch the current PR body
69+
CURRENT_BODY=$(gh pr view "$PR_NUMBER" --repo "$REPOSITORY" --json body --jq '.body')
70+
71+
# Strip previous coverage section if it exists
72+
CLEAN_BODY=$(printf '%s\n' "$CURRENT_BODY" | sed '/<!-- coverage-report -->/,$d')
73+
74+
# Build New PR body with coverage appended at the bottom
75+
{
76+
printf '%s\n' "$CLEAN_BODY"
77+
echo "<!-- coverage-report -->"
78+
echo "## Coverage Report"
79+
echo ""
80+
cat coverage.md
81+
} > new_body.md
82+
83+
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/*", "venv/*"]
55+
branch = true
56+
57+
[tool.coverage.report]
58+
show_missing = true
59+
skip_empty = true

0 commit comments

Comments
 (0)