From add634d51762c0918184fa7158ad8293113d79af Mon Sep 17 00:00:00 2001 From: Aleksey Kladov <aleksey.kladov@gmail.com> Date: Tue, 3 Apr 2018 14:05:36 +0300 Subject: [PATCH] Fix `to_file_path` for relative paths with drive letters --- src/lib.rs | 5 +++-- tests/unit.rs | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f24285fe1..d1012fa64 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2411,8 +2411,9 @@ fn file_url_segments_to_pathbuf_windows(host: Option<&str>, mut segments: str::S } } let path = PathBuf::from(string); - debug_assert!(path.is_absolute(), - "to_file_path() failed to produce an absolute Path"); + if !path.is_absolute() { + return Err(()); + } Ok(path) } diff --git a/tests/unit.rs b/tests/unit.rs index 10bb86a9d..6c5413d45 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -86,6 +86,29 @@ fn new_path_windows_fun() { } } +#[test] +fn to_file_path_for_relative_windows_paths() { + if cfg!(windows) { + let url = Url::parse("file://example.com/C:foo").unwrap(); + assert!(url.to_file_path().is_ok()); + + let url = Url::parse("file://example.com/C:").unwrap(); + assert!(url.to_file_path().is_err()); + + let url = Url::parse("file:///E:foo").unwrap(); + assert!(url.to_file_path().is_err()); + + let url = Url::parse("file:///E:").unwrap(); + assert!(url.to_file_path().is_err()); + + let url = Url::parse("file://localhost/C:foo").unwrap(); + assert!(url.to_file_path().is_err()); + + let url = Url::parse("file://localhost/C:").unwrap(); + assert!(url.to_file_path().is_err()); + } +} + #[test] fn new_directory_paths() {