From ff60e25143e02b34e14b4468cada12c30b41b0f9 Mon Sep 17 00:00:00 2001 From: h1alexbel Date: Wed, 5 Jun 2024 14:50:16 +0300 Subject: [PATCH] feat(#9): anyhow, no clean, option --- src/main.rs | 2 +- src/xml/storage.rs | 27 +++++++++++++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 078b8a0c..784ba9d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,7 @@ use crate::xml::storage::touch_storage; #[tokio::main] async fn main() { - touch_storage().unwrap(); + touch_storage(Some("fakehub.xml")).unwrap(); let app = Router::new().route("/", get(home::home)); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, app).await.unwrap(); diff --git a/src/xml/storage.rs b/src/xml/storage.rs index ad9f7379..3e33d9c1 100644 --- a/src/xml/storage.rs +++ b/src/xml/storage.rs @@ -25,7 +25,7 @@ use std::fs::File; use log::info; pub fn touch_storage(name: Option<&str>) -> Result { - let path = "fakehub.xml"; + let path = name.unwrap_or("fakehub.xml"); info!("Initializing XML storage: {path}"); let creating = File::create(path); match creating { @@ -41,24 +41,31 @@ pub fn touch_storage(name: Option<&str>) -> Result { #[cfg(test)] mod tests { - use std::fs; use std::path::Path; use crate::xml::storage::touch_storage; - fn clean() { - fs::remove_file("fakehub.xml").unwrap(); - } - #[test] - fn creates_xml_storage() { - touch_storage().unwrap(); + fn creates_xml_storage() -> anyhow::Result<()> { + touch_storage(None).unwrap(); let storage = "fakehub.xml"; let exists = Path::new(storage).exists(); assert!( exists, - "storage file '{storage}' was not created, but should" + "storage file '{storage}' was not created, but should be" + ); + 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(); + assert!( + exists, + "storage file '{path}' was not created, but should be" ); - clean(); + Ok(()) } }