Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9b71180

Browse files
committed
Create a proper normalization regex parser
1 parent bc07c19 commit 9b71180

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

ui_test/src/comments.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl Comments {
197197
':' | ' ' => args.as_str(),
198198
_ => bail!("expected space or `:`, got `{next}`"),
199199
};
200-
(command, args)
200+
(command, args.trim())
201201
}
202202
};
203203

@@ -217,12 +217,37 @@ impl Comments {
217217
self.env_vars.push((k.to_string(), v.to_string()));
218218
},
219219
"normalize-stderr-test" => {
220-
let (from, to) = args
221-
.split_once("->")
222-
.ok_or_else(|| eyre!("normalize-stderr-test needs a `->`"))?;
223-
let from = from.trim().trim_matches('"');
224-
let to = to.trim().trim_matches('"');
225-
let from = Regex::new(from).ok().ok_or_else(|| eyre!("invalid regex"))?;
220+
fn parse_str(s: &str) -> Result<(&str, &str)> {
221+
let mut chars = s.char_indices();
222+
match chars.next().ok_or_else(|| eyre!("missing arguments"))?.1 {
223+
'"' => {
224+
let s = chars.as_str();
225+
let mut escaped = false;
226+
for (i, c) in chars {
227+
if escaped {
228+
escaped = false;
229+
} else if c == '"' {
230+
return Ok((&s[..(i - 1)], s[i..].trim_start()))
231+
} else {
232+
escaped = c == '\\';
233+
}
234+
}
235+
bail!("no closing quotes found for {s}")
236+
}
237+
c => bail!("expected '\"', got {c}"),
238+
}
239+
}
240+
241+
let (from, rest) = parse_str(args)?;
242+
243+
let to = rest.strip_prefix("->").ok_or_else(|| {
244+
eyre!("normalize-stderr-test needs a pattern and replacement separated by `->`")
245+
})?.trim_start();
246+
let (to, rest) = parse_str(to)?;
247+
248+
ensure!(rest.is_empty(), "trailing text after pattern replacement: {rest}");
249+
250+
let from = Regex::new(from)?;
226251
self.normalize_stderr.push((from, to.to_string()));
227252
}
228253
"error-pattern" => {

0 commit comments

Comments
 (0)