From 4e2a83ddbd97783f5794307aa2fee945f6986b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8D=E5=80=BE=E6=A2=A6=E5=8D=8EV?= <185041488+CQMHV@users.noreply.github.com> Date: Tue, 6 Oct 2026 22:46:21 +0800 Subject: [PATCH] Fix preference usage scanning on CRLF checkouts (#331) (#331) --- crates/engine/tests/prefs_usage.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/engine/tests/prefs_usage.rs b/crates/engine/tests/prefs_usage.rs index f7dd35b281..7b9adebdd1 100644 --- a/crates/engine/tests/prefs_usage.rs +++ b/crates/engine/tests/prefs_usage.rs @@ -46,7 +46,12 @@ fn has_ident(text: &str, prefix: &str, ident: &str) -> bool { /// Production source of a file: everything before its unit-test module. fn production(text: &str) -> &str { - let cut = text.find("#[cfg(test)]\nmod ").or_else(|| text.find("#[cfg(test)]\npub mod ")).unwrap_or(text.len()); + // Windows checkouts may use CRLF; test-only reads must stay excluded. + let cut = ["#[cfg(test)]\nmod ", "#[cfg(test)]\r\nmod ", "#[cfg(test)]\npub mod ", "#[cfg(test)]\r\npub mod "] + .into_iter() + .filter_map(|marker| text.find(marker)) + .min() + .unwrap_or(text.len()); &text[..cut] } @@ -167,3 +172,23 @@ fn the_scanner_finds_reads() { assert_eq!(snake("recentFileCount"), "recent_file_count"); assert_eq!(production("fn a() {}\n#[cfg(test)]\nmod tests {\n p.x.y\n}"), "fn a() {}\n"); } + +#[test] +fn production_excludes_test_modules_with_lf_or_crlf() { + for newline in ["\n", "\r\n"] { + for visibility in ["", "pub "] { + let prefix = format!("// Préférences{newline}fn a() {{}}{newline}"); + let source = format!( + "{prefix}#[cfg(test)]{newline}{visibility}mod tests {{{newline} #[test]{newline} fn hidden_setting() {{ let _ = \"type.smartQuotes\"; }}{newline}}}{newline}" + ); + assert_eq!(production(&source), prefix, "newline={newline:?}, visibility={visibility:?}"); + } + } +} + +#[test] +fn production_keeps_sources_without_a_test_module() { + for source in ["", "fn a() {}\n", "// Préférences\r\nfn a() {}\r\n"] { + assert_eq!(production(source), source); + } +}