From 8b9f510097f118bda5a90a9450a8425a56a60524 Mon Sep 17 00:00:00 2001 From: Abraham Toriz Date: Wed, 4 Aug 2021 18:40:35 -0500 Subject: [PATCH] prevent editing a tracked database in tests --- src/commands/edit.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/commands/edit.rs b/src/commands/edit.rs index 93126dd..99fabbf 100644 --- a/src/commands/edit.rs +++ b/src/commands/edit.rs @@ -122,9 +122,12 @@ impl<'a> Command<'a> for EditCommand { #[cfg(test)] mod tests { + use std::fs; + use pretty_assertions::assert_eq; use chrono::{Duration, TimeZone}; use ansi_term::Color::Yellow; + use tempfile::NamedTempFile; use crate::database::SqliteDatabase; use crate::test_utils::Ps; @@ -349,11 +352,20 @@ mod tests { assert_eq!(Ps(&String::from_utf8_lossy(&err)), Ps("")); } + fn copy_db(path: &str) -> NamedTempFile { + let tmpfile = NamedTempFile::new().unwrap(); + + fs::copy(path, &tmpfile).unwrap(); + + tmpfile + } + #[test] fn warn_old() { std::env::set_var("TZ", "UTC"); - let mut db = SqliteDatabase::from_path("assets/test_old_db.db").unwrap(); + let database_file = copy_db("assets/test_old_db.db"); + let mut db = SqliteDatabase::from_path(&database_file).unwrap(); let mut out = Vec::new(); let mut err = Vec::new(); let now = Utc.ymd(2021, 8, 3).and_hms(20, 29, 0); @@ -376,5 +388,8 @@ mod tests { String::from_utf8_lossy(&err), format!("{} You are using the old timetrap format, it is advised that you update your database using t migrate\n", Yellow.bold().paint("[WARNING]")), ); + + std::mem::drop(db); + std::mem::drop(database_file); } }