Skip to content

Commit c322535

Browse files
committed
Add some tests for finding associated executables
The tests are not limited to finding `sh`, but this may still not be ready for use in finding other commands, for the commented reasons. These tests are a step toward that, but they are mainly to make sure the search works as expected both when the looked-for command is and is not found in one of the searched `bin` dirs. This is to say that, in the tests, the commands that can be found but in `usr/bin` rather than the first choice of `bin` are to an extent a stand-in for `sh` when searched for in an environment that doesn't have `(git root)/bin` (like the Git for Windows SDK), and the commands that cannot bve found are to an extent a standi-in for `sh` on systems where it cannot be found (such as due to `git` not being installed or installed from MinGit or some other minimal or nonstandard Git installation, or `GIT_EXEC_PATH` being defined and having a value that cannot be used or that can be used but points to a directory structure that does not have a usable `sh`).
1 parent 3c35932 commit c322535

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

gix-path/src/env/auxiliary.rs

+63-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ const BIN_DIR_FRAGMENTS: &[&str] = &["bin", "usr/bin"];
7070
/// The resulting path uses only `/` separators so long as the path obtained from `git --exec-path`
7171
/// does, which is the case unless it is overridden by setting `GIT_EXEC_PATH` to an unusual value.
7272
///
73-
/// This is currently only used (and only exercised in tests) for finding `sh.exe`. It may be used
74-
/// to find other executables in the future, but may require adjustment. In particular, depending
75-
/// on the desired semantics, it should possibly also check inside a `cmd` directory; directories
73+
/// This is currently only used (and only heavily exercised in tests) for finding `sh.exe`. It may
74+
/// be used to find other executables in the future, but may need adjustment. In particular,
75+
/// depending on desired semantics, it should possibly also check a `cmd` directory; directories
7676
/// like `<platform>/bin`, for any applicable variants (such as `mingw64`); and `super::core_dir()`
7777
/// itself, which it could safely check even if its value is not safe for inferring other paths.
7878
fn find_git_associated_windows_executable(stem: &str) -> Option<OsString> {
@@ -101,3 +101,63 @@ pub(super) fn find_git_associated_windows_executable_with_fallback(stem: &str) -
101101
raw_path
102102
})
103103
}
104+
105+
#[cfg(all(windows, test))]
106+
mod tests {
107+
use std::path::Path;
108+
109+
/// Some commands with `.exe` files in `bin` and `usr/bin` that should be found.
110+
///
111+
/// Tests are expected to run with a full Git for Windows installation (not MinGit).
112+
const SHOULD_FIND: &[&str] = &[
113+
"sh", "bash", "dash", "diff", "tar", "less", "sed", "awk", "perl", "cygpath",
114+
];
115+
116+
/// Shouldn't find anything nonexistent, or only in PATH or in `bin`s we don't mean to search.
117+
///
118+
/// If dirs like `mingsw64/bin` are added, `git-credential-manager` should be moved to `SHOULD_FIND`.
119+
/// Likewise, if `super::core_dir()` is added, `git-daemon` should be moved to `SHOULD_FIND`.
120+
const SHOULD_NOT_FIND: &[&str] = &[
121+
"nonexistent-command",
122+
"cmd",
123+
"powershell",
124+
"explorer",
125+
"git-credential-manager",
126+
"git-daemon",
127+
];
128+
129+
#[test]
130+
fn find_git_associated_windows_executable() {
131+
for stem in SHOULD_FIND {
132+
let path = super::find_git_associated_windows_executable(stem);
133+
assert!(path.is_some(), "should find {stem:?}");
134+
}
135+
}
136+
137+
#[test]
138+
fn find_git_associated_windows_executable_no_extra() {
139+
for stem in SHOULD_NOT_FIND {
140+
let path = super::find_git_associated_windows_executable(stem);
141+
assert_eq!(path, None, "should not find {stem:?}");
142+
}
143+
}
144+
145+
#[test]
146+
fn find_git_associated_windows_executable_with_fallback() {
147+
for stem in SHOULD_FIND {
148+
let path = super::find_git_associated_windows_executable_with_fallback(stem);
149+
assert!(Path::new(&path).is_absolute(), "should find {stem:?}");
150+
}
151+
}
152+
153+
#[test]
154+
fn find_git_associated_windows_executable_with_fallback_falls_back() {
155+
for stem in SHOULD_NOT_FIND {
156+
let path = super::find_git_associated_windows_executable_with_fallback(stem)
157+
.to_str()
158+
.expect("valid Unicode")
159+
.to_owned();
160+
assert_eq!(path, format!("{stem}.exe"), "should fall back for {stem:?}");
161+
}
162+
}
163+
}

0 commit comments

Comments
 (0)