Skip to content

Commit 242d438

Browse files
committed
fix: Fix failing PR tests in GitHub Actions
The PR tests were failing in CI because the mock gh executable path wasn't being passed to the subprocess. Tests modified PATH with env::set_var() but this only affected the current process, not subprocesses spawned by assert_cmd. Changes: - Add run_test_bin_with_env() helper to pass environment variables to subprocesses - Update all PR tests that use mock gh to pass PATH explicitly - Remove unnecessary env::set_var() and env::restore() calls This fixes the test_list_command_with_pr_flag failure that was causing CI to fail on the master branch after PR #45.
1 parent ff2bfa8 commit 242d438

2 files changed

Lines changed: 35 additions & 25 deletions

File tree

tests/common/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,33 @@ where
229229
.expect("Failed to run git-chain")
230230
}
231231

232+
pub fn run_test_bin_with_env<I, T, E, K, V, P: AsRef<Path>>(
233+
current_dir: P,
234+
arguments: I,
235+
env_vars: E,
236+
) -> Output
237+
where
238+
I: IntoIterator<Item = T>,
239+
T: AsRef<OsStr>,
240+
E: IntoIterator<Item = (K, V)>,
241+
K: AsRef<OsStr>,
242+
V: AsRef<OsStr>,
243+
{
244+
let mut current_dir_buf: PathBuf = current_dir.as_ref().into();
245+
if current_dir_buf.is_relative() {
246+
current_dir_buf = current_dir_buf.canonicalize().unwrap();
247+
}
248+
249+
let mut cmd =
250+
assert_cmd::Command::cargo_bin(env!("CARGO_PKG_NAME")).expect("Failed to get git-chain");
251+
252+
cmd.current_dir(current_dir_buf)
253+
.args(arguments)
254+
.envs(env_vars);
255+
256+
cmd.output().expect("Failed to run git-chain")
257+
}
258+
232259
#[allow(dead_code)]
233260
pub fn run_test_bin_expect_err<I, T, P: AsRef<Path>>(current_dir: P, arguments: I) -> Output
234261
where

tests/pr.rs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,8 @@ fn test_pr_command_creates_prs_for_chain() {
183183
let absolute_mock_dir = mock_dir.canonicalize().unwrap();
184184
let new_path = format!("{}:{}", absolute_mock_dir.display(), original_path);
185185

186-
env::set_var("PATH", new_path);
187-
188-
// Run pr command
189-
let output = run_test_bin(path_to_repo, ["pr"]);
190-
191-
// Restore original PATH
192-
env::set_var("PATH", original_path);
186+
// Run pr command with modified PATH
187+
let output = run_test_bin_with_env(path_to_repo, ["pr"], vec![("PATH", new_path)]);
193188

194189
let stdout = String::from_utf8_lossy(&output.stdout);
195190
let stderr = String::from_utf8_lossy(&output.stderr);
@@ -321,13 +316,9 @@ fn test_pr_command_with_draft_flag() {
321316
let original_path = env::var("PATH").unwrap_or_default();
322317
let absolute_mock_dir = mock_dir.canonicalize().unwrap();
323318
let new_path = format!("{}:{}", absolute_mock_dir.display(), original_path);
324-
env::set_var("PATH", new_path);
325319

326-
// Run pr command with draft flag
327-
let output = run_test_bin(path_to_repo, ["pr", "--draft"]);
328-
329-
// Restore original PATH
330-
env::set_var("PATH", original_path);
320+
// Run pr command with draft flag and modified PATH
321+
let output = run_test_bin_with_env(path_to_repo, ["pr", "--draft"], vec![("PATH", new_path)]);
331322

332323
let stdout = String::from_utf8_lossy(&output.stdout);
333324
let stderr = String::from_utf8_lossy(&output.stderr);
@@ -452,13 +443,9 @@ fn test_list_command_with_pr_flag() {
452443
let original_path = env::var("PATH").unwrap_or_default();
453444
let absolute_mock_dir = mock_dir.canonicalize().unwrap();
454445
let new_path = format!("{}:{}", absolute_mock_dir.display(), original_path);
455-
env::set_var("PATH", new_path);
456-
457-
// Run list command with --pr flag
458-
let output = run_test_bin(path_to_repo, ["list", "--pr"]);
459446

460-
// Restore original PATH
461-
env::set_var("PATH", original_path);
447+
// Run list command with --pr flag, passing the modified PATH
448+
let output = run_test_bin_with_env(path_to_repo, ["list", "--pr"], vec![("PATH", new_path)]);
462449

463450
let stdout = String::from_utf8_lossy(&output.stdout);
464451
let stderr = String::from_utf8_lossy(&output.stderr);
@@ -523,13 +510,9 @@ fn test_status_command_with_pr_flag() {
523510
let original_path = env::var("PATH").unwrap_or_default();
524511
let absolute_mock_dir = mock_dir.canonicalize().unwrap();
525512
let new_path = format!("{}:{}", absolute_mock_dir.display(), original_path);
526-
env::set_var("PATH", new_path);
527-
528-
// Run status command with --pr flag
529-
let output = run_test_bin(path_to_repo, ["status", "--pr"]);
530513

531-
// Restore original PATH
532-
env::set_var("PATH", original_path);
514+
// Run status command with --pr flag and modified PATH
515+
let output = run_test_bin_with_env(path_to_repo, ["status", "--pr"], vec![("PATH", new_path)]);
533516

534517
let stdout = String::from_utf8_lossy(&output.stdout);
535518
let stderr = String::from_utf8_lossy(&output.stderr);

0 commit comments

Comments
 (0)