Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(cli): serialize every JAN_DATA_FOLDER test on one lock

thinhlpg0f75f3632a0e
diff --git a/src-tauri/src/core/app/commands.rs b/src-tauri/src/core/app/commands.rs
index a1db403a1..aef1a3e5f 100644
--- a/src-tauri/src/core/app/commands.rs
+++ b/src-tauri/src/core/app/commands.rs
@@ -119,6 +119,29 @@ pub fn resolve_config_file_path() -> PathBuf {
app_data.join(CONFIGURATION_FILE_NAME)
}
+/// Run `f` with `JAN_DATA_FOLDER` pointed at a fresh temp directory, restoring
+/// the previous value afterwards. Serialized on `SECRET_STORE_TEST_LOCK`, the
+/// one lock every `JAN_DATA_FOLDER` mutator takes: the env is process-wide and
+/// Rust runs tests on threads, so a private lock here would exclude only the
+/// other callers of this helper while the secret-store tests redirected the
+/// folder (and dropped its temp dir) underneath a run already in progress.
+#[cfg(all(test, feature = "cli"))]
+pub(crate) fn with_temp_data_folder<T>(f: impl FnOnce(&std::path::Path) -> T) -> T {
+ let _guard = crate::core::server::provider_secrets::SECRET_STORE_TEST_LOCK
+ .lock()
+ .unwrap_or_else(|e| e.into_inner());
+
+ let dir = tempfile::tempdir().expect("tempdir");
+ let prev = std::env::var_os("JAN_DATA_FOLDER");
+ std::env::set_var("JAN_DATA_FOLDER", dir.path());
+ let result = f(dir.path());
+ match prev {
+ Some(p) => std::env::set_var("JAN_DATA_FOLDER", p),
+ None => std::env::remove_var("JAN_DATA_FOLDER"),
+ }
+ result
+}
+
/// Resolve the Jan data folder path without an AppHandle (for CLI use).
/// Reads AppConfiguration from the config file; falls back to the default location.
pub fn resolve_jan_data_folder() -> PathBuf {
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rs
index 71dbdfcbe..ff729f018 100644
--- a/src-tauri/src/core/cli/tui.rs
+++ b/src-tauri/src/core/cli/tui.rs
@@ -19376,7 +19376,9 @@ mod tests {
/// but keeps the provider entry and its config, distinct from `d` delete.
#[test]
fn provider_settings_x_logout_clears_credential_and_keeps_config() {
- crate::core::agent::global_config::with_temp_home(|_| {
+ // Seeds the process-global secret store, so it has to hold the same
+ // lock (and get its own data folder) as every other credential test.
+ with_isolated_login_state(|| {
use crate::core::cli::auth::{Credential, CredentialStore};
crate::core::agent::global_config::set_provider(
"myprovider",
diff --git a/src-tauri/src/core/server/provider_secrets.rs b/src-tauri/src/core/server/provider_secrets.rs
index 471dff1a4..2cb9944b6 100644
--- a/src-tauri/src/core/server/provider_secrets.rs
+++ b/src-tauri/src/core/server/provider_secrets.rs
@@ -278,10 +278,12 @@ mod tests {
use std::sync::MutexGuard;
+ // Field order is drop order: `_dir` must be removed and the env restored
+ // while the lock is still held, so `_guard` has to be the last field.
struct TempDataFolder {
- _guard: MutexGuard<'static, ()>,
prev_data_folder: Option<String>,
_dir: tempfile::TempDir,
+ _guard: MutexGuard<'static, ()>,
}
impl TempDataFolder {
@@ -293,9 +295,9 @@ mod tests {
// relying on it fails the fallback tests on macOS/Windows.
std::env::set_var("JAN_DATA_FOLDER", dir.path());
Self {
- _guard: guard,
prev_data_folder,
_dir: dir,
+ _guard: guard,
}
}
}