use std::process::Command; use tempfile::TempDir; fn arc_cmd() -> Command { let mut cmd = Command::new(env!("CARGO_BIN_EXE_arc")); cmd.env("NO_COLOR", "1"); cmd } fn init_repo() -> TempDir { let dir = TempDir::new().unwrap(); arc_cmd() .arg("init") .current_dir(dir.path()) .output() .expect("failed to init"); dir } fn commit_file(dir: &TempDir, name: &str, content: &str, msg: &str) -> String { std::fs::write(dir.path().join(name), content).unwrap(); let output = arc_cmd() .args(["commit", msg]) .current_dir(dir.path()) .output() .expect("failed to commit"); assert!(output.status.success()); String::from_utf8_lossy(&output.stdout) .trim() .strip_prefix("✓ committed ") .unwrap() .to_string() } #[test] fn graft_single_commit_onto_bookmark() { let dir = init_repo(); commit_file(&dir, "base.txt", "base\n", "base"); arc_cmd() .args(["mark", "add", "feature"]) .current_dir(dir.path()) .output() .expect("failed"); arc_cmd() .args(["switch", "feature"]) .current_dir(dir.path()) .output() .expect("failed"); let cherry = commit_file(&dir, "cherry.txt", "cherry\n", "cherry pick me"); arc_cmd() .args(["switch", "main"]) .current_dir(dir.path()) .output() .expect("failed"); let output = arc_cmd() .args(["graft", &cherry, "--onto", "main"]) .current_dir(dir.path()) .output() .expect("failed"); assert!(output.status.success()); let stdout = String::from_utf8_lossy(&output.stdout); assert!(stdout.contains("grafted")); assert!(dir.path().join("cherry.txt").exists()); let content = std::fs::read_to_string(dir.path().join("cherry.txt")).unwrap(); assert_eq!(content, "cherry\n"); } #[test] fn graft_creates_new_commit() { let dir = init_repo(); commit_file(&dir, "base.txt", "base\n", "base"); arc_cmd() .args(["mark", "add", "feature"]) .current_dir(dir.path()) .output() .expect("failed"); arc_cmd() .args(["switch", "feature"]) .current_dir(dir.path()) .output() .expect("failed"); let cherry = commit_file(&dir, "cherry.txt", "cherry\n", "cherry"); arc_cmd() .args(["switch", "main"]) .current_dir(dir.path()) .output() .expect("failed"); arc_cmd() .args(["graft", &cherry, "--onto", "main"]) .current_dir(dir.path()) .output() .expect("failed"); let log_output = arc_cmd() .args(["log"]) .current_dir(dir.path()) .output() .expect("failed"); let log_stdout = String::from_utf8_lossy(&log_output.stdout); assert!(log_stdout.contains("graft")); } #[test] fn graft_fails_with_dirty_worktree() { let dir = init_repo(); let id = commit_file(&dir, "a.txt", "a\n", "first"); std::fs::write(dir.path().join("dirty.txt"), "dirty\n").unwrap(); let output = arc_cmd() .args(["graft", &id, "--onto", "main"]) .current_dir(dir.path()) .output() .expect("failed"); assert!(!output.status.success()); let stderr = String::from_utf8_lossy(&output.stderr); assert!(stderr.contains("uncommitted changes")); } #[test] fn graft_preserves_original_commits() { let dir = init_repo(); commit_file(&dir, "base.txt", "base\n", "base"); arc_cmd() .args(["mark", "add", "feature"]) .current_dir(dir.path()) .output() .expect("failed"); arc_cmd() .args(["switch", "feature"]) .current_dir(dir.path()) .output() .expect("failed"); let cherry = commit_file(&dir, "cherry.txt", "cherry\n", "cherry"); arc_cmd() .args(["switch", "main"]) .current_dir(dir.path()) .output() .expect("failed"); arc_cmd() .args(["graft", &cherry, "--onto", "main"]) .current_dir(dir.path()) .output() .expect("failed"); let show_output = arc_cmd() .args(["show", &cherry]) .current_dir(dir.path()) .output() .expect("failed"); assert!(show_output.status.success()); let show_stdout = String::from_utf8_lossy(&show_output.stdout); assert!(show_stdout.contains("cherry")); } #[test] fn graft_with_commit_prefix() { let dir = init_repo(); commit_file(&dir, "base.txt", "base\n", "base"); arc_cmd() .args(["mark", "add", "feature"]) .current_dir(dir.path()) .output() .expect("failed"); arc_cmd() .args(["switch", "feature"]) .current_dir(dir.path()) .output() .expect("failed"); let cherry = commit_file(&dir, "cherry.txt", "cherry\n", "cherry"); arc_cmd() .args(["switch", "main"]) .current_dir(dir.path()) .output() .expect("failed"); let short = &cherry[..12]; let output = arc_cmd() .args(["graft", short, "--onto", "main"]) .current_dir(dir.path()) .output() .expect("failed"); assert!(output.status.success()); }