use std::fs;
use std::path::{Path, PathBuf};
fn source_files() -> Vec<PathBuf> {
fn walk(dir: &Path, out: &mut Vec<PathBuf>) {
for e in fs::read_dir(dir).unwrap().flatten() {
let p = e.path();
if p.is_dir() {
walk(&p, out);
} else if p.extension().and_then(|s| s.to_str()) == Some("rs") {
out.push(p);
}
}
}
let mut out = Vec::new();
walk(Path::new("src"), &mut out);
out
}
fn is_comment(line: &str) -> bool {
let t = line.trim_start();
t.starts_with("//") || t.starts_with('*')
}
fn scan(exempt: &[&str], needles: &[&str]) -> Vec<String> {
let mut hits = Vec::new();
for f in source_files() {
let name = f.file_name().unwrap().to_str().unwrap().to_string();
if exempt.contains(&name.as_str()) {
continue;
}
let src = fs::read_to_string(&f).unwrap();
for (i, line) in src.lines().enumerate() {
if is_comment(line) {
continue;
}
if needles.iter().any(|n| line.contains(n)) {
hits.push(format!(" {}:{} {}", f.display(), i + 1, line.trim()));
}
}
}
hits
}
#[test]
fn git_apply_is_confined_to_patch_rs() {
let offenders = scan(&["patch.rs"], &["[\"apply\"", "vec![\"apply\""]);
assert!(
offenders.is_empty(),
"`git apply` invoked outside patch.rs — route the apply through a ValidatedPatch \
(design/48 §1.2a), don't allowlist:\n{}",
offenders.join("\n")
);
}
#[test]
fn hub_dirs_is_reached_only_through_the_scoped_resolver() {
let offenders = scan(&["api.rs", "main.rs", "reposdiscover.rs", "refcmd.rs"], &["crosshub::hub_dirs()"]);
assert!(
offenders.is_empty(),
"crosshub::hub_dirs() called outside allowed_hubs()/CLI — route it through ServeScope \
(design/48 §1.2b), don't allowlist:\n{}",
offenders.join("\n")
);
}