Skip to content

Commit

Permalink
chore: clear test data directory once for each test run, implement te…
Browse files Browse the repository at this point in the history
…st name thread local to further specify data directory
  • Loading branch information
Toromyx committed Mar 5, 2024
1 parent 96980be commit ad1e530
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,16 @@ fn setup() -> tauri::Builder<Wry> {

#[cfg(test)]
mod tests {
use std::{sync::Once, thread};
use std::{cell::RefCell, sync::Once, thread};

use super::*;

static RUN_ONCE: Once = Once::new();

thread_local! {
pub static TEST_NAME: RefCell<Option<String>> = RefCell::new(None);
}

/// Run the tauri app, but only once.
pub fn run() {
RUN_ONCE.call_once(|| {
Expand Down
22 changes: 19 additions & 3 deletions src-tauri/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,29 @@ pub fn app_data_dir() -> PathBuf {
#[cfg(any(debug_assertions, test))]
{
#[cfg(test)]
let env = ".TEST";
{
dir.push(".TEST");
tests::CLEAR_DATA_DIR_ONCE.call_once(|| {
std::fs::remove_dir_all(&dir).unwrap();
});
crate::tests::TEST_NAME.with_borrow(|test_name_option| {
if let Some(test_name) = test_name_option {
dir.push(test_name)
}
});
}
#[cfg(not(test))]
let env = ".DEVELOPMENT";
dir.push(env);
dir.push(".DEVELOPMENT");
}
if let Err(err) = create_dir_all(&dir) {
panic!("Could not create app data directory: {err}");
}
dir
}

#[cfg(test)]
mod tests {
use std::sync::Once;

pub static CLEAR_DATA_DIR_ONCE: Once = Once::new();
}

0 comments on commit ad1e530

Please sign in to comment.