Files
tokio/tokio-fs/tests/dir.rs
T

90 lines
1.9 KiB
Rust
Raw Normal View History

2019-05-14 10:27:36 -07:00
#![deny(warnings, rust_2018_idioms)]
2019-07-11 11:05:49 -05:00
#![feature(async_await)]
2019-07-11 11:05:49 -05:00
use futures_util::future;
use futures_util::try_stream::TryStreamExt;
use std::fs;
use std::sync::{Arc, Mutex};
use tempfile::tempdir;
use tokio_fs::*;
2018-11-16 14:50:06 -08:00
mod pool;
#[test]
fn create() {
let base_dir = tempdir().unwrap();
let new_dir = base_dir.path().join("foo");
2019-07-11 11:05:49 -05:00
let new_dir_2 = new_dir.clone();
2019-07-11 11:05:49 -05:00
pool::run(async move {
create_dir(new_dir).await?;
Ok(())
});
2019-07-11 11:05:49 -05:00
assert!(new_dir_2.is_dir());
}
#[test]
fn create_all() {
let base_dir = tempdir().unwrap();
let new_dir = base_dir.path().join("foo").join("bar");
2019-07-11 11:05:49 -05:00
let new_dir_2 = new_dir.clone();
2019-07-11 11:05:49 -05:00
pool::run(async move {
create_dir_all(new_dir).await?;
Ok(())
});
2019-07-11 11:05:49 -05:00
assert!(new_dir_2.is_dir());
}
#[test]
fn remove() {
let base_dir = tempdir().unwrap();
let new_dir = base_dir.path().join("foo");
2019-07-11 11:05:49 -05:00
let new_dir_2 = new_dir.clone();
fs::create_dir(new_dir.clone()).unwrap();
2019-07-11 11:05:49 -05:00
pool::run(async move {
remove_dir(new_dir).await?;
Ok(())
});
2019-07-11 11:05:49 -05:00
assert!(!new_dir_2.exists());
}
#[test]
fn read() {
let base_dir = tempdir().unwrap();
let p = base_dir.path();
fs::create_dir(p.join("aa")).unwrap();
fs::create_dir(p.join("bb")).unwrap();
fs::create_dir(p.join("cc")).unwrap();
let files = Arc::new(Mutex::new(Vec::new()));
let f = files.clone();
let p = p.to_path_buf();
2019-07-11 11:05:49 -05:00
pool::run(async move {
let read_dir_fut = read_dir(p).await?;
read_dir_fut
.try_for_each(move |e| {
let s = e.file_name().to_str().unwrap().to_string();
f.lock().unwrap().push(s);
future::ok(())
})
.await?;
Ok(())
});
let mut files = files.lock().unwrap();
files.sort(); // because the order is not guaranteed
assert_eq!(
*files,
vec!["aa".to_string(), "bb".to_string(), "cc".to_string()]
);
}