Skip to content

Commit

Permalink
Added testing for CARGO_ENCODED_RUSTFLAGS
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Mar 4, 2022
1 parent 9d56b7b commit 2e8ab9c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cargo/cargo_build_script_runner/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ fn run_buildrs() -> Result<(), String> {
// Bazel does not support byte strings so in order to correctly represent `CARGO_ENCODED_RUSTFLAGS`
// the escaped `\x1f` sequences need to be unescaped
if let Ok(encoded_rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") {
command.env("CARGO_ENCODED_RUSTFLAGS", encoded_rustflags.replace("\\x1f", "\x1f"));
command.env(
"CARGO_ENCODED_RUSTFLAGS",
encoded_rustflags.replace("\\x1f", "\x1f"),
);
}

if let Some(ar_path) = env::var_os("AR") {
Expand Down
2 changes: 2 additions & 0 deletions test/cargo_build_script/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ cargo_build_script(
name = "tools_exec_build_rs",
srcs = ["build.rs"],
build_script_env = {"TOOL": "$(execpath :tool)"},
# Add a flag to test that they're exposed to the build script
rustc_flags = ["--verbose"],
tools = [":tool"],
)

Expand Down
20 changes: 20 additions & 0 deletions test/cargo_build_script/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
//! A Cargo build script binary used in unit tests for the Bazel `cargo_build_script` rule

/// `cargo_build_script` should always set `CARGO_ENCODED_RUSTFLAGS`
fn test_encoded_rustflags() {
let encoded_rustflags = std::env::var("CARGO_ENCODED_RUSTFLAGS").unwrap();

let flags: Vec<String> = encoded_rustflags
.split('\x1f')
.map(str::to_string)
.collect();
assert_eq!(flags.len(), 2);

assert!(flags[0].starts_with("--sysroot"));
assert!(flags[1].starts_with("--verbose"));
}

fn main() {

// Perform some unit testing
test_encoded_rustflags();

// Pass the TOOL_PATH along to the rust_test so we can assert on it.
println!(
"cargo:rustc-env=TOOL_PATH={}",
Expand Down

0 comments on commit 2e8ab9c

Please sign in to comment.