soft-canonicalize 0.5.5

Path canonicalization that works with non-existing paths.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#[cfg(test)]
mod test_python_inspired {
    use crate::soft_canonicalize;
    use std::sync::Mutex;

    // Global mutex to serialize tests that change working directory
    static WORKING_DIR_MUTEX: Mutex<()> = Mutex::new(());
    use std::{env, fs};
    use tempfile::TempDir;

    /// Test inspired by Python's test_resolve_nonexist_relative_issue38671
    /// Tests non-existing relative paths resolve correctly
    #[test]
    fn test_resolve_nonexist_relative() {
        // Serialize tests that change working directory
        let _lock = WORKING_DIR_MUTEX.lock().unwrap();

        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();

        // Test relative non-existing path from a real directory
        let original_cwd = env::current_dir().unwrap();

        let test_result = std::panic::catch_unwind(|| {
            // Only change directory if it exists and is accessible
            if base_path.exists() {
                if let Ok(()) = env::set_current_dir(base_path) {
                    let result = soft_canonicalize("non/exist/path.txt");
                    assert!(result.is_ok());
                    let resolved = result.unwrap();
                    assert!(resolved.is_absolute());
                    assert!(resolved.ends_with("non/exist/path.txt"));
                }
            }
        });

        // Always try to restore the original directory
        let _ = env::set_current_dir(original_cwd);

        // Re-raise any panic that occurred during the test
        if let Err(e) = test_result {
            std::panic::resume_unwind(e);
        }
    }

    /// Test inspired by Python's test_resolve_common with strict=False
    /// Tests various combinations of existing and non-existing components
    #[test]
    fn test_resolve_mixed_existing_nonexisting() {
        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();

        // Create some existing structure
        let existing_dir = base_path.join("existing_dir");
        fs::create_dir(&existing_dir).unwrap();
        let existing_file = existing_dir.join("existing_file.txt");
        fs::write(&existing_file, "test").unwrap();

        // Test: existing_dir/existing_file/non/existing/path
        let mixed_path = existing_file.join("non/existing/path.txt");
        let result = soft_canonicalize(mixed_path);
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert!(resolved.is_absolute());
        assert!(resolved.to_string_lossy().contains("existing_file.txt"));
        // Check for platform-appropriate path separators
        #[cfg(windows)]
        assert!(resolved
            .to_string_lossy()
            .contains("non\\existing\\path.txt"));
        #[cfg(not(windows))]
        assert!(resolved.to_string_lossy().contains("non/existing/path.txt"));

        // Test: existing_dir/non_existing/file.txt
        let partial_path = existing_dir.join("non_existing/file.txt");
        let result = soft_canonicalize(partial_path);
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert!(resolved.is_absolute());
        assert!(resolved.to_string_lossy().contains("existing_dir"));
        // Check for platform-appropriate path separators
        #[cfg(windows)]
        assert!(resolved
            .to_string_lossy()
            .contains("non_existing\\file.txt"));
        #[cfg(not(windows))]
        assert!(resolved.to_string_lossy().contains("non_existing/file.txt"));
    }

    /// Test inspired by Python's test_resolve_dot with strict=False
    /// Tests resolution of symlinks pointing to current directory
    #[cfg(unix)]
    #[test]
    fn test_resolve_dot_symlinks() {
        use std::os::unix::fs::symlink;

        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();

        // Create a simple symlink to current directory
        let link_dot = base_path.join("link_to_dot");
        symlink(".", &link_dot).unwrap();

        // Test resolving through the symlink with non-existing suffix
        let test_path = link_dot.join("some/nonexisting/path.txt");
        let result = soft_canonicalize(test_path);
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert!(resolved.is_absolute());

        // Should resolve to base_path/some/nonexisting/path.txt
        let expected_suffix = base_path.join("some/nonexisting/path.txt");

        // Handle macOS /private prefix for /tmp and /var paths
        let resolved_str = resolved.to_string_lossy();
        let expected_str = expected_suffix.to_string_lossy();
        assert!(
            resolved_str == expected_str || resolved_str == format!("/private{expected_str}"),
            "Mismatch:\n  left:  {resolved_str}\n right: {expected_str} or /private{expected_str}"
        );
    }

