use super::*;
use crate::testrepo::TestRepo;
const BASE: &str = "pub fn old() -> u8 {\n 1\n}\n\n#[cfg(test)]\nmod tests {\n #[test]\n fn t() { super::old(); }\n}\n";
const WITH_STUB: &str = "pub fn old() -> u8 {\n 1\n}\n\npub fn added(_x: u8) -> u8 {\n 0\n}\n\n#[cfg(test)]\nmod tests {\n #[test]\n fn t() { super::old(); }\n #[test]\n fn u() { assert_eq!(super::added(1), 0); }\n}\n";
fn repo_with_a_stub(tag: &str) -> Result<TestRepo, String> {
let r = TestRepo::new(tag)?;
r.write("src/thing/mod.rs", BASE)?;
r.commit("base")?;
r.write("src/thing/mod.rs", WITH_STUB)?;
r.commit("add a fn")?;
Ok(r)
}
#[test]
fn added_in_commit_reads_pub_fns_out_of_the_diff() {
assert_eq!(check_added(), Ok(()));
}
fn names_in(added: &AddedFns) -> Vec<&str> {
added
.iter()
.flat_map(|(_, v)| v.iter().map(String::as_str))
.collect()
}
const MIXED: &str = concat!(
"+++ b/src/x/mod.rs\n",
"+pub fn added(a: u8) -> bool {\n",
"+pub fn generic<T>(t: T) {\n",
"-pub fn removed() {\n",
" pub fn untouched() {\n",
"+fn private() {\n",
"+++ b/src/x/other.rs\n",
"+pub fn not_a_module_file() {\n"
);
#[test]
fn only_added_pub_fns_in_a_module_are_collected() {
let got = added_fns(MIXED);
assert_eq!(
names_in(&got),
vec!["added", "generic"],
"a REMOVED fn is not added, a private one is not public, and a \
file that is not `mod.rs` is not a node module"
);
}
#[test]
fn an_empty_diff_adds_nothing_without_erroring() {
assert!(added_fns("").is_empty());
}
fn check_added() -> Result<(), String> {
let r = repo_with_a_stub("review-added")?;
let added = added_in_commit(r.path(), "HEAD").map_err(|e| e.to_string())?;
let names = names_in(&added);
assert!(
names.contains(&"added"),
"the new fn must be seen: {names:?}"
);
assert!(
!names.contains(&"old"),
"not added by this commit: {names:?}"
);
Ok(())
}
#[test]
fn a_commit_touching_no_module_yields_nothing() {
assert_eq!(check_spec_only(), Ok(()));
}
fn check_spec_only() -> Result<(), String> {
let r = TestRepo::new("review-spec")?;
r.write("SPEC.md", "# SPEC\n\nchanged\n")?;
r.commit("spec only")?;
assert!(
added_in_commit(r.path(), "HEAD")
.map_err(|e| e.to_string())?
.is_empty(),
"a spec-only commit adds no pub fn"
);
Ok(())
}
#[test]
fn commit_finds_the_stub_shape_end_to_end() {
assert_eq!(check_stub_found(), Ok(()));
}
fn check_stub_found() -> Result<(), String> {
let r = repo_with_a_stub("review-stub")?;
let found = commit(r.path(), "HEAD").map_err(|e| e.to_string())?;
assert!(
found.iter().any(|(_, f)| f.rule == "unwired"),
"expected an unwired finding, got {found:?}"
);
Ok(())
}
#[test]
fn an_unreadable_module_is_an_error_not_a_clean_review() {
let missing = Path::new("/nonexistent/thing/mod.rs");
assert!(node(missing, &["x".to_string()]).is_err());
}