diff --git a/Cargo.toml b/Cargo.toml index 10f17bee..c23b6b20 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,10 +33,11 @@ all. authors = ["Aliaksei BialĂ­auski ", "Ivanchuk Ivan "] [dependencies] -env_logger = "0.11.3" anyhow = "1.0.86" -log = { version = "0.4.21", features = [] } serde = { version = "1.0.203", features = ["derive"] } serde_json = "1.0.117" -axum = "0.7.5" tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros", "fs"] } +axum = "0.7.5" +log = { version = "0.4.21", features = [] } +env_logger = "0.11.3" +tempdir = "0.3.7" diff --git a/src/xml/storage.rs b/src/xml/storage.rs index c01f52be..742698a3 100644 --- a/src/xml/storage.rs +++ b/src/xml/storage.rs @@ -24,51 +24,51 @@ use std::fs::File; use log::info; -pub fn touch_storage(name: Option<&str>) -> Result { - let path = name.unwrap_or("fakehub.xml"); - info!("Initializing XML storage: {path}"); - let creating = File::create(path); - match creating { +pub fn touch_storage(path: Option<&str>) -> Result { + let location = path.unwrap_or("fakehub.xml"); + info!("Initializing XML storage: {location}"); + match File::create(location) { Ok(file) => { - info!("'{path}' initialized"); + info!("'{location}' initialized"); Ok(file) } Err(err) => { - panic!("fakehub storage failed to initialize in '{path}': {err}") + panic!("fakehub storage failed to initialize in '{location}': {err}") } } } #[cfg(test)] mod tests { - use std::fs; - use std::path::Path; + use tempdir::TempDir; use crate::xml::storage::touch_storage; #[test] fn creates_xml_storage() -> anyhow::Result<()> { - touch_storage(None).unwrap(); - let storage = "fakehub.xml"; - let exists = Path::new(storage).exists(); + let temp = TempDir::new("temp")?; + let path = temp.path().join("fakehub.xml"); + let storage = path.to_str(); + touch_storage(storage).unwrap(); assert!( - exists, - "storage file '{storage}' was not created, but should be" + path.exists(), + "storage file {:?} was not created, but should be", + storage ); - fs::remove_file(storage).unwrap(); Ok(()) } #[test] fn creates_xml_storage_with_different_name() -> anyhow::Result<()> { - let path = "test.xml"; - touch_storage(Some(path)).unwrap(); - let exists = Path::new(path).exists(); + let temp = TempDir::new("temp")?; + let path = temp.path().join("test.xml"); + let storage = path.to_str(); + touch_storage(storage).unwrap(); assert!( - exists, - "storage file '{path}' was not created, but should be" + path.exists(), + "storage file {:?} was not created, but should be", + storage ); - fs::remove_file(path).unwrap(); Ok(()) } }