use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
const ALLOWLIST: &[&str] = &[
"atomic_file.rs",
"daemon/client.rs",
"daemon/client_tests.rs",
"daemon/frame.rs",
"daemon/protocol.rs",
"daemon/server_tests.rs",
"lib.rs",
"log_dedup.rs",
"orchestrator/dispatch.rs",
"orchestrator/dispatch/backend.rs",
"orchestrator/dispatch/backend/calibration.rs",
"orchestrator/dispatch/backend/evidence.rs",
"orchestrator/dispatch/backend/evidence/timing.rs",
"orchestrator/dispatch/backend/store.rs",
"orchestrator/dispatch/backend/store/inspection.rs",
"orchestrator/dispatch/backend/store/persistence.rs",
"orchestrator/dispatch/backend/workload.rs",
"orchestrator/mod.rs",
"orchestrator/reporting.rs",
"orchestrator_config/runtime_tests.rs",
"reporting.rs",
"subcommands/backend.rs",
"subcommands/calibrate_autoroute.rs",
"subcommands/doctor.rs",
"subcommands/watch.rs",
];
fn src_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src")
}
fn opens_inline_body(lines: &[&str], index: usize) -> bool {
let next = lines
.iter()
.skip(index + 1)
.find(|line| !line.trim().is_empty())
.map(|line| line.trim())
.unwrap_or_default();
!next.starts_with("#[path")
}
fn offenders() -> BTreeSet<String> {
let root = src_dir();
let mut found = BTreeSet::new();
let mut stack = vec![root.clone()];
while let Some(dir) = stack.pop() {
let entries = std::fs::read_dir(&dir)
.unwrap_or_else(|error| panic!("read_dir({}) failed: {error}", dir.display()));
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
stack.push(path);
continue;
}
if path.extension().and_then(|value| value.to_str()) != Some("rs") {
continue;
}
let source = std::fs::read_to_string(&path)
.unwrap_or_else(|error| panic!("read {} failed: {error}", path.display()));
let lines: Vec<&str> = source.lines().map(str::trim).collect();
let inline = lines
.iter()
.enumerate()
.any(|(index, line)| *line == "#[cfg(test)]" && opens_inline_body(&lines, index));
if inline {
let rel = path
.strip_prefix(&root)
.unwrap_or(&path)
.to_string_lossy()
.replace('\\', "/");
found.insert(rel);
}
}
}
found
}
#[test]
fn no_inline_tests_in_src() {
let found = offenders();
let allowed: BTreeSet<String> = ALLOWLIST.iter().map(|entry| (*entry).to_string()).collect();
let unexpected: Vec<&String> = found.difference(&allowed).collect();
assert!(
unexpected.is_empty(),
"these files carry an inline #[cfg(test)] body; move it under crates/cli/tests/ and \
include it with `#[cfg(test)] #[path = \"../tests/...\"] mod ...;`: {unexpected:?}"
);
let stale: Vec<&String> = allowed.difference(&found).collect();
assert!(
stale.is_empty(),
"these files no longer carry an inline test body; delete their ALLOWLIST entries: {stale:?}"
);
}
#[test]
fn a_path_included_module_is_not_an_inline_body() {
let included = ["#[cfg(test)]", "#[path = \"../tests/unit/x.rs\"]", "mod x;"];
assert!(!opens_inline_body(&included, 0));
let inline = ["#[cfg(test)]", "mod tests {", "}"];
assert!(opens_inline_body(&inline, 0));
let spaced = [
"#[cfg(test)]",
"",
"#[path = \"../tests/unit/x.rs\"]",
"mod x;",
];
assert!(!opens_inline_body(&spaced, 0));
}
#[test]
fn the_scan_reaches_nested_source_directories() {
let root = src_dir();
let nested = root.join("subcommands");
assert!(
nested.is_dir(),
"expected a nested source directory to prove the walk descends"
);
let seen_nested = {
let mut stack = vec![root.clone()];
let mut count = 0usize;
while let Some(dir) = stack.pop() {
for entry in std::fs::read_dir(&dir).expect("read_dir").flatten() {
let path = entry.path();
if path.is_dir() {
stack.push(path);
} else if path.extension().and_then(|v| v.to_str()) == Some("rs")
&& path.starts_with(&nested)
{
count += 1;
}
}
}
count
};
assert!(
seen_nested > 1,
"the walk must descend into nested directories; saw {seen_nested} files under subcommands"
);
let _ = Path::new("");
}