Add a centralized ui module with Arc's visual identity: colored commit IDs (magenta), bookmarks (cyan), tags (yellow), status symbols, and diff highlighting. Update all command output and tests accordingly.
161 lines
4.2 KiB
Rust
161 lines
4.2 KiB
Rust
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 revert_single_commit() {
|
|
let dir = init_repo();
|
|
commit_file(&dir, "a.txt", "original\n", "first");
|
|
let id2 = commit_file(&dir, "a.txt", "changed\n", "second");
|
|
|
|
let output = arc_cmd()
|
|
.args(["revert", &id2])
|
|
.current_dir(dir.path())
|
|
.output()
|
|
.expect("failed");
|
|
|
|
assert!(output.status.success());
|
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
assert!(stdout.contains("reverted"));
|
|
|
|
let content = std::fs::read_to_string(dir.path().join("a.txt")).unwrap();
|
|
assert_eq!(content, "original\n");
|
|
}
|
|
|
|
#[test]
|
|
fn revert_creates_new_commit() {
|
|
let dir = init_repo();
|
|
commit_file(&dir, "a.txt", "v1\n", "first");
|
|
let id2 = commit_file(&dir, "a.txt", "v2\n", "second");
|
|
|
|
let output = arc_cmd()
|
|
.args(["revert", &id2])
|
|
.current_dir(dir.path())
|
|
.output()
|
|
.expect("failed");
|
|
|
|
assert!(output.status.success());
|
|
|
|
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("revert"));
|
|
}
|
|
|
|
#[test]
|
|
fn revert_file_addition() {
|
|
let dir = init_repo();
|
|
commit_file(&dir, "a.txt", "a\n", "first");
|
|
let id2 = commit_file(&dir, "b.txt", "b\n", "add b");
|
|
|
|
let output = arc_cmd()
|
|
.args(["revert", &id2])
|
|
.current_dir(dir.path())
|
|
.output()
|
|
.expect("failed");
|
|
|
|
assert!(output.status.success());
|
|
assert!(!dir.path().join("b.txt").exists());
|
|
}
|
|
|
|
#[test]
|
|
fn revert_file_deletion() {
|
|
let dir = init_repo();
|
|
commit_file(&dir, "a.txt", "a\n", "first");
|
|
commit_file(&dir, "b.txt", "b\n", "add b");
|
|
|
|
std::fs::remove_file(dir.path().join("b.txt")).unwrap();
|
|
let id3 = {
|
|
let output = arc_cmd()
|
|
.args(["commit", "delete b"])
|
|
.current_dir(dir.path())
|
|
.output()
|
|
.expect("failed");
|
|
assert!(output.status.success());
|
|
String::from_utf8_lossy(&output.stdout)
|
|
.trim()
|
|
.strip_prefix("✓ committed ")
|
|
.unwrap()
|
|
.to_string()
|
|
};
|
|
|
|
let output = arc_cmd()
|
|
.args(["revert", &id3])
|
|
.current_dir(dir.path())
|
|
.output()
|
|
.expect("failed");
|
|
|
|
assert!(output.status.success());
|
|
let content = std::fs::read_to_string(dir.path().join("b.txt")).unwrap();
|
|
assert_eq!(content, "b\n");
|
|
}
|
|
|
|
#[test]
|
|
fn revert_fails_with_dirty_worktree() {
|
|
let dir = init_repo();
|
|
let id = commit_file(&dir, "a.txt", "a\n", "first");
|
|
|
|
std::fs::write(dir.path().join("a.txt"), "dirty\n").unwrap();
|
|
|
|
let output = arc_cmd()
|
|
.args(["revert", &id])
|
|
.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 revert_with_prefix() {
|
|
let dir = init_repo();
|
|
commit_file(&dir, "a.txt", "v1\n", "first");
|
|
let id2 = commit_file(&dir, "a.txt", "v2\n", "second");
|
|
|
|
let short = &id2[..12];
|
|
let output = arc_cmd()
|
|
.args(["revert", short])
|
|
.current_dir(dir.path())
|
|
.output()
|
|
.expect("failed");
|
|
|
|
assert!(output.status.success());
|
|
let content = std::fs::read_to_string(dir.path().join("a.txt")).unwrap();
|
|
assert_eq!(content, "v1\n");
|
|
}
|