cli-test: Expand env vars in commands

This commit is contained in:
cloudhead 2024-01-04 13:42:05 +01:00
parent 2d71093608
commit 50460842f4
No known key found for this signature in database
2 changed files with 20 additions and 8 deletions

View File

@ -85,8 +85,16 @@ impl TestRun {
self.home.path = path; self.home.path = path;
} }
fn envs(&self) -> impl Iterator<Item = (&String, &String)> { fn envs(&self) -> impl Iterator<Item = (String, String)> + '_ {
self.home.envs.iter().chain(self.env.iter()) self.home
.envs
.iter()
.chain(self.env.iter())
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.chain(Some((
"PWD".to_owned(),
self.home.path.to_string_lossy().to_string(),
)))
} }
fn path(&self) -> PathBuf { fn path(&self) -> PathBuf {
@ -403,6 +411,13 @@ impl TestFormula {
// For each command. // For each command.
for assertion in &test.assertions { for assertion in &test.assertions {
// Expand environment variables.
let mut args = assertion.args.clone();
for arg in &mut args {
for (k, v) in run.envs() {
*arg = arg.replace(format!("${k}").as_str(), &v);
}
}
let path = assertion let path = assertion
.path .path
.file_name() .file_name()
@ -411,10 +426,7 @@ impl TestFormula {
let cmd = if assertion.command == "rad" { let cmd = if assertion.command == "rad" {
snapbox::cmd::cargo_bin("rad") snapbox::cmd::cargo_bin("rad")
} else if assertion.command == "cd" { } else if assertion.command == "cd" {
let mut arg = assertion.args.first().unwrap().clone(); let arg = assertion.args.first().unwrap();
for (k, v) in run.envs() {
arg = arg.replace(&format!("${k}"), v);
}
let dir: PathBuf = arg.into(); let dir: PathBuf = arg.into();
let dir = run.path().join(dir); let dir = run.path().join(dir);
@ -454,7 +466,7 @@ impl TestFormula {
.env("RUST_BACKTRACE", "1") .env("RUST_BACKTRACE", "1")
.envs(run.envs()) .envs(run.envs())
.current_dir(run.path()) .current_dir(run.path())
.args(&assertion.args) .args(args)
.with_assert(assert.clone()) .with_assert(assert.clone())
.output(); .output();

View File

@ -584,7 +584,7 @@ pub fn push<'a>(
) -> Result<(), git2::Error> { ) -> Result<(), git2::Error> {
let refspecs = refspecs let refspecs = refspecs
.into_iter() .into_iter()
.map(|(src, dst)| format!("{}:{}", src.as_str(), dst.as_str())); .map(|(src, dst)| format!("{src}:{dst}"));
repo.find_remote(remote)? repo.find_remote(remote)?
.push(refspecs.collect::<Vec<_>>().as_slice(), None)?; .push(refspecs.collect::<Vec<_>>().as_slice(), None)?;