Skip to content

Commit ed8e1fa

Browse files
author
Orion Gonzalez
committed
implemented custom differ
1 parent 5610223 commit ed8e1fa

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

src/tools/compiletest/src/runtest.rs

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2480,7 +2480,7 @@ impl<'test> TestCx<'test> {
24802480
}
24812481
}
24822482

2483-
fn compare_output(&self, kind: &str, actual: &str, expected: &str) -> usize {
2483+
fn compare_output(&self, stream: &str, actual: &str, expected: &str) -> usize {
24842484
let are_different = match (self.force_color_svg(), expected.find('\n'), actual.find('\n')) {
24852485
// FIXME: We ignore the first line of SVG files
24862486
// because the width parameter is non-deterministic.
@@ -2520,56 +2520,65 @@ impl<'test> TestCx<'test> {
25202520
(expected, actual)
25212521
};
25222522

2523+
// Write the actual output to a file in build/
2524+
let test_name = self.config.compare_mode.as_ref().map_or("", |m| m.to_str());
2525+
let actual_path = self
2526+
.output_base_name()
2527+
.with_extra_extension(self.revision.unwrap_or(""))
2528+
.with_extra_extension(test_name)
2529+
.with_extra_extension(stream);
2530+
2531+
if let Err(err) = fs::write(&actual_path, &actual) {
2532+
self.fatal(&format!("failed to write {stream} to `{actual_path:?}`: {err}",));
2533+
}
2534+
println!("Saved the actual {stream} to {actual_path:?}");
2535+
2536+
let expected_path =
2537+
expected_output_path(self.testpaths, self.revision, &self.config.compare_mode, stream);
2538+
25232539
if !self.config.bless {
25242540
if expected.is_empty() {
2525-
println!("normalized {}:\n{}\n", kind, actual);
2541+
println!("normalized {}:\n{}\n", stream, actual);
25262542
} else {
2527-
println!("diff of {}:\n", kind);
2528-
print!("{}", write_diff(expected, actual, 3));
2543+
println!("diff of {stream}:\n");
2544+
if let Some(diff_command) = self.config.diff_command.as_deref() {
2545+
let mut args = diff_command.split_whitespace();
2546+
let name = args.next().unwrap();
2547+
match Command::new(name)
2548+
.args(args)
2549+
.args([&expected_path, &actual_path])
2550+
.output()
2551+
{
2552+
Err(err) => {
2553+
self.fatal(&format!(
2554+
"failed to call custom diff command `{diff_command}`: {err}"
2555+
));
2556+
}
2557+
Ok(output) => {
2558+
io::stdout().lock().write_all(&output.stdout).unwrap();
2559+
}
2560+
}
2561+
} else {
2562+
print!("{}", write_diff(expected, actual, 3));
2563+
}
25292564
}
2530-
}
2531-
2532-
let mode = self.config.compare_mode.as_ref().map_or("", |m| m.to_str());
2533-
let output_file = self
2534-
.output_base_name()
2535-
.with_extra_extension(self.revision.unwrap_or(""))
2536-
.with_extra_extension(mode)
2537-
.with_extra_extension(kind);
2538-
2539-
let mut files = vec![output_file];
2540-
if self.config.bless {
2565+
} else {
25412566
// Delete non-revision .stderr/.stdout file if revisions are used.
25422567
// Without this, we'd just generate the new files and leave the old files around.
25432568
if self.revision.is_some() {
25442569
let old =
2545-
expected_output_path(self.testpaths, None, &self.config.compare_mode, kind);
2570+
expected_output_path(self.testpaths, None, &self.config.compare_mode, stream);
25462571
self.delete_file(&old);
25472572
}
2548-
files.push(expected_output_path(
2549-
self.testpaths,
2550-
self.revision,
2551-
&self.config.compare_mode,
2552-
kind,
2553-
));
2554-
}
25552573

2556-
for output_file in &files {
2557-
if actual.is_empty() {
2558-
self.delete_file(output_file);
2559-
} else if let Err(err) = fs::write(&output_file, &actual) {
2560-
self.fatal(&format!(
2561-
"failed to write {} to `{}`: {}",
2562-
kind,
2563-
output_file.display(),
2564-
err,
2565-
));
2574+
if let Err(err) = fs::write(&expected_path, &actual) {
2575+
self.fatal(&format!("failed to write {stream} to `{expected_path:?}`: {err}"));
25662576
}
2577+
println!("Blessing the {stream} of {test_name} in {expected_path:?}");
25672578
}
25682579

2569-
println!("\nThe actual {0} differed from the expected {0}.", kind);
2570-
for output_file in files {
2571-
println!("Actual {} saved to {}", kind, output_file.display());
2572-
}
2580+
println!("\nThe actual {0} differed from the expected {0}.", stream);
2581+
25732582
if self.config.bless { 0 } else { 1 }
25742583
}
25752584

0 commit comments

Comments
 (0)