Skip to content

Commit 037eb24

Browse files
committed
Use temp-env to prevent the modification of shared state in tests
1 parent 469c0c0 commit 037eb24

4 files changed

Lines changed: 75 additions & 23 deletions

File tree

Cargo.lock

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ ureq = "2"
3232
hostname = "0.4"
3333

3434
[dev-dependencies]
35+
temp-env = "0.3"
3536

3637
[profile.release]
3738
opt-level = 3

src/config.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,18 @@ history_days = 90
201201

202202
#[test]
203203
fn test_claude_config_dir_env_override() {
204-
std::env::set_var("CLAUDE_CONFIG_DIR", "/tmp/custom-claude");
205-
let dir = claude_config_dir().unwrap();
206-
std::env::remove_var("CLAUDE_CONFIG_DIR");
207-
assert_eq!(dir, PathBuf::from("/tmp/custom-claude"));
204+
temp_env::with_var("CLAUDE_CONFIG_DIR", Some("/tmp/custom-claude"), || {
205+
let dir = claude_config_dir().unwrap();
206+
assert_eq!(dir, PathBuf::from("/tmp/custom-claude"));
207+
});
208208
}
209209

210210
#[test]
211211
fn test_claude_config_dir_default() {
212-
std::env::remove_var("CLAUDE_CONFIG_DIR");
213-
let dir = claude_config_dir().unwrap();
214-
let home = dirs::home_dir().unwrap();
215-
assert_eq!(dir, home.join(".claude"));
212+
temp_env::with_var_unset("CLAUDE_CONFIG_DIR", || {
213+
let dir = claude_config_dir().unwrap();
214+
let home = dirs::home_dir().unwrap();
215+
assert_eq!(dir, home.join(".claude"));
216+
});
216217
}
217218
}

src/tracking.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,27 +1291,20 @@ mod tests {
12911291
// 7. get_db_path respects environment variable RTK_DB_PATH
12921292
#[test]
12931293
fn test_custom_db_path_env() {
1294-
use std::env;
1295-
12961294
let custom_path = "/tmp/rtk_test_custom.db";
1297-
env::set_var("RTK_DB_PATH", custom_path);
1298-
1299-
let db_path = get_db_path().expect("Failed to get db path");
1300-
assert_eq!(db_path, PathBuf::from(custom_path));
1301-
1302-
env::remove_var("RTK_DB_PATH");
1295+
temp_env::with_var("RTK_DB_PATH", Some(custom_path), || {
1296+
let db_path = get_db_path().expect("Failed to get db path");
1297+
assert_eq!(db_path, PathBuf::from(custom_path));
1298+
});
13031299
}
13041300

13051301
// 8. get_db_path falls back to default when no custom config
13061302
#[test]
13071303
fn test_default_db_path() {
1308-
use std::env;
1309-
1310-
// Ensure no env var is set
1311-
env::remove_var("RTK_DB_PATH");
1312-
1313-
let db_path = get_db_path().expect("Failed to get db path");
1314-
assert!(db_path.ends_with("rtk/history.db"));
1304+
temp_env::with_var_unset("RTK_DB_PATH", || {
1305+
let db_path = get_db_path().expect("Failed to get db path");
1306+
assert!(db_path.ends_with("rtk/history.db"));
1307+
});
13151308
}
13161309

13171310
// 9. project_filter_params uses GLOB pattern with * wildcard // added

0 commit comments

Comments
 (0)