#![allow(dead_code)]
use assert_cmd::Command as Cli;
use std::process::Command as Sys;
use tempfile::TempDir;
pub fn git(dir: &TempDir) -> Sys {
hunkpick::gitenv::insulated_git(dir.path())
}
pub fn git_with_stdin(dir: &TempDir, args: &[&str], stdin_bytes: &[u8]) -> std::process::Output {
let mut cmd = git(dir);
cmd.args(args);
hunkpick::gitenv::feed_and_wait(&mut cmd, stdin_bytes)
.unwrap_or_else(|e| panic!("git {args:?} in {}: {e}", dir.path().display()))
}
pub fn repo_with(old_files: &[(&str, &str)]) -> TempDir {
let dir = tempfile::tempdir().unwrap();
sys(&dir, &["init", "-q"]);
sys(&dir, &["config", "user.email", "t@t"]);
sys(&dir, &["config", "user.name", "t"]);
sys(&dir, &["config", "core.autocrlf", "false"]);
if old_files.is_empty() {
sys(&dir, &["commit", "-q", "-m", "init", "--allow-empty"]);
} else {
for (p, c) in old_files {
let full = dir.path().join(p);
std::fs::create_dir_all(full.parent().unwrap()).unwrap();
std::fs::write(full, c).unwrap();
}
sys(&dir, &["add", "."]);
sys(&dir, &["commit", "-q", "-m", "init"]);
}
dir
}
pub fn sys(dir: &TempDir, args: &[&str]) {
let ok = git(dir).args(args).status().unwrap().success();
assert!(ok, "git {args:?} failed");
}
pub fn git_output(dir: &TempDir, args: &[&str]) -> String {
String::from_utf8(git_output_bytes(dir, args)).unwrap()
}
pub fn git_output_bytes(dir: &TempDir, args: &[&str]) -> Vec<u8> {
git(dir).args(args).output().unwrap().stdout
}
pub fn diff_after(dir: &TempDir, new_files: &[(&str, &str)]) -> String {
for (p, c) in new_files {
std::fs::write(dir.path().join(p), c).unwrap();
}
git_output(dir, &["diff"])
}
pub fn diff_staged(dir: &TempDir) -> String {
git_output(dir, &["diff", "--staged"])
}
pub fn revert(dir: &TempDir) {
sys(dir, &["checkout", "--", "."]);
}
pub fn apply_cached(dir: &TempDir, diff: &[u8]) {
apply_diff(dir, &["apply", "--cached"], diff, "git apply --cached");
}
pub fn apply_diff(dir: &TempDir, args: &[&str], diff: &[u8], what: &str) {
let out = git_with_stdin(dir, args, diff);
assert!(
out.status.success(),
"{what} failed: {}",
String::from_utf8_lossy(&out.stderr).trim()
);
}
pub fn run(args: &[&str], stdin: impl AsRef<[u8]>) -> assert_cmd::assert::Assert {
Cli::cargo_bin("hunkpick")
.unwrap()
.args(args)
.write_stdin(stdin.as_ref().to_vec())
.assert()
}
pub fn stdout_of(assert: assert_cmd::assert::Assert) -> Vec<u8> {
assert.success().get_output().stdout.clone()
}
pub fn run_ok(args: &[&str], stdin: impl AsRef<[u8]>) -> Vec<u8> {
stdout_of(run(args, stdin))
}
pub fn list_json(diff: &str) -> serde_json::Value {
serde_json::from_slice(&run_ok(&["list", "--json"], diff)).unwrap()
}
pub fn run_ok_text(args: &[&str], stdin: &str) -> String {
String::from_utf8(run_ok(args, stdin)).unwrap()
}
pub fn select_checked(dir: &TempDir, diff: &str, selectors: &[&str]) -> assert_cmd::assert::Assert {
let mut args = vec!["select"];
args.extend_from_slice(selectors);
args.extend_from_slice(&[
"--verify-result-diff-git",
"-C",
dir.path().to_str().unwrap(),
]);
run(&args, diff)
}
pub fn select_checked_ok(dir: &TempDir, diff: &str, selectors: &[&str]) -> String {
String::from_utf8(stdout_of(select_checked(dir, diff, selectors))).unwrap()
}