|
| 1 | +# Ferrocene Rust Coverage |
| 2 | + |
| 3 | +This directory provides the Ferrocene Rust coverage workflow for Bazel-based |
| 4 | +projects. It uses Ferrocene's `symbol-report` and `blanket` tools to generate |
| 5 | +HTML coverage reports from `.profraw` files produced by Rust tests. |
| 6 | + |
| 7 | +The workflow is intentionally split: |
| 8 | +- Tests produce `.profraw` files (can run on host or target hardware). |
| 9 | +- Reports are generated later on a host machine. |
| 10 | + |
| 11 | +This makes it easy to collect coverage from cross-compiled tests or from |
| 12 | +hardware-in-the-loop runs. |
| 13 | + |
| 14 | +## Quick Start (Developers) |
| 15 | + |
| 16 | +1) Run tests with coverage enabled: |
| 17 | + |
| 18 | +```bash |
| 19 | +bazel test --config=ferrocene-x86_64-linux --config=ferrocene-coverage \ |
| 20 | + --nocache_test_results \ |
| 21 | + //path/to:rust_tests |
| 22 | +``` |
| 23 | + |
| 24 | +2) Generate coverage reports: |
| 25 | + |
| 26 | +```bash |
| 27 | +bazel run //:rust_coverage -- --min-line-coverage 80 |
| 28 | +``` |
| 29 | + |
| 30 | +The default report directory is: |
| 31 | + |
| 32 | +``` |
| 33 | +$(bazel info bazel-bin)/coverage/rust-tests/<target>/blanket/index.html |
| 34 | +``` |
| 35 | + |
| 36 | +The script prints per-target line coverage plus an overall summary line. |
| 37 | + |
| 38 | +## Integrator Setup |
| 39 | + |
| 40 | +### 1) MODULE.bazel |
| 41 | + |
| 42 | +Add `score_tooling` and `score_toolchains_rust` as dependencies: |
| 43 | + |
| 44 | +```starlark |
| 45 | +bazel_dep(name = "score_tooling", version = "1.0.0") |
| 46 | +bazel_dep(name = "score_toolchains_rust", version = "0.4.0") |
| 47 | +``` |
| 48 | + |
| 49 | +### 2) .bazelrc |
| 50 | + |
| 51 | +Add a Ferrocene coverage config. Names are examples; choose names that fit |
| 52 | +your repo: |
| 53 | + |
| 54 | +``` |
| 55 | +# Ferrocene toolchain for host execution |
| 56 | +build:ferrocene-x86_64-linux --host_platform=@score_bazel_platforms//:x86_64-linux |
| 57 | +build:ferrocene-x86_64-linux --platforms=@score_bazel_platforms//:x86_64-linux |
| 58 | +build:ferrocene-x86_64-linux --extra_toolchains=@score_toolchains_rust//toolchains/ferrocene:ferrocene_x86_64_unknown_linux_gnu |
| 59 | +
|
| 60 | +# Coverage flags for rustc |
| 61 | +build:ferrocene-coverage --@rules_rust//rust/settings:extra_rustc_flag=-Cinstrument-coverage |
| 62 | +build:ferrocene-coverage --@rules_rust//rust/settings:extra_rustc_flag=-Clink-dead-code |
| 63 | +build:ferrocene-coverage --@rules_rust//rust/settings:extra_rustc_flag=-Ccodegen-units=1 |
| 64 | +build:ferrocene-coverage --@rules_rust//rust/settings:extra_rustc_flag=-Cdebuginfo=2 |
| 65 | +build:ferrocene-coverage --@rules_rust//rust/settings:extra_exec_rustc_flag=-Cinstrument-coverage |
| 66 | +build:ferrocene-coverage --@rules_rust//rust/settings:extra_exec_rustc_flag=-Clink-dead-code |
| 67 | +build:ferrocene-coverage --@rules_rust//rust/settings:extra_exec_rustc_flag=-Ccodegen-units=1 |
| 68 | +build:ferrocene-coverage --@rules_rust//rust/settings:extra_exec_rustc_flag=-Cdebuginfo=2 |
| 69 | +test:ferrocene-coverage --run_under=@score_tooling//coverage:llvm_profile_wrapper |
| 70 | +``` |
| 71 | + |
| 72 | +### 3) Add a repo-local wrapper target |
| 73 | + |
| 74 | +In a root `BUILD` file: |
| 75 | + |
| 76 | +```starlark |
| 77 | +load("@score_tooling//coverage:coverage.bzl", "rust_coverage_report") |
| 78 | + |
| 79 | +rust_coverage_report( |
| 80 | + name = "rust_coverage", |
| 81 | + bazel_configs = [ |
| 82 | + "ferrocene-x86_64-linux", |
| 83 | + "ferrocene-coverage", |
| 84 | + ], |
| 85 | + query = 'kind("rust_test", //...)', |
| 86 | + min_line_coverage = "80", |
| 87 | +) |
| 88 | +``` |
| 89 | + |
| 90 | +Run it with: |
| 91 | + |
| 92 | +```bash |
| 93 | +bazel run //:rust_coverage |
| 94 | +``` |
| 95 | + |
| 96 | +### 4) Optional: exclude known-problematic targets |
| 97 | + |
| 98 | +```starlark |
| 99 | +query = 'kind("rust_test", //...) except //path/to:tests', |
| 100 | +``` |
| 101 | + |
| 102 | +## Cross/Target Execution |
| 103 | + |
| 104 | +If tests run on target hardware, copy the `.profraw` files back to the host |
| 105 | +and point the report generator to the directory: |
| 106 | + |
| 107 | +```bash |
| 108 | +bazel run //:rust_coverage -- --profraw-dir /path/to/profraw |
| 109 | +``` |
| 110 | + |
| 111 | +## Coverage Gate Behavior |
| 112 | + |
| 113 | +`--min-line-coverage` applies per target. If any target is below the minimum, |
| 114 | +the script exits non-zero so CI can fail the job. An overall summary is printed |
| 115 | +for visibility but does not change gating behavior. |
| 116 | + |
| 117 | +## Common Pitfalls |
| 118 | + |
| 119 | +- **"running 0 tests"**: The Rust test harness found no `#[test]` functions, |
| 120 | + so coverage is 0%. Add tests or exclude the target from the query. |
| 121 | +- **"couldn't find source file"** warnings: Usually path remapping or crate |
| 122 | + mapping issues. Check that `crate` attributes in `rust_test` targets point to |
| 123 | + the library crate (or exclude the target). |
| 124 | +- **Cached test results**: Use `--nocache_test_results` if you need to re-run |
| 125 | + tests and regenerate `.profraw` files. |
| 126 | + |
| 127 | +## Troubleshooting |
| 128 | + |
| 129 | +### Coverage is 0% but tests ran |
| 130 | +- Verify the target contains real `#[test]` functions. A rust_test target with |
| 131 | + no tests will run but report 0% coverage. |
| 132 | +- Ensure you ran tests with `--config=ferrocene-coverage` so `.profraw` files |
| 133 | + exist. |
| 134 | +- If the test binary is cached, use `--nocache_test_results`. |
| 135 | + |
| 136 | +### "couldn't find source file" warnings |
| 137 | +- Check `crate` mapping on `rust_test` targets. If `crate = "name"` is used, |
| 138 | + ensure it refers to the library crate in the same package. |
| 139 | +- Confirm the reported paths exist in the workspace. Path remapping is required |
| 140 | + so `blanket` can resolve files under `--ferrocene-src`. |
| 141 | + |
| 142 | +### No `.profraw` files found |
| 143 | +- Ensure `test:ferrocene-coverage` sets `--run_under=@score_tooling//coverage:llvm_profile_wrapper`. |
| 144 | +- Re-run tests with `--nocache_test_results`. |
| 145 | +- If tests ran on target hardware, copy the `.profraw` files back and pass |
| 146 | + `--profraw-dir`. |
| 147 | + |
| 148 | +### Coverage gate fails in CI |
| 149 | +- The gate is per-target. A single target below the threshold fails the job. |
| 150 | +- Use a stricter query (exclude known-zero targets) or add tests. |
| 151 | + |
| 152 | +## CI Integration (Suggested Pattern) |
| 153 | + |
| 154 | +Keep coverage generation separate from docs: |
| 155 | + |
| 156 | +1) Coverage workflow: |
| 157 | + - run `bazel run //:rust_coverage` |
| 158 | + - upload `bazel-bin/coverage/rust-tests` as an artifact |
| 159 | + |
| 160 | +2) Docs workflow: |
| 161 | + - download the artifact |
| 162 | + - copy into the docs output (e.g. `docs/_static/coverage/`) |
| 163 | + - publish Sphinx docs to GitHub Pages |
0 commit comments