|
| 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::DirectoryEntry; |
| 21 | +use wasi::filesystem::types::DescriptorType; |
| 22 | + |
| 23 | +async fn test_read_directory(dir: &Descriptor) { |
| 24 | + // read-directory: async func() -> tuple<stream<directory-entry>, future<result<_, error-code>>>; |
| 25 | + let (stream, result) = dir.read_directory().await; |
| 26 | + let mut entries = stream.collect().await; |
| 27 | + result.await.unwrap(); |
| 28 | + entries.sort_by_key(|e| e.name.clone()); |
| 29 | + assert_eq!( |
| 30 | + &entries, |
| 31 | + &[ |
| 32 | + DirectoryEntry { |
| 33 | + type_: DescriptorType::RegularFile, |
| 34 | + name: "a.txt".to_string() |
| 35 | + }, |
| 36 | + DirectoryEntry { |
| 37 | + type_: DescriptorType::RegularFile, |
| 38 | + name: "b.txt".to_string() |
| 39 | + }, |
| 40 | + DirectoryEntry { |
| 41 | + type_: DescriptorType::SymbolicLink, |
| 42 | + name: "parent".to_string() |
| 43 | + } |
| 44 | + ] |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +struct Component; |
| 49 | +export!(Component); |
| 50 | +impl exports::wasi::cli::run::Guest for Component { |
| 51 | + async fn run() -> Result<(), ()> { |
| 52 | + match &wasi::filesystem::preopens::get_directories()[..] { |
| 53 | + [(dir, dirname)] if dirname == "fs-tests.dir" => { |
| 54 | + test_read_directory(dir).await; |
| 55 | + } |
| 56 | + [..] => { |
| 57 | + eprintln!("usage: run with one open dir named 'fs-tests.dir'"); |
| 58 | + process::exit(1) |
| 59 | + } |
| 60 | + }; |
| 61 | + Ok(()) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +fn main() { |
| 66 | + unreachable!("main is a stub"); |
| 67 | +} |
0 commit comments