Skip to content

Commit

Permalink
feat(#9): anyhow, no clean, option
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Jun 5, 2024
1 parent 58a29ea commit ff60e25
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
27 changes: 17 additions & 10 deletions src/xml/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::fs::File;
use log::info;

pub fn touch_storage(name: Option<&str>) -> Result<File, Error> {
let path = "fakehub.xml";
let path = name.unwrap_or("fakehub.xml");
info!("Initializing XML storage: {path}");
let creating = File::create(path);
match creating {
Expand All @@ -41,24 +41,31 @@ pub fn touch_storage(name: Option<&str>) -> Result<File, Error> {

#[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(())
}
}

0 comments on commit ff60e25

Please sign in to comment.