-
Notifications
You must be signed in to change notification settings - Fork 3
126 lines (102 loc) · 4.13 KB
/
test-coverage.yml
File metadata and controls
126 lines (102 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
name: Tests & Coverage
on:
pull_request:
branches: [main]
jobs:
test:
if: github.actor != 'dotenv-diff-release-bot'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.sha }}
- uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320
- uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f
with:
node-version: 20
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- name: Install diff-cover
run: pip install diff-cover
- name: Run coverage
run: pnpm run coverage
- name: Fix lcov paths
run: |
if [ -f "coverage/lcov.info" ]; then
sed -i "s|^SF:|SF:|g" coverage/lcov.info
fi
- name: Run diff-cover analysis
id: diff_cover
run: |
echo "## 📊 Unit Test Coverage" > comment.md
echo "" >> comment.md
COVERAGE_THRESHOLD=80
all_passed=true
overall_coverage="N/A"
if [ -f "coverage/coverage-summary.json" ]; then
overall_coverage=$(jq -r '.total.lines.pct' "coverage/coverage-summary.json")
fi
diff-cover coverage/lcov.info \
--compare-branch=origin/${{ github.event.pull_request.base.ref }} \
--diff-range-notation=... \
--json-report diff-coverage.json \
--fail-under=0 || true
echo "**Overall coverage**: ${overall_coverage}%" >> comment.md
echo "" >> comment.md
if [ -f "diff-coverage.json" ]; then
total_lines=$(jq -r '.total_num_lines' diff-coverage.json)
if [ "$total_lines" != "0" ] && [ "$total_lines" != "null" ]; then
percent=$(jq -r '.total_percent_covered' diff-coverage.json)
violations=$(jq -r '.total_num_violations' diff-coverage.json)
covered=$((total_lines - violations))
if [ "$percent" -lt "$COVERAGE_THRESHOLD" ]; then
all_passed=false
fi
if [ "$percent" = "100" ]; then
emoji="✅"
status="PASS"
elif [ "$percent" -ge "$COVERAGE_THRESHOLD" ]; then
emoji="⚠️"
status="PASS"
else
emoji="❌"
status="FAIL"
fi
echo "**Diff coverage**: $emoji ${percent}% (${covered}/${total_lines} lines) - $status" >> comment.md
echo "" >> comment.md
if [ "$violations" != "0" ]; then
echo "<details>" >> comment.md
echo "<summary>🔍 Uncovered lines in diff</summary>" >> comment.md
echo "" >> comment.md
echo '```' >> comment.md
jq -r '.src_stats | to_entries[] | .key as $file | .value.violation_lines[] | "\($file):\(.)"' diff-coverage.json >> comment.md
echo '```' >> comment.md
echo "" >> comment.md
echo "</details>" >> comment.md
fi
else
echo "✨ No new testable lines in this PR" >> comment.md
fi
fi
echo "---" >> comment.md
echo "" >> comment.md
if [ "$all_passed" = true ]; then
echo "✅ **Coverage threshold met** (≥${COVERAGE_THRESHOLD}%)" >> comment.md
else
echo "❌ **Coverage threshold not met** (required: ≥${COVERAGE_THRESHOLD}%)" >> comment.md
fi
echo "all_passed=$all_passed" >> $GITHUB_OUTPUT
- name: Comment PR with coverage
if: github.event.pull_request.head.repo.fork == false
uses: marocchino/sticky-pull-request-comment@70d2764d1a7d5d9560b100cbea0077fc8f633987
with:
path: comment.md
- name: Fail if coverage is below threshold
if: steps.diff_cover.outputs.all_passed == 'false'
run: |
echo "❌ Coverage check failed: Coverage below 80%"
exit 1