xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/test_env_launcher/tests/run.rs (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1 #[test]
run()2 fn run() {
3     let path = env!("CARGO_BIN_EXE_hello-world");
4     let output = std::process::Command::new(path)
5         .output()
6         .expect("Failed to run process");
7     assert_eq!(&b"Hello world\n"[..], output.stdout.as_slice());
8 
9     // Test the `env` attribute of `rust_test` at run time
10     assert_eq!(
11         std::env::var("FERRIS_SAYS").unwrap(),
12         "Hello fellow Rustaceans!"
13     );
14 
15     // Test the behavior of `rootpath` and that a binary can be found relative to current_dir
16     let hello_world_bin = std::path::PathBuf::from(std::env::var_os("HELLO_WORLD_BIN").unwrap());
17 
18     assert_eq!(
19         hello_world_bin.as_path(),
20         std::path::Path::new(if std::env::consts::OS == "windows" {
21             "test/test_env_launcher/hello-world.exe"
22         } else {
23             "test/test_env_launcher/hello-world"
24         })
25     );
26     assert!(!hello_world_bin.is_absolute());
27     assert!(hello_world_bin.exists());
28 
29     // Ensure `execpath` expanded variables map to real files and have absolute paths
30     let hello_world_src = std::path::PathBuf::from(std::env::var("HELLO_WORLD_SRC").unwrap());
31     assert!(!hello_world_src.is_absolute());
32     assert!(hello_world_src.exists());
33 }
34