Skip to content

Commit 14eb6f3

Browse files
authored
Fixed expanded locations in test launcher (#588)
In #579 I had intended to take advantage of the same behavior `cargo_build_script` was for `expand_location` but forgot to implement the counterpart in the `launcher` target. This fixes that and updates tests to check this behavior.
1 parent 7b83ef7 commit 14eb6f3

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

test/test_env/BUILD

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@ rust_binary(
1010
edition = "2018",
1111
)
1212

13+
filegroup(
14+
name = "hello_world_main",
15+
srcs = ["src/main.rs"],
16+
)
17+
1318
rust_test(
1419
name = "test",
1520
srcs = ["tests/run.rs"],
16-
data = [":hello-world"],
21+
data = [
22+
":hello-world",
23+
":hello_world_main",
24+
],
1725
edition = "2018",
1826
env = {
1927
"FERRIS_SAYS": "Hello fellow Rustaceans!",
20-
"HELLO_WORLD_BIN": "$(rootpath :hello-world)",
28+
"HELLO_WORLD_BIN_ROOTPATH": "$(rootpath :hello-world)",
29+
"HELLO_WORLD_SRC_EXECPATH": "$(execpath :hello_world_main)",
2130
},
2231
)

test/test_env/tests/run.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,18 @@ fn run() {
66

77
// Test the `env` attribute of `rust_test` at run time
88
assert_eq!(std::env::var("FERRIS_SAYS").unwrap(), "Hello fellow Rustaceans!");
9-
assert_eq!(std::env::var("HELLO_WORLD_BIN").unwrap(), "test/test_env/hello-world");
9+
10+
// Test the behavior of `rootpath` and that a binary can be found relative to current_dir
11+
let hello_world_bin = std::path::PathBuf::from(std::env::var_os("HELLO_WORLD_BIN_ROOTPATH").unwrap());
12+
assert_eq!(
13+
hello_world_bin.as_path(),
14+
std::path::Path::new("test/test_env/hello-world"),
15+
);
16+
assert!(!hello_world_bin.is_absolute());
17+
assert!(hello_world_bin.exists());
18+
19+
// Ensure `execpath` expanded variables map to real files and have absolute paths
20+
let hello_world_src = std::path::PathBuf::from(std::env::var("HELLO_WORLD_SRC_EXECPATH").unwrap());
21+
assert!(hello_world_src.is_absolute());
22+
assert!(hello_world_src.exists());
1023
}

util/launcher/launcher_main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ fn environ() -> BTreeMap<String, String> {
2121
let env_path = std::env::args().nth(0).expect("arg 0 was not set") + LAUNCHFILES_ENV_PATH;
2222
let file = File::open(env_path).expect("Failed to load the environment file");
2323

24+
// Variables will have the `${pwd}` variable replaced which is rendered by
25+
// `@rules_rust//rust/private:util.bzl::expand_locations`
26+
let pwd = std::env::current_dir().expect("Failed to get current working directory");
27+
let pwd_str = pwd.to_string_lossy();
28+
2429
// Find all environment variables by reading pairs of lines as key/value pairs
2530
for line in BufReader::new(file).lines() {
2631
if key.is_none() {
@@ -29,8 +34,8 @@ fn environ() -> BTreeMap<String, String> {
2934
}
3035

3136
environ.insert(
32-
key.expect("Key is not set"),
33-
line.expect("Failed to read line"),
37+
key.expect("Key is not set"),
38+
line.expect("Failed to read line").replace("${pwd}", &pwd_str),
3439
);
3540

3641
key = None;

0 commit comments

Comments
 (0)