Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions crates/pet-fs/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ pub fn strip_trailing_separator<P: AsRef<Path>>(path: P) -> PathBuf {

#[cfg(windows)]
{
// On Windows, preserve root paths like "C:\"
// On Windows, preserve root paths (e.g. "C:\", "\\server\", "\\?\C:\")
let mut result = path_str.to_string();
while result.len() > 3 && (result.ends_with('\\') || result.ends_with('/')) {
while (result.ends_with('\\') || result.ends_with('/'))
&& Path::new(&result).parent().is_some()
{
result.pop();
}
PathBuf::from(result)
Expand Down Expand Up @@ -354,6 +356,31 @@ mod tests {
assert_eq!(strip_trailing_separator("C:\\"), PathBuf::from("C:\\"));
}

#[test]
#[cfg(windows)]
fn test_strip_trailing_separator_windows_unc_paths() {
// UNC path with trailing separator - should strip it
assert_eq!(
strip_trailing_separator("\\\\server\\share\\folder\\"),
PathBuf::from("\\\\server\\share\\folder")
);
// UNC root path should be preserved
assert_eq!(
strip_trailing_separator("\\\\server\\share\\"),
PathBuf::from("\\\\server\\share\\")
);
// Extended-length path root should be preserved
assert_eq!(
strip_trailing_separator("\\\\?\\C:\\"),
PathBuf::from("\\\\?\\C:\\")
);
// Extended-length path with subfolder - should strip trailing separator
assert_eq!(
strip_trailing_separator("\\\\?\\C:\\Users\\"),
PathBuf::from("\\\\?\\C:\\Users")
);
}

// ==================== norm_case tests ====================

#[test]
Expand Down
Loading