use std::{
fs,
path::{Path, PathBuf},
process::Command,
sync::atomic::{AtomicU64, Ordering},
time::{SystemTime, UNIX_EPOCH},
};
use weavatrix_git::{EntryKind, Repository};
static SEQUENCE: AtomicU64 = AtomicU64::new(0);
#[test]
fn snapshot_is_sorted_and_reads_files_without_checkout() {
let root = fixture();
fs::create_dir_all(root.join("src")).unwrap();
fs::write(root.join("z.txt"), "z").unwrap();
fs::write(root.join("src").join("a.rs"), "fn main() {}").unwrap();
git(&root, &["add", "-A"]);
git(
&root,
&[
"-c",
"user.name=Test",
"-c",
"user.email=test@example.invalid",
"commit",
"-qm",
"snapshot",
],
);
let repository = Repository::open(&root).unwrap();
let snapshot = repository.snapshot("HEAD").unwrap();
let paths = snapshot
.entries
.iter()
.map(|entry| entry.path.as_slice())
.collect::<Vec<_>>();
assert_eq!(paths, [b"src/a.rs".as_slice(), b"z.txt".as_slice()]);
assert!(
snapshot
.entries
.iter()
.all(|entry| entry.kind == EntryKind::Blob)
);
for entry in snapshot.entries {
assert!(!repository.object(entry.id).unwrap().data.is_empty());
}
fs::remove_dir_all(root).ok();
}
fn fixture() -> PathBuf {
let unique = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
let root = std::env::temp_dir().join(format!(
"weavatrix-snapshot-{}-{unique}-{}",
std::process::id(),
SEQUENCE.fetch_add(1, Ordering::Relaxed)
));
fs::create_dir_all(&root).unwrap();
git(&root, &["init", "-q"]);
root
}
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)
);
}