You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR introduces native code coverage support for Ruby targets using Bazel's coverage command. It integrates SimpleCov and simplecov-lcov to generate LCOV-formatted reports.
Key implementation details:
- Coverage Helper: A specialized coverage.rb script is automatically required via RUBYOPT when coverage is enabled.
- Workspace-Relative Paths: To ensure Bazel can aggregate coverage from multiple targets, we ensure all SF: paths in the LCOV report are relative to the workspace root.
- MRI: MRI preserves loaded paths as symlinks within the Bazel sandbox. By setting the SimpleCov root to Dir.pwd (the sandbox root), we naturally get workspace-relative paths.
- JRuby: JRuby resolves all files to their absolute realpaths on the physical disk, bypassing the sandbox symlinks. To generate relative paths, we dynamically calculate the realpath of the workspace root relative to the coverage.rb helper and set it as the SimpleCov root.
- Bazel Integration:
- COVERAGE_OUTPUT_FILE: SimpleCov is configured to write directly to the path provided by Bazel.
- BAZEL_TARGET: Used as the command_name to provide unique identification for merged results and avoid warnings.
- JRuby Support: Automatically enables --debug via JRUBY_OPTS when coverage is requested, as required for JRuby coverage collection.
- Documentation: Updated the main README and rb_test rule documentation.
- Examples: Added a test_coverage.sh script to the gem example to verify coverage across different engines.
- CI Verification: Added a coverage verification step to the CI matrix in examples/gem to ensure it works across all supported MRI and JRuby versions and platforms.
Fixes#347
Copy file name to clipboardExpand all lines: README.md
+33Lines changed: 33 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -214,6 +214,37 @@ However, some are known not to work or work only partially (e.g. mRuby has no bu
214
214
To work it around, use [`--experimental_inprocess_symlink_creation`][16] Bazel flag.
215
215
See [`bazelbuild/bazel#4327`][17] for more details.
216
216
217
+
## Code Coverage
218
+
219
+
Ruby rules support Bazel's native coverage collection using [SimpleCov][21] and [simplecov-lcov][22].
220
+
221
+
> [!NOTE]
222
+
> Code coverage is currently not supported on Windows.
223
+
224
+
To enable coverage:
225
+
226
+
1. Add `simplecov` and `simplecov-lcov` gems to your `Gemfile`.
227
+
2. Run your tests with the `coverage` command:
228
+
229
+
```bash
230
+
bazel coverage //...
231
+
```
232
+
233
+
The rules automatically configure SimpleCov to use the LCOV formatter and output reports to the location expected by Bazel. Coverage reports will have workspace-relative paths, allowing Bazel to aggregate results from multiple targets.
234
+
235
+
For JRuby, coverage requires `--debug` mode, which is automatically enabled by the rules when coverage is requested.
236
+
237
+
You can add additional coverage filters using `coverage_filters` attribute in `rb_test` or `rb_binary`:
238
+
239
+
```python
240
+
rb_test(
241
+
name="my_test",
242
+
...
243
+
coverage_filters= ["/vendor/", "/custom/"],
244
+
)
245
+
```
246
+
247
+
217
248
[1]: https://www.ruby-lang.org
218
249
[2]: https://bazel.build
219
250
[3]: docs/repository_rules.md
@@ -234,3 +265,5 @@ However, some are known not to work or work only partially (e.g. mRuby has no bu
0 commit comments