workflows/check: Add test code coverage#763
workflows/check: Add test code coverage#763RobertZ2011 wants to merge 1 commit intoOpenDevicePartnership:v0.2.0from
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends the existing CI “check” workflow by adding Rust test code coverage generation and uploading the resulting HTML report as a workflow artifact.
Changes:
- Attempts to install LLVM coverage tooling for the Rust toolchain.
- Runs
cargo testwith coverage instrumentation enabled. - Adds steps to merge raw profiles, generate an HTML coverage report via
grcov, and upload it as an artifact.
.github/workflows/check.yml
Outdated
| submodules: true | ||
| - name: Install stable | ||
| uses: dtolnay/rust-toolchain@stable | ||
| components: llvm-tools |
There was a problem hiding this comment.
dtolnay/rust-toolchain step inputs must be under with:. As written, components: llvm-tools is not passed to the action, so the LLVM tools (and rust-profdata) likely won’t be installed and later coverage steps will fail. Move the component under with: (and ensure the correct rustup component name is used for rust-profdata).
| components: llvm-tools | |
| with: | |
| components: llvm-tools-preview |
| - name: cargo test | ||
| run: cargo test --locked | ||
| env: | ||
| RUSTFLAGS: '-C instrument-coverage' |
There was a problem hiding this comment.
Coverage instrumentation sets RUSTFLAGS=-C instrument-coverage, but LLVM_PROFILE_FILE isn’t set. Without a per-process filename pattern, test binaries can overwrite the same .profraw, producing incomplete/incorrect coverage data. Set LLVM_PROFILE_FILE to a unique pattern (e.g., including %p/%m) and preferably write it into a known directory under target/.
.github/workflows/check.yml
Outdated
| - name: Install rustfilt | ||
| run: cargo install rustfilt | ||
| - name : Merge raw profiles | ||
| run: rust-profdata merge -sparse *.profraw -o coverage.profdata |
There was a problem hiding this comment.
The merge step assumes .profraw files exist in the repo root (*.profraw). Depending on where the tests run and how profiling output is configured, the files may be emitted elsewhere (or not match the glob), causing this step to fail. Consider merging from the directory you configure via LLVM_PROFILE_FILE, or using find to collect the .profraw inputs robustly.
| run: rust-profdata merge -sparse *.profraw -o coverage.profdata | |
| run: rust-profdata merge -sparse $(find . -name '*.profraw' -print) -o coverage.profdata |
.github/workflows/check.yml
Outdated
| - name: Install grcov | ||
| run: cargo install grcov | ||
| - name: Install rustfilt | ||
| run: cargo install rustfilt |
There was a problem hiding this comment.
Installing grcov and rustfilt via cargo install without pinning versions makes CI non-reproducible and can break when upstream releases change. Prefer pinning --version (and --locked) or using an install action (e.g., taiki-e/install-action) to get consistent binaries and faster runs via caching.
| - name: Install grcov | |
| run: cargo install grcov | |
| - name: Install rustfilt | |
| run: cargo install rustfilt | |
| - name: Install grcov and rustfilt | |
| uses: taiki-e/install-action@v2 | |
| with: | |
| tool: grcov,rustfilt | |
| tool-version: 0.8.18,0.2.1 |
4bd6053 to
5e295dc
Compare
5e295dc to
e40955b
Compare
.github/workflows/check.yml
Outdated
| run: rust-profdata merge -sparse *.profraw -o coverage.profdata | ||
| - name: Generate code coverage report | ||
| run: | | ||
| grcov . --binary-path ./target/debug/ -s . -t html --ignore-not-existing -o ./target/debug/coverage/ coverage.profdata |
There was a problem hiding this comment.
The grcov invocation is generating a report from an LLVM .profdata, but it doesn't pass LLVM-specific options and only points --binary-path at ./target/debug/ (test binaries usually live under target/debug/deps). As written this can produce an empty/incomplete report. Consider enabling LLVM mode in grcov and including the deps directory (or otherwise pointing grcov at the actual test binaries used to generate the profiles).
| grcov . --binary-path ./target/debug/ -s . -t html --ignore-not-existing -o ./target/debug/coverage/ coverage.profdata | |
| grcov . \ | |
| --binary-path ./target/debug/ \ | |
| --binary-path ./target/debug/deps/ \ | |
| -s . \ | |
| -t html \ | |
| --llvm \ | |
| --ignore-not-existing \ | |
| -o ./target/debug/coverage/ \ | |
| coverage.profdata |
| - name: cargo test | ||
| run: cargo test --locked | ||
| env: | ||
| RUSTFLAGS: '-C instrument-coverage' |
There was a problem hiding this comment.
-C instrument-coverage relies on LLVM profile output files; without setting LLVM_PROFILE_FILE to a unique pattern (e.g., including %p), parallel cargo test processes can overwrite/corrupt the same .profraw and make coverage flaky/incorrect. Set LLVM_PROFILE_FILE (and consider CARGO_INCREMENTAL=0) in this step's env so all profiles are written deterministically to a known directory.
| RUSTFLAGS: '-C instrument-coverage' | |
| RUSTFLAGS: '-C instrument-coverage' | |
| LLVM_PROFILE_FILE: 'default-%p-%m.profraw' | |
| CARGO_INCREMENTAL: '0' |
.github/workflows/check.yml
Outdated
| - name : Merge raw profiles | ||
| run: rust-profdata merge -sparse *.profraw -o coverage.profdata |
There was a problem hiding this comment.
This step installs llvm-tools-preview but merges profiles using rust-profdata. For clarity/portability across toolchains, prefer using the LLVM tool name (llvm-profdata) that is provided by the component, and make the input glob/path match wherever LLVM_PROFILE_FILE writes the .profraw files.
66036c8 to
950eb34
Compare
15d3fa4 to
b4b6053
Compare
| name: code-coverage-report | ||
| path: ./target/debug/coverage/ | ||
|
|
There was a problem hiding this comment.
grcov typically needs LLVM mode enabled when consuming .profraw/.profdata generated by -C instrument-coverage; otherwise it may not interpret the profile data correctly and can produce an empty/incorrect report. Consider adding the appropriate LLVM flag(s) for grcov when generating the HTML report.
| uses: actions/upload-artifact@v4 | ||
| with: |
There was a problem hiding this comment.
Minor YAML style: this step uses - name : whereas the rest of the workflow uses - name:. Consider normalizing formatting for consistency/readability.
b4b6053 to
d8a8f6b
Compare
No description provided.