test: the format-message scan skips only inline test modules

This commit is contained in:
aiongg
2026-10-26 21:51:51 +00:00
parent 69048937d0
commit 259208948b
+11 -7
View File
@@ -1041,13 +1041,7 @@ fn format_messages() -> std::collections::BTreeSet<String> {
dirs.push(path);
} else if path.extension().is_some_and(|ext| ext == "rs") {
let source = std::fs::read_to_string(&path).unwrap();
let app = source
.split(
"#[cfg(test)]
mod ",
)
.next()
.unwrap_or_default();
let app = test_module_start(&source).map_or(source.as_str(), |at| &source[..at]);
all.extend(tr_format_messages(app));
}
}
@@ -1055,6 +1049,16 @@ mod ",
all
}
/// Where the first inline `#[cfg(test)] mod x { … }` starts. A `#[cfg(test)] mod x;` line only
/// declares a file, and app code can follow it.
fn test_module_start(source: &str) -> Option<usize> {
let head = "#[cfg(test)]\nmod ";
source
.match_indices(head)
.map(|(at, _)| at)
.find(|at| source[at + head.len()..].trim_start_matches(|c: char| c.is_alphanumeric() || c == '_').starts_with(" {"))
}
/// The first argument of each `tr_format!` call in `source`, when it is a string literal.
fn tr_format_messages(source: &str) -> Vec<String> {
// built from parts, so this file's own source holds no call to find