From 5f1c1efc6f560c2cb3ba4acc62cd867c038391e3 Mon Sep 17 00:00:00 2001 From: Troy Benson Date: Wed, 2 Jul 2025 18:40:48 +0000 Subject: [PATCH 1/2] coverage binary override Allow for the binary that the `//util/collect_coverage` to be overridden. The usecase for this is because i have written a custom extension rule ``` nextest_test = rule( doc = """A test rule that runs tests using a custom test runner. Args: binary: The test binary to run crate_name: The name of the crate this test is for env: Additional environment variables data: Runtime dependencies for the test workspace_root: Optional workspace root setting """, implementation = _nextest_test_impl, parent = rust_test, attrs = { "workspace_root": attr.label(mandatory = False), "_profile": attr.label(mandatory = False, default = "//:test_profile"), "_test_runner": attr.label( default = "//dev-tools/test-runner", executable = True, cfg = "exec" ), "_nextest_config": attr.label( default = "//:.config/nextest.toml", allow_single_file = True ), "_windows_constraint": attr.label( default = "@platforms//os:windows" ), }, ) ``` That extends the `rust_test` rule and then adds a wrapper script to invoke the test via a custom runner. Due to that the `TEST_BINARY` env variable points to the wrapper and not the `rust_test` binary. --- util/collect_coverage/collect_coverage.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/util/collect_coverage/collect_coverage.rs b/util/collect_coverage/collect_coverage.rs index 7aba667885..c528ad3010 100644 --- a/util/collect_coverage/collect_coverage.rs +++ b/util/collect_coverage/collect_coverage.rs @@ -14,6 +14,7 @@ //! - `ROOT`: Location from where the code coverage collection was invoked. //! - `RUNFILES_DIR`: Location of the test's runfiles. //! - `VERBOSE_COVERAGE`: Print debug info from the coverage scripts +//! - `COVERAGE_BINARY`: The binary that should be used for coverage (optional by default uses `TEST_BINARY`) //! //! The script looks in $COVERAGE_DIR for the Rust metadata coverage files //! (profraw) and uses lcov to get the coverage data. The coverage data @@ -47,9 +48,11 @@ fn find_metadata_file(execroot: &Path, runfiles_dir: &Path, path: &str) -> PathB } fn find_test_binary(execroot: &Path, runfiles_dir: &Path) -> PathBuf { + let bin = env::var("COVERAGE_BINARY").unwrap_or_else(|_| env::var("TEST_BINARY").unwrap()); + let test_binary = runfiles_dir .join(env::var("TEST_WORKSPACE").unwrap()) - .join(env::var("TEST_BINARY").unwrap()); + .join(&bin); if !test_binary.exists() { let configuration = runfiles_dir @@ -72,7 +75,7 @@ fn find_test_binary(execroot: &Path, runfiles_dir: &Path) -> PathBuf { let test_binary = execroot .join(configuration) - .join(env::var("TEST_BINARY").unwrap()); + .join(&bin); debug_log!( "TEST_BINARY is not found in runfiles. Falling back to: {}", From 1eaf26d171ebcdb59bf54519cddb7ccbe1d14196 Mon Sep 17 00:00:00 2001 From: Troy Benson Date: Thu, 3 Jul 2025 11:37:35 +0000 Subject: [PATCH 2/2] fix fmt --- util/collect_coverage/collect_coverage.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/util/collect_coverage/collect_coverage.rs b/util/collect_coverage/collect_coverage.rs index c528ad3010..4052fe407c 100644 --- a/util/collect_coverage/collect_coverage.rs +++ b/util/collect_coverage/collect_coverage.rs @@ -73,9 +73,7 @@ fn find_test_binary(execroot: &Path, runfiles_dir: &Path) -> PathBuf { path }); - let test_binary = execroot - .join(configuration) - .join(&bin); + let test_binary = execroot.join(configuration).join(&bin); debug_log!( "TEST_BINARY is not found in runfiles. Falling back to: {}",