use std::fs; use std::path::PathBuf; use std::process::Command; use agent686::git; fn temp_dir(tag: &str) -> PathBuf { let dir = std::env::temp_dir().join(format!("agent686-git-{tag}-{}", std::process::id())); let _ = fs::remove_dir_all(&dir); fs::create_dir_all(&dir).unwrap(); dir } fn repo(tag: &str) -> PathBuf { let dir = temp_dir(tag); for args in [ &["init", "-q"][..], &["config", "user.email", "agent686@test"][..], &["config", "user.name", "agent686"][..], ] { assert!( Command::new("git").arg("-C").arg(&dir).args(args).status().unwrap().success() ); } dir } #[test] fn checkpoint_then_undo_restores_the_tree() { let dir = repo("roundtrip"); fs::write(dir.join("a.txt"), "original\n").unwrap(); let sha = git::checkpoint(&dir).expect("in a repo"); assert!(!sha.is_empty()); // The turn mutates the tree. fs::write(dir.join("a.txt"), "changed\n").unwrap(); fs::write(dir.join("b.txt"), "untracked\n").unwrap(); git::undo(&dir, &sha); assert_eq!(fs::read_to_string(dir.join("a.txt")).unwrap(), "original\n"); assert!(!dir.join("b.txt").exists(), "untracked files must be removed"); } #[test] fn checkpoint_is_none_outside_a_repo() { let dir = temp_dir("norepo"); assert!(git::checkpoint(&dir).is_none()); }