use super::super::fixtures::*;
use super::*;
#[test]
fn a_dir_in_another_repo_resolves_that_repo_s_root() -> Result<(), String> {
let theirs = crate::testrepo::TestRepo::new("cli-foreign")?;
theirs.write("SPEC.md", "# SPEC\n\n## \u{a7}G GOAL\n\ntheirs\n")?;
theirs.commit("a repo that is not ours")?;
let arg = theirs.path().display().to_string();
assert_eq!(
root_for(&["check".into(), arg]),
theirs.path().to_path_buf(),
"the root is the one ABOVE the given dir"
);
let here = root_for(&["review".into(), "HEAD".into()]);
assert_eq!(here, repo_root(), "a rev is not a dir");
assert_eq!(root_for(&["check".into()]), repo_root(), "no arg at all");
Ok(())
}
#[test]
fn a_flag_before_the_dir_does_not_hide_it() -> Result<(), String> {
let theirs = crate::testrepo::TestRepo::new("cli-flag-first")?;
theirs.write("SPEC.md", "# SPEC\n\n## \u{a7}G GOAL\n\ntheirs\n")?;
theirs.commit("a repo behind a flag")?;
let arg = theirs.path().display().to_string();
let want = theirs.path().to_path_buf();
for args in [
vec!["plan".to_string(), "--triage".into(), arg.clone()],
vec!["sync".to_string(), "--check".into(), arg.clone()],
vec!["split".to_string(), "--apply".into(), arg.clone()],
vec!["check".to_string(), arg.clone()],
] {
assert_eq!(root_for(&args), want, "{args:?}");
}
assert_eq!(
root_for(&["sync".to_string(), "--check".into()]),
repo_root()
);
Ok(())
}
#[test]
fn every_spelling_of_a_dir_resolves_to_one_root() -> Result<(), String> {
let r = crate::testrepo::TestRepo::new("cli-relative")?;
r.write("src/code/SPEC.md", "# SPEC\n\n## \u{a7}G GOAL\n\na node\n")?;
r.commit("a node two levels down")?;
let root = r.path().to_path_buf();
let abs = root.join("src").join("code").display().to_string();
for (typed_in, arg) in [
(root.clone(), "src/code"),
(root.clone(), "./src/code"),
(root.join("src"), "code"),
(root.join("src"), "./code"),
(root.clone(), abs.as_str()),
] {
assert_eq!(
root_for_from(&argv(&["seam", arg]), &typed_in),
root,
"`sherd seam {arg}` typed in {}",
typed_in.display()
);
}
assert_eq!(
root_for_from(&argv(&["review", "HEAD"]), &root),
root,
"a rev is not a dir"
);
Ok(())
}
#[test]
fn repo_root_finds_the_tree_that_has_both_markers() {
let Ok(repo) = crate::testrepo::TestRepo::new("cli-root") else {
unreachable!("a fixture repository is buildable")
};
let root = repo.path();
assert_eq!(repo_root_from(root), root);
let nested = root.join("src").join("deep");
let Ok(()) = std::fs::create_dir_all(&nested) else {
unreachable!("a nested dir is creatable")
};
assert_eq!(repo_root_from(&nested), root, "the walk goes UP");
}
#[test]
fn a_tree_that_is_not_a_repository_resolves_to_itself() {
let dir = std::env::temp_dir().join(format!(
"sherd-notarepo-{}-{}",
std::process::id(),
line!()
));
let _ = std::fs::remove_dir_all(&dir);
let Ok(()) = std::fs::create_dir_all(&dir) else {
unreachable!("a scratch dir is creatable")
};
let Ok(()) = std::fs::write(dir.join("SPEC.md"), "# SPEC\n") else {
unreachable!("a scratch file is writable")
};
assert_eq!(repo_root_from(&dir), dir);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn depth_defaults_to_rule_and_refuses_a_typo() {
let depth = |a: &[&str]| {
let mut v = vec!["sherd", "lens", "."];
v.extend(a);
match parse::Cli::try_parse_from(v).map(|c| c.cmd) {
Ok(Some(Cmd::Lens { depth, .. })) => Some(lens::Depth::from(depth)),
_ => None,
}
};
assert_eq!(depth(&[]), Some(lens::Depth::Rule));
assert_eq!(depth(&["--depth", "all"]), Some(lens::Depth::All));
assert_eq!(depth(&["--depth", "why"]), Some(lens::Depth::Why));
assert_eq!(depth(&["--depth", "rules"]), None);
assert_eq!(depth(&["--depth"]), None);
}
#[test]
fn arg_dir_resolves_against_root_not_the_cwd() {
let root = Path::new("/tmp/xyz");
assert_eq!(arg_dir(None, root), root.to_path_buf());
assert_eq!(arg_dir(Some("src/tdd"), root), root.join("src/tdd"));
assert_eq!(
arg_dir(Some("/elsewhere"), root),
PathBuf::from("/elsewhere")
);
}
#[test]
fn a_miss_names_the_spelling_that_would_have_worked() {
let r = crate::testrepo::TestRepo::new("cli-miss").expect("fixture");
r.write("src/code/SPEC.md", "# SPEC\n").expect("write");
let root = r.path();
let msg = no_node(root, &root.join("code"));
assert!(msg.contains("matched no node"), "{msg}");
assert!(msg.contains("resolved against the repo ROOT"), "{msg}");
assert!(msg.contains("did you mean `src/code`?"), "{msg}");
let none = no_node(root, &root.join("nowhere"));
assert!(none.contains("matched no node"), "{none}");
assert_eq!(none.lines().count(), 1, "{none}");
}
#[test]
fn a_miss_with_several_candidates_names_them_all() -> Result<(), String> {
let r = crate::testrepo::TestRepo::new("cli-miss-many")?;
r.write("a/code/SPEC.md", "# SPEC\n")?;
r.write("b/code/SPEC.md", "# SPEC\n")?;
let msg = no_node(r.path(), &r.path().join("code"));
assert!(msg.contains("nodes with that name:"), "{msg}");
assert!(
msg.contains("`a/code`") && msg.contains("`b/code`"),
"{msg}"
);
assert!(!msg.contains("did you mean"), "no single guess: {msg}");
Ok(())
}