Skip to content

Add support for windows verbatim disk paths #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,15 @@ pub fn glob_with(pattern: &str, options: MatchOptions) -> Result<Paths, PatternE
#[cfg(windows)]
fn check_windows_verbatim(p: &Path) -> bool {
match p.components().next() {
Some(Component::Prefix(ref p)) => p.kind().is_verbatim(),
Some(Component::Prefix(ref p)) => {
// Allow VerbatimDisk paths. std canonicalize() generates them, and they work fine
p.kind().is_verbatim()
&& if let std::path::Prefix::VerbatimDisk(_) = p.kind() {
false
} else {
true
}
}
_ => false,
}
}
Expand Down
19 changes: 19 additions & 0 deletions tests/glob-std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ fn main() {
)
);

// std-canonicalized windows verbatim disk paths should work
if env::consts::FAMILY == "windows" {
let r_verbatim = PathBuf::from("r").canonicalize().unwrap();
assert_eq!(
glob_vec(&format!("{}\\**", r_verbatim.display().to_string()))
.into_iter()
.map(|p| p.strip_prefix(&r_verbatim).unwrap().to_owned())
.collect::<Vec<_>>(),
vec!(
PathBuf::from("another"),
PathBuf::from("one"),
PathBuf::from("one\\another"),
PathBuf::from("one\\another\\deep"),
PathBuf::from("three"),
PathBuf::from("two")
)
);
}

// collapse consecutive recursive patterns
assert_eq!(
glob_vec("r/**/**"),
Expand Down