mirror of
https://gitee.com/openharmony/third_party_rust_which-rs
synced 2024-11-23 07:29:42 +00:00
Fix extension casing on windows
This commit is contained in:
parent
a6bc221c44
commit
653aedac49
@ -9,7 +9,7 @@ use regex::Regex;
|
||||
use std::borrow::Borrow;
|
||||
use std::env;
|
||||
use std::ffi::OsStr;
|
||||
#[cfg(feature = "regex")]
|
||||
#[cfg(any(feature = "regex", target_os = "windows"))]
|
||||
use std::fs;
|
||||
use std::iter;
|
||||
use std::path::{Path, PathBuf};
|
||||
@ -80,7 +80,9 @@ impl Finder {
|
||||
}
|
||||
};
|
||||
|
||||
Ok(binary_path_candidates.filter(move |p| binary_checker.is_valid(p)))
|
||||
Ok(binary_path_candidates
|
||||
.filter(move |p| binary_checker.is_valid(p))
|
||||
.map(correct_casing))
|
||||
}
|
||||
|
||||
#[cfg(feature = "regex")]
|
||||
@ -207,3 +209,24 @@ impl Finder {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
fn correct_casing(mut p: PathBuf) -> PathBuf {
|
||||
if let (Some(parent), Some(file_name)) = (p.parent(), p.file_name()) {
|
||||
if let Ok(iter) = fs::read_dir(parent) {
|
||||
for e in iter.filter_map(std::result::Result::ok) {
|
||||
if e.file_name().eq_ignore_ascii_case(file_name) {
|
||||
p.pop();
|
||||
p.push(e.file_name());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
p
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
fn correct_casing(p: PathBuf) -> PathBuf {
|
||||
p
|
||||
}
|
||||
|
@ -67,6 +67,10 @@ impl TestFixture {
|
||||
bins.push(mk_bin(&p, BIN_NAME, "cmd").unwrap());
|
||||
paths.push(p);
|
||||
}
|
||||
let p = tempdir.path().join("win-bin");
|
||||
builder.create(&p).unwrap();
|
||||
bins.push(mk_bin(&p, "win-bin", "exe").unwrap());
|
||||
paths.push(p);
|
||||
TestFixture {
|
||||
tempdir,
|
||||
paths: env::join_paths(paths).unwrap(),
|
||||
@ -194,6 +198,17 @@ fn test_which_extension() {
|
||||
assert_eq!(_which(&f, &b).unwrap(), f.bins[2])
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(windows)]
|
||||
fn test_which_no_extension() {
|
||||
let f = TestFixture::new();
|
||||
let b = Path::new("win-bin");
|
||||
let which_result = which::which_in(&b, Some(&f.paths), ".").unwrap();
|
||||
// Make sure the extension is the correct case.
|
||||
assert_eq!(which_result.extension(), f.bins[9].extension());
|
||||
assert_eq!(fs::canonicalize(&which_result).unwrap(), f.bins[9])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_which_not_found() {
|
||||
let f = TestFixture::new();
|
||||
@ -221,6 +236,7 @@ fn test_which_all() {
|
||||
.collect::<Vec<_>>();
|
||||
#[cfg(windows)]
|
||||
{
|
||||
expected.retain(|p| p.file_stem().unwrap() == BIN_NAME);
|
||||
expected.retain(|p| p.extension().map(|ext| ext == "exe" || ext == "cmd") == Some(true));
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
|
Loading…
Reference in New Issue
Block a user