Skip to content

Commit

Permalink
feat(#9): temp dir
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Jun 5, 2024
1 parent c160bca commit 0d3c5d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ all.
authors = ["Aliaksei Bialíauski <[email protected]>", "Ivanchuk Ivan <[email protected]>"]

[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"
42 changes: 21 additions & 21 deletions src/xml/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,51 @@ use std::fs::File;

use log::info;

pub fn touch_storage(name: Option<&str>) -> Result<File, Error> {
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<File, Error> {
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(())
}
}

0 comments on commit 0d3c5d5

Please sign in to comment.