Skip to content
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

Make the bash_program() helper in gix-testtools more robust #1864

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
59 changes: 47 additions & 12 deletions tests/tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,20 +653,27 @@ fn configure_command<'a, I: IntoIterator<Item = S>, S: AsRef<OsStr>>(
}

fn bash_program() -> &'static Path {
if cfg!(windows) {
// TODO(deps): Once `gix_path::env::shell()` is available, maybe do `shell().parent()?.join("bash.exe")`
static GIT_BASH: Lazy<Option<PathBuf>> = Lazy::new(|| {
// TODO(deps): *Maybe* add something to `gix-path` like `env::shell()` that can be used to
// find bash and, once the version `gix-testtools` depends on has it, use it.
static GIT_BASH: Lazy<PathBuf> = Lazy::new(|| {
if cfg!(windows) {
GIT_CORE_DIR
.parent()?
.parent()?
.parent()
.map(|installation_dir| installation_dir.join("bin").join("bash.exe"))
.ancestors()
.nth(3)
.map(OsString::from)
.map(|mut raw_path| {
// Go down to where `bash.exe` usually is. Keep using `/` separators (not `\`).
raw_path.push("/usr/bin/bash.exe");
raw_path
})
.map(PathBuf::from)
.filter(|bash| bash.is_file())
});
GIT_BASH.as_deref().unwrap_or(Path::new("bash.exe"))
} else {
Path::new("bash")
}
.unwrap_or_else(|| "bash.exe".into())
} else {
"bash".into()
}
});
GIT_BASH.as_ref()
}

fn write_failure_marker(failure_marker: &Path) {
Expand Down Expand Up @@ -1059,4 +1066,32 @@ mod tests {
fn bash_program_ok_for_platform() {
assert_eq!(bash_program(), Path::new("bash"));
}

#[test]
fn bash_program_unix_path() {
let path = bash_program()
.to_str()
.expect("This test depends on the bash path being valid Unicode");
assert!(
!path.contains('\\'),
"The path to bash should have no backslashes, barring very unusual environments"
);
}

fn is_rooted_relative(path: impl AsRef<Path>) -> bool {
let p = path.as_ref();
p.is_relative() && p.has_root()
}

#[test]
#[cfg(windows)]
fn unix_style_absolute_is_rooted_relative() {
assert!(is_rooted_relative("/bin/bash"), "can detect paths like /bin/bash");
}

#[test]
fn bash_program_absolute_or_unrooted() {
let bash = bash_program();
assert!(!is_rooted_relative(bash), "{bash:?}");
}
}
Loading