Skip to content

Commit 194a2fd

Browse files
authored
Merge pull request #138 from wingo/filesystem-is-same-object
wasi:filesystem@0.3.0-rc-2025-09-16: Add tests for is-same-object
2 parents 077e496 + a8a3406 commit 194a2fd

2 files changed

Lines changed: 113 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: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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;
20+
use wasi::filesystem::types::{DescriptorFlags, OpenFlags, PathFlags};
21+
22+
async fn check_one(fd: &Descriptor, same: &[&Descriptor], different: &[&Descriptor]) {
23+
assert!(fd.is_same_object(fd).await);
24+
for other in same {
25+
assert!(fd.is_same_object(other).await);
26+
}
27+
for other in different {
28+
assert!(!fd.is_same_object(other).await);
29+
}
30+
}
31+
32+
async fn test_is_same_object(dir: &Descriptor) {
33+
let afd = dir
34+
.open_at(
35+
PathFlags::empty(),
36+
"a.txt".to_string(),
37+
OpenFlags::empty(),
38+
DescriptorFlags::READ,
39+
)
40+
.await
41+
.unwrap();
42+
let bfd = dir
43+
.open_at(
44+
PathFlags::empty(),
45+
"b.txt".to_string(),
46+
OpenFlags::empty(),
47+
DescriptorFlags::READ,
48+
)
49+
.await
50+
.unwrap();
51+
52+
dir.link_at(
53+
PathFlags::empty(),
54+
"a.txt".to_string(),
55+
dir,
56+
"c.cleanup".to_string(),
57+
)
58+
.await
59+
.unwrap();
60+
61+
let cfd = dir
62+
.open_at(
63+
PathFlags::empty(),
64+
"c.cleanup".to_string(),
65+
OpenFlags::empty(),
66+
DescriptorFlags::READ,
67+
)
68+
.await
69+
.unwrap();
70+
71+
// is-same-object: async func(other: borrow<descriptor>) -> bool;
72+
check_one(dir, &[], &[&afd, &bfd, &cfd]).await;
73+
check_one(&afd, &[&cfd], &[dir, &bfd]).await;
74+
check_one(&bfd, &[], &[dir, &afd, &cfd]).await;
75+
check_one(&cfd, &[&afd], &[dir, &bfd]).await;
76+
77+
{
78+
let other = dir
79+
.open_at(
80+
PathFlags::empty(),
81+
".".to_string(),
82+
OpenFlags::empty(),
83+
DescriptorFlags::READ,
84+
)
85+
.await
86+
.unwrap();
87+
assert!(dir.is_same_object(&other).await);
88+
}
89+
}
90+
91+
struct Component;
92+
export!(Component);
93+
impl exports::wasi::cli::run::Guest for Component {
94+
async fn run() -> Result<(), ()> {
95+
match &wasi::filesystem::preopens::get_directories()[..] {
96+
[(dir, dirname)] if dirname == "fs-tests.dir" => {
97+
test_is_same_object(dir).await;
98+
}
99+
[..] => {
100+
eprintln!("usage: run with one open dir named 'fs-tests.dir'");
101+
process::exit(1)
102+
}
103+
};
104+
Ok(())
105+
}
106+
}
107+
108+
fn main() {
109+
unreachable!("main is a stub");
110+
}

0 commit comments

Comments
 (0)