Skip to content

Commit e5e0f0c

Browse files
authored
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.
1 parent 0bac4ac commit e5e0f0c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/test_utils.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! This private module is only compiled for test runs.
44
use std::collections::{BTreeMap, HashSet};
5+
use std::env::current_dir;
56
use std::fmt::Write;
67
use std::ops::{Deref, DerefMut};
78
use std::panic;
@@ -1063,6 +1064,27 @@ impl Drop for TestContext {
10631064
// Print the chats if runtime still exists.
10641065
handle.block_on(async move {
10651066
self.print_chats().await;
1067+
1068+
// If you set this to true, and a test fails,
1069+
// the sql databases will be saved into the current working directory
1070+
// so that you can examine them.
1071+
if std::env::var("DELTACHAT_SAVE_TMP_DB").is_ok() {
1072+
let _: u32 = self
1073+
.sql
1074+
.query_get_value("PRAGMA wal_checkpoint;", ())
1075+
.await
1076+
.unwrap()
1077+
.unwrap();
1078+
1079+
let from = self.get_dbfile();
1080+
let target = current_dir()
1081+
.unwrap()
1082+
.join(format!("test-account-{}.db", self.name()));
1083+
tokio::fs::copy(from, &target).await.unwrap();
1084+
eprintln!("Copied database from {from:?} to {target:?}\n");
1085+
} else {
1086+
eprintln!("Hint: If you want to examine the database files, set environment variable DELTACHAT_SAVE_TMP_DB=1\n")
1087+
}
10661088
});
10671089
}
10681090
});

0 commit comments

Comments
 (0)