Skip to content

Commit a74471f

Browse files
echobtfactorydroid
andauthored
fix(tests): fix Windows CI test failures for cwd_guard and styled_output (#482)
- Fix cwd_guard tests on Windows: Add helper functions (normalized_current_dir and normalize_path) that use dunce::canonicalize to normalize paths before comparison, handling Windows 8.3 paths and UNC path format differences - Fix styled_output tests: Add serial_test dependency to cortex-cli and #[serial] attribute to tests that manipulate NO_COLOR environment variable to prevent race conditions when tests run in parallel Fixes test failures: - cwd_guard::tests::test_cwd_guard_disabled - cwd_guard::tests::test_cwd_guard_restores - cwd_guard::tests::test_in_directory - styled_output::tests::test_colors_disabled Co-authored-by: Droid Agent <droid@factory.ai>
1 parent 9d60b0a commit a74471f

4 files changed

Lines changed: 40 additions & 23 deletions

File tree

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-cli/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,6 @@ signal-hook = "0.3"
9999

100100
[build-dependencies]
101101
chrono = "0.4"
102+
103+
[dev-dependencies]
104+
serial_test = { workspace = true }

cortex-cli/src/styled_output.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ pub fn styled_icon(msg_type: MessageType) -> String {
382382
#[cfg(test)]
383383
mod tests {
384384
use super::*;
385+
use serial_test::serial;
385386

386387
#[test]
387388
fn test_message_type_icons() {
@@ -393,6 +394,7 @@ mod tests {
393394
}
394395

395396
#[test]
397+
#[serial]
396398
fn test_format_styled_no_color() {
397399
// When colors are disabled, should just return icon + message
398400
// SAFETY: These tests run serially and we restore env vars immediately
@@ -404,6 +406,7 @@ mod tests {
404406
}
405407

406408
#[test]
409+
#[serial]
407410
fn test_colors_disabled() {
408411
// SAFETY: These tests run serially and we restore env vars immediately
409412
unsafe { std::env::set_var("NO_COLOR", "1") };

cortex-common/src/cwd_guard.rs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -309,27 +309,39 @@ mod tests {
309309
use serial_test::serial;
310310
use tempfile::TempDir;
311311

312+
/// Helper to get normalized current directory for comparison.
313+
/// On Windows, uses dunce::canonicalize to handle 8.3 paths and UNC paths.
314+
fn normalized_current_dir() -> PathBuf {
315+
let cwd = env::current_dir().unwrap();
316+
dunce::canonicalize(&cwd).unwrap_or(cwd)
317+
}
318+
319+
/// Helper to normalize a path for comparison.
320+
/// On Windows, uses dunce::canonicalize to handle 8.3 paths and UNC paths.
321+
fn normalize_path(path: &Path) -> PathBuf {
322+
dunce::canonicalize(path).unwrap_or_else(|_| path.to_path_buf())
323+
}
324+
312325
#[test]
313326
#[serial]
314327
fn test_cwd_guard_restores() {
315-
let original = env::current_dir().unwrap();
328+
let original = normalized_current_dir();
316329
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();
330+
let expected_temp_path = normalize_path(temp_dir.path());
319331

320332
{
321333
let _guard = CwdGuard::new(temp_dir.path()).unwrap();
322-
assert_eq!(env::current_dir().unwrap(), expected_temp_path);
334+
assert_eq!(normalized_current_dir(), expected_temp_path);
323335
}
324336

325337
// Should be back to original after guard dropped
326-
assert_eq!(env::current_dir().unwrap(), original);
338+
assert_eq!(normalized_current_dir(), original);
327339
}
328340

329341
#[test]
330342
#[serial]
331343
fn test_cwd_guard_restores_on_panic() {
332-
let original = env::current_dir().unwrap();
344+
let original = normalized_current_dir();
333345
let temp_dir = TempDir::new().unwrap();
334346

335347
let result = std::panic::catch_unwind(|| {
@@ -339,24 +351,23 @@ mod tests {
339351

340352
assert!(result.is_err());
341353
// Should still be back to original
342-
assert_eq!(env::current_dir().unwrap(), original);
354+
assert_eq!(normalized_current_dir(), original);
343355
}
344356

345357
#[test]
346358
#[serial]
347359
fn test_cwd_guard_disabled() {
348-
let original = env::current_dir().unwrap();
360+
let original = normalized_current_dir();
349361
let temp_dir = TempDir::new().unwrap();
350-
// Use dunce::canonicalize on Windows to avoid UNC paths (\\?\C:\...)
351-
let temp_path = dunce::canonicalize(temp_dir.path()).unwrap();
362+
let temp_path = normalize_path(temp_dir.path());
352363

353364
{
354365
let mut guard = CwdGuard::new(temp_dir.path()).unwrap();
355366
guard.disable_restore();
356367
}
357368

358369
// Should NOT be back to original
359-
assert_eq!(env::current_dir().unwrap(), temp_path);
370+
assert_eq!(normalized_current_dir(), temp_path);
360371

361372
// Restore manually
362373
env::set_current_dir(&original).unwrap();
@@ -365,36 +376,35 @@ mod tests {
365376
#[test]
366377
#[serial]
367378
fn test_cwd_guard_restore_now() {
368-
let original = env::current_dir().unwrap();
379+
let original = normalized_current_dir();
369380
let temp_dir = TempDir::new().unwrap();
370381

371382
let mut guard = CwdGuard::new(temp_dir.path()).unwrap();
372383

373384
// Manually restore
374385
guard.restore_now().unwrap();
375-
assert_eq!(env::current_dir().unwrap(), original);
386+
assert_eq!(normalized_current_dir(), original);
376387

377388
// Guard drop should not panic (it's disabled after restore_now)
378389
}
379390

380391
#[test]
381392
#[serial]
382393
fn test_in_directory() {
383-
let original = env::current_dir().unwrap();
394+
let original = normalized_current_dir();
384395
let temp_dir = TempDir::new().unwrap();
385-
// Use dunce::canonicalize on Windows to avoid UNC paths (\\?\C:\...)
386-
let temp_path = dunce::canonicalize(temp_dir.path()).unwrap();
396+
let temp_path = normalize_path(temp_dir.path());
387397

388-
let result = in_directory(temp_dir.path(), || env::current_dir().unwrap()).unwrap();
398+
let result = in_directory(temp_dir.path(), || normalized_current_dir()).unwrap();
389399

390400
assert_eq!(result, temp_path);
391-
assert_eq!(env::current_dir().unwrap(), original);
401+
assert_eq!(normalized_current_dir(), original);
392402
}
393403

394404
#[test]
395405
#[serial]
396406
fn test_in_directory_result() {
397-
let original = env::current_dir().unwrap();
407+
let original = normalized_current_dir();
398408
let temp_dir = TempDir::new().unwrap();
399409

400410
let result: Result<(), io::Error> = in_directory_result(temp_dir.path(), || {
@@ -403,24 +413,24 @@ mod tests {
403413

404414
assert!(result.is_err());
405415
// Should still be back to original
406-
assert_eq!(env::current_dir().unwrap(), original);
416+
assert_eq!(normalized_current_dir(), original);
407417
}
408418

409419
#[test]
410420
#[serial]
411421
fn test_save_current() {
412-
let original = env::current_dir().unwrap();
422+
let original = normalized_current_dir();
413423
let temp_dir = TempDir::new().unwrap();
414424

415425
{
416426
let guard = CwdGuard::save_current().unwrap();
417-
assert_eq!(guard.original(), original);
427+
assert_eq!(normalize_path(guard.original()), original);
418428

419429
// Manually change directory
420430
env::set_current_dir(temp_dir.path()).unwrap();
421431
}
422432

423433
// Should be back to original after guard dropped
424-
assert_eq!(env::current_dir().unwrap(), original);
434+
assert_eq!(normalized_current_dir(), original);
425435
}
426436
}

0 commit comments

Comments
 (0)