Skip to content

Commit db72336

Browse files
committed
Create a more compact diff format
1 parent 90fbdad commit db72336

File tree

5 files changed

+120
-89
lines changed

5 files changed

+120
-89
lines changed

Cargo.lock

+3-43
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui_test/Cargo.lock

+3-43
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui_test/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ rustc_version = "0.4"
1212
colored = "2"
1313
# Features chosen to match those required by env_logger, to avoid rebuilds
1414
regex = { version = "1.5.5", default-features = false, features = ["perf", "std"] }
15-
pretty_assertions = "1.2.1"
15+
diff = "0.1.13"
1616
crossbeam = "0.8.1"
1717
lazy_static = "1.4.0"
1818
serde = { version = "1.0", features = ["derive"] }

ui_test/src/diff.rs

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
use colored::*;
2+
use diff::{chars, lines, Result, Result::*};
3+
4+
#[derive(Default)]
5+
struct DiffState<'a> {
6+
skipped_lines: usize,
7+
/// When we see a removed line, we don't print it, we
8+
/// keep it around to compare it with the next added line.
9+
prev_left: Option<&'a str>,
10+
}
11+
12+
impl<'a> DiffState<'a> {
13+
fn print_skip(&mut self) {
14+
if self.skipped_lines != 0 {
15+
eprintln!("... {} lines skipped", self.skipped_lines);
16+
}
17+
self.skipped_lines = 0;
18+
}
19+
20+
fn print_prev(&mut self) {
21+
if let Some(l) = self.prev_left.take() {
22+
self.print_left(l);
23+
}
24+
}
25+
26+
fn print_left(&self, l: &str) {
27+
eprintln!("{}{}", "-".red(), l.red());
28+
}
29+
30+
fn print_right(&self, r: &str) {
31+
eprintln!("{}{}", "+".green(), r.green());
32+
}
33+
34+
fn row(&mut self, row: Result<&'a str>) {
35+
match row {
36+
Left(l) => {
37+
self.print_skip();
38+
self.print_prev();
39+
self.prev_left = Some(l);
40+
}
41+
Both(..) => {
42+
self.print_prev();
43+
self.skipped_lines += 1
44+
}
45+
Right(r) => {
46+
if let Some(l) = self.prev_left.take() {
47+
// If the lines only add chars or only remove chars, display an inline diff
48+
let diff = chars(l, r);
49+
let mut seen_l = false;
50+
let mut seen_r = false;
51+
for char in &diff {
52+
match char {
53+
Left(l) if !l.is_whitespace() => seen_l = true,
54+
Right(r) if !r.is_whitespace() => seen_r = true,
55+
_ => {}
56+
}
57+
}
58+
if seen_l && seen_r {
59+
// the line both adds and removes chars, print both lines, but highlight their differences
60+
eprint!("{}", "-".red());
61+
for char in &diff {
62+
match char {
63+
Left(l) => eprint!("{}", l.to_string().red()),
64+
Right(_) => {}
65+
Both(l, _) => eprint!("{}", l),
66+
}
67+
}
68+
eprintln!();
69+
eprint!("{}", "+".green());
70+
for char in &diff {
71+
match char {
72+
Left(_) => {}
73+
Right(r) => eprint!("{}", r.to_string().green()),
74+
Both(l, _) => eprint!("{}", l),
75+
}
76+
}
77+
eprintln!();
78+
} else {
79+
eprint!("{}", "~".yellow());
80+
for char in diff {
81+
match char {
82+
Left(l) => eprint!("{}", l.to_string().red()),
83+
Both(l, _) => eprint!("{}", l),
84+
Right(r) => eprint!("{}", r.to_string().green()),
85+
}
86+
}
87+
eprintln!();
88+
}
89+
} else {
90+
self.print_skip();
91+
self.print_right(r);
92+
}
93+
}
94+
}
95+
}
96+
97+
fn finish(self) {
98+
if self.skipped_lines != 0 {
99+
eprintln!("... {} lines skipped ...", self.skipped_lines);
100+
}
101+
eprintln!()
102+
}
103+
}
104+
105+
pub fn print_diff(expected: &str, actual: &str) {
106+
let mut state = DiffState::default();
107+
for row in lines(expected, actual) {
108+
state.row(row);
109+
}
110+
state.finish();
111+
}

ui_test/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_stderr::{Level, Message};
1616

1717
use crate::parser::{Comments, Condition};
1818

19+
mod diff;
1920
mod parser;
2021
mod rustc_stderr;
2122
#[cfg(test)]
@@ -198,8 +199,7 @@ pub fn run_tests(config: Config) -> Result<()> {
198199
dump_stderr = false;
199200
}
200201
eprintln!("actual output differed from expected {}", path.display());
201-
eprintln!("{}", pretty_assertions::StrComparison::new(expected, actual));
202-
eprintln!()
202+
diff::print_diff(expected, actual);
203203
}
204204
Error::ErrorsWithoutPattern { path: None, msgs } => {
205205
eprintln!(

0 commit comments

Comments
 (0)