diff --git a/Cargo.toml b/Cargo.toml index ea8a1e1..20cffa6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ path = "src/main.rs" [dependencies] chrono = "0.4.35" diff = "0.1.10" -regex = "1.10.4" +regex = "1.10.3" same-file = "1.0.6" unicode-width = "0.1.11" diff --git a/src/context_diff.rs b/src/context_diff.rs index 80c602d..93c38a5 100644 --- a/src/context_diff.rs +++ b/src/context_diff.rs @@ -726,9 +726,8 @@ mod tests { #[test] fn test_stop_early() { - use regex::Regex; + use crate::assert_diff_eq; use std::fs::File; - use std::str; let target = "target/context-diff"; let _ = std::fs::create_dir(target); @@ -749,10 +748,6 @@ mod tests { }, ); - let diff_full_text = str::from_utf8(&diff_full).unwrap(); - let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [+-]\d{4}").unwrap(); - let diff_full = re.replace_all(diff_full_text, ""); - let expected_full = [ "*** target/context-diff/foo\t", "--- target/context-diff/bar\t", @@ -768,7 +763,7 @@ mod tests { "", ] .join("\n"); - assert_eq!(diff_full, expected_full); + assert_diff_eq!(diff_full, expected_full); let diff_brief = diff( from.as_bytes(), @@ -781,16 +776,13 @@ mod tests { }, ); - let diff_brief_text = str::from_utf8(&diff_brief).unwrap(); - let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [+-]\d{4}").unwrap(); - let diff_brief = re.replace_all(diff_brief_text, ""); let expected_brief = [ "*** target/context-diff/foo\t", "--- target/context-diff/bar\t", "", ] .join("\n"); - assert_eq!(diff_brief, expected_brief); + assert_diff_eq!(diff_brief, expected_brief); let nodiff_full = diff( from.as_bytes(), diff --git a/src/lib.rs b/src/lib.rs index 7ed36a6..0bb911b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ pub mod context_diff; pub mod ed_diff; +pub mod macros; pub mod normal_diff; pub mod params; pub mod unified_diff; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..401bff4 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,13 @@ +#[macro_export] +macro_rules! assert_diff_eq { + ($actual:expr, $expected:expr) => {{ + use regex::Regex; + use std::str; + + let diff = str::from_utf8(&$actual).unwrap(); + let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [+-]\d{4}").unwrap(); + let actual = re.replace_all(diff, ""); + + assert_eq!(actual, $expected); + }}; +} diff --git a/src/main.rs b/src/main.rs index a19e646..624bc3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use std::process::{exit, ExitCode}; mod context_diff; mod ed_diff; +mod macros; mod normal_diff; mod params; mod unified_diff; diff --git a/src/unified_diff.rs b/src/unified_diff.rs index 62dd198..57925e1 100644 --- a/src/unified_diff.rs +++ b/src/unified_diff.rs @@ -880,9 +880,8 @@ mod tests { #[test] fn test_stop_early() { - use regex::Regex; + use crate::assert_diff_eq; use std::fs::File; - use std::str; let target = "target/context-diff"; let _ = std::fs::create_dir(target); @@ -903,10 +902,6 @@ mod tests { }, ); - let diff_full_text = str::from_utf8(&diff_full).unwrap(); - let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [+-]\d{4}").unwrap(); - let diff_full = re.replace_all(diff_full_text, ""); - let expected_full = [ "--- target/context-diff/foo\t", "+++ target/context-diff/bar\t", @@ -918,7 +913,7 @@ mod tests { "", ] .join("\n"); - assert_eq!(diff_full, expected_full); + assert_diff_eq!(diff_full, expected_full); let diff_brief = diff( from.as_bytes(), @@ -931,16 +926,13 @@ mod tests { }, ); - let diff_brief_text = str::from_utf8(&diff_brief).unwrap(); - let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [+-]\d{4}").unwrap(); - let diff_brief = re.replace_all(diff_brief_text, ""); let expected_brief = [ "--- target/context-diff/foo\t", "+++ target/context-diff/bar\t", "", ] .join("\n"); - assert_eq!(diff_brief, expected_brief); + assert_diff_eq!(diff_brief, expected_brief); let nodiff_full = diff( from.as_bytes(), diff --git a/tests/integration.rs b/tests/integration.rs index 5ad624e..4291ef4 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -4,6 +4,7 @@ // files that was distributed with this source code. use assert_cmd::cmd::Command; +use diffutilslib::assert_diff_eq; use predicates::prelude::*; use std::io::Write; use tempfile::NamedTempFile; @@ -173,26 +174,32 @@ fn read_from_stdin() -> Result<(), Box> { .arg(file1.path()) .arg("-") .write_stdin("bar\n"); - cmd.assert() - .code(predicate::eq(1)) - .failure() - .stdout(predicate::eq(format!( + cmd.assert().code(predicate::eq(1)).failure(); + + let output = cmd.output().unwrap().stdout; + assert_diff_eq!( + output, + format!( "--- {}\t\n+++ /dev/stdin\t\n@@ -1 +1 @@\n-foo\n+bar\n", file1.path().to_string_lossy() - ))); + ) + ); let mut cmd = Command::cargo_bin("diffutils")?; cmd.arg("-u") .arg("-") .arg(file2.path()) .write_stdin("foo\n"); - cmd.assert() - .code(predicate::eq(1)) - .failure() - .stdout(predicate::eq(format!( + cmd.assert().code(predicate::eq(1)).failure(); + + let output = cmd.output().unwrap().stdout; + assert_diff_eq!( + output, + format!( "--- /dev/stdin\t\n+++ {}\t\n@@ -1 +1 @@\n-foo\n+bar\n", file2.path().to_string_lossy() - ))); + ) + ); let mut cmd = Command::cargo_bin("diffutils")?; cmd.arg("-u").arg("-").arg("-").write_stdin("foo\n");