Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty print unified diff with 5 line before/after window #10

Merged
merged 2 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ keywords = ["test", "fixture"]
categories = ["development-tools::testing"]

[dependencies]
difference = "2.0.0"
similar = "2.1.0"
newline-converter = "0.2.0"
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
//! -Ten points for Gaston
//! ```

use difference::Changeset;
use newline_converter::dos2unix;
use similar::{udiff::unified_diff, Algorithm};
use std::{env, fs, path::Path};

/// Compare the contents of the file to the string provided
Expand Down Expand Up @@ -69,9 +69,14 @@ pub fn assert_contents<P: AsRef<Path>>(path: P, actual: &str) {
let expected = dos2unix(&expected_s);

if expected != actual {
let changeset =
Changeset::new(expected.as_ref(), actual.as_ref(), "\n");
println!("{}", changeset);
let diff = unified_diff(
Algorithm::Myers, // default algorithm used by git
&expected,
&actual,
5, // lines before and after
None,
);
println!("{}", diff);
panic!(
r#"assertion failed: string doesn't match the contents of file: "{}" see diffset above
set EXPECTORATE=overwrite if these changes are intentional"#,
Expand Down
20 changes: 20 additions & 0 deletions tests/data_a.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,23 @@ A eh?
a a A
a aye
eh.

A eh?
a a A
a aye
eh.

A eh?
a a A
a aye
eh.

A eh?
a a A
a aye
eh.

A eh?
a a A
a aye
eh.
24 changes: 24 additions & 0 deletions tests/data_a2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
A eh?
a a A
a aye
eh.

A eh?
a a A
a aye
eh.

A eh?
a a A
a aye
eh.

A eh?
this line changed
a aye
eh.

A eh?
a a A
a aye
eh.
7 changes: 7 additions & 0 deletions tests/test_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ fn bad() {
let actual = include_str!("data_a.txt");
assert_contents("tests/data_b.txt", actual);
}

#[test]
#[should_panic]
fn one_line_change() {
let actual = include_str!("data_a.txt");
assert_contents("tests/data_a2.txt", actual);
}