-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathnode_fs.rs
More file actions
47 lines (41 loc) · 1.5 KB
/
node_fs.rs
File metadata and controls
47 lines (41 loc) · 1.5 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
mod test_utils;
use std::env::{current_dir, vars_os};
use fspy::{AccessMode, PathAccessIterable};
use test_utils::assert_contains;
async fn track_node_script(script: &str) -> anyhow::Result<PathAccessIterable> {
let mut command = fspy::Spy::global()?.new_command("node");
command
.arg("-e")
.envs(vars_os()) // https://github.com/jdx/mise/discussions/5968
.arg(script);
let child = command.spawn().await?;
let termination = child.wait_handle.await?;
assert!(termination.status.success());
Ok(termination.path_accesses)
}
#[tokio::test]
async fn read_sync() -> anyhow::Result<()> {
let accesses = track_node_script("try { fs.readFileSync('hello') } catch {}").await?;
assert_contains(&accesses, current_dir().unwrap().join("hello").as_path(), AccessMode::Read);
Ok(())
}
#[tokio::test]
async fn read_dir_sync() -> anyhow::Result<()> {
let accesses = track_node_script("try { fs.readdirSync('.') } catch {}").await?;
assert_contains(&accesses, ¤t_dir().unwrap(), AccessMode::ReadDir);
Ok(())
}
#[tokio::test]
async fn subprocess() -> anyhow::Result<()> {
let cmd = if cfg!(windows) {
r"'cmd', ['/c', 'type hello']"
} else {
r"'/bin/sh', ['-c', 'cat hello']"
};
let accesses = track_node_script(&format!(
"try {{ child_process.spawnSync({cmd}, {{ stdio: 'ignore' }}) }} catch {{}}"
))
.await?;
assert_contains(&accesses, current_dir().unwrap().join("hello").as_path(), AccessMode::Read);
Ok(())
}