use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
static SEQUENCE: AtomicU64 = AtomicU64::new(0);
pub struct GitFixture {
pub root: PathBuf,
}
impl GitFixture {
pub fn new() -> Self {
let time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
let root = std::env::temp_dir().join(format!(
"weavatrix-contract-{}-{time}-{}",
std::process::id(),
SEQUENCE.fetch_add(1, Ordering::Relaxed)
));
fs::create_dir_all(&root).unwrap();
git(&root, &["init", "-q"]);
Self { root }
}
pub fn write(&self, relative: &str, contents: &str) {
let path = self.root.join(relative);
fs::create_dir_all(path.parent().unwrap_or(Path::new("."))).unwrap();
fs::write(path, contents).unwrap();
}
pub fn commit(&self, message: &str) {
git(&self.root, &["add", "-A"]);
git(
&self.root,
&[
"-c",
"user.name=Test",
"-c",
"user.email=test@example.invalid",
"commit",
"-qm",
message,
],
);
}
}
impl Drop for GitFixture {
fn drop(&mut self) {
fs::remove_dir_all(&self.root).ok();
}
}
fn git(path: &Path, args: &[&str]) {
let output = Command::new("git")
.args(args)
.current_dir(path)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
}