-
Notifications
You must be signed in to change notification settings - Fork 0
180 lines (159 loc) · 5.43 KB
/
Copy pathtests.yml
File metadata and controls
180 lines (159 loc) · 5.43 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
name: Tests
on:
pull_request:
push:
branches:
- main
workflow_dispatch:
permissions:
contents: read
issues: write
pull-requests: write
concurrency:
group: tests-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
name: test (${{ matrix.os }})
runs-on: ${{ matrix.os }}
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- name: Check out repository
uses: actions/checkout@v7
with:
fetch-depth: 0
- name: Install Pixi
uses: prefix-dev/setup-pixi@v0
with:
environments: test
- name: Run tests with coverage (Ubuntu only)
if: runner.os == 'Linux'
shell: bash
run: |
pixi run -e test test-cov-report
- name: Run tests
if: runner.os != 'Linux'
run: pixi run -e test test-cov
- name: Upload coverage artifact
if: runner.os == 'Linux'
uses: actions/upload-artifact@v7
with:
name: coverage-xml
path: coverage.xml
- name: Fetch PR base branch
if: runner.os == 'Linux' && github.event_name == 'pull_request'
shell: bash
run: |
git fetch origin \
"${{ github.base_ref }}:refs/remotes/origin/${{ github.base_ref }}"
- name: Check diff coverage threshold
id: diff_cover
if: runner.os == 'Linux' && github.event_name == 'pull_request'
continue-on-error: true
shell: bash
run: |
python -m pip install --disable-pip-version-check diff-cover
diff-cover coverage.xml \
--compare-branch "origin/${{ github.base_ref }}" \
--fail-under=99 | tee diff-cover.txt
- name: Summarize coverage
id: coverage_summary
if: runner.os == 'Linux'
shell: bash
run: |
python - <<'PY'
import os
import pathlib
import re
import xml.etree.ElementTree as ET
overall = 100.0 * float(ET.parse("coverage.xml").getroot().attrib["line-rate"])
diff_text = ""
diff_path = pathlib.Path("diff-cover.txt")
if diff_path.exists():
diff_text = diff_path.read_text(encoding="utf-8")
match = re.search(r"(?m)^(?:Diff )?Coverage:\s+([0-9.]+)%$", diff_text)
diff = match.group(1) if match else "n/a"
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as fh:
fh.write(f"overall={overall:.2f}\n")
fh.write(f"diff={diff}\n")
PY
- name: Comment coverage on PR
if: >
runner.os == 'Linux' &&
github.event_name == 'pull_request' &&
github.event.pull_request.head.repo.full_name == github.repository
uses: actions/github-script@v9
env:
OVERALL_COVERAGE: ${{ steps.coverage_summary.outputs.overall }}
DIFF_COVERAGE: ${{ steps.coverage_summary.outputs.diff }}
DIFF_STATUS: ${{ steps.diff_cover.outcome }}
with:
script: |
const fs = require('fs');
const marker = '<!-- voids-coverage-report -->';
const diffText = fs.existsSync('diff-cover.txt')
? fs.readFileSync('diff-cover.txt', 'utf8').trim()
: 'diff-cover did not run.';
const result = process.env.DIFF_STATUS === 'success' ? 'passed' : 'failed';
const icon = process.env.DIFF_STATUS === 'success' ? '✅' : '❌';
const body = `${marker}
## Coverage
- Overall line coverage on Ubuntu: \`${process.env.OVERALL_COVERAGE}%\`
- Diff coverage: \`${process.env.DIFF_COVERAGE}%\`
- Required diff coverage: \`99%\`
- Result: ${icon} ${result}
<details><summary>diff-cover output</summary>
\`\`\`text
${diffText}
\`\`\`
</details>`;
const { owner, repo } = context.repo;
const issue_number = context.issue.number;
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number,
});
const previous = comments.find(
(comment) =>
comment.user.type === 'Bot' &&
comment.body &&
comment.body.includes(marker),
);
if (previous) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: previous.id,
body,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body,
});
}
- name: Upload coverage to Codecov
if: runner.os == 'Linux' && env.CODECOV_TOKEN != ''
uses: codecov/codecov-action@v7
with:
files: coverage.xml
fail_ci_if_error: false
token: ${{ env.CODECOV_TOKEN }}
verbose: true
- name: Enforce diff coverage threshold
if: runner.os == 'Linux' && github.event_name == 'pull_request' && steps.diff_cover.outcome == 'failure'
shell: bash
run: |
echo "Diff coverage is below the required 99% threshold."
exit 1