-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rust: add rust_test_binary rule (#316)
I have a case where I would like to build a rust binary exactly as output by `rust_test`, but not call it directly. Instead, I have a [`sh_test`](https://docs.bazel.build/versions/master/be/shell.html#sh_test) rule that will be the entrypoint for the test, acting as a test runner and setting up environment variables required by the test code. This already works (amazing!), but I don't want to run the binary output by `rust_test` directly, as it will fail without the setup performed in the `sh_test` rule. This happens when running all tests with: ```bash bazel test //... ``` In this PR, I have added the new rule `rust_test_binary` which is simply the definition of `rust_test` with `test = True` removed, and docs updated. If there's a way to make this an argument of the existing `rust_test` rule please let me know, but I couldn't find any examples of how I might set the value of `test` on a rule dynamically.
- Loading branch information
1 parent
60b89d0
commit 619fcab
Showing
5 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
load( | ||
"//rust:rust.bzl", | ||
"rust_test_binary", | ||
) | ||
|
||
# Here we build the rust binary that, when run, will run our test cases. | ||
# However, it requires some other arbitrary setup first. | ||
# | ||
# If this was just `rust_test`, running `bazel test //...` would fail. | ||
rust_test_binary( | ||
name = "rust_test_that_requires_wrapping", | ||
srcs = ["tests/rust_test_that_requires_wrapping.rs"], | ||
) | ||
|
||
# We do our arbitrary setup in another rule such as `sh_test` which can depend | ||
# on `rust_test_binary` in its `data` attribute. | ||
# | ||
# This is a trivial case, but demonstrates that a rust_test_binary output can be | ||
# executed from another test rule. | ||
sh_test( | ||
name = "wrapped_rust_test", | ||
srcs = ["scripts/exec_with_test_env.sh"], | ||
data = [":rust_test_that_requires_wrapping"], | ||
args = ["$(location :rust_test_that_requires_wrapping)"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/sh | ||
|
||
# Run the given binary, setting up a required test environment variable. | ||
|
||
set -ex | ||
|
||
export USER_DEFINED_KEY=USER_DEFINED_VALUE | ||
|
||
"$@" |
11 changes: 11 additions & 0 deletions
11
test/rust_test_binary/tests/rust_test_that_requires_wrapping.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use std::env; | ||
|
||
#[test] | ||
pub fn rust_test_that_requires_wrapping() { | ||
let actual = format!( | ||
"This test requires {} at runtime.", | ||
env::var("USER_DEFINED_KEY").unwrap() | ||
); | ||
let expected = "This test requires USER_DEFINED_VALUE at runtime."; | ||
assert_eq!(actual, expected); | ||
} |