Skip to content

Commit c7a92de

Browse files
committed
ci: Calculate delta coverage using diff-cover
Now each coverage report also includes delta coverage, i.e. coverage scoped to current changes. For PRs, it's code modified in the PR, for branches it's code since the last push. The delta coverage report is included as a separate report and also a markdown summary is posted on GitHub Actions.
1 parent 4e6b9a5 commit c7a92de

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

.github/workflows/test_rust.yml

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ concurrency:
1313
env:
1414
FEATURES: lzma,jpegxr
1515
TEST_OPTS: --workspace --locked --no-fail-fast -j 4
16-
LLVM_COV_OPTS: --branch --profile ci ${TEST_OPTS} --features ${FEATURES},imgtests
16+
LLVM_COV_OPTS: --branch --profile ci
17+
LLVM_COV_TEST_OPTS: ${LLVM_COV_OPTS} ${TEST_OPTS} --features ${FEATURES},imgtests
1718

1819
LINUX_DEPENDENCIES: libasound2-dev mesa-vulkan-drivers libudev-dev
1920

@@ -163,6 +164,37 @@ jobs:
163164
runs-on: ubuntu-24.04
164165
steps:
165166
- uses: actions/checkout@v5
167+
with:
168+
# We need history for delta coverage, but we also don't want to fetch
169+
# the whole repo. 42 commits seem reasonable
170+
fetch-depth: 42
171+
172+
- name: Set up Python
173+
uses: actions/setup-python@v5
174+
with:
175+
python-version: "3.13"
176+
177+
- name: Install diff-cover
178+
run: pip install diff-cover==9.6.0
179+
180+
- name: Calculate base ref
181+
id: base_ref
182+
run: |
183+
if [ "${{ github.event_name }}" = "pull_request" ] || [ "${{ github.event_name }}" = "pull_request_target" ]; then
184+
# For PRs always compute delta with base ref
185+
git fetch origin ${{ github.base_ref }}
186+
echo "::notice::Using the PR base as base ref for delta coverage."
187+
echo "value=origin/${{ github.base_ref }}" | tee -a $GITHUB_OUTPUT
188+
elif git merge-base --is-ancestor ${{ github.event.before }} HEAD; then
189+
# Use the before SHA if it's an ancestor of HEAD
190+
echo "::notice::Using the last pushed commit as base ref for delta coverage."
191+
echo "value=${{ github.event.before }}" | tee -a $GITHUB_OUTPUT
192+
else
193+
# If not, someone force pushed and just use the parent
194+
echo "::warning::Failed to compute the base ref for delta coverage. Using parent commit."
195+
parent=$(git rev-parse HEAD~1)
196+
echo "value=${parent}" | tee -a $GITHUB_OUTPUT
197+
fi
166198
167199
- name: Install Rust toolchain
168200
# We need nightly for coverage due to:
@@ -192,15 +224,41 @@ jobs:
192224
working-directory: tests
193225
env:
194226
RUFFLE_TEST_OPTS: --ignore-known-failures
195-
run: cargo llvm-cov --html ${{ env.LLVM_COV_OPTS }}
227+
run: cargo llvm-cov --no-report ${{ env.LLVM_COV_TEST_OPTS }}
228+
229+
- name: Generate llvm-cov reports
230+
run: |
231+
mkdir -p target/llvm-cov
232+
cargo llvm-cov report --html --output-dir target/llvm-cov/ ${{ env.LLVM_COV_OPTS }}
233+
cargo llvm-cov report --lcov --output-path target/llvm-cov/coverage.lcov ${{ env.LLVM_COV_OPTS }}
196234
197-
- name: Upload report
235+
- name: Upload llvm-cov reports
198236
if: always()
199237
uses: actions/upload-artifact@v4
200238
with:
201239
name: llvm-cov
202240
path: target/llvm-cov
203241

242+
- name: Run diff-cover
243+
run: |
244+
mkdir -p target/diff-cover
245+
diff-cover target/llvm-cov/coverage.lcov \
246+
--html-report target/diff-cover/report.html \
247+
--json-report target/diff-cover/report.json \
248+
--markdown-report target/diff-cover/report.md \
249+
--compare-branch ${{ steps.base_ref.outputs.value }}
250+
251+
- name: Upload diff-cover reports
252+
if: always()
253+
uses: actions/upload-artifact@v4
254+
with:
255+
name: diff-cover
256+
path: target/diff-cover
257+
258+
- name: Upload diff-cover summary
259+
if: always()
260+
run: cat target/diff-cover/report.md >> $GITHUB_STEP_SUMMARY
261+
204262
dependencies:
205263
needs: changes
206264
if: needs.changes.outputs.should_run == 'true'

0 commit comments

Comments
 (0)