Skip to content

Commit d66363a

Browse files
committed
Update container_shim_test to set PATH via export and envs
1 parent e366ff3 commit d66363a

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

tests/fixtures/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ pub struct ContainerPathShim {
1919
impl ContainerPathShim {
2020
pub const BIN_SHIMS: [&str; 3] = ["docker", "singularity", "apptainer"];
2121

22-
23-
2422
pub fn new() -> Self {
25-
let root = std::path::PathBuf::from("target/tmp");
26-
eprintln!("root:{root:?}");
23+
let root = std::path::PathBuf::from("target/fixtures");
24+
std::fs::create_dir_all(&root).expect("create fixtures dir");
25+
//eprintln!("root:{root:?}");
2726
let dir = TempDir::new_in(root).expect("create temp dir");
2827

2928
// add a label directory inside for readability
@@ -61,13 +60,15 @@ impl ContainerPathShim {
6160
/// Example use in a test:
6261
/// `let cmd = format!("{} && my-runner ...", shim.export_path_cmd());`
6362
pub fn export_path_cmd(&self) -> String {
64-
format!("export PATH='{}'", self.bin_dir().display())
63+
format!("export PATH={}:$PATH", self.bin_dir().display())
6564
}
6665

6766
/// Returns env overrides you can pass to `std::process::Command` directly.
6867
/// This mirrors what `export_path_cmd()` would do in a shell.
6968
pub fn env_overrides(&self) -> [(&'static str, String); 1] {
70-
[("PATH", self.bin_dir().display().to_string())]
69+
let current_path = std::env::var("PATH").unwrap_or_default();
70+
let new_path = format!("{}:{}", self.bin_dir().display(), current_path);
71+
[("PATH", new_path)]
7172
}
7273
}
7374

tests/main.rs

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
mod fixtures;
22

3-
use std::thread::sleep;
4-
53
use assert_cmd::cargo::cargo_bin_cmd;
64
use assert_cmd::prelude::*;
75
use predicates::prelude::*;
@@ -32,32 +30,46 @@ fn no_run_no_app() {
3230
}
3331

3432
#[test]
35-
fn shim_test() {
36-
let s = fixtures::ContainerPathShim::new();
37-
let bin = s.install_all();
33+
fn container_shim_test() {
34+
let fixture = fixtures::ContainerPathShim::new();
35+
let bin = fixture.install_all();
3836

3937
// Call each shim with dummy arguments and check the invocation logs
4038
for shim_name in fixtures::ContainerPathShim::BIN_SHIMS {
4139
let log_file = bin.join(format!("{}.log", shim_name));
4240
let shim_path = bin.join(shim_name);
4341

44-
std::process::Command::new(&shim_path)
45-
.args(["arg1", "arg2", "--flag"])
46-
.env("TEST_INVOCATIONS_LOG", &log_file)
47-
.output()
48-
.expect("Failed to execute shim");
49-
50-
// Read the log file and verify it contains the expected command line
51-
let log_contents = std::fs::read_to_string(&log_file).expect("Failed to read log file");
52-
53-
let expected = format!("{} arg1 arg2 --flag", shim_path.display());
54-
assert!(
55-
log_contents.trim() == expected,
56-
"Log file for {} should contain '{}', but got '{}'",
57-
shim_name,
58-
expected,
59-
log_contents.trim()
60-
);
42+
for cmd in [
43+
std::process::Command::new("sh")
44+
.args([
45+
"-c",
46+
&format!(
47+
"{} && {shim_name} arg1 arg2 --flag",
48+
fixture.export_path_cmd()
49+
),
50+
])
51+
.env("TEST_INVOCATIONS_LOG", &log_file),
52+
std::process::Command::new(shim_name)
53+
.args(["arg1", "arg2", "--flag"])
54+
.envs(fixture.env_overrides())
55+
.env("TEST_INVOCATIONS_LOG", &log_file),
56+
] {
57+
let _r = cmd.output().expect("Failed to execute shim");
58+
// eprintln!("{_r:?}");
59+
60+
// Read the log file and verify it contains the expected command line
61+
let log_contents = std::fs::read_to_string(&log_file).expect("Failed to read log file");
62+
63+
let expected = format!("{} arg1 arg2 --flag", shim_path.display());
64+
assert!(
65+
log_contents.trim() == expected,
66+
"Log file for {} should contain '{}', but got '{}'",
67+
shim_name,
68+
expected,
69+
log_contents.trim()
70+
);
71+
std::fs::remove_file(&log_file).expect("Failed to delete log file");
72+
}
6173
}
62-
//sleep(std::time::Duration::from_secs(60));
74+
//std::thread::sleep(std::time::Duration::from_secs(60));
6375
}

0 commit comments

Comments
 (0)