fix(mocking): some moking will leak
Fix leakage for the config while using `git config`.
This commit is contained in:
parent
1bc109ae64
commit
bb41d4acda
2 changed files with 53 additions and 3 deletions
12
src/git.rs
12
src/git.rs
|
|
@ -97,9 +97,13 @@ 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)
|
||||||
|
if is_mocking() {
|
||||||
|
eprintln!("[DRY-RUN] {:?}", cmd);
|
||||||
|
} else {
|
||||||
let _ = cmd.output();
|
let _ = cmd.output();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -165,17 +169,25 @@ 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
|
||||||
|
if is_mocking() {
|
||||||
|
eprintln!("[DRY-RUN] {:?}", cmd);
|
||||||
|
} else {
|
||||||
let _ = cmd.output();
|
let _ = cmd.output();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Unset keys
|
// Unset keys
|
||||||
let keys = sanitizer::BLACKLIST_KEYS;
|
let keys = sanitizer::BLACKLIST_KEYS;
|
||||||
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]);
|
||||||
|
if is_mocking() {
|
||||||
|
eprintln!("[DRY-RUN] {:?}", cmd);
|
||||||
|
} else {
|
||||||
let _ = cmd.output();
|
let _ = cmd.output();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 0. Clean existing gosh profiles
|
// 0. Clean existing gosh profiles
|
||||||
let profiles_dir = get_profile_dir(config)?;
|
let profiles_dir = get_profile_dir(config)?;
|
||||||
|
|
|
||||||
38
tests/test_force_mocking.rs
Normal file
38
tests/test_force_mocking.rs
Normal 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(())
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue