Skip to content

Commit 0f8b08b

Browse files
committed
wasi:[email protected]: Add tests for rename
1 parent d1a979e commit 0f8b08b

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dirs": ["fs-tests.dir"]
3+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use std::process;
2+
extern crate wit_bindgen;
3+
4+
wit_bindgen::generate!({
5+
inline: r"
6+
package test:test;
7+
8+
world test {
9+
include wasi:filesystem/[email protected];
10+
include wasi:cli/[email protected];
11+
}
12+
",
13+
additional_derives: [PartialEq, Eq, Hash, Clone],
14+
// Work around https://github.com/bytecodealliance/wasm-tools/issues/2285.
15+
features:["clocks-timezone"],
16+
generate_all
17+
});
18+
19+
use wasi::filesystem::types::Descriptor;
20+
use wasi::filesystem::types::ErrorCode;
21+
22+
async fn test_rename(dir: &Descriptor) {
23+
// rename-at: async func(old-path: string, new-descriptor: borrow<descriptor>, new-path: string) -> result<_, error-code>;
24+
let mv = |from: &str, to: &str| -> _ { dir.rename_at(from.to_string(), dir, to.to_string()) };
25+
let ln_s = |from: &str, to: &str| -> _ { dir.symlink_at(from.to_string(), to.to_string()) };
26+
27+
mv("a.txt", "a.txt").await.unwrap();
28+
ln_s("a.txt", "c.cleanup").await.unwrap();
29+
mv("a.txt", "c.cleanup").await.unwrap();
30+
assert_eq!(mv("a.txt", "a.txt").await, Err(ErrorCode::NoEntry));
31+
mv("c.cleanup", "a.txt").await.unwrap();
32+
assert_eq!(mv("c.cleanup", "a.txt").await, Err(ErrorCode::NoEntry));
33+
assert_eq!(
34+
mv("does-not-exist.txt", "q.txt").await,
35+
Err(ErrorCode::NoEntry)
36+
);
37+
match mv(".", "q.txt").await {
38+
Err(ErrorCode::Busy) => {}
39+
Err(ErrorCode::Invalid) => {}
40+
Ok(()) => {
41+
panic!("mv . q.txt unexpectedly succeeded");
42+
}
43+
Err(err) => {
44+
panic!("mv . q.txt: unexpected error {}", err);
45+
}
46+
};
47+
mv("a.txt", "c.cleanup").await.unwrap();
48+
assert_eq!(mv("a.txt", "q.txt").await, Err(ErrorCode::NoEntry));
49+
mv("c.cleanup", "a.txt").await.unwrap();
50+
assert_eq!(mv("a.txt", "../q.txt").await, Err(ErrorCode::NotPermitted));
51+
assert_eq!(
52+
mv("a.txt", "parent/q.txt").await,
53+
Err(ErrorCode::NotPermitted)
54+
);
55+
assert_eq!(
56+
mv("a.txt", "/tmp/q.txt").await,
57+
Err(ErrorCode::NotPermitted)
58+
);
59+
}
60+
61+
struct Component;
62+
export!(Component);
63+
impl exports::wasi::cli::run::Guest for Component {
64+
async fn run() -> Result<(), ()> {
65+
match &wasi::filesystem::preopens::get_directories()[..] {
66+
[(dir, dirname)] if dirname == "fs-tests.dir" => {
67+
test_rename(dir).await;
68+
}
69+
[..] => {
70+
eprintln!("usage: run with one open dir named 'fs-tests.dir'");
71+
process::exit(1)
72+
}
73+
};
74+
Ok(())
75+
}
76+
}
77+
78+
fn main() {
79+
unreachable!("main is a stub");
80+
}

0 commit comments

Comments
 (0)