Skip to content

Commit 15d2d57

Browse files
committed
fix(cortex-common): fix Windows test compatibility for cwd_guard, dirs, and path_utils
- Use dunce::canonicalize() in cwd_guard tests to avoid UNC path prefix on Windows (\\?\C:\...) which differs from env::current_dir() format - Use std::env::temp_dir() for cross-platform temp directory in dirs test_env_override instead of hardcoded Unix path /tmp - Add cfg_attr ignore for test_ensure_parent_dir_root_path on Windows since Unix root path /file.txt is not applicable on Windows
1 parent d907acc commit 15d2d57

File tree

5 files changed

+14
-8
lines changed

5 files changed

+14
-8
lines changed

Cargo.lock

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

cortex-common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ async = ["tokio"]
3939
[dev-dependencies]
4040
tempfile = { workspace = true }
4141
serial_test = { workspace = true }
42+
dunce = { workspace = true }

cortex-common/src/cwd_guard.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,12 @@ mod tests {
314314
fn test_cwd_guard_restores() {
315315
let original = env::current_dir().unwrap();
316316
let temp_dir = TempDir::new().unwrap();
317+
// Use dunce::canonicalize on Windows to avoid UNC paths (\\?\C:\...)
318+
let expected_temp_path = dunce::canonicalize(temp_dir.path()).unwrap();
317319

318320
{
319321
let _guard = CwdGuard::new(temp_dir.path()).unwrap();
320-
assert_eq!(
321-
env::current_dir().unwrap(),
322-
temp_dir.path().canonicalize().unwrap()
323-
);
322+
assert_eq!(env::current_dir().unwrap(), expected_temp_path);
324323
}
325324

326325
// Should be back to original after guard dropped
@@ -348,7 +347,8 @@ mod tests {
348347
fn test_cwd_guard_disabled() {
349348
let original = env::current_dir().unwrap();
350349
let temp_dir = TempDir::new().unwrap();
351-
let temp_path = temp_dir.path().canonicalize().unwrap();
350+
// Use dunce::canonicalize on Windows to avoid UNC paths (\\?\C:\...)
351+
let temp_path = dunce::canonicalize(temp_dir.path()).unwrap();
352352

353353
{
354354
let mut guard = CwdGuard::new(temp_dir.path()).unwrap();
@@ -382,7 +382,8 @@ mod tests {
382382
fn test_in_directory() {
383383
let original = env::current_dir().unwrap();
384384
let temp_dir = TempDir::new().unwrap();
385-
let temp_path = temp_dir.path().canonicalize().unwrap();
385+
// Use dunce::canonicalize on Windows to avoid UNC paths (\\?\C:\...)
386+
let temp_path = dunce::canonicalize(temp_dir.path()).unwrap();
386387

387388
let result = in_directory(temp_dir.path(), || env::current_dir().unwrap()).unwrap();
388389

cortex-common/src/dirs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,14 @@ mod tests {
218218

219219
#[test]
220220
fn test_env_override() {
221+
// Use a cross-platform temp directory path for the test
222+
let test_path = std::env::temp_dir().join("test-cortex");
221223
// SAFETY: This test runs in isolation and only modifies test-specific env vars
222224
unsafe {
223-
std::env::set_var("CORTEX_HOME", "/tmp/test-cortex");
225+
std::env::set_var("CORTEX_HOME", &test_path);
224226
}
225227
let dirs = AppDirs::new().unwrap();
226-
assert_eq!(dirs.config_dir, PathBuf::from("/tmp/test-cortex"));
228+
assert_eq!(dirs.config_dir, test_path);
227229
unsafe {
228230
std::env::remove_var("CORTEX_HOME");
229231
}

cortex-common/src/path_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ mod tests {
514514
}
515515

516516
#[test]
517+
#[cfg_attr(windows, ignore = "Unix root path not applicable on Windows")]
517518
fn test_ensure_parent_dir_root_path() {
518519
let path = Path::new("/file.txt");
519520

0 commit comments

Comments
 (0)