@@ -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