From f86e6a29fdb0997def9ac819f4d87ce75e1a2201 Mon Sep 17 00:00:00 2001 From: Simon Guest Date: Fri, 28 Jun 2024 09:42:38 +1200 Subject: [PATCH] Make WithCwd a bit nicer --- tests/helpers/mod.rs | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/tests/helpers/mod.rs b/tests/helpers/mod.rs index 9566b23..48e5e72 100644 --- a/tests/helpers/mod.rs +++ b/tests/helpers/mod.rs @@ -33,34 +33,41 @@ pub fn root_dir() -> PathBuf { #[derive(Debug)] pub struct WithCwd { + cwd: PathBuf, mutex: Mutex<()>, } impl WithCwd { - fn new() -> WithCwd { + fn new

(path: P) -> WithCwd + where + P: AsRef, + { WithCwd { + cwd: path.as_ref().to_path_buf(), mutex: Mutex::new(()), } } - /// run the closure with cwd set to `cwd` - pub fn run(&self, cwd: P, f: F, arg: T) -> R + /// run the closure with our cwd + pub fn run(&self, f: F, arg: T) -> R where - P: AsRef, F: Fn(T) -> R, { let _guard = match self.mutex.lock() { Ok(guard) => guard, Err(poisoned) => poisoned.into_inner(), }; - set_current_dir(cwd.as_ref()).unwrap(); + set_current_dir(&self.cwd).unwrap(); f(arg) } } -pub fn with_cwd() -> &'static WithCwd { +pub fn with_cwd

(cwd: P) -> &'static WithCwd +where + P: AsRef, +{ static CWD: OnceLock = OnceLock::new(); - CWD.get_or_init(WithCwd::new) + CWD.get_or_init(|| WithCwd::new(cwd)) } #[derive(Debug)] @@ -241,8 +248,7 @@ pub fn check_path_ok( farm.print(); // test with relative paths - with_cwd().run( - farm.absolute(override_cwd.unwrap_or(".")), + with_cwd(farm.absolute(override_cwd.unwrap_or("."))).run( |path| { let actual = f(path); is_expected_or_alt_path_ok(path, actual, expected, None, true); @@ -255,8 +261,7 @@ pub fn check_path_ok( let abs_path = farm.absolute(Path::new(override_cwd.unwrap_or("")).join(path)); let abs_expected = farm.absolute(expected); let cwd = tempdir().unwrap(); - with_cwd().run( - cwd.path(), + with_cwd(cwd.path()).run( |path| { let actual = f(path); // if we ascended out of the farm rootdir it's not straightforward to verify the logical path @@ -400,7 +405,7 @@ where farm.print(); // test with relative paths - let actual = with_cwd().run(farm.absolute("."), f, path); + let actual = with_cwd(farm.absolute(".")).run(f, path); if actual.is_ok() { panic!("expected error but f({}) succeeded", path.to_string_lossy()) @@ -418,8 +423,7 @@ where farm.print(); // test with relative paths - with_cwd().run( - farm.absolute("."), + with_cwd(farm.absolute(".")).run( |path| { let actual = path.is_real_root(); is_expected_ok(path, actual, expected); @@ -430,8 +434,7 @@ where // test with absolute paths let abs_path = farm.absolute(path); let cwd = tempdir().unwrap(); - with_cwd().run( - cwd.path(), + with_cwd(cwd.path()).run( |path| { let actual = path.is_real_root(); is_expected_ok(abs_path.as_path(), actual, expected); @@ -451,8 +454,7 @@ where let path: &Path = path.as_ref(); // test with relative paths - with_cwd().run( - cwd, + with_cwd(cwd).run( |path| { let actual = path.is_real_root(); is_expected_ok(path, actual, expected);