Set up temperory working directories
This commit is contained in:
parent
94aa67a156
commit
75a90bbdff
|
@ -681,6 +681,7 @@ dependencies = [
|
||||||
"serde 1.0.188",
|
"serde 1.0.188",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"sha2",
|
"sha2",
|
||||||
|
"tempdir",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
"uuid 0.4.0",
|
"uuid 0.4.0",
|
||||||
|
|
|
@ -32,4 +32,4 @@ log = { version = "0.4" }
|
||||||
bytes = { version = "1" }
|
bytes = { version = "1" }
|
||||||
futures-util = { version = "0.3" }
|
futures-util = { version = "0.3" }
|
||||||
cool_asserts = { version = "2" }
|
cool_asserts = { version = "2" }
|
||||||
|
tempdir = { version = "0.3" }
|
||||||
|
|
|
@ -211,8 +211,8 @@ fn load_content(path: &Path) -> Result<Vec<u8>, ReadFileError> {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::store::utils::DirCleanup;
|
|
||||||
use std::{convert::TryFrom, path::PathBuf};
|
use std::{convert::TryFrom, path::PathBuf};
|
||||||
|
use tempdir::TempDir;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn paths() {
|
fn paths() {
|
||||||
|
@ -235,21 +235,23 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_opens_a_file() {
|
fn it_opens_a_file() {
|
||||||
let _cleanup = DirCleanup(PathBuf::from("var/"));
|
let tmp = TempDir::new("var").unwrap();
|
||||||
FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed");
|
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_deletes_a_file() {
|
fn it_deletes_a_file() {
|
||||||
let _cleanup = DirCleanup(PathBuf::from("var/"));
|
let tmp = TempDir::new("var").unwrap();
|
||||||
let f = FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed");
|
let f =
|
||||||
|
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
|
||||||
f.delete();
|
f.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_can_return_a_thumbnail() {
|
fn it_can_return_a_thumbnail() {
|
||||||
let _cleanup = DirCleanup(PathBuf::from("var/"));
|
let tmp = TempDir::new("var").unwrap();
|
||||||
let _ = FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed");
|
let _ =
|
||||||
|
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
|
||||||
/*
|
/*
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
f.thumbnail(),
|
f.thumbnail(),
|
||||||
|
@ -263,15 +265,16 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_can_return_a_file_stream() {
|
fn it_can_return_a_file_stream() {
|
||||||
let _cleanup = DirCleanup(PathBuf::from("var/"));
|
let tmp = TempDir::new("var").unwrap();
|
||||||
let _ = FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed");
|
let _ =
|
||||||
|
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
|
||||||
// f.stream().expect("to succeed");
|
// f.stream().expect("to succeed");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_raises_an_error_when_file_not_found() {
|
fn it_raises_an_error_when_file_not_found() {
|
||||||
let _cleanup = DirCleanup(PathBuf::from("var/"));
|
let tmp = TempDir::new("var").unwrap();
|
||||||
match FileHandle::load(&FileId::from("rawr"), &PathBuf::from("var/")) {
|
match FileHandle::load(&FileId::from("rawr"), tmp.path()) {
|
||||||
Err(ReadFileError::FileNotFound(_)) => assert!(true),
|
Err(ReadFileError::FileNotFound(_)) => assert!(true),
|
||||||
_ => assert!(false),
|
_ => assert!(false),
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,11 +41,12 @@ impl FileInfo {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::store::{utils::DirCleanup, FileId};
|
use crate::store::FileId;
|
||||||
|
use tempdir::TempDir;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_saves_and_loads_metadata() {
|
fn it_saves_and_loads_metadata() {
|
||||||
let _cleanup = DirCleanup(PathBuf::from("var/"));
|
let tmp = TempDir::new("var").unwrap();
|
||||||
let created = Utc::now();
|
let created = Utc::now();
|
||||||
|
|
||||||
let info = FileInfo {
|
let info = FileInfo {
|
||||||
|
@ -56,10 +57,11 @@ mod test {
|
||||||
hash: "abcdefg".to_owned(),
|
hash: "abcdefg".to_owned(),
|
||||||
extension: "png".to_owned(),
|
extension: "png".to_owned(),
|
||||||
};
|
};
|
||||||
info.save(PathBuf::from(format!("var/{}", *info.id)))
|
let mut path = tmp.path().to_owned();
|
||||||
.unwrap();
|
path.push(&PathBuf::from(info.id.clone()));
|
||||||
|
info.save(path.clone()).unwrap();
|
||||||
|
|
||||||
let info_ = FileInfo::load(PathBuf::from(format!("var/{}", *info.id))).unwrap();
|
let info_ = FileInfo::load(path).unwrap();
|
||||||
assert_eq!(info_.size, 23777);
|
assert_eq!(info_.size, 23777);
|
||||||
assert_eq!(info_.created, info.created);
|
assert_eq!(info_.created, info.created);
|
||||||
assert_eq!(info_.file_type, "image/png");
|
assert_eq!(info_.file_type, "image/png");
|
||||||
|
|
|
@ -5,7 +5,6 @@ use thiserror::Error;
|
||||||
|
|
||||||
mod filehandle;
|
mod filehandle;
|
||||||
mod fileinfo;
|
mod fileinfo;
|
||||||
pub mod utils;
|
|
||||||
|
|
||||||
pub use filehandle::FileHandle;
|
pub use filehandle::FileHandle;
|
||||||
pub use fileinfo::FileInfo;
|
pub use fileinfo::FileInfo;
|
||||||
|
@ -161,49 +160,50 @@ impl Store {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::{utils::DirCleanup, *};
|
use super::*;
|
||||||
use cool_asserts::assert_matches;
|
use cool_asserts::assert_matches;
|
||||||
use std::{collections::HashSet, io::Read};
|
use std::{collections::HashSet, io::Read};
|
||||||
|
use tempdir::TempDir;
|
||||||
|
|
||||||
fn with_file<F>(test_fn: F)
|
fn with_file<F>(test_fn: F)
|
||||||
where
|
where
|
||||||
F: FnOnce(Store, FileId),
|
F: FnOnce(Store, FileId, TempDir),
|
||||||
{
|
{
|
||||||
let _cleanup = DirCleanup(PathBuf::from("var/"));
|
let tmp = TempDir::new("var").unwrap();
|
||||||
|
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
let mut file = std::fs::File::open("fixtures/rawr.png").unwrap();
|
let mut file = std::fs::File::open("fixtures/rawr.png").unwrap();
|
||||||
file.read_to_end(&mut buf).unwrap();
|
file.read_to_end(&mut buf).unwrap();
|
||||||
|
|
||||||
let mut store = Store::new(PathBuf::from("var/"));
|
let mut store = Store::new(PathBuf::from(tmp.path()));
|
||||||
let file_record = store.add_file("rawr.png".to_owned(), buf).unwrap();
|
let file_record = store.add_file("rawr.png".to_owned(), buf).unwrap();
|
||||||
|
|
||||||
test_fn(store, file_record.id);
|
test_fn(store, file_record.id, tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn adds_files() {
|
fn adds_files() {
|
||||||
with_file(|store, id| {
|
with_file(|store, id, tmp| {
|
||||||
let file = store.get_file(&id).expect("to retrieve the file");
|
let file = store.get_file(&id).expect("to retrieve the file");
|
||||||
|
|
||||||
assert_eq!(file.content().map(|file| file.len()).unwrap(), 23777);
|
assert_eq!(file.content().map(|file| file.len()).unwrap(), 23777);
|
||||||
|
|
||||||
assert!(PathBuf::from(format!("var/{}.png", *id)).exists());
|
assert!(tmp.path().join(&(*id)).with_extension("png").exists());
|
||||||
assert!(PathBuf::from(format!("var/{}.json", *id)).exists());
|
assert!(tmp.path().join(&(*id)).with_extension("json").exists());
|
||||||
assert!(PathBuf::from(format!("var/{}.tn.png", *id)).exists());
|
assert!(tmp.path().join(&(*id)).with_extension("tn.png").exists());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sets_up_metadata_for_file() {
|
fn sets_up_metadata_for_file() {
|
||||||
with_file(|store, id| {
|
with_file(|store, id, tmp| {
|
||||||
assert!(PathBuf::from(format!("var/{}.png", *id)).exists());
|
assert!(tmp.path().join(&(*id)).with_extension("png").exists());
|
||||||
let info = store.get_metadata(&id).expect("to retrieve the metadata");
|
let info = store.get_metadata(&id).expect("to retrieve the metadata");
|
||||||
|
|
||||||
assert_matches!(info, FileInfo { size, file_type, hash, extension, .. } => {
|
assert_matches!(info, FileInfo { size, file_type, hash, extension, .. } => {
|
||||||
assert_eq!(size, 23777);
|
assert_eq!(size, 23777);
|
||||||
assert_eq!(file_type, "image/png");
|
assert_eq!(file_type, "image/png");
|
||||||
assert_eq!(hash, "".to_owned());
|
assert_eq!(hash, "b6cd35e113b95d62f53d9cbd27ccefef47d3e324aef01a2db6c0c6d3a43c89ee".to_owned());
|
||||||
assert_eq!(extension, "png".to_owned());
|
assert_eq!(extension, "png".to_owned());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -221,22 +221,21 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deletes_associated_files() {
|
fn deletes_associated_files() {
|
||||||
with_file(|mut store, id| {
|
with_file(|mut store, id, tmp| {
|
||||||
store.delete_file(&id).expect("file to be deleted");
|
store.delete_file(&id).expect("file to be deleted");
|
||||||
|
|
||||||
assert!(!PathBuf::from(format!("var/{}.png", *id)).exists());
|
assert!(!tmp.path().join(&(*id)).with_extension("png").exists());
|
||||||
assert!(!PathBuf::from(format!("var/{}.json", *id)).exists());
|
assert!(!tmp.path().join(&(*id)).with_extension("json").exists());
|
||||||
assert!(!PathBuf::from(format!("var/{}.tn.png", *id)).exists());
|
assert!(!tmp.path().join(&(*id)).with_extension("tn.png").exists());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn lists_files_in_the_db() {
|
fn lists_files_in_the_db() {
|
||||||
with_file(|store, id| {
|
with_file(|store, id, _| {
|
||||||
let resolvers = store.list_files().expect("file listing to succeed");
|
let resolvers = store.list_files().expect("file listing to succeed");
|
||||||
let ids = resolvers.into_iter().collect::<HashSet<FileId>>();
|
let ids = resolvers.into_iter().collect::<HashSet<FileId>>();
|
||||||
|
|
||||||
println!("ids: {:?}", ids);
|
|
||||||
assert_eq!(ids.len(), 1);
|
assert_eq!(ids.len(), 1);
|
||||||
assert!(ids.contains(&id));
|
assert!(ids.contains(&id));
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
use std::{ffi::OsStr, path::PathBuf};
|
|
||||||
|
|
||||||
pub struct DirCleanup(pub PathBuf);
|
|
||||||
|
|
||||||
impl Drop for DirCleanup {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
let files = std::fs::read_dir(&self.0).unwrap();
|
|
||||||
for file in files {
|
|
||||||
let filename = file.unwrap().path();
|
|
||||||
if filename.file_name() != Some(&OsStr::new(".placeholder")) {
|
|
||||||
let _ = std::fs::remove_file(filename);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue