Skip to content

Commit

Permalink
Make EXE_INFO testable and add a basic test for it
Browse files Browse the repository at this point in the history
This extracts the code used to lazily initialize `EXE_INFO` into a
separate `exe_info` helper, which is an implementation detail, but
can be called any number of times without caching from the tests.

It also adds one test.

The tests of downstream functions (those that use `EXE_INFO`)
remain more important. But this will allow the test to be expanded,
or another added, to test more expectations that `EXE_INFO` is
supposed to satisfy.
  • Loading branch information
EliahKagan committed Aug 28, 2024
1 parent ccd0401 commit 1ee98bf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions gix-path/src/env/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ pub(super) static EXE_NAME: &str = "git";
/// Invoke the git executable to obtain the origin configuration, which is cached and returned.
///
/// The git executable is the one found in PATH or an alternative location.
pub(super) static EXE_INFO: Lazy<Option<BString>> = Lazy::new(|| {
pub(super) static EXE_INFO: Lazy<Option<BString>> = Lazy::new(exe_info);

fn exe_info() -> Option<BString> {
let mut cmd = git_cmd(EXE_NAME.into());
gix_trace::debug!(cmd = ?cmd, "invoking git for installation config path");
let cmd_output = match cmd.output() {
Expand All @@ -98,7 +100,7 @@ pub(super) static EXE_INFO: Lazy<Option<BString>> = Lazy::new(|| {
};

first_file_from_config_with_origin(cmd_output.as_slice().into()).map(ToOwned::to_owned)
});
}

fn git_cmd(executable: PathBuf) -> Command {
let mut cmd = Command::new(executable);
Expand Down
13 changes: 13 additions & 0 deletions gix-path/src/env/git/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,19 @@ mod locations {

use std::path::Path;

#[test]
fn exe_info() {
let path = super::exe_info()
.map(crate::from_bstring)
.expect("Nonempty config in the test environment");

assert!(
path.is_absolute(),
"Absolute, unless overridden such as with GIT_CONFIG_SYSTEM"
);
assert!(path.exists(), "Exists, since `git config` just found an entry there");
}

#[test]
fn first_file_from_config_with_origin() {
let macos =
Expand Down

0 comments on commit 1ee98bf

Please sign in to comment.