Skip to content

Commit

Permalink
io-tests (#22)
Browse files Browse the repository at this point in the history
* io-tests

* metadata
  • Loading branch information
sergey-shandar authored Mar 12, 2024
1 parent b9a6be1 commit 8a12c93
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion io-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "io-test"
version = "0.10.0"
version = "0.10.1"
edition = "2021"
description = "I/O implementations for testing"
authors.workspace = true
Expand Down
47 changes: 45 additions & 2 deletions io-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,21 @@ impl Io for VirtualIo {
}
fn metadata(&self, path: &str) -> io::Result<Metadata> {
let fs = self.fs.borrow();
fs.entity_map
let dir_end = path.ends_with('/');
let path = if dir_end {
&path[..path.len() - 1]
} else {
path
};
let result = fs
.entity_map
.get(path)
.map(Entity::metadata)
.ok_or_else(not_found)
.ok_or_else(not_found)?;
if !result.is_dir && dir_end {
return Err(not_found());
}
Ok(result)
}
fn create(&self, path: &str) -> io::Result<Self::File> {
let mut fs = self.fs.borrow_mut();
Expand Down Expand Up @@ -482,4 +493,36 @@ mod test {
assert_eq!(io.now().as_millis(), 0);
assert_eq!(io.now().as_millis(), 1);
}

fn check_len(m: &super::Metadata, f: fn(m: &super::Metadata) -> u64, len: u64) {
assert_eq!(m.len(), len);
}

#[wasm_bindgen_test]
#[test]
fn test_metadata() {
let io = VirtualIo::new(&[]);
io.write("test.txt", "Hello, world!".as_bytes()).unwrap();
io.create_dir("a").unwrap();
{
let m = io.metadata("test.txt").unwrap();
// assert_eq!(m.len(), 13);
check_len(&m, super::Metadata::len, 13);
assert!(!m.is_dir());
}
{
io.metadata("test.txt/").unwrap_err();
}
{
io.metadata("b").unwrap_err();
}
{
let m = io.metadata("a").unwrap();
assert!(m.is_dir());
}
{
let m = io.metadata("a/").unwrap();
assert!(m.is_dir());
}
}
}

0 comments on commit 8a12c93

Please sign in to comment.