mirror of
https://github.com/uutils/diffutils.git
synced 2026-06-10 15:48:59 -07:00
Define assert_diff_eq macro for context&unified diff comparison
This commit is contained in:
+1
-1
@@ -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"
|
||||
|
||||
|
||||
+3
-11
@@ -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(),
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}};
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
+3
-11
@@ -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(),
|
||||
|
||||
+17
-10
@@ -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<dyn std::error::Error>> {
|
||||
.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");
|
||||
|
||||
Reference in New Issue
Block a user