From da9b7b2a7c1981255a13162e2bd9f80d9b0bbe3e Mon Sep 17 00:00:00 2001 From: dylni <46035563+dylni@users.noreply.github.com> Date: Sun, 10 Oct 2021 23:09:04 -0400 Subject: [PATCH] Temporarily fix rust-lang/rust#89658 --- src/windows.rs | 16 ++++++++++++++++ tests/edge_cases.rs | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 tests/edge_cases.rs diff --git a/src/windows.rs b/src/windows.rs index 0f548a1..dd7dc5f 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -143,11 +143,27 @@ fn get_prefix(base: &BasePath) -> PrefixComponent<'_> { } fn push_separator(base: &mut BasePathBuf) { + // https://github.com/rust-lang/rust/issues/89658 + /* base.replace_with(|mut base| { // Add a separator if necessary. base.push(""); base }); + */ + + const SEPARATOR: &str = "\\"; + + if let Some(Component::Prefix(prefix)) = base.components().next_back() { + if matches!(prefix.kind(), Prefix::Disk(_) | Prefix::VerbatimDisk(_)) { + return; + } + } + // This inefficient implementation must be used until the above issue is + // resolved. + if !base.0.to_string_lossy().ends_with(SEPARATOR) { + base.0.push(SEPARATOR); + } } pub(super) fn push(base: &mut BasePathBuf, initial_path: &Path) { diff --git a/tests/edge_cases.rs b/tests/edge_cases.rs new file mode 100644 index 0000000..43797a8 --- /dev/null +++ b/tests/edge_cases.rs @@ -0,0 +1,21 @@ +mod common; + +#[cfg(windows)] +#[test] +fn test_edge_cases() { + use std::path::Path; + + use normpath::BasePath; + use normpath::PathExt; + + // https://github.com/dylni/normpath/pull/4#issuecomment-938596259 + tj(r"X:\X:", r"ABC", r"X:\X:\ABC"); + tj(r"\\?\X:\X:", r"ABC", r"\\?\X:\X:\ABC"); + + #[rustversion::attr(since(1.46.0), track_caller)] + fn tj(base: &str, path: &str, joined_path: &str) { + let joined_path = Path::new(joined_path); + assert_eq!(joined_path, BasePath::try_new(base).unwrap().join(path)); + common::assert_eq(joined_path, joined_path.normalize_virtually()); + } +}