use std::path::PathBuf;
use tempfile::TempDir;
#[allow(dead_code)]
pub fn setup_git_repo() -> TempDir {
let temp = TempDir::new().expect("failed to create temp dir");
std::process::Command::new("git")
.args(["init"])
.current_dir(temp.path())
.output()
.expect("failed to run git init");
std::process::Command::new("git")
.args(["config", "user.name", "Test User"])
.current_dir(temp.path())
.output()
.expect("failed to set git user.name");
std::process::Command::new("git")
.args(["config", "user.email", "test@example.com"])
.current_dir(temp.path())
.output()
.expect("failed to set git user.email");
temp
}
#[allow(dead_code)]
pub fn setup_dataset(temp: &TempDir) -> PathBuf {
let dataset = temp.path().join("dataset");
std::fs::create_dir_all(&dataset).expect("failed to create dataset dir");
dataset
}
#[allow(dead_code)]
pub struct CwdGuard {
original: PathBuf,
}
#[allow(dead_code)]
impl CwdGuard {
pub fn new() -> Self {
CwdGuard {
original: std::env::current_dir().expect("failed to get cwd"),
}
}
}
impl Drop for CwdGuard {
fn drop(&mut self) {
let _ = std::env::set_current_dir(&self.original);
}
}
#[allow(dead_code)]
pub fn setup_repo_and_dataset() -> (TempDir, PathBuf) {
let temp = setup_git_repo();
let dataset = setup_dataset(&temp);
std::process::Command::new("git")
.args(["config", "user.name", "Test User"])
.current_dir(&dataset)
.output()
.ok();
std::process::Command::new("git")
.args(["config", "user.email", "test@example.com"])
.current_dir(&dataset)
.output()
.ok();
(temp, dataset)
}
#[allow(dead_code)]
pub fn setup_cli_env() -> (TempDir, PathBuf) {
let temp = setup_git_repo();
let dataset = setup_dataset(&temp);
std::process::Command::new("git")
.args(["config", "user.name", "Test User"])
.current_dir(&dataset)
.output()
.ok();
std::process::Command::new("git")
.args(["config", "user.email", "test@example.com"])
.current_dir(&dataset)
.output()
.ok();
(temp, dataset)
}
#[allow(dead_code)]
#[cfg(feature = "git")]
pub fn git_prolly_cmd(dir: &std::path::Path) -> assert_cmd::Command {
let mut cmd = assert_cmd::Command::cargo_bin("git-prolly").expect("binary not found");
cmd.current_dir(dir);
cmd
}