Skip to content

Commit 9cab53f

Browse files
committed
test: use DGR_TEST_GH_PATH to mock missing gh executable
Instead of destructively replacing the PATH on Windows to hide the `gh` executable—which caused Rust 1.77.2 to fail spawning `git` due to implicit extension resolution changes—this commit adds a `DGR_TEST_GH_PATH` environment variable for testing. The test now injects a nonexistent path to cleanly simulate a missing `gh` CLI without breaking access to system executables like `git`.
1 parent b794c8a commit 9cab53f

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/core/gh.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::path::PathBuf;
44
use std::process::{Command, ExitStatus, Output, Stdio};
55
use std::thread;
66

7-
#[cfg(windows)]
87
use std::env;
98

109
use serde::Deserialize;
@@ -500,11 +499,19 @@ fn gh_command() -> io::Result<Command> {
500499

501500
#[cfg(not(windows))]
502501
fn resolve_gh_executable() -> io::Result<PathBuf> {
502+
if let Some(path) = env::var_os("DGR_TEST_GH_PATH") {
503+
return Ok(PathBuf::from(path));
504+
}
505+
503506
Ok(PathBuf::from("gh"))
504507
}
505508

506509
#[cfg(windows)]
507510
fn resolve_gh_executable() -> io::Result<PathBuf> {
511+
if let Some(path) = env::var_os("DGR_TEST_GH_PATH") {
512+
return Ok(PathBuf::from(path));
513+
}
514+
508515
let path = env::var_os("PATH").unwrap_or_default();
509516
let pathext = env::var_os("PATHEXT")
510517
.unwrap_or_else(|| ".COM;.EXE;.BAT;.CMD".into())

tests/pr.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
55

66
use serde_json::json;
77
use support::{
8-
dgr_ok, dgr_ok_with_env, dgr_with_input_and_env, find_node, git_binary_path, git_ok,
8+
dgr_ok, dgr_ok_with_env, dgr_with_input_and_env, find_node, git_ok,
99
git_stdout, initialize_main_repo, install_fake_executable, load_events_json, load_state_json,
1010
path_with_prepend, strip_ansi, with_temp_repo,
1111
};
@@ -870,16 +870,8 @@ fn pr_reports_missing_gh_cli() {
870870
dgr_ok(repo, &["init"]);
871871
dgr_ok(repo, &["branch", "feat/auth"]);
872872

873-
let bin_dir = repo.join("fake-bin");
874-
let git_path = git_binary_path();
875-
install_fake_executable(
876-
&bin_dir,
877-
"git",
878-
&format!("#!/bin/sh\nset -eu\nexec \"{}\" \"$@\"\n", git_path),
879-
);
880-
let path = bin_dir.display().to_string();
881-
882-
let output = support::dgr_with_env(repo, &["pr"], &[("PATH", path.as_str())]);
873+
let output =
874+
support::dgr_with_env(repo, &["pr"], &[("DGR_TEST_GH_PATH", "/does/not/exist/gh")]);
883875

884876
assert!(!output.status.success());
885877
let stderr = String::from_utf8(output.stderr).unwrap();

0 commit comments

Comments
 (0)