Skip to content

Commit a41670e

Browse files
committed
wasi:filesystem@0.3.0-rc-2025-09-16: Add tests for rename
1 parent 194a2fd commit a41670e

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

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: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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/imports@0.3.0-rc-2025-09-16;
10+
include wasi:cli/command@0.3.0-rc-2025-09-16;
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, DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
20+
21+
async fn test_rename(dir: &Descriptor) {
22+
// rename-at: async func(old-path: string, new-descriptor: borrow<descriptor>, new-path: string) -> result<_, error-code>;
23+
dir.create_directory_at("child.cleanup".to_string())
24+
.await
25+
.unwrap();
26+
let child = dir
27+
.open_at(
28+
PathFlags::empty(),
29+
"child.cleanup".to_string(),
30+
OpenFlags::DIRECTORY,
31+
DescriptorFlags::MUTATE_DIRECTORY,
32+
)
33+
.await
34+
.unwrap();
35+
let mv = |from: &str, to: &str| -> _ { dir.rename_at(from.to_string(), dir, to.to_string()) };
36+
let mv_to_child =
37+
|from: &str, to: &str| -> _ { dir.rename_at(from.to_string(), &child, to.to_string()) };
38+
let mv_from_child =
39+
|from: &str, to: &str| -> _ { child.rename_at(from.to_string(), dir, to.to_string()) };
40+
let ln_s = |from: &str, to: &str| -> _ { dir.symlink_at(from.to_string(), to.to_string()) };
41+
42+
mv("a.txt", "a.txt").await.unwrap();
43+
ln_s("a.txt", "c.cleanup").await.unwrap();
44+
mv("a.txt", "c.cleanup").await.unwrap();
45+
assert_eq!(mv("a.txt", "a.txt").await, Err(ErrorCode::NoEntry));
46+
mv("c.cleanup", "a.txt").await.unwrap();
47+
assert_eq!(mv("c.cleanup", "a.txt").await, Err(ErrorCode::NoEntry));
48+
assert_eq!(
49+
mv("does-not-exist.txt", "q.txt").await,
50+
Err(ErrorCode::NoEntry)
51+
);
52+
match mv(".", "q.txt").await {
53+
Err(ErrorCode::Busy | ErrorCode::Invalid | ErrorCode::Access) => {}
54+
Ok(()) => {
55+
panic!("mv . q.txt unexpectedly succeeded");
56+
}
57+
Err(err) => {
58+
panic!("mv . q.txt: unexpected error {}", err);
59+
}
60+
};
61+
mv("a.txt", "c.cleanup").await.unwrap();
62+
assert_eq!(mv("a.txt", "q.txt").await, Err(ErrorCode::NoEntry));
63+
mv("c.cleanup", "a.txt").await.unwrap();
64+
assert_eq!(mv("a.txt", "../q.txt").await, Err(ErrorCode::NotPermitted));
65+
assert_eq!(
66+
mv("a.txt", "parent/q.txt").await,
67+
Err(ErrorCode::NotPermitted)
68+
);
69+
assert_eq!(
70+
mv("a.txt", "/tmp/q.txt").await,
71+
Err(ErrorCode::NotPermitted)
72+
);
73+
74+
mv_to_child("a.txt", "whatever").await.unwrap();
75+
assert_eq!(mv("a.txt", "whatever").await, Err(ErrorCode::NoEntry));
76+
assert_eq!(mv("whatever", "whatever").await, Err(ErrorCode::NoEntry));
77+
assert_eq!(
78+
mv_to_child("a.txt", "whatever").await,
79+
Err(ErrorCode::NoEntry)
80+
);
81+
mv_from_child("whatever", "a.txt").await.unwrap();
82+
assert_eq!(
83+
mv_from_child("whatever", "a.txt").await,
84+
Err(ErrorCode::NoEntry)
85+
);
86+
87+
drop(child);
88+
dir.remove_directory_at("child.cleanup".to_string())
89+
.await
90+
.unwrap();
91+
}
92+
93+
struct Component;
94+
export!(Component);
95+
impl exports::wasi::cli::run::Guest for Component {
96+
async fn run() -> Result<(), ()> {
97+
match &wasi::filesystem::preopens::get_directories()[..] {
98+
[(dir, dirname)] if dirname == "fs-tests.dir" => {
99+
test_rename(dir).await;
100+
}
101+
[..] => {
102+
eprintln!("usage: run with one open dir named 'fs-tests.dir'");
103+
process::exit(1)
104+
}
105+
};
106+
Ok(())
107+
}
108+
}
109+
110+
fn main() {
111+
unreachable!("main is a stub");
112+
}

0 commit comments

Comments
 (0)