Skip to content

Commit eaf190a

Browse files
author
Elrond
committed
test(buzz-db): track brace depth when skipping cfg(test) items
A multi-line #[cfg(test)] statement containing a block, such as [const { ... }; N], made the skipper wait for the next column-0 brace and hide the production function after it (runtime/observability.rs publish_waiters). Track depth instead and pin the const-block case. Signed-off-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
1 parent a267be8 commit eaf190a

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

‎crates/buzz-db/tests/observability_source.rs‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,8 +1329,9 @@ fn unadmitted_event_write_transaction_openers(sources: &[(String, String)]) -> V
13291329

13301330
/// Remove each top-level `#[cfg(test)]` item and keep the production code
13311331
/// around it. Truncating at the first `#[cfg(test)]` would hide production
1332-
/// functions that follow a test-only item. Relies on rustfmt layout: a
1333-
/// top-level item closes on a column-0 `}` line, or ends on its first line.
1332+
/// functions that follow a test-only item. Tracks brace depth: the item ends
1333+
/// on the first line that leaves depth at or below zero and ends with `;` or
1334+
/// `}`. A column-0 `}` line (rustfmt's top-level close) always ends it.
13341335
fn strip_cfg_test_items(source: &str) -> String {
13351336
let mut kept = String::with_capacity(source.len());
13361337
let mut lines = source.lines();
@@ -1340,17 +1341,12 @@ fn strip_cfg_test_items(source: &str) -> String {
13401341
kept.push('\n');
13411342
continue;
13421343
}
1343-
let mut opened = false;
1344-
for (index, line) in lines
1345-
.by_ref()
1346-
.skip_while(|line| line.starts_with("#["))
1347-
.enumerate()
1348-
{
1349-
let opens = line.matches('{').count();
1350-
opened |= opens > 0;
1351-
let single_line_block = index == 0 && opens > 0 && opens == line.matches('}').count();
1352-
let statement_end = !opened && line.trim_end().ends_with(';');
1353-
if single_line_block || statement_end || line == "}" || line == "};" {
1344+
let mut depth = 0_isize;
1345+
for line in lines.by_ref().skip_while(|line| line.starts_with("#[")) {
1346+
depth += line.matches('{').count() as isize - line.matches('}').count() as isize;
1347+
let trimmed = line.trim_end();
1348+
let item_end = depth <= 0 && (trimmed.ends_with(';') || trimmed.ends_with('}'));
1349+
if item_end || line == "}" || line == "};" {
13541350
break;
13551351
}
13561352
}
@@ -1369,6 +1365,10 @@ static LOCK: std::sync::Mutex<()> =\n\
13691365
std::sync::Mutex::new(());\n\
13701366
pub fn after_static() {}\n\
13711367
#[cfg(test)]\n\
1368+
static S: [u8; 1] =\n\
1369+
[const { 0 }; 1];\n\
1370+
pub fn after_const_block() {}\n\
1371+
#[cfg(test)]\n\
13721372
#[derive(Debug)]\n\
13731373
struct Fields {\n\
13741374
value: u8,\n\
@@ -1385,6 +1385,7 @@ pub fn after_module() {}\n";
13851385
"before",
13861386
"after_struct",
13871387
"after_static",
1388+
"after_const_block",
13881389
"after_fields",
13891390
"after_module",
13901391
] {
@@ -1393,7 +1394,7 @@ pub fn after_module() {}\n";
13931394
"{name} is production code and must stay visible: {production}"
13941395
);
13951396
}
1396-
for hidden in ["Marker", "LOCK", "value: u8", "fn hidden"] {
1397+
for hidden in ["Marker", "LOCK", "static S", "value: u8", "fn hidden"] {
13971398
assert!(
13981399
!production.contains(hidden),
13991400
"{hidden} is test-only and must be skipped: {production}"

0 commit comments

Comments
 (0)