|
3 | 3 | //! This module provides utilities to test command functions directly |
4 | 4 | //! instead of spawning the CLI binary, which improves test coverage. |
5 | 5 |
|
6 | | -use crate::{GitXError, Result}; |
| 6 | +use crate::Result; |
7 | 7 | use std::env; |
8 | 8 |
|
9 | 9 | /// Test result that captures both stdout and stderr along with exit code |
@@ -46,40 +46,49 @@ pub fn sync_command_direct(_merge: bool) -> TestCommandResult { |
46 | 46 | // We need to check the git state to determine if it would succeed |
47 | 47 |
|
48 | 48 | // Try to get current branch to test if we're in a git repo |
49 | | - if std::process::Command::new("git") |
| 49 | + let git_check = std::process::Command::new("git") |
50 | 50 | .args(["rev-parse", "--is-inside-work-tree"]) |
51 | | - .output() |
52 | | - .map(|output| !output.status.success()) |
53 | | - .unwrap_or(true) |
54 | | - { |
55 | | - return TestCommandResult::failure("❌ Git command failed".to_string(), 1); |
| 51 | + .output(); |
| 52 | + |
| 53 | + match git_check { |
| 54 | + Ok(output) if output.status.success() => { |
| 55 | + // We're in a git repo, check for upstream |
| 56 | + } |
| 57 | + _ => { |
| 58 | + return TestCommandResult::failure("❌ Git command failed".to_string(), 1); |
| 59 | + } |
56 | 60 | } |
57 | 61 |
|
58 | 62 | // Check if there's an upstream configured |
59 | | - if std::process::Command::new("git") |
| 63 | + let upstream_check = std::process::Command::new("git") |
60 | 64 | .args(["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"]) |
61 | | - .output() |
62 | | - .map(|output| !output.status.success()) |
63 | | - .unwrap_or(true) |
64 | | - { |
65 | | - return TestCommandResult::failure("❌ No upstream configured".to_string(), 1); |
| 65 | + .output(); |
| 66 | + |
| 67 | + match upstream_check { |
| 68 | + Ok(output) if output.status.success() => { |
| 69 | + // Upstream exists, command would succeed |
| 70 | + TestCommandResult::success("✅ Already up to date".to_string()) |
| 71 | + } |
| 72 | + _ => { |
| 73 | + return TestCommandResult::failure("❌ No upstream configured".to_string(), 1); |
| 74 | + } |
66 | 75 | } |
67 | | - |
68 | | - // If we get here, the command would likely succeed |
69 | | - // For testing purposes, we'll simulate success |
70 | | - TestCommandResult::success("✅ Already up to date".to_string()) |
71 | 76 | } |
72 | 77 |
|
73 | 78 | /// Execute a large files command directly |
74 | 79 | pub fn large_files_command_direct(_limit: usize, threshold: Option<f64>) -> TestCommandResult { |
75 | 80 | // Try to check if we're in a git repo |
76 | | - if std::process::Command::new("git") |
| 81 | + let git_check = std::process::Command::new("git") |
77 | 82 | .args(["rev-parse", "--is-inside-work-tree"]) |
78 | | - .output() |
79 | | - .map(|output| !output.status.success()) |
80 | | - .unwrap_or(true) |
81 | | - { |
82 | | - return TestCommandResult::failure("❌ Git command failed".to_string(), 1); |
| 83 | + .output(); |
| 84 | + |
| 85 | + match git_check { |
| 86 | + Ok(output) if output.status.success() => { |
| 87 | + // We're in a git repo, proceed with simulation |
| 88 | + } |
| 89 | + _ => { |
| 90 | + return TestCommandResult::failure("❌ Git command failed".to_string(), 1); |
| 91 | + } |
83 | 92 | } |
84 | 93 |
|
85 | 94 | // Simulate the output based on threshold |
@@ -133,15 +142,39 @@ pub fn execute_command_in_dir<P: AsRef<std::path::Path>>( |
133 | 142 | dir: P, |
134 | 143 | command: impl TestCommand, |
135 | 144 | ) -> Result<TestCommandResult> { |
136 | | - let original_dir = env::current_dir().map_err(GitXError::Io)?; |
137 | | - |
138 | | - // Change to target directory |
139 | | - env::set_current_dir(dir).map_err(GitXError::Io)?; |
| 145 | + let dir_path = dir.as_ref(); |
| 146 | + |
| 147 | + // Check if directory exists before trying to change to it |
| 148 | + if !dir_path.exists() { |
| 149 | + return Ok(TestCommandResult::failure( |
| 150 | + "❌ Git command failed".to_string(), |
| 151 | + 1 |
| 152 | + )); |
| 153 | + } |
| 154 | + |
| 155 | + // Check if we can get current directory |
| 156 | + let original_dir = match env::current_dir() { |
| 157 | + Ok(dir) => dir, |
| 158 | + Err(_) => { |
| 159 | + return Ok(TestCommandResult::failure( |
| 160 | + "❌ Git command failed".to_string(), |
| 161 | + 1 |
| 162 | + )); |
| 163 | + } |
| 164 | + }; |
| 165 | + |
| 166 | + // Try to change to target directory |
| 167 | + if let Err(_) = env::set_current_dir(dir_path) { |
| 168 | + return Ok(TestCommandResult::failure( |
| 169 | + "❌ Git command failed".to_string(), |
| 170 | + 1 |
| 171 | + )); |
| 172 | + } |
140 | 173 |
|
141 | 174 | // Execute command |
142 | 175 | let result = command.execute(); |
143 | 176 |
|
144 | | - // Restore original directory |
| 177 | + // Always try to restore original directory, but don't fail if we can't |
145 | 178 | let _ = env::set_current_dir(original_dir); |
146 | 179 |
|
147 | 180 | Ok(result) |
|
0 commit comments