    /// Test inspired by Python's resolve with parent directory traversal
    /// Tests that .. components are handled correctly in mixed scenarios
    /// Consolidates: path_traversal::test_parent_directory_traversal
    #[test]
    fn test_resolve_parent_traversal_mixed() {
        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();

        // Test basic parent directory traversal (from path_traversal module)
        // Create: base_path/level1/level2/
        let level1 = base_path.join("level1");
        let level2 = level1.join("level2");
        fs::create_dir_all(&level2).unwrap();

        // Test path: level2/subdir/../../../target.txt -> base_path/target.txt
        let basic_traversal = level2
            .join("subdir")
            .join("..")
            .join("..")
            .join("..")
            .join("target.txt");

        let result = soft_canonicalize(basic_traversal);
        assert!(result.is_ok());
        let resolved = result.unwrap();
        let expected = fs::canonicalize(base_path).unwrap().join("target.txt");

        #[cfg(not(feature = "dunce"))]
        {
            assert_eq!(resolved, expected, "Without dunce: exact match");
        }

        #[cfg(feature = "dunce")]
        {
            #[cfg(windows)]
            {
                let resolved_str = resolved.to_string_lossy();
                let expected_str = expected.to_string_lossy();
                assert!(!resolved_str.starts_with(r"\\?\"), "dunce should simplify");
                assert!(expected_str.starts_with(r"\\?\"), "std returns UNC");
            }
            #[cfg(not(windows))]
            {
                assert_eq!(resolved, expected);
            }
        }

        // Test advanced: Create nested directory structure for complex scenarios
        let dir_a = base_path.join("dirA");
        let dir_b = base_path.join("dirB");
        fs::create_dir(&dir_a).unwrap();
        fs::create_dir(dir_b).unwrap();

        // Test: dirA/../dirB/non_existing_file.txt
        let traversal_path = dir_a.join("../dirB/non_existing_file.txt");
        let result = soft_canonicalize(traversal_path);
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert!(resolved.is_absolute());
        assert!(resolved.to_string_lossy().contains("dirB"));
        assert!(resolved.to_string_lossy().contains("non_existing_file.txt"));
        assert!(!resolved.to_string_lossy().contains("dirA"));
        assert!(!resolved.to_string_lossy().contains(".."));

        // Test multiple parent traversals with non-existing components
        let complex_traversal = dir_a
            .join("../../../temp/../")
            .join(base_path.file_name().unwrap())
            .join("dirB/deep/non/existing.txt");
        let result = soft_canonicalize(complex_traversal);
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert!(resolved.is_absolute());
        assert!(!resolved.to_string_lossy().contains(".."));
    }

    /// Test inspired by Python's handling of symlink loops with strict=False
    /// Tests that symlink loops are detected and return the looping path
    #[cfg(unix)]
    #[test]
    fn test_resolve_symlink_loops_with_suffix() {
        use std::os::unix::fs::symlink;

        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();

        // Create symlink loop: linkX -> linkX/inside
        let link_x = base_path.join("linkX");
        symlink("linkX/inside", &link_x).unwrap();

        // Python's strict=False handles this by returning the path as-is when hitting a loop
        let loop_path = link_x.join("foo/bar.txt");
        let result = soft_canonicalize(loop_path);

        // Our implementation should detect the loop and handle it gracefully
        // The exact behavior may vary, but it should not infinite loop
        assert!(result.is_ok() || result.is_err());
        if let Ok(resolved) = result {
            // If successful, should be absolute and contain the non-existing suffix
            assert!(resolved.is_absolute());
        }
    }

    /// Test inspired by Python's realpath with non-terminal file handling
    /// Tests paths that traverse through files (which should fail in strict mode)
    #[test]
    fn test_resolve_through_file() {
        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();

        // Create a file
        let file_path = base_path.join("regular_file.txt");
        fs::write(&file_path, "test content").unwrap();

        // Try to resolve a path that goes "through" the file
        let through_file = file_path.join("subdir/file.txt");
        let result = soft_canonicalize(through_file);

        // Our implementation should handle this - the existing portion resolves to the file,
        // and the non-existing portion is appended
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert!(resolved.is_absolute());
        assert!(resolved.to_string_lossy().contains("regular_file.txt"));
        // Check for platform-appropriate path separators
        #[cfg(windows)]
        assert!(resolved.to_string_lossy().contains("subdir\\file.txt"));
        #[cfg(not(windows))]
        assert!(resolved.to_string_lossy().contains("subdir/file.txt"));
    }

    /// Test inspired by Python's handling of invalid path characters
    /// Tests edge cases with unusual but valid path components  
    #[test]
    fn test_resolve_unusual_characters() {
        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();

        // Test paths with spaces, unicode, etc.
        let unusual_paths = vec![
            "path with spaces/file.txt",
            "path-with-dashes/file.txt",
            "path_with_underscores/file.txt",
            "pathwith中文/file.txt",
            "path.with.dots/file.txt",
        ];

        for unusual_path in unusual_paths {
            let full_path = base_path.join(unusual_path);
            let result = soft_canonicalize(&full_path);
            assert!(result.is_ok(), "Failed to resolve path: {unusual_path}");
            let resolved = result.unwrap();
            assert!(resolved.is_absolute());
            assert!(resolved.to_string_lossy().contains("file.txt"));
        }
    }

    /// Test inspired by Python's handling of very deep paths
    /// Tests performance and correctness with deep directory structures
    /// Consolidates: basic_functionality::test_deeply_non_existing_path
    #[test]
    fn test_resolve_deep_paths() {
        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();

        // Test basic deeply non-existing path (from basic_functionality)
        let basic_deep_path = base_path.join("a/b/c/d/e/file.txt");
        let result = soft_canonicalize(&basic_deep_path);
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert!(resolved.is_absolute());
        let expected = fs::canonicalize(base_path)
            .unwrap()
            .join("a/b/c/d/e/file.txt");

        #[cfg(not(feature = "dunce"))]
        {
            assert_eq!(resolved, expected, "Without dunce: exact match");
        }

        #[cfg(feature = "dunce")]
        {
            #[cfg(windows)]
            {
                let resolved_str = resolved.to_string_lossy();
                let expected_str = expected.to_string_lossy();
                assert!(!resolved_str.starts_with(r"\\?\"), "dunce should simplify");
                assert!(expected_str.starts_with(r"\\?\"), "std returns UNC");
            }
            #[cfg(not(windows))]
            {
                assert_eq!(resolved, expected);
            }
        }

        // Test advanced: Create a moderately deep existing structure
        let mut current = base_path.to_path_buf();
        for i in 0..10 {
            current = current.join(format!("level_{i}"));
            fs::create_dir(&current).unwrap();
        }

        // Add a very deep non-existing suffix
        let mut deep_path = current;
        for i in 0..50 {
            deep_path = deep_path.join(format!("deep_{i}"));
        }
        deep_path = deep_path.join("final_file.txt");

        let result = soft_canonicalize(&deep_path);
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert!(resolved.is_absolute());
        assert!(resolved.to_string_lossy().contains("level_9")); // Should contain the deepest existing level
        assert!(resolved.to_string_lossy().contains("deep_49")); // Should contain the deepest non-existing level
        assert!(resolved.to_string_lossy().contains("final_file.txt"));
    }

    /// Test inspired by Python's current working directory edge cases
    /// Tests resolution from different working directories
    #[test]
    fn test_resolve_from_different_cwd() {
        // Serialize tests that change working directory
        let _lock = WORKING_DIR_MUTEX.lock().unwrap();

        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();

        // Create subdirectories
        let sub_dir1 = base_path.join("sub1");
        let sub_dir2 = base_path.join("sub2");
        fs::create_dir(&sub_dir1).unwrap();
        fs::create_dir(&sub_dir2).unwrap();

        let original_cwd = env::current_dir().unwrap();

        // Wrap in a closure to ensure proper cleanup
        let test_result = std::panic::catch_unwind(|| {
            // Test relative resolution from sub_dir1
            if sub_dir1.exists() {
                env::set_current_dir(&sub_dir1).unwrap();
            }
            let result1 = soft_canonicalize("../sub2/non_existing.txt");
            assert!(result1.is_ok());
            let resolved1 = result1.unwrap();
            assert!(resolved1.is_absolute());
            assert!(resolved1.to_string_lossy().contains("sub2"));
            assert!(resolved1.to_string_lossy().contains("non_existing.txt"));

            // Test relative resolution from sub_dir2
            if sub_dir2.exists() {
                env::set_current_dir(&sub_dir2).unwrap();
            }
            let result2 = soft_canonicalize("../sub1/non_existing.txt");
            assert!(result2.is_ok());
            let resolved2 = result2.unwrap();
            assert!(resolved2.is_absolute());
            assert!(resolved2.to_string_lossy().contains("sub1"));
        });

        // Always restore the original directory, even if the test panicked
        let _ = env::set_current_dir(original_cwd);

        // Re-raise any panic that occurred
        if let Err(e) = test_result {
            std::panic::resume_unwind(e);
        }
    }

    /// Test inspired by Python's empty and minimal path handling
    /// Tests edge cases with empty components and minimal paths
    #[test]
    fn test_resolve_minimal_paths() {
        // Serialize tests that change working directory
        let _lock = WORKING_DIR_MUTEX.lock().unwrap();

        use std::env;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let original_cwd = env::current_dir().unwrap();

        // Use a closure with proper cleanup to ensure we always restore the directory
        let test_result = std::panic::catch_unwind(|| {
            // Only change directory if it exists and is accessible
            if temp_dir.path().exists() {
                if let Ok(()) = env::set_current_dir(temp_dir.path()) {
                    // Successfully changed directory

                    // Test current directory
                    let result = soft_canonicalize(".");
                    assert!(result.is_ok());
                    let resolved = result.unwrap();
                    assert!(resolved.is_absolute());

                    // Test parent directory
                    let result = soft_canonicalize("..");
                    assert!(result.is_ok());
                    let resolved = result.unwrap();
                    assert!(resolved.is_absolute());

                    // Test single file name
                    let result = soft_canonicalize("single_file.txt");
                    assert!(result.is_ok());
                    let resolved = result.unwrap();
                    assert!(resolved.is_absolute());
                    assert!(resolved.to_string_lossy().contains("single_file.txt"));
                }
            }
        });

        // Always try to restore the original directory, but don't panic if it fails
        // On CI systems, the original directory might become inaccessible
        let _ = env::set_current_dir(original_cwd);

        // Re-raise any panic that occurred during the test
        if let Err(e) = test_result {
            std::panic::resume_unwind(e);
        }
    }

    /// Test to ensure std::fs::canonicalize compatibility for existing paths
    /// This validates that we provide a superset of std functionality
    /// Consolidates: api_compatibility::test_std_compatibility_api
    #[test]
    fn test_std_compatibility_existing_paths() {
        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();

        // Create existing file and directory
        let existing_file = base_path.join("existing.txt");
        let existing_dir = base_path.join("existing_dir");
        fs::write(&existing_file, "test").unwrap();
        fs::create_dir(&existing_dir).unwrap();

        // Test that our results match std::fs::canonicalize for existing paths
        for existing_path in [&existing_file, &existing_dir, base_path] {
            let std_result = fs::canonicalize(existing_path);
            let our_result = soft_canonicalize(existing_path);

            assert!(
                std_result.is_ok(),
                "std::fs::canonicalize should work for existing path"
            );
            assert!(
                our_result.is_ok(),
                "soft_canonicalize should work for existing path"
            );

            // Results should be equivalent (modulo potential differences in canonicalization)
            let std_canonical = std_result.unwrap();
            let our_canonical = our_result.unwrap();

            // Both should be absolute
            assert!(std_canonical.is_absolute());
            assert!(our_canonical.is_absolute());

            // Test API compatibility patterns (from api_compatibility module)
            // Pattern 1: String literal
            let str_literal = existing_path.to_string_lossy();
            let our_str_result = soft_canonicalize(str_literal.as_ref()).unwrap();
            let std_str_result = fs::canonicalize(str_literal.as_ref()).unwrap();

            #[cfg(not(feature = "dunce"))]
            {
                assert_eq!(our_str_result, std_str_result, "Without dunce: exact match");
            }

            #[cfg(feature = "dunce")]
            {
                #[cfg(windows)]
                {
                    let our_str = our_str_result.to_string_lossy();
                    let std_str = std_str_result.to_string_lossy();
                    assert!(!our_str.starts_with(r"\\?\"), "dunce should simplify");
                    assert!(std_str.starts_with(r"\\?\"), "std returns UNC");
                }
                #[cfg(not(windows))]
                {
                    assert_eq!(our_str_result, std_str_result);
                }
            }

            // Pattern 2: PathBuf by value
            let pathbuf = existing_path.to_path_buf();
            let our_pathbuf_result = soft_canonicalize(pathbuf.clone()).unwrap();
            let std_pathbuf_result = fs::canonicalize(pathbuf).unwrap();

            #[cfg(not(feature = "dunce"))]
            {
                assert_eq!(
                    our_pathbuf_result, std_pathbuf_result,
                    "Without dunce: exact match"
                );
            }

            #[cfg(feature = "dunce")]
            {
                #[cfg(windows)]
                {
                    let our_buf_str = our_pathbuf_result.to_string_lossy();
                    let std_buf_str = std_pathbuf_result.to_string_lossy();
                    assert!(!our_buf_str.starts_with(r"\\?\"), "dunce should simplify");
                    assert!(std_buf_str.starts_with(r"\\?\"), "std returns UNC");
                }
                #[cfg(not(windows))]
                {
                    assert_eq!(our_pathbuf_result, std_pathbuf_result);
                }
            }

            // Pattern 3: &PathBuf
            let pathbuf = existing_path.to_path_buf();
            let our_ref_result = soft_canonicalize(&pathbuf).unwrap();
            let std_ref_result = fs::canonicalize(&pathbuf).unwrap();

            #[cfg(not(feature = "dunce"))]
            {
                assert_eq!(our_ref_result, std_ref_result, "Without dunce: exact match");
            }

            #[cfg(feature = "dunce")]
            {
                #[cfg(windows)]
                {
                    let our_ref_str = our_ref_result.to_string_lossy();
                    let std_ref_str = std_ref_result.to_string_lossy();
                    assert!(!our_ref_str.starts_with(r"\\?\"), "dunce should simplify");
                    assert!(std_ref_str.starts_with(r"\\?\"), "std returns UNC");
                }
                #[cfg(not(windows))]
                {
                    assert_eq!(our_ref_result, std_ref_result);
                }
            }
        }
    }
}