-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest_utils.rs
More file actions
56 lines (51 loc) · 1.63 KB
/
test_utils.rs
File metadata and controls
56 lines (51 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::path::{Path, PathBuf, StripPrefixError};
use fspy::{AccessMode, PathAccessIterable};
#[doc(hidden)]
#[allow(unused)]
pub use fspy_test_utils::command_executing;
#[track_caller]
pub fn assert_contains(
accesses: &PathAccessIterable,
expected_path: &Path,
expected_mode: AccessMode,
) {
let found = accesses.iter().any(|access| {
let Ok(stripped) =
access.path.strip_path_prefix::<_, Result<PathBuf, StripPrefixError>, _>(
expected_path,
|strip_result| strip_result.map(Path::to_path_buf),
)
else {
return false;
};
stripped.as_os_str().is_empty() && access.mode == expected_mode
});
if !found {
panic!(
"Expected to find access to path {:?} with mode {:?}, but it was not found in: {:?}",
expected_path,
expected_mode,
accesses.iter().collect::<Vec<_>>()
);
}
}
#[macro_export]
macro_rules! track_child {
($body: block) => {{
let std_cmd = $crate::test_utils::command_executing!((), |(): ()| {
let _ = $body;
});
$crate::test_utils::spawn_std(std_cmd)
}};
}
#[doc(hidden)]
#[allow(unused)]
pub async fn spawn_std(std_cmd: std::process::Command) -> anyhow::Result<PathAccessIterable> {
let mut command = fspy::Command::new(std_cmd.get_program());
command
.args(std_cmd.get_args())
.envs(std_cmd.get_envs().filter_map(|(name, value)| Some((name, value?))));
let termination = command.spawn().await?.wait_handle.await?;
assert!(termination.status.success());
Ok(termination.path_accesses)
}