From 50460842f48b461a53529814078e20efc9dfda32 Mon Sep 17 00:00:00 2001 From: cloudhead Date: Thu, 4 Jan 2024 13:42:05 +0100 Subject: [PATCH] cli-test: Expand env vars in commands --- radicle-cli-test/src/lib.rs | 26 +++++++++++++++++++------- radicle/src/git.rs | 2 +- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/radicle-cli-test/src/lib.rs b/radicle-cli-test/src/lib.rs index 65e8d1c0..2af43454 100644 --- a/radicle-cli-test/src/lib.rs +++ b/radicle-cli-test/src/lib.rs @@ -85,8 +85,16 @@ impl TestRun { self.home.path = path; } - fn envs(&self) -> impl Iterator { - self.home.envs.iter().chain(self.env.iter()) + fn envs(&self) -> impl Iterator + '_ { + 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 { @@ -403,6 +411,13 @@ impl TestFormula { // For each command. 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 .path .file_name() @@ -411,10 +426,7 @@ impl TestFormula { let cmd = if assertion.command == "rad" { snapbox::cmd::cargo_bin("rad") } else if assertion.command == "cd" { - let mut arg = assertion.args.first().unwrap().clone(); - for (k, v) in run.envs() { - arg = arg.replace(&format!("${k}"), v); - } + let arg = assertion.args.first().unwrap(); let dir: PathBuf = arg.into(); let dir = run.path().join(dir); @@ -454,7 +466,7 @@ impl TestFormula { .env("RUST_BACKTRACE", "1") .envs(run.envs()) .current_dir(run.path()) - .args(&assertion.args) + .args(args) .with_assert(assert.clone()) .output(); diff --git a/radicle/src/git.rs b/radicle/src/git.rs index bde9fb74..1a102db5 100644 --- a/radicle/src/git.rs +++ b/radicle/src/git.rs @@ -584,7 +584,7 @@ pub fn push<'a>( ) -> Result<(), git2::Error> { let refspecs = refspecs .into_iter() - .map(|(src, dst)| format!("{}:{}", src.as_str(), dst.as_str())); + .map(|(src, dst)| format!("{src}:{dst}")); repo.find_remote(remote)? .push(refspecs.collect::>().as_slice(), None)?;