Skip to content

Commit

Permalink
Fix extension casing on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaeroxe committed Aug 30, 2022
1 parent a6bc221 commit 653aeda
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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
}
16 changes: 16 additions & 0 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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))]
Expand Down

0 comments on commit 653aeda

Please sign in to comment.