const EXEMPT: &[(&str, &str, &str)] = &[
(
"did_templates.rs",
"cmd_show",
"prints the template (or its rendering) as JSON on every destination",
),
(
"did_templates.rs",
"cmd_export",
"prints the portable template JSON on every destination",
),
(
"rooms/files.rs",
"cmd_rooms_file_get",
"writes the file's bytes, not a description of them",
),
(
"webs.rs",
"cmd_webs_list",
"delegates to dids::cmd_dids_list (the method-neutral listing), which is held here",
),
(
"rooms_custody.rs",
"cmd_rooms_keys_list",
"delegates to cmd_rooms_keys_list_named, which is held here",
),
];
const JSON_MARKERS: &[&str] = &[
"is_json_output",
"print_json",
"print_result(",
"show(&",
"_presenting(",
];
const READ_WORDS: &[&str] = &[
"get", "list", "show", "seeds", "info", "status", "view", "history", "export", "explain",
"report",
];
fn handlers(src: &str) -> Vec<(String, String)> {
let src = src.split("#[cfg(test)]").next().unwrap_or(src);
let mut starts: Vec<(usize, String)> = Vec::new();
for prefix in ["pub async fn cmd_", "pub fn cmd_"] {
let mut from = 0;
while let Some(i) = src[from..].find(prefix) {
let at = from + i;
if at == 0 || src.as_bytes()[at - 1] == b'\n' {
let name_start = at + prefix.len() - "cmd_".len();
let name: String = src[name_start..]
.chars()
.take_while(|c| c.is_alphanumeric() || *c == '_')
.collect();
starts.push((at, name));
}
from = at + prefix.len();
}
}
starts.sort();
let mut out = Vec::new();
for (i, (at, name)) in starts.iter().enumerate() {
let end = starts.get(i + 1).map(|(e, _)| *e).unwrap_or(src.len());
out.push((name.clone(), src[*at..end].to_string()));
}
out
}
fn rust_files(dir: &std::path::Path, out: &mut Vec<std::path::PathBuf>) {
for entry in std::fs::read_dir(dir).expect("src/commands") {
let path = entry.expect("entry").path();
if path.is_dir() {
rust_files(&path, out);
} else if path.extension().is_some_and(|x| x == "rs") {
out.push(path);
}
}
}
#[test]
fn every_read_handler_has_a_json_path() {
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/commands");
let mut missing = Vec::new();
let mut seen = 0;
let mut files = Vec::new();
rust_files(&dir, &mut files);
files.sort();
for path in files {
let file = path
.strip_prefix(&dir)
.unwrap()
.to_string_lossy()
.replace('\\', "/");
let src = std::fs::read_to_string(&path).expect("read");
for (name, body) in handlers(&src) {
let words: Vec<&str> = name.split('_').collect();
if !READ_WORDS.iter().any(|w| words.contains(w)) {
continue;
}
seen += 1;
if EXEMPT.iter().any(|(f, n, _)| *f == file && *n == name) {
continue;
}
if !JSON_MARKERS.iter().any(|m| body.contains(m)) {
missing.push(format!("{file}::{name}"));
}
}
}
assert!(seen > 40, "the census found only {seen} read handlers");
assert!(
missing.is_empty(),
"read handlers with no JSON output (#2588) — give each an `is_json_output` branch \
printing the SDK response, or list it in EXEMPT with the reason:\n {}",
missing.join("\n ")
);
}
#[test]
fn every_exemption_names_a_live_handler() {
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/commands");
for (file, name, why) in EXEMPT {
let src = std::fs::read_to_string(dir.join(file)).expect("exempt file exists");
assert!(
handlers(&src).iter().any(|(n, _)| n == name),
"{file}::{name} is exempt ({why}) but no longer exists"
);
}
}