fs: add tests for directory-related functions (#704) (#724)

This change adds a few tests around directory-related functions.
This commit is contained in:
Kazuyoshi Kato 2018-11-13 15:28:52 -08:00 committed by Toby Lawrence
parent d246964bdf
commit 9153067d66
2 changed files with 84 additions and 0 deletions

View File

@ -27,6 +27,7 @@ tokio-io = { version = "0.1.6", path = "../tokio-io" }
[dev-dependencies] [dev-dependencies]
rand = "0.5" rand = "0.5"
tempfile = "3" tempfile = "3"
tempdir = "0.3"
tokio-io = { version = "0.1.6", path = "../tokio-io" } tokio-io = { version = "0.1.6", path = "../tokio-io" }
tokio-codec = { version = "0.1.0", path = "../tokio-codec" } tokio-codec = { version = "0.1.0", path = "../tokio-codec" }
tokio = { version = "0.1.7", path = ".." } tokio = { version = "0.1.7", path = ".." }

83
tokio-fs/tests/dir.rs Normal file
View File

@ -0,0 +1,83 @@
extern crate futures;
extern crate tempdir;
extern crate tokio_fs;
extern crate tokio_threadpool;
use futures::sync::oneshot;
use futures::{Future, Stream};
use std::fs;
use std::sync::{Arc, Mutex};
use tempdir::TempDir;
use tokio_fs::*;
use tokio_threadpool::Builder;
fn run_in_pool<F>(f: F)
where
F: Future<Item = (), Error = std::io::Error> + Send + 'static,
{
let pool = Builder::new().pool_size(1).build();
let (tx, rx) = oneshot::channel::<()>();
pool.spawn(f.then(|_| tx.send(())));
rx.wait().unwrap()
}
#[test]
fn create() {
let base_dir = TempDir::new("base").unwrap();
let new_dir = base_dir.path().join("foo");
run_in_pool({ create_dir(new_dir.clone()) });
assert!(new_dir.is_dir());
}
#[test]
fn create_all() {
let base_dir = TempDir::new("base").unwrap();
let new_dir = base_dir.path().join("foo").join("bar");
run_in_pool({ create_dir_all(new_dir.clone()) });
assert!(new_dir.is_dir());
}
#[test]
fn remove() {
let base_dir = TempDir::new("base").unwrap();
let new_dir = base_dir.path().join("foo");
fs::create_dir(new_dir.clone()).unwrap();
run_in_pool({ remove_dir(new_dir.clone()) });
assert!(!new_dir.exists());
}
#[test]
fn read() {
let base_dir = TempDir::new("base").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();
run_in_pool({
read_dir(p).flatten_stream().for_each(move |e| {
let s = e.file_name().to_str().unwrap().to_string();
f.lock().unwrap().push(s);
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()]
);
}