Skip to content

Commit

Permalink
Tests with cwd is root_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
tesujimath committed Jun 25, 2024
1 parent 63f6b14 commit ee08d28
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
40 changes: 33 additions & 7 deletions tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ impl LinkFarm {
self.cwd.set_during(self.tempdir.path(), f, arg)
}

/// run the closure somewhere other than the link farm
fn run_without<T, R, F>(&self, f: F, arg: T) -> R
/// run the closure with the specified cwd
fn run_with_cwd<T, R, F, P>(&self, f: F, arg: T, cwd: P) -> R
where
F: Fn(T) -> R,
P: AsRef<Path>,
{
let other_dir = tempdir().unwrap();
self.cwd.set_during(other_dir.path(), f, arg)
self.cwd.set_during(cwd.as_ref(), f, arg)
}

/// dump the link farm as a diagnostic
Expand Down Expand Up @@ -232,7 +232,8 @@ where
// test with absolute paths
let abs_path = farm.absolute(path);
let abs_expected = farm.absolute(expected);
farm.run_without(
let other_dir = tempdir().unwrap();
farm.run_with_cwd(
|path| {
let actual = path.real_parent();
// if we ascended out of the farm rootdir it's not straightforward to verify the logical path
Expand All @@ -247,6 +248,7 @@ where
);
},
abs_path.as_path(),
other_dir.path(),
);

test_real_parent_with_unc_path(farm, &abs_path, &abs_expected);
Expand Down Expand Up @@ -380,7 +382,7 @@ where
}
}

// check is_real_root() succeeds with false, with both absolute and relative paths
// check is_real_root() succeeds with expected, with both absolute and relative paths
pub fn check_is_real_root_ok<P>(farm: &LinkFarm, path: P, expected: bool)
where
P: AsRef<Path> + Debug,
Expand All @@ -401,17 +403,41 @@ where

// test with absolute paths
let abs_path = farm.absolute(path);
farm.run_without(
let other_dir = tempdir().unwrap();
farm.run_with_cwd(
|path| {
let actual = path.is_real_root();
is_expected_ok(abs_path.as_path(), actual, expected);
},
abs_path.as_path(),
other_dir.path(),
);

test_is_real_root_with_unc_path(farm, &abs_path, expected);
}

// check is_real_root() succeeds with expected, with both absolute and relative paths
pub fn check_is_real_root_in_cwd_ok<P1, P2>(cwd: P1, path: P2, expected: bool)
where
P1: AsRef<Path> + Debug,
P2: AsRef<Path> + Debug,
{
// for the mutual exclusion only:
let farm = LinkFarm::new();

let path: &Path = path.as_ref();

// test with relative paths
farm.run_with_cwd(
|path| {
let actual = path.is_real_root();
is_expected_ok(path, actual, expected);
},
path,
cwd,
);
}

#[cfg(target_family = "windows")]
fn test_is_real_root_with_unc_path<P>(farm: &LinkFarm, abs_path: P, expected: bool)
where
Expand Down
14 changes: 12 additions & 2 deletions tests/path_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,21 @@ fn test_real_parent_symlink_cycle_look_alikes(path: &str, expected: &str) {

#[test]
fn test_is_real_root_root_dir() {
let path = root_dir();
let actual = path.as_path().is_real_root().unwrap();
let root_dir = root_dir();

let actual = root_dir.as_path().is_real_root().unwrap();
assert!(actual);
}

#[test_case(""; "empty")]
#[test_case("."; "dot")]
#[test_case(".."; "dotdot")]
fn test_is_real_root_in_root_dir(path: &str) {
let root_dir = root_dir();

check_is_real_root_in_cwd_ok(root_dir.as_path(), path, true);
}

#[test_case("x1")]
#[test_case("A")]
#[test_case("A/a1")]
Expand Down

0 comments on commit ee08d28

Please sign in to comment.