diff --git a/src/git.rs b/src/git.rs index 93d607f..1bbaa10 100644 --- a/src/git.rs +++ b/src/git.rs @@ -97,7 +97,11 @@ fn clean_existing_profiles(profile_dir: &Path) -> Result<()> { let mut cmd = Command::new("git"); cmd.args(&["config", "--local", "--unset", "include.path", val]); // We tolerate failure here (e.g. if key doesn't exist anymore for some reason) - let _ = cmd.output(); + if is_mocking() { + eprintln!("[DRY-RUN] {:?}", cmd); + } else { + let _ = cmd.output(); + } } } @@ -165,7 +169,11 @@ fn run_switch(config: &GoshConfig, profile_id: &str, force: bool) -> Result<()> let mut cmd = Command::new("git"); cmd.args(&["config", "--remove-section", section]); // Ignore errors - let _ = cmd.output(); + if is_mocking() { + eprintln!("[DRY-RUN] {:?}", cmd); + } else { + let _ = cmd.output(); + } } // Unset keys @@ -173,7 +181,11 @@ fn run_switch(config: &GoshConfig, profile_id: &str, force: bool) -> Result<()> for key in keys { let mut cmd = Command::new("git"); cmd.args(&["config", "--unset", key]); - let _ = cmd.output(); + if is_mocking() { + eprintln!("[DRY-RUN] {:?}", cmd); + } else { + let _ = cmd.output(); + } } } diff --git a/tests/test_force_mocking.rs b/tests/test_force_mocking.rs new file mode 100644 index 0000000..5de950d --- /dev/null +++ b/tests/test_force_mocking.rs @@ -0,0 +1,38 @@ +use assert_cmd::Command; +use std::fs; +use tempfile::TempDir; + +#[test] +fn test_switch_force_mocking() -> Result<(), Box> { + let temp_dir = TempDir::new()?; + let config_path = temp_dir.path().join("config"); + let repo_dir = temp_dir.path().join("repo"); + + fs::create_dir_all(&repo_dir)?; + std::process::Command::new("git") + .arg("init") + .current_dir(&repo_dir) + .output()?; + + // Create profile + Command::new(env!("CARGO_BIN_EXE_gosh")) + .env("GOSH_CONFIG_PATH", &config_path) + .args(&["-c", "User", "u@e.com", "mock_test"]) + .assert() + .success(); + + // Run force switch with mocking + Command::new(env!("CARGO_BIN_EXE_gosh")) + .env("GOSH_CONFIG_PATH", &config_path) + .env("GOSH_MOCKING", "1") + .current_dir(&repo_dir) + .args(&["mock_test", "-f"]) + .assert() + .success() + // Check for dry-run output of cleanup commands + .stderr(predicates::str::contains("config")) + .stderr(predicates::str::contains("--remove-section")) + .stderr(predicates::str::contains("user")); + + Ok(()) +}