#![cfg(target_arch = "wasm32")]
use std::io::ErrorKind;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_dedicated_worker);
use browser_fs::{create_dir, create_dir_all, read_dir, remove_dir, remove_dir_all};
#[wasm_bindgen_test]
async fn should_create_single_directory() {
assert!(create_dir("/new-dir").await.is_ok());
}
#[wasm_bindgen_test]
async fn shouldnt_create_multiple_directory() {
let err = create_dir("/this/dir/doesnt/exist").await.unwrap_err();
assert_eq!(err.kind(), ErrorKind::NotFound);
}
#[wasm_bindgen_test]
async fn shouldnt_create_invalid_path() {
let err = create_dir("/../../baz").await.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidInput);
}
#[wasm_bindgen_test]
async fn should_create_multiple_directory() {
create_dir_all("/foo/bar/baz").await.unwrap();
create_dir_all("/foo/../baz").await.unwrap();
}
#[wasm_bindgen_test]
async fn should_remove_dir() {
create_dir("/remove-this-dir").await.unwrap();
remove_dir("/remove-this-dir").await.unwrap();
}
#[wasm_bindgen_test]
async fn shouldnt_remove_dir_with_data() {
create_dir_all("/remove/this/dir").await.unwrap();
let err = remove_dir("/remove").await.unwrap_err();
assert_eq!(err.kind(), ErrorKind::DirectoryNotEmpty);
}
#[wasm_bindgen_test]
async fn should_remove_dir_with_data() {
create_dir_all("/remove/this/dir").await.unwrap();
remove_dir_all("/remove").await.unwrap();
}
#[wasm_bindgen_test]
async fn should_list_directory() {
use futures_lite::stream::StreamExt;
create_dir_all("/content/inside").await.unwrap();
create_dir_all("/content/this").await.unwrap();
create_dir_all("/content/directory").await.unwrap();
let mut stream = read_dir("/content").await.unwrap();
let mut result = Vec::with_capacity(3);
while let Some(item) = stream.next().await {
result.push(item);
}
assert_eq!(result.len(), 3);
}
#[wasm_bindgen_test]
async fn should_read_directory_metadata() {
let name = "/some/directory/meta";
create_dir_all(name).await.unwrap();
let meta = browser_fs::metadata(name).await.unwrap();
assert!(meta.is_dir());
assert_eq!(meta.len(), 0);
assert!(meta.modified().is_err());
}