fedimint_testing_core/
lib.rs

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