-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathnode_fs.rs
More file actions
104 lines (93 loc) · 3.3 KB
/
node_fs.rs
File metadata and controls
104 lines (93 loc) · 3.3 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
mod test_utils;
use std::{
env::{current_dir, vars_os},
ffi::OsStr,
};
use fspy::{AccessMode, PathAccessIterable};
use test_log::test;
use test_utils::assert_contains;
async fn track_node_script(script: &str, args: &[&OsStr]) -> anyhow::Result<PathAccessIterable> {
let mut command = fspy::Command::new("node");
command
.arg("-e")
.envs(vars_os()) // https://github.com/jdx/mise/discussions/5968
.arg(script)
.args(args);
let child = command.spawn(tokio_util::sync::CancellationToken::new()).await?;
let termination = child.wait_handle.await?;
assert!(termination.status.success());
Ok(termination.path_accesses)
}
#[test(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(())
}
#[test(tokio::test)]
async fn exist_sync() -> anyhow::Result<()> {
let accesses = track_node_script("try { fs.existsSync('hello') } catch {}", &[]).await?;
assert_contains(&accesses, current_dir().unwrap().join("hello").as_path(), AccessMode::READ);
Ok(())
}
#[test(tokio::test)]
async fn stat_sync() -> anyhow::Result<()> {
let accesses = track_node_script("try { fs.statSync('hello') } catch {}", &[]).await?;
assert_contains(&accesses, current_dir().unwrap().join("hello").as_path(), AccessMode::READ);
Ok(())
}
#[test(tokio::test)]
async fn create_read_stream() -> anyhow::Result<()> {
let accesses = track_node_script(
"try { fs.createReadStream('hello').on('error', () => {}) } catch {}",
&[],
)
.await?;
assert_contains(&accesses, current_dir().unwrap().join("hello").as_path(), AccessMode::READ);
Ok(())
}
#[test(tokio::test)]
async fn create_write_stream() -> anyhow::Result<()> {
let tmpdir = tempfile::tempdir()?;
let file_path = tmpdir.path().join("hello");
let accesses = track_node_script(
"try { fs.createWriteStream(process.argv[1]).on('error', () => {}) } catch {}",
&[file_path.as_os_str()],
)
.await?;
assert_contains(&accesses, file_path.as_path(), AccessMode::WRITE);
Ok(())
}
#[test(tokio::test)]
async fn write_sync() -> anyhow::Result<()> {
let tmpdir = tempfile::tempdir()?;
let file_path = tmpdir.path().join("hello");
let accesses = track_node_script(
"try { fs.writeFileSync(process.argv[1], '') } catch {}",
&[file_path.as_os_str()],
)
.await?;
assert_contains(&accesses, &file_path, AccessMode::WRITE);
Ok(())
}
#[test(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::READ_DIR);
Ok(())
}
#[test(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(())
}