Skip to content

Commit

Permalink
🚦 Extract and cache code-coverge results
Browse files Browse the repository at this point in the history
The code-coverage step on CI is currently one of the longest stalls on
the system, largely due to the fact that it needs to re-run tests, which
does require some level of recompilation between steps.

Since this is something that should not change if the source input
or dependencies has not changed, this adds new caching that will cache
the last generated code-coverage result and pull it back up whenever
it is needed.
  • Loading branch information
bitwizeshift committed Dec 13, 2023
1 parent d35bf34 commit 1a5b0c2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
57 changes: 57 additions & 0 deletions .github/actions/cargo-code-coverage/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: cargo-code-coverage
description: "An action for generating code-coverage for a project using cargo-llvm-cov"

inputs:
output-file:
required: true
description: "The output file to write the coverage information to, which gets cached."
minimum-requirement:
required: false
description: "A decimal value indicating the minimum required coverage requirements. 0 disables this."
default: "0.0"

runs:
using: "composite"
steps:
- name: Cache Coverage
id: cargo-coverage
uses: actions/cache@v3
with:
path: ${{ inputs.output-file }}
key: cargo-coverage-${{ runner.os }}-${{ hashFiles('**/*.rs', '**/Cargo.toml', 'Cargo.toml') }}

- name: Install cargo-llvm-cov
if: steps.cargo-coverage.outputs.cache-hit != 'true'
uses: taiki-e/install-action@cargo-llvm-cov

- name: Generate Coverage Coverage
if: steps.cargo-coverage.outputs.cache-hit != 'true'
shell: bash
run: cargo llvm-cov --json --output-path ${{ inputs.output-file }}

- name: Emit 'coverage' output
id: report
shell: bash
run: echo "coverage=$(cat ${{ inputs.output-file }})" >> "${GITHUB_OUTPUT}"

- name: Check Coverage Requirement
if: inputs.minimum-requirement > 0.0
shell: bash
run: |
coverage=${{fromJson(steps.report.outputs.coverage).data[0].totals.lines.percent}}
if [[ ${{inputs.minimum-requirement}} > ${coverage} ]]; then
echo \
"::error::Project code coverage fell below minimum desired '${{inputs.minimum-requirement}}%'!" \
"The current coverage is '${COVERAGE}%.'" \
"Please either add more tests, or lower the requirements."
exit 1
fi
- name: Upload Coverage Artifact
uses: actions/upload-artifact@v1
if: steps.report.outcome == 'success'
continue-on-error: true
with:
name: ${{ runner.os }}-codecov
path: ${{ inputs.output-file }}
34 changes: 4 additions & 30 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ jobs:
defaults:
run:
shell: bash
env:
OUTPUT_FILE: ${{ matrix.os }}-codecov.json
COVERAGE_MINIMUM: ${{ 0.0 }}

steps:
- name: Checkout
Expand All @@ -174,9 +171,6 @@ jobs:
with:
components: llvm-tools-preview

- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov

- name: Build
id: build
uses: ./.github/actions/cargo-build
Expand All @@ -185,28 +179,8 @@ jobs:
id: test
run: cargo test --verbose

- name: Generate Coverage Report
id: report
run: |
cargo llvm-cov --json --output-path ${{ env.OUTPUT_FILE }}
echo "coverage=$(cat ${{ env.OUTPUT_FILE }})" >> "${GITHUB_OUTPUT}"
- name: Check Coverage Requirement
if: env.COVERAGE_MINIMUM > 0.0
env:
COVERAGE: ${{fromJson(steps.report.outputs.coverage).data[0].totals.lines.percent}}
run: |
if [[ ${{env.COVERAGE_MINIMUM}} > ${{env.COVERAGE}} ]]; then
echo "Error: Project code coverage fell below minimum desired '${COVERAGE_MINIMUM}%'!" >&2
echo "The current coverage is '${COVERAGE}%.'" >&2
echo "Please either add more tests, or lower the requirements." >&2
exit 1
fi
- name: Upload Coverage Artifact
uses: actions/upload-artifact@v1
if: steps.report.outcome == 'success'
continue-on-error: true
- name: Coverage
uses: ./.github/actions/cargo-code-coverage
with:
name: ${{matrix.os}}-codecov
path: ${{env.OUTPUT_FILE}}
output-file: ${{ matrix.os }}-codecov.json
minimum-required: 0.0

0 comments on commit 1a5b0c2

Please sign in to comment.