fedimint_testing_core/
lib.rs

1pub mod db;
2pub mod envs;
3
4use std::path::PathBuf;
5use std::{env, fs};
6
7use envs::FM_TEST_DIR_ENV;
8use tempfile::TempDir;
9
10/// If `FM_TEST_DIR` is set, use it as a base, otherwise use a tempdir
11///
12/// Callers must hold onto the tempdir until it is no longer needed
13pub fn test_dir(pathname: &str) -> (PathBuf, Option<TempDir>) {
14    let (parent, maybe_tmp_dir_guard) = if let Ok(directory) = env::var(FM_TEST_DIR_ENV) {
15        (directory, None)
16    } else {
17        let random = format!("test-{}", rand::random::<u64>());
18        let guard = tempfile::Builder::new().prefix(&random).tempdir().unwrap();
19        let directory = guard.path().to_str().unwrap().to_owned();
20        (directory, Some(guard))
21    };
22    let fullpath = PathBuf::from(parent).join(pathname);
23    fs::create_dir_all(fullpath.clone()).expect("Can make dirs");
24    (fullpath, maybe_tmp_dir_guard)
25}