use super::git::run_git;
use std::fs;
use std::path::PathBuf;
use tempfile::{TempDir, tempdir};
pub(crate) struct Fixture {
_dir: TempDir,
pub(crate) path: PathBuf,
pub(super) repo: PathBuf,
}
impl Fixture {
pub(crate) fn new() -> Self {
let dir = tempdir().unwrap();
let path = dir.path().to_path_buf();
let repo = path.join("repo.git");
fs::create_dir_all(&repo).unwrap();
run_git(&repo, &["init", "-q", "--bare", "-b", "config/default"]);
run_git(&repo, &["config", "user.email", "t@t.local"]);
run_git(&repo, &["config", "user.name", "Tester"]);
run_git(&repo, &["config", "commit.gpgsign", "false"]);
let fx = Self {
_dir: dir,
path,
repo,
};
let author = fx.path.join(".author");
let author_str = author.to_string_lossy().to_string();
run_git(
&fx.repo,
&[
"worktree",
"add",
"-q",
"--orphan",
"-b",
"config/default",
author_str.as_str(),
],
);
fs::write(author.join("version"), "1\n").unwrap();
run_git(&author, &["add", "version"]);
run_git(
&author,
&["commit", "-q", "-m", "config: init [config/default]"],
);
run_git(&fx.repo, &["worktree", "remove", author_str.as_str()]);
fx
}
pub(crate) fn commit_other(&self, file: &str, body: &str) {
let author = self.path.join(".amend");
let author_str = author.to_string_lossy().to_string();
run_git(
&self.repo,
&[
"worktree",
"add",
"-q",
author_str.as_str(),
"config/default",
],
);
let dest = author.join(file);
fs::create_dir_all(dest.parent().expect("a file has a parent")).unwrap();
fs::write(&dest, body).unwrap();
run_git(&author, &["add", file]);
run_git(&author, &["commit", "-q", "-m", &format!("add {file}")]);
run_git(&self.repo, &["worktree", "remove", author_str.as_str()]);
}
}