cli: Make sure envs don't bleed

Previously, if you set an env var in one test, it could also appear in a
later test. This ensures we don't persist envs between tests.
This commit is contained in:
cloudhead 2023-11-22 15:46:55 +01:00
parent 725660ed51
commit 7acd025d7f
No known key found for this signature in database
2 changed files with 17 additions and 19 deletions

View File

@ -77,6 +77,7 @@ pub struct Home {
#[derive(Debug)] #[derive(Debug)]
pub struct TestRun { pub struct TestRun {
home: Home, home: Home,
env: HashMap<String, String>,
} }
impl TestRun { impl TestRun {
@ -85,7 +86,7 @@ impl TestRun {
} }
fn envs(&self) -> impl Iterator<Item = (&String, &String)> { fn envs(&self) -> impl Iterator<Item = (&String, &String)> {
self.home.envs.iter() self.home.envs.iter().chain(self.env.iter())
} }
fn path(&self) -> PathBuf { fn path(&self) -> PathBuf {
@ -110,31 +111,26 @@ impl<'a> TestRunner<'a> {
} }
fn run(&mut self, test: &'a Test) -> TestRun { fn run(&mut self, test: &'a Test) -> TestRun {
let mut envs = self.formula.env.clone(); let mut env = self.formula.env.clone();
env.extend(test.env.clone());
if let Some(ref h) = test.home { if let Some(ref h) = test.home {
if let Some(home) = self.homes.get(h) { if let Some(home) = self.homes.get(h) {
envs.extend(home.envs.clone()); return TestRun {
envs.extend(test.env.clone()); home: home.clone(),
env,
let home = Home {
name: home.name.clone(),
path: home.path.clone(),
envs,
}; };
return TestRun { home };
} else { } else {
panic!("TestRunner::test: home `~{h}` does not exist"); panic!("TestRunner::test: home `~{h}` does not exist");
} }
} }
envs.extend(test.env.clone());
TestRun { TestRun {
home: Home { home: Home {
name: None, name: None,
path: self.cwd.clone().unwrap_or_else(|| self.formula.cwd.clone()), path: self.cwd.clone().unwrap_or_else(|| self.formula.cwd.clone()),
envs, envs: HashMap::new(),
}, },
env,
} }
} }
@ -504,7 +500,7 @@ mod tests {
fn test_parse() { fn test_parse() {
let input = r#" let input = r#"
Let's try to track @dave and @sean: Let's try to track @dave and @sean:
``` ``` RAD_HINT=true
$ rad track @dave $ rad track @dave
Tracking relationship established for @dave. Tracking relationship established for @dave.
Nothing to do. Nothing to do.
@ -514,7 +510,7 @@ Tracking relationship established for @sean.
Nothing to do. Nothing to do.
``` ```
Super, now let's move on to the next step. Super, now let's move on to the next step.
``` ``` ~alice (stderr)
$ rad sync $ rad sync
``` ```
"# "#
@ -564,11 +560,13 @@ $ rad sync
], ],
fail: false, fail: false,
stderr: false, stderr: false,
env: HashMap::default(), env: vec![("RAD_HINT".to_owned(), "true".to_owned())]
.into_iter()
.collect(),
}, },
Test { Test {
context: vec![String::from("Super, now let's move on to the next step.")], context: vec![String::from("Super, now let's move on to the next step.")],
home: None, home: Some("alice".to_owned()),
assertions: vec![Assertion { assertions: vec![Assertion {
path: path.clone(), path: path.clone(),
command: String::from("rad"), command: String::from("rad"),
@ -577,7 +575,7 @@ $ rad sync
exit: ExitStatus::Success, exit: ExitStatus::Success,
}], }],
fail: false, fail: false,
stderr: false, stderr: true,
env: HashMap::default(), env: HashMap::default(),
}, },
], ],

View File

@ -77,7 +77,7 @@ $ git push -f
We have to use the `allow.rollback` option: We have to use the `allow.rollback` option:
``` ~alice (stderr) ``` ~alice RAD_SOCKET=/dev/null (stderr)
$ git push -f -o allow.rollback $ git push -f -o allow.rollback
To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
+ f6cff86...319a7dc master -> master (forced update) + f6cff86...319a7dc master -> master (forced update)