#![allow(dead_code)]
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::TempDir;
pub struct TestRepo {
pub dir: TempDir,
_remote_dir: Option<TempDir>,
_custom_dirs: Vec<TempDir>,
}
impl TestRepo {
pub fn new() -> Self {
let dir = TempDir::new().expect("Failed to create temp dir");
let path = dir.path();
git(path, &["init", "-b", "main"]);
git(path, &["config", "user.name", "Test"]);
git(path, &["config", "user.email", "test@test.com"]);
git(path, &["config", "commit.gpgsign", "false"]);
std::fs::write(path.join("README.md"), "# Test\n").unwrap();
git(path, &["add", "."]);
git(path, &["commit", "-m", "Initial commit"]);
Self {
dir,
_remote_dir: None,
_custom_dirs: Vec::new(),
}
}
pub fn path(&self) -> &Path {
self.dir.path()
}
pub fn create_branch(&self, name: &str) {
git(self.path(), &["branch", name]);
}
pub fn commit_file(&self, name: &str, content: &str, msg: &str) {
std::fs::write(self.path().join(name), content).unwrap();
git(self.path(), &["add", name]);
git(self.path(), &["commit", "-m", msg]);
}
pub fn git(&self, args: &[&str]) {
git(self.path(), args);
}
pub fn git_stdout(&self, args: &[&str]) -> String {
let output = Command::new("git")
.args(args)
.current_dir(self.path())
.env("GIT_AUTHOR_NAME", "Test")
.env("GIT_AUTHOR_EMAIL", "test@test.com")
.env("GIT_COMMITTER_NAME", "Test")
.env("GIT_COMMITTER_EMAIL", "test@test.com")
.output()
.expect("Failed to run git");
String::from_utf8_lossy(&output.stdout).to_string()
}
pub fn git_at(path: &Path, args: &[&str]) {
git(path, args);
}
pub fn git_stdout_at(path: &Path, args: &[&str]) -> String {
let output = Command::new("git")
.args(args)
.current_dir(path)
.env("GIT_AUTHOR_NAME", "Test")
.env("GIT_AUTHOR_EMAIL", "test@test.com")
.env("GIT_COMMITTER_NAME", "Test")
.env("GIT_COMMITTER_EMAIL", "test@test.com")
.output()
.expect("Failed to run git");
String::from_utf8_lossy(&output.stdout).to_string()
}
pub fn cw_at(path: &Path, args: &[&str]) -> std::process::Output {
Command::new(Self::cw_bin())
.args(args)
.current_dir(path)
.env("CW_LAUNCH_METHOD", "foreground")
.output()
.expect("Failed to run cw")
}
pub fn cw_stdout_at(path: &Path, args: &[&str]) -> String {
let output = Self::cw_at(path, args);
String::from_utf8_lossy(&output.stdout).to_string()
}
pub fn cw_bin() -> PathBuf {
PathBuf::from(env!("CARGO_BIN_EXE_gw"))
}
pub fn cw(&self, args: &[&str]) -> std::process::Output {
Command::new(Self::cw_bin())
.args(args)
.current_dir(self.path())
.env("CW_LAUNCH_METHOD", "foreground")
.output()
.expect("Failed to run cw")
}
pub fn cw_stdout(&self, args: &[&str]) -> String {
let output = self.cw(args);
String::from_utf8_lossy(&output.stdout).to_string()
}
pub fn cw_stderr(&self, args: &[&str]) -> String {
let output = self.cw(args);
String::from_utf8_lossy(&output.stderr).to_string()
}
pub fn cw_combined(&self, args: &[&str]) -> String {
let output = self.cw(args);
format!(
"{}{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
)
}
pub fn cw_ok(&self, args: &[&str]) -> bool {
self.cw(args).status.success()
}
pub fn create_worktree(&self, branch: &str) -> PathBuf {
let output = self.cw(&["new", branch, "--no-term"]);
assert!(
output.status.success(),
"Failed to create worktree '{}': {}{}",
branch,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
let wt_path = self.path().parent().unwrap().join(format!(
"{}-{}",
self.path().file_name().unwrap().to_str().unwrap(),
branch
));
assert!(
wt_path.exists(),
"Worktree path does not exist: {:?}",
wt_path
);
wt_path
}
pub fn commit_file_at(path: &Path, name: &str, content: &str, msg: &str) {
std::fs::write(path.join(name), content).unwrap();
git(path, &["add", name]);
git(path, &["commit", "-m", msg]);
}
pub fn custom_path(&mut self, name: &str) -> PathBuf {
let custom_dir = TempDir::new().expect("Failed to create custom temp dir");
let path = custom_dir.path().join(name);
self._custom_dirs.push(custom_dir);
path
}
pub fn setup_remote(&mut self) -> PathBuf {
let remote_dir = TempDir::new().expect("Failed to create remote temp dir");
let remote_path = remote_dir.path().join("remote_repo.git");
let output = Command::new("git")
.args([
"clone",
"--bare",
self.path().to_str().unwrap(),
remote_path.to_str().unwrap(),
])
.output()
.expect("Failed to clone bare repo");
assert!(
output.status.success(),
"Failed to create bare remote: {}",
String::from_utf8_lossy(&output.stderr)
);
git(
self.path(),
&["remote", "add", "origin", remote_path.to_str().unwrap()],
);
self._remote_dir = Some(remote_dir);
remote_path
}
}
fn git(path: &Path, args: &[&str]) {
let output = Command::new("git")
.args(args)
.current_dir(path)
.env("GIT_AUTHOR_NAME", "Test")
.env("GIT_AUTHOR_EMAIL", "test@test.com")
.env("GIT_COMMITTER_NAME", "Test")
.env("GIT_COMMITTER_EMAIL", "test@test.com")
.output()
.expect("Failed to run git");
assert!(
output.status.success(),
"git {:?} failed: {}",
args,
String::from_utf8_lossy(&output.stderr)
);
}