-
Notifications
You must be signed in to change notification settings - Fork 3
153 lines (123 loc) Β· 5.23 KB
/
test-coverage.yml
File metadata and controls
153 lines (123 loc) Β· 5.23 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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@0e279bb959325dab635dd2c09392533439d90093
with:
version: 10.33.0
- uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e
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 --filter dotenv-diff run coverage
- name: Locate and normalize coverage files
id: coverage_files
run: |
LCOV_FILE="coverage/lcov.info"
SUMMARY_FILE="coverage/coverage-summary.json"
if [ -f "packages/cli/coverage/lcov.info" ]; then
LCOV_FILE="packages/cli/coverage/lcov.info"
fi
if [ -f "packages/cli/coverage/coverage-summary.json" ]; then
SUMMARY_FILE="packages/cli/coverage/coverage-summary.json"
fi
if [ ! -f "$LCOV_FILE" ]; then
echo "Missing lcov file at $LCOV_FILE"
exit 1
fi
if [ ! -f "$SUMMARY_FILE" ]; then
echo "Missing coverage summary at $SUMMARY_FILE"
exit 1
fi
# Normalize source-file paths for monolith layout so diff-cover can
# map coverage entries to changed files under packages/cli.
sed -i -E 's|^SF:src/|SF:packages/cli/src/|; s|^SF:src\\|SF:packages/cli/src/|; s|^SF:packages\\cli\\src\\|SF:packages/cli/src/|' "$LCOV_FILE"
echo "lcov_file=$LCOV_FILE" >> $GITHUB_OUTPUT
echo "summary_file=$SUMMARY_FILE" >> $GITHUB_OUTPUT
- 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 "${{ steps.coverage_files.outputs.summary_file }}" ]; then
overall_coverage=$(jq -r '.total.lines.pct' "${{ steps.coverage_files.outputs.summary_file }}")
fi
diff-cover "${{ steps.coverage_files.outputs.lcov_file }}" \
--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@0ea0beb66eb9baf113663a64ec522f60e49231c0
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