use std::fs;
use walkdir::WalkDir;
const FORBIDDEN: &[&str] = &[".post(", ".patch(", ".put(", ".delete("];
#[test]
fn no_write_methods_in_src() {
let mut violations: Vec<String> = Vec::new();
for entry in WalkDir::new("src").into_iter().filter_map(|e| e.ok()) {
let path = entry.path();
if !path.is_file() {
continue;
}
if path.extension().and_then(|e| e.to_str()) != Some("rs") {
continue;
}
let path_str = path.to_string_lossy().replace('\\', "/");
if path_str.ends_with("auth/windows.rs") {
continue;
}
let body = match fs::read_to_string(path) {
Ok(s) => s,
Err(_) => continue,
};
for needle in FORBIDDEN {
for (line_no, line) in body.lines().enumerate() {
let stripped = line.split("//").next().unwrap_or(line);
if stripped.contains(needle) {
violations.push(format!(
"{}:{}: forbidden write call {:?} in `{}`",
path_str,
line_no + 1,
needle,
line.trim()
));
}
}
}
}
assert!(
violations.is_empty(),
"Read-only invariant violated:\n{}",
violations.join("\n")
);
}