File tree 3 files changed +32
-5
lines changed
3 files changed +32
-5
lines changed Original file line number Diff line number Diff line change @@ -10,13 +10,22 @@ rust_binary(
10
10
edition = "2018" ,
11
11
)
12
12
13
+ filegroup (
14
+ name = "hello_world_main" ,
15
+ srcs = ["src/main.rs" ],
16
+ )
17
+
13
18
rust_test (
14
19
name = "test" ,
15
20
srcs = ["tests/run.rs" ],
16
- data = [":hello-world" ],
21
+ data = [
22
+ ":hello-world" ,
23
+ ":hello_world_main" ,
24
+ ],
17
25
edition = "2018" ,
18
26
env = {
19
27
"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)" ,
21
30
},
22
31
)
Original file line number Diff line number Diff line change @@ -6,5 +6,18 @@ fn run() {
6
6
7
7
// Test the `env` attribute of `rust_test` at run time
8
8
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( ) ) ;
10
23
}
Original file line number Diff line number Diff line change @@ -21,6 +21,11 @@ fn environ() -> BTreeMap<String, String> {
21
21
let env_path = std:: env:: args ( ) . nth ( 0 ) . expect ( "arg 0 was not set" ) + LAUNCHFILES_ENV_PATH ;
22
22
let file = File :: open ( env_path) . expect ( "Failed to load the environment file" ) ;
23
23
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
+
24
29
// Find all environment variables by reading pairs of lines as key/value pairs
25
30
for line in BufReader :: new ( file) . lines ( ) {
26
31
if key. is_none ( ) {
@@ -29,8 +34,8 @@ fn environ() -> BTreeMap<String, String> {
29
34
}
30
35
31
36
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 ) ,
34
39
) ;
35
40
36
41
key = None ;
You can’t perform that action at this time.
0 commit comments