From 81858b30840cd708422d95951fd1965fc2da67e3 Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Mon, 15 Sep 2025 16:37:38 +0200 Subject: [PATCH] wasi:filesystem@0.3.0-rc-2025-09-16: Add tests for is-same-object --- .../src/bin/filesystem-is-same-object.json | 3 + .../src/bin/filesystem-is-same-object.rs | 81 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 tests/rust/wasm32-wasip3/src/bin/filesystem-is-same-object.json create mode 100644 tests/rust/wasm32-wasip3/src/bin/filesystem-is-same-object.rs diff --git a/tests/rust/wasm32-wasip3/src/bin/filesystem-is-same-object.json b/tests/rust/wasm32-wasip3/src/bin/filesystem-is-same-object.json new file mode 100644 index 000000000..ff216a756 --- /dev/null +++ b/tests/rust/wasm32-wasip3/src/bin/filesystem-is-same-object.json @@ -0,0 +1,3 @@ +{ + "dirs": ["fs-tests.dir"] +} diff --git a/tests/rust/wasm32-wasip3/src/bin/filesystem-is-same-object.rs b/tests/rust/wasm32-wasip3/src/bin/filesystem-is-same-object.rs new file mode 100644 index 000000000..e7c2d5de6 --- /dev/null +++ b/tests/rust/wasm32-wasip3/src/bin/filesystem-is-same-object.rs @@ -0,0 +1,81 @@ +use std::process; +extern crate wit_bindgen; + +wit_bindgen::generate!({ + inline: r" + package test:test; + + world test { + include wasi:filesystem/imports@0.3.0-rc-2025-09-16; + include wasi:cli/command@0.3.0-rc-2025-09-16; + } +", + additional_derives: [PartialEq, Eq, Hash, Clone], + // Work around https://github.com/bytecodealliance/wasm-tools/issues/2285. + features:["clocks-timezone"], + generate_all +}); + +use wasi::filesystem::types::Descriptor; +use wasi::filesystem::types::{DescriptorFlags, OpenFlags, PathFlags}; + +async fn test_is_same_object(dir: &Descriptor) { + let afd = dir + .open_at( + PathFlags::empty(), + "a.txt".to_string(), + OpenFlags::empty(), + DescriptorFlags::READ, + ) + .await + .unwrap(); + let bfd = dir + .open_at( + PathFlags::empty(), + "b.txt".to_string(), + OpenFlags::empty(), + DescriptorFlags::READ, + ) + .await + .unwrap(); + + // is-same-object: async func(other: borrow) -> bool; + assert!(dir.is_same_object(dir).await); + { + let other = dir + .open_at( + PathFlags::empty(), + ".".to_string(), + OpenFlags::empty(), + DescriptorFlags::READ, + ) + .await + .unwrap(); + assert!(dir.is_same_object(&other).await); + } + assert!(!dir.is_same_object(&afd).await); + assert!(afd.is_same_object(&afd).await); + assert!(!afd.is_same_object(&bfd).await); + assert!(bfd.is_same_object(&bfd).await); +} + +struct Component; +export!(Component); +impl exports::wasi::cli::run::Guest for Component { + async fn run() -> Result<(), ()> { + match &wasi::filesystem::preopens::get_directories()[..] { + [(dir, dirname)] if dirname == "fs-tests.dir" => { + test_is_same_object(dir).await; + } + [..] => { + eprintln!("usage: run with one open dir named 'fs-tests.dir'"); + process::exit(1) + } + }; + Ok(()) + } +} + +fn main() { + unreachable!("main is a stub"); +}