Skip to content

Commit 5f0fea0

Browse files
AndreasBackxmeta-codesync[bot]
authored andcommitted
escape lines before calculating max line length
Summary: We were calculating the max line length of all the lines. Then we were looping over the lines and altering it, making them longer as we were non-printable characters. Then we were doing `max - line.length()` which would underflow because `line.length()` would be longer on the longest line. This changes it so we're escaping all lines first. Reviewed By: richardjrossiii Differential Revision: D88066881 fbshipit-source-id: 33dac68a37caced18b4a302fc27072d2c2116811
1 parent 3c28919 commit 5f0fea0

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/renderers/pretty.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -244,26 +244,36 @@ impl ErrorRenderer for PrettyColorRenderer {
244244
lines.to_vec()
245245
};
246246

247-
let max_line_length =
248-
lines.iter().map(|(_, line)| line.len()).max().unwrap_or(0);
249-
for (output_index, (line_index, line)) in lines.into_iter().enumerate()
247+
let escaped_lines = lines.into_iter().map(|(line_index, line)| {
248+
(
249+
line_index,
250+
outcome
251+
.escaping
252+
.escaped_printable((&line as &[u8]).trim_newlines()),
253+
)
254+
});
255+
let max_line_length = escaped_lines
256+
.clone()
257+
.map(|(_, line)| line.len())
258+
.max()
259+
.unwrap_or(0);
260+
for (output_index, (line_index, line)) in
261+
escaped_lines.into_iter().enumerate()
250262
{
251-
let mut line = outcome
252-
.escaping
253-
.escaped_printable((&line as &[u8]).trim_newlines())
254-
.to_string();
255-
256-
// append expectation as a "comment" infirst line
257-
if output_index == 0 {
258-
line += &format!(
259-
"{}{} {}",
260-
" ".repeat(max_line_length - line.len() + 1),
263+
let output_line = if output_index == 0 {
264+
// append expectation as a "comment" infirst line
265+
format!(
266+
"{line}{}{} {}",
267+
" ".repeat(max_line_length - line.len() + 2),
261268
style("//").magenta(),
262269
style(expectation.to_expression_string(&outcome.escaping))
263270
.magenta()
264271
.bold()
265-
);
266-
}
272+
)
273+
} else {
274+
line
275+
};
276+
267277
output.push_str(
268278
&decorator
269279
.line(
@@ -275,7 +285,7 @@ impl ErrorRenderer for PrettyColorRenderer {
275285
},
276286
expectation.multiline,
277287
" ",
278-
&line,
288+
&output_line,
279289
)
280290
.assure_newline(),
281291
);

0 commit comments

Comments
 (0)