Skip to content

Commit 10541ef

Browse files
committed
Create a more compact diff format
1 parent 44e2fe7 commit 10541ef

File tree

7 files changed

+130
-154
lines changed

7 files changed

+130
-154
lines changed

Cargo.lock

Lines changed: 3 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ rustc_version = "0.4"
1515
colored = "2"
1616
# Features chosen to match those required by env_logger, to avoid rebuilds
1717
regex = { version = "1.5.5", default-features = false, features = ["perf", "std"] }
18-
pretty_assertions = "1.2.1"
18+
diff = "0.1.13"
1919
crossbeam = "0.8.1"
2020
lazy_static = "1.4.0"
2121
serde = { version = "1.0", features = ["derive"] }

src/diff.rs

Lines changed: 111 additions & 0 deletions
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+
}

src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::dependencies::build_dependencies;
2525
use crate::parser::{Comments, Condition};
2626

2727
mod dependencies;
28+
mod diff;
2829
mod parser;
2930
mod rustc_stderr;
3031
#[cfg(test)]
@@ -322,11 +323,7 @@ pub fn run_tests_generic(config: Config, file_filter: impl Fn(&Path) -> bool + S
322323
expected,
323324
} => {
324325
eprintln!("actual output differed from expected {}", path.display());
325-
eprintln!(
326-
"{}",
327-
pretty_assertions::StrComparison::new(expected, actual)
328-
);
329-
eprintln!()
326+
diff::print_diff(expected, actual);
330327
}
331328
Error::ErrorsWithoutPattern { path: None, msgs } => {
332329
eprintln!(

tests/integrations/basic-fail/Cargo.lock

Lines changed: 1 addition & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integrations/basic-fail/Cargo.stderr

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,17 @@ tests/actual_tests/foomp.rs FAILED:
4040
command: "rustc" "--error-format=json" "--edition=2021" "--extern" "basic_fail=$DIR/$DIR/../../../target/debug/libbasic_fail-$HASH.rmeta" "-L" "$DIR/$DIR/../../../target/debug" "-L" "$DIR/$DIR/../../../target/debug" "tests/actual_tests/foomp.rs"
4141

4242
actual output differed from expected tests/actual_tests/foomp.stderr
43-
Diff < left / right > :
44-
error[E0308]: mismatched types
45-
--> $DIR/foomp.rs:4:9
46-
|
47-
4 | add("42", 3);
48-
| --- ^^^^ expected `usize`, found `&str`
49-
> | |
50-
> | arguments to this function are incorrect
51-
|
52-
note: function defined here
53-
< --> $DIR/tests/integrations/basic/src/lib.rs:1:8
54-
> --> $DIR/tests/integrations/basic-fail/src/lib.rs:1:8
55-
|
56-
1 | pub fn add(left: usize, right: usize) -> usize {
57-
< | ^^^ some expected text that isn't in the actual message
58-
> | ^^^
59-
60-
<error: aborting doo to previous error
61-
>error: aborting due to previous error
62-
63-
For more information about this error, try `rustc --explain E0308`.
64-
65-
43+
... 5 lines skipped
44+
+ | |
45+
+ | arguments to this function are incorrect
46+
... 2 lines skipped
47+
~ --> $DIR/$DIR/src/lib.rs:1:8
48+
... 2 lines skipped
49+
~ | ^^^ some expected text that isn't in the actual message
50+
... 1 lines skipped
51+
-error: aborting doo to previous error
52+
+error: aborting due to previous error
53+
... 3 lines skipped ...
6654

6755

6856
full stderr:

0 commit comments

Comments
 (0)