Skip to content

Commit 0dd4eac

Browse files
committed
Enhance strip_trailing_separator to handle UNC and extended-length paths on Windows
1 parent 01fa883 commit 0dd4eac

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

crates/pet-fs/src/path.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ pub fn strip_trailing_separator<P: AsRef<Path>>(path: P) -> PathBuf {
3030

3131
#[cfg(windows)]
3232
{
33-
// On Windows, preserve root paths like "C:\"
33+
// On Windows, preserve root paths (e.g. "C:\", "\\server\", "\\?\C:\")
3434
let mut result = path_str.to_string();
35-
while result.len() > 3 && (result.ends_with('\\') || result.ends_with('/')) {
35+
while (result.ends_with('\\') || result.ends_with('/'))
36+
&& Path::new(&result).parent().is_some()
37+
{
3638
result.pop();
3739
}
3840
PathBuf::from(result)
@@ -354,6 +356,31 @@ mod tests {
354356
assert_eq!(strip_trailing_separator("C:\\"), PathBuf::from("C:\\"));
355357
}
356358

359+
#[test]
360+
#[cfg(windows)]
361+
fn test_strip_trailing_separator_windows_unc_paths() {
362+
// UNC path with trailing separator - should strip it
363+
assert_eq!(
364+
strip_trailing_separator("\\\\server\\share\\folder\\"),
365+
PathBuf::from("\\\\server\\share\\folder")
366+
);
367+
// UNC root path should be preserved
368+
assert_eq!(
369+
strip_trailing_separator("\\\\server\\share\\"),
370+
PathBuf::from("\\\\server\\share\\")
371+
);
372+
// Extended-length path root should be preserved
373+
assert_eq!(
374+
strip_trailing_separator("\\\\?\\C:\\"),
375+
PathBuf::from("\\\\?\\C:\\")
376+
);
377+
// Extended-length path with subfolder - should strip trailing separator
378+
assert_eq!(
379+
strip_trailing_separator("\\\\?\\C:\\Users\\"),
380+
PathBuf::from("\\\\?\\C:\\Users")
381+
);
382+
}
383+
357384
// ==================== norm_case tests ====================
358385

359386
#[test]

0 commit comments

Comments
 (0)