Skip to content

Commit 1530563

Browse files
committed
compiletest: print diff for pretty tests
This commit modifies compiletest so that a diff of actual and expected output is shown for pretty tests. This makes it far easier to work out what has changed. Signed-off-by: David Wood <[email protected]>
1 parent c773226 commit 1530563

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/tools/compiletest/src/runtest.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -178,27 +178,30 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Misma
178178
results
179179
}
180180

181-
fn print_diff(expected: &str, actual: &str, context_size: usize) {
181+
fn write_diff(expected: &str, actual: &str, context_size: usize) -> String {
182+
use std::fmt::Write;
183+
let mut output = String::new();
182184
let diff_results = make_diff(expected, actual, context_size);
183185
for result in diff_results {
184186
let mut line_number = result.line_number;
185187
for line in result.lines {
186188
match line {
187189
DiffLine::Expected(e) => {
188-
println!("-\t{}", e);
190+
writeln!(output, "-\t{}", e).unwrap();
189191
line_number += 1;
190192
}
191193
DiffLine::Context(c) => {
192-
println!("{}\t{}", line_number, c);
194+
writeln!(output, "{}\t{}", line_number, c).unwrap();
193195
line_number += 1;
194196
}
195197
DiffLine::Resulting(r) => {
196-
println!("+\t{}", r);
198+
writeln!(output, "+\t{}", r).unwrap();
197199
}
198200
}
199201
}
200-
println!();
202+
writeln!(output, "").unwrap();
201203
}
204+
output
202205
}
203206

204207
pub fn run(config: Config, testpaths: &TestPaths, revision: Option<&str>) {
@@ -655,8 +658,12 @@ impl<'test> TestCx<'test> {
655658
------------------------------------------\n\
656659
{}\n\
657660
------------------------------------------\n\
658-
\n",
659-
expected, actual
661+
diff:\n\
662+
------------------------------------------\n\
663+
{}\n",
664+
expected,
665+
actual,
666+
write_diff(expected, actual, 3),
660667
));
661668
}
662669
}
@@ -3227,7 +3234,7 @@ impl<'test> TestCx<'test> {
32273234
}
32283235
let expected_string = fs::read_to_string(&expected_file).unwrap();
32293236
if dumped_string != expected_string {
3230-
print_diff(&expected_string, &dumped_string, 3);
3237+
print!("{}", write_diff(&expected_string, &dumped_string, 3));
32313238
panic!(
32323239
"Actual MIR output differs from expected MIR output {}",
32333240
expected_file.display()
@@ -3452,7 +3459,7 @@ impl<'test> TestCx<'test> {
34523459
println!("normalized {}:\n{}\n", kind, actual);
34533460
} else {
34543461
println!("diff of {}:\n", kind);
3455-
print_diff(expected, actual, 3);
3462+
print!("{}", write_diff(expected, actual, 3));
34563463
}
34573464
}
34583465

0 commit comments

Comments
 (0)