From e5e0f0cdd7bb5cd036f6ef4c4eccb792d86e449c Mon Sep 17 00:00:00 2001 From: Hocuri Date: Fri, 11 Jul 2025 14:01:04 +0200 Subject: [PATCH] test: Add option to save database on test failure (#6992) I had it a few times now that I wanted to examine the database in order to debug a test failure. Setting this environment variable makes this easy in the future. --- src/test_utils.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/test_utils.rs b/src/test_utils.rs index c6da615d4..19f1729ad 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -2,6 +2,7 @@ //! //! This private module is only compiled for test runs. use std::collections::{BTreeMap, HashSet}; +use std::env::current_dir; use std::fmt::Write; use std::ops::{Deref, DerefMut}; use std::panic; @@ -1063,6 +1064,27 @@ impl Drop for TestContext { // Print the chats if runtime still exists. handle.block_on(async move { self.print_chats().await; + + // If you set this to true, and a test fails, + // the sql databases will be saved into the current working directory + // so that you can examine them. + if std::env::var("DELTACHAT_SAVE_TMP_DB").is_ok() { + let _: u32 = self + .sql + .query_get_value("PRAGMA wal_checkpoint;", ()) + .await + .unwrap() + .unwrap(); + + let from = self.get_dbfile(); + let target = current_dir() + .unwrap() + .join(format!("test-account-{}.db", self.name())); + tokio::fs::copy(from, &target).await.unwrap(); + eprintln!("Copied database from {from:?} to {target:?}\n"); + } else { + eprintln!("Hint: If you want to examine the database files, set environment variable DELTACHAT_SAVE_TMP_DB=1\n") + } }); } });