#[cfg(test)]
mod tests {
fn rust_files_under(dir: &std::path::Path) -> Vec<std::path::PathBuf> {
let mut files = Vec::new();
let Ok(entries) = std::fs::read_dir(dir) else {
return files;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
files.extend(rust_files_under(&path));
} else if path.extension().is_some_and(|ext| ext == "rs") {
files.push(path);
}
}
files
}
fn crates_dir() -> std::path::PathBuf {
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("..")
}
fn is_scan_target(path: &std::path::Path) -> bool {
!path.ends_with("suppressions_guard.rs")
}
fn attribute_bodies_after<'a>(source: &'a str, prefix: &str) -> Vec<&'a str> {
let mut bodies = Vec::new();
let mut rest = source;
while let Some(pos) = rest.find(prefix) {
let after_open = &rest[pos + prefix.len()..];
let mut depth = 1i32;
let mut end = None;
for (i, c) in after_open.char_indices() {
match c {
'[' => depth += 1,
']' => {
depth -= 1;
if depth == 0 {
end = Some(i);
break;
}
}
_ => {}
}
}
let Some(end) = end else { break };
bodies.push(&after_open[..end]);
rest = &after_open[end..];
}
bodies
}
fn count_allow_attributes(source: &str) -> usize {
let mut count = 0;
for prefix in ["#![", "#["] {
for body in attribute_bodies_after(source, prefix) {
if body.contains("allow(") {
count += 1;
}
}
}
count
}
const EXPECTED: &[(&str, usize)] = &[
("knotra-app/src/state/palette.rs", 1),
("knotra-app/src/view/command_palette.rs", 1),
("knotra-app/src/view/detail_panel.rs", 1),
("knotra-app/src/view/shortcuts_overlay.rs", 1),
("knotra-vcs/src/vcs/git.rs", 1),
];
#[test]
fn every_suppression_is_tracked_and_justified() {
let dir = crates_dir();
let files = rust_files_under(&dir);
assert!(
files.len() > 90,
"found only {} .rs files under {} -- path resolution is \
broken (expected 90+ across all three crates), not that \
nothing needed checking",
files.len(),
dir.display()
);
let mut actual: std::collections::BTreeMap<String, usize> =
std::collections::BTreeMap::new();
for file in files.iter().filter(|f| is_scan_target(f)) {
let Ok(source) = std::fs::read_to_string(file) else {
continue;
};
let n = count_allow_attributes(&source);
if n > 0 {
let rel = file
.strip_prefix(&dir)
.unwrap_or(file)
.to_string_lossy()
.replace('\\', "/");
actual.insert(rel, n);
}
}
let expected: std::collections::BTreeMap<String, usize> = EXPECTED
.iter()
.map(|(path, n)| ((*path).to_owned(), *n))
.collect();
assert_eq!(
actual, expected,
"lint suppressions under crates/ changed. If you added a new \
one, name it in suppressions_guard.rs's EXPECTED list with a \
justification comment at its own site — a suppression with no \
comment, in any form, is exactly what this guard exists to \
catch. If you removed or resolved one, remove its entry from \
EXPECTED here too.\n\
actual: {actual:?}\n\
expected: {expected:?}"
);
}
}