fix(mocking): some moking will leak

Fix leakage for the config while using `git config`.
This commit is contained in:
INX "Xenon" 2026-01-28 21:01:48 +08:00
parent 1bc109ae64
commit bb41d4acda
Signed by: inx
SSH key fingerprint: SHA256:oEFbclBdeqw4M09C3hfnDej0ioZdzZW6BKxsZH6quX8
2 changed files with 53 additions and 3 deletions

View file

@ -97,7 +97,11 @@ fn clean_existing_profiles(profile_dir: &Path) -> Result<()> {
let mut cmd = Command::new("git"); let mut cmd = Command::new("git");
cmd.args(&["config", "--local", "--unset", "include.path", val]); cmd.args(&["config", "--local", "--unset", "include.path", val]);
// We tolerate failure here (e.g. if key doesn't exist anymore for some reason) // 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"); let mut cmd = Command::new("git");
cmd.args(&["config", "--remove-section", section]); cmd.args(&["config", "--remove-section", section]);
// Ignore errors // Ignore errors
let _ = cmd.output(); if is_mocking() {
eprintln!("[DRY-RUN] {:?}", cmd);
} else {
let _ = cmd.output();
}
} }
// Unset keys // Unset keys
@ -173,7 +181,11 @@ fn run_switch(config: &GoshConfig, profile_id: &str, force: bool) -> Result<()>
for key in keys { for key in keys {
let mut cmd = Command::new("git"); let mut cmd = Command::new("git");
cmd.args(&["config", "--unset", key]); cmd.args(&["config", "--unset", key]);
let _ = cmd.output(); if is_mocking() {
eprintln!("[DRY-RUN] {:?}", cmd);
} else {
let _ = cmd.output();
}
} }
} }

View file

@ -0,0 +1,38 @@
use assert_cmd::Command;
use std::fs;
use tempfile::TempDir;
#[test]
fn test_switch_force_mocking() -> Result<(), Box<dyn std::error::Error>> {
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(())
}