From 96f0ac204e2476bfb3bd5f5e42997d2e85d1a954 Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 26 Oct 2026 22:07:49 +0000 Subject: [PATCH] test(engine): read past #[cfg(test)] imports in the file-system scan --- crates/engine/src/file_access.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/file_access.rs b/crates/engine/src/file_access.rs index bcda8492..c2fb8a3c 100644 --- a/crates/engine/src/file_access.rs +++ b/crates/engine/src/file_access.rs @@ -468,8 +468,20 @@ mod tests { continue; } let text = std::fs::read_to_string(&path).unwrap(); - // Test modules (which build their own fixtures) come last in each file. - let code = text.split("#[cfg(test)]").next().unwrap_or(""); + // Test modules (which build their own fixtures) come last in each file; a + // `#[cfg(test)]` import near the top doesn't start them. + let mut code = text.as_str(); + let mut from = 0; + while let Some(i) = text[from..].find("#[cfg(test)]") { + let at = from + i; + let next = text[at + "#[cfg(test)]".len()..].trim_start(); + if ["use ", "pub use ", "pub(crate) use ", "pub(super) use "].iter().any(|p| next.starts_with(p)) { + from = at + 1; + continue; + } + code = &text[..at]; + break; + } for (n, line) in code.lines().enumerate() { let line = line.trim_start(); let touches = ["std::fs", "File::open", "File::create", "OpenOptions"].iter().any(|w| line.contains(w));