From 3294262d8d6f66e3d4b9a83b75a5e10cd56d4c4f Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Mon, 25 Sep 2023 00:58:35 -0400 Subject: [PATCH] Set up temperory working directories --- Cargo.lock | 1 + file-service/Cargo.toml | 2 +- file-service/src/store/filehandle.rs | 25 ++++++++++--------- file-service/src/store/fileinfo.rs | 12 +++++---- file-service/src/store/mod.rs | 37 ++++++++++++++-------------- file-service/src/store/utils.rs | 15 ----------- 6 files changed, 41 insertions(+), 51 deletions(-) delete mode 100644 file-service/src/store/utils.rs diff --git a/Cargo.lock b/Cargo.lock index b38f3d4..6f11967 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -692,6 +692,7 @@ dependencies = [ "serde 1.0.188", "serde_json", "sha2", + "tempdir", "thiserror", "tokio", "uuid 0.4.0", diff --git a/file-service/Cargo.toml b/file-service/Cargo.toml index 531ef4c..f080ffe 100644 --- a/file-service/Cargo.toml +++ b/file-service/Cargo.toml @@ -32,4 +32,4 @@ log = { version = "0.4" } bytes = { version = "1" } futures-util = { version = "0.3" } cool_asserts = { version = "2" } - +tempdir = { version = "0.3" } diff --git a/file-service/src/store/filehandle.rs b/file-service/src/store/filehandle.rs index 4259954..101254d 100644 --- a/file-service/src/store/filehandle.rs +++ b/file-service/src/store/filehandle.rs @@ -211,8 +211,8 @@ fn load_content(path: &Path) -> Result, ReadFileError> { #[cfg(test)] mod test { use super::*; - use crate::store::utils::DirCleanup; use std::{convert::TryFrom, path::PathBuf}; + use tempdir::TempDir; #[test] fn paths() { @@ -235,21 +235,23 @@ mod test { #[test] fn it_opens_a_file() { - let _cleanup = DirCleanup(PathBuf::from("var/")); - FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed"); + let tmp = TempDir::new("var").unwrap(); + FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed"); } #[test] fn it_deletes_a_file() { - let _cleanup = DirCleanup(PathBuf::from("var/")); - let f = FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed"); + let tmp = TempDir::new("var").unwrap(); + let f = + FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed"); f.delete(); } #[test] fn it_can_return_a_thumbnail() { - let _cleanup = DirCleanup(PathBuf::from("var/")); - let _ = FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed"); + let tmp = TempDir::new("var").unwrap(); + let _ = + FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed"); /* assert_eq!( f.thumbnail(), @@ -263,15 +265,16 @@ mod test { #[test] fn it_can_return_a_file_stream() { - let _cleanup = DirCleanup(PathBuf::from("var/")); - let _ = FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed"); + let tmp = TempDir::new("var").unwrap(); + let _ = + FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed"); // f.stream().expect("to succeed"); } #[test] fn it_raises_an_error_when_file_not_found() { - let _cleanup = DirCleanup(PathBuf::from("var/")); - match FileHandle::load(&FileId::from("rawr"), &PathBuf::from("var/")) { + let tmp = TempDir::new("var").unwrap(); + match FileHandle::load(&FileId::from("rawr"), tmp.path()) { Err(ReadFileError::FileNotFound(_)) => assert!(true), _ => assert!(false), } diff --git a/file-service/src/store/fileinfo.rs b/file-service/src/store/fileinfo.rs index ba71c40..69da79f 100644 --- a/file-service/src/store/fileinfo.rs +++ b/file-service/src/store/fileinfo.rs @@ -41,11 +41,12 @@ impl FileInfo { #[cfg(test)] mod test { use super::*; - use crate::store::{utils::DirCleanup, FileId}; + use crate::store::FileId; + use tempdir::TempDir; #[test] fn it_saves_and_loads_metadata() { - let _cleanup = DirCleanup(PathBuf::from("var/")); + let tmp = TempDir::new("var").unwrap(); let created = Utc::now(); let info = FileInfo { @@ -56,10 +57,11 @@ mod test { hash: "abcdefg".to_owned(), extension: "png".to_owned(), }; - info.save(PathBuf::from(format!("var/{}", *info.id))) - .unwrap(); + let mut path = tmp.path().to_owned(); + 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_.created, info.created); assert_eq!(info_.file_type, "image/png"); diff --git a/file-service/src/store/mod.rs b/file-service/src/store/mod.rs index 9975788..c023673 100644 --- a/file-service/src/store/mod.rs +++ b/file-service/src/store/mod.rs @@ -5,7 +5,6 @@ use thiserror::Error; mod filehandle; mod fileinfo; -pub mod utils; pub use filehandle::FileHandle; pub use fileinfo::FileInfo; @@ -161,49 +160,50 @@ impl Store { #[cfg(test)] mod test { - use super::{utils::DirCleanup, *}; + use super::*; use cool_asserts::assert_matches; use std::{collections::HashSet, io::Read}; + use tempdir::TempDir; fn with_file(test_fn: F) 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 file = std::fs::File::open("fixtures/rawr.png").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(); - test_fn(store, file_record.id); + test_fn(store, file_record.id, tmp); } #[test] fn adds_files() { - with_file(|store, id| { + with_file(|store, id, tmp| { let file = store.get_file(&id).expect("to retrieve the file"); assert_eq!(file.content().map(|file| file.len()).unwrap(), 23777); - assert!(PathBuf::from(format!("var/{}.png", *id)).exists()); - assert!(PathBuf::from(format!("var/{}.json", *id)).exists()); - assert!(PathBuf::from(format!("var/{}.tn.png", *id)).exists()); + assert!(tmp.path().join(&(*id)).with_extension("png").exists()); + assert!(tmp.path().join(&(*id)).with_extension("json").exists()); + assert!(tmp.path().join(&(*id)).with_extension("tn.png").exists()); }); } #[test] fn sets_up_metadata_for_file() { - with_file(|store, id| { - assert!(PathBuf::from(format!("var/{}.png", *id)).exists()); + with_file(|store, id, tmp| { + assert!(tmp.path().join(&(*id)).with_extension("png").exists()); let info = store.get_metadata(&id).expect("to retrieve the metadata"); assert_matches!(info, FileInfo { size, file_type, hash, extension, .. } => { assert_eq!(size, 23777); assert_eq!(file_type, "image/png"); - assert_eq!(hash, "".to_owned()); + assert_eq!(hash, "b6cd35e113b95d62f53d9cbd27ccefef47d3e324aef01a2db6c0c6d3a43c89ee".to_owned()); assert_eq!(extension, "png".to_owned()); }); }); @@ -221,22 +221,21 @@ mod test { #[test] fn deletes_associated_files() { - with_file(|mut store, id| { + with_file(|mut store, id, tmp| { store.delete_file(&id).expect("file to be deleted"); - assert!(!PathBuf::from(format!("var/{}.png", *id)).exists()); - assert!(!PathBuf::from(format!("var/{}.json", *id)).exists()); - assert!(!PathBuf::from(format!("var/{}.tn.png", *id)).exists()); + assert!(!tmp.path().join(&(*id)).with_extension("png").exists()); + assert!(!tmp.path().join(&(*id)).with_extension("json").exists()); + assert!(!tmp.path().join(&(*id)).with_extension("tn.png").exists()); }); } #[test] 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 ids = resolvers.into_iter().collect::>(); - println!("ids: {:?}", ids); assert_eq!(ids.len(), 1); assert!(ids.contains(&id)); }); diff --git a/file-service/src/store/utils.rs b/file-service/src/store/utils.rs deleted file mode 100644 index 92b7099..0000000 --- a/file-service/src/store/utils.rs +++ /dev/null @@ -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); - } - } - } -}