fix: return tmp dir in context creation test util to prolong

auto-cleanup

DESCRIPTION
    > Once the variable goes out of scope, the underlying file system resource is removed.
    https://docs.rs/mktemp/latest/mktemp/

    Return mktemp::Temp instance created to prolong lifetime and
    postpone auto-cleanup via Drop until the test suite runs to
    completion
This commit is contained in:
Aravinth Manivannan 2022-04-27 14:34:35 +05:30
parent 101234e300
commit 46c88b74c7
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
2 changed files with 6 additions and 7 deletions

View file

@ -65,7 +65,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn build_details_works() { async fn build_details_works() {
let ctx = tests::get_data().await; let (_dir, ctx) = tests::get_data().await;
println!("[log] test configuration {:#?}", ctx.settings); println!("[log] test configuration {:#?}", ctx.settings);
let app = get_app!(ctx).await; let app = get_app!(ctx).await;

View file

@ -17,24 +17,23 @@
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use actix_http::StatusCode;
use actix_web::dev::ServiceResponse;
use mktemp::Temp; use mktemp::Temp;
use crate::ctx::Ctx; use crate::ctx::Ctx;
use crate::page::Page; use crate::page::Page;
use crate::settings::Settings; use crate::settings::Settings;
pub async fn get_data() -> Arc<Ctx> { pub async fn get_data() -> (Temp, Arc<Ctx>) {
// mktemp::Temp is returned because the temp directory created
// is removed once the variable goes out of scope
let mut settings = Settings::new().unwrap(); let mut settings = Settings::new().unwrap();
let tmp_dir = Temp::new_dir().unwrap(); let tmp_dir = Temp::new_dir().unwrap();
println!("[log] Test temp directory: {}", tmp_dir.to_str().unwrap()); println!("[log] Test temp directory: {}", tmp_dir.to_str().unwrap());
let tmp_dir = tmp_dir.as_path();
let mut pages = Vec::with_capacity(settings.pages.len()); let mut pages = Vec::with_capacity(settings.pages.len());
for page in settings.pages.iter() { for page in settings.pages.iter() {
let name = Path::new(&page.path).file_name().unwrap().to_str().unwrap(); let name = Path::new(&page.path).file_name().unwrap().to_str().unwrap();
let path = tmp_dir.join(name); let path = tmp_dir.as_path().join(name);
let page = Page { let page = Page {
path: path.to_str().unwrap().to_string(), path: path.to_str().unwrap().to_string(),
secret: page.secret.clone(), secret: page.secret.clone(),
@ -49,7 +48,7 @@ pub async fn get_data() -> Arc<Ctx> {
println!("[log] Initialzing settings again with test config"); println!("[log] Initialzing settings again with test config");
settings.init(); settings.init();
Ctx::new(settings) (tmp_dir, Ctx::new(settings))
} }
#[allow(dead_code, clippy::upper_case_acronyms)] #[allow(dead_code, clippy::upper_case_acronyms)]