use std::path::{Path, PathBuf};
use std::process::Command;
const BIN: &str = env!("CARGO_BIN_EXE_roteiro");
fn git(dir: &Path, args: &[&str]) {
let status = Command::new("git")
.args([
"-c",
"user.name=Test",
"-c",
"user.email=test@example.com",
"-c",
"commit.gpgsign=false",
"-c",
"init.defaultBranch=main",
])
.args(args)
.current_dir(dir)
.status()
.expect("run git");
assert!(status.success(), "git {args:?} failed");
}
fn roteiro(dir: &Path, args: &[&str]) -> std::process::Output {
Command::new(BIN)
.args(args)
.current_dir(dir)
.output()
.expect("run roteiro")
}
fn write(dir: &Path, rel: &str, content: &str) {
let path = dir.join(rel);
std::fs::create_dir_all(path.parent().unwrap()).expect("mkdir");
std::fs::write(path, content).expect("write");
}
fn fresh_dir(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("roteiro-check-cli-{}-{name}", std::process::id()));
std::fs::remove_dir_all(&dir).ok();
std::fs::create_dir_all(&dir).expect("mkdir");
dir
}
const ADR: &str = "---\n\
adr-id: \"0001\"\n\
status: Accepted\n\
---\n\
\n\
# ADR-0001: Thing\n\
\n\
## Decision\n\
\n\
The design centres on [[src/lib.rs#Thing]].\n";
#[test]
fn worktree_check_gates_a_drift_introducing_edit_that_committed_ignores() {
let dir = fresh_dir("worktree");
git(&dir, &["init", "-q"]);
write(&dir, "src/lib.rs", "pub struct Thing;\n");
write(&dir, "docs/adr/0001-thing.md", ADR);
git(&dir, &["add", "."]);
git(&dir, &["commit", "-q", "-m", "init"]);
assert!(
roteiro(&dir, &["check"]).status.success(),
"clean worktree check should pass"
);
assert!(
roteiro(&dir, &["check", "--committed"]).status.success(),
"clean committed check should pass"
);
write(&dir, "src/lib.rs", "pub struct Other;\n");
let worktree = roteiro(&dir, &["check"]);
assert!(
!worktree.status.success(),
"worktree check must fail on the drift about to be committed: {}",
String::from_utf8_lossy(&worktree.stderr)
);
assert!(
roteiro(&dir, &["check", "--committed"]).status.success(),
"committed check should still pass — HEAD is unchanged"
);
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn staged_check_validates_the_index_not_the_working_tree() {
let dir = fresh_dir("staged");
git(&dir, &["init", "-q"]);
write(&dir, "src/lib.rs", "pub struct Thing;\n");
write(&dir, "docs/adr/0001-thing.md", ADR);
git(&dir, &["add", "."]);
git(&dir, &["commit", "-q", "-m", "init"]);
write(&dir, "src/lib.rs", "pub struct Other;\n");
git(&dir, &["add", "src/lib.rs"]);
write(&dir, "src/lib.rs", "pub struct Thing;\n");
assert!(
roteiro(&dir, &["check"]).status.success(),
"worktree check should pass (Thing is present on disk)"
);
let staged = roteiro(&dir, &["check", "--staged"]);
assert!(
!staged.status.success(),
"staged check must fail on drift the commit would record: {}",
String::from_utf8_lossy(&staged.stderr)
);
assert!(
String::from_utf8_lossy(&staged.stderr).contains("does not resolve"),
"reports the dangling staged link: {}",
String::from_utf8_lossy(&staged.stderr)
);
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn check_validates_blueprint_links_like_adrs() {
let dir = fresh_dir("blueprint");
git(&dir, &["init", "-q"]);
write(&dir, "src/lib.rs", "pub struct Widget;\n");
write(
&dir,
"docs/plans/widget.md",
"# Widget — Technical Implementation Plan\n\n\
## 1. Design\n\nThe core type is [[src/lib.rs#Widget]].\n",
);
git(&dir, &["add", "."]);
git(&dir, &["commit", "-q", "-m", "init"]);
let ok = roteiro(&dir, &["check", "--committed"]);
assert!(
ok.status.success(),
"blueprint with a resolvable link should pass: {}",
String::from_utf8_lossy(&ok.stderr)
);
assert!(
String::from_utf8_lossy(&ok.stdout).contains("1 blueprint(s)"),
"check reports the blueprint: {}",
String::from_utf8_lossy(&ok.stdout)
);
write(
&dir,
"docs/plans/widget.md",
"# Widget — Technical Implementation Plan\n\n\
## 1. Design\n\nGone: [[src/lib.rs#Ghost]].\n",
);
git(&dir, &["commit", "-qam", "dangle"]);
let bad = roteiro(&dir, &["check", "--committed"]);
assert!(
!bad.status.success(),
"a blueprint link to a missing symbol must fail check"
);
assert!(
String::from_utf8_lossy(&bad.stderr).contains("does not resolve"),
"reports the dangling blueprint link: {}",
String::from_utf8_lossy(&bad.stderr)
);
std::fs::remove_dir_all(&dir).ok();
}