Skip to content

Commit 653aeda

Browse files
committed
Fix extension casing on windows
1 parent a6bc221 commit 653aeda

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/finder.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use regex::Regex;
99
use std::borrow::Borrow;
1010
use std::env;
1111
use std::ffi::OsStr;
12-
#[cfg(feature = "regex")]
12+
#[cfg(any(feature = "regex", target_os = "windows"))]
1313
use std::fs;
1414
use std::iter;
1515
use std::path::{Path, PathBuf};
@@ -80,7 +80,9 @@ impl Finder {
8080
}
8181
};
8282

83-
Ok(binary_path_candidates.filter(move |p| binary_checker.is_valid(p)))
83+
Ok(binary_path_candidates
84+
.filter(move |p| binary_checker.is_valid(p))
85+
.map(correct_casing))
8486
}
8587

8688
#[cfg(feature = "regex")]
@@ -207,3 +209,24 @@ impl Finder {
207209
})
208210
}
209211
}
212+
213+
#[cfg(target_os = "windows")]
214+
fn correct_casing(mut p: PathBuf) -> PathBuf {
215+
if let (Some(parent), Some(file_name)) = (p.parent(), p.file_name()) {
216+
if let Ok(iter) = fs::read_dir(parent) {
217+
for e in iter.filter_map(std::result::Result::ok) {
218+
if e.file_name().eq_ignore_ascii_case(file_name) {
219+
p.pop();
220+
p.push(e.file_name());
221+
break;
222+
}
223+
}
224+
}
225+
}
226+
p
227+
}
228+
229+
#[cfg(not(target_os = "windows"))]
230+
fn correct_casing(p: PathBuf) -> PathBuf {
231+
p
232+
}

tests/basic.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ impl TestFixture {
6767
bins.push(mk_bin(&p, BIN_NAME, "cmd").unwrap());
6868
paths.push(p);
6969
}
70+
let p = tempdir.path().join("win-bin");
71+
builder.create(&p).unwrap();
72+
bins.push(mk_bin(&p, "win-bin", "exe").unwrap());
73+
paths.push(p);
7074
TestFixture {
7175
tempdir,
7276
paths: env::join_paths(paths).unwrap(),
@@ -194,6 +198,17 @@ fn test_which_extension() {
194198
assert_eq!(_which(&f, &b).unwrap(), f.bins[2])
195199
}
196200

201+
#[test]
202+
#[cfg(windows)]
203+
fn test_which_no_extension() {
204+
let f = TestFixture::new();
205+
let b = Path::new("win-bin");
206+
let which_result = which::which_in(&b, Some(&f.paths), ".").unwrap();
207+
// Make sure the extension is the correct case.
208+
assert_eq!(which_result.extension(), f.bins[9].extension());
209+
assert_eq!(fs::canonicalize(&which_result).unwrap(), f.bins[9])
210+
}
211+
197212
#[test]
198213
fn test_which_not_found() {
199214
let f = TestFixture::new();
@@ -221,6 +236,7 @@ fn test_which_all() {
221236
.collect::<Vec<_>>();
222237
#[cfg(windows)]
223238
{
239+
expected.retain(|p| p.file_stem().unwrap() == BIN_NAME);
224240
expected.retain(|p| p.extension().map(|ext| ext == "exe" || ext == "cmd") == Some(true));
225241
}
226242
#[cfg(not(windows))]

0 commit comments

Comments
 (0)