diff --git a/fuzz/fuzz_targets/fuzz_ed.rs b/fuzz/fuzz_targets/fuzz_ed.rs index fefb9c13..dbb7701b 100644 --- a/fuzz/fuzz_targets/fuzz_ed.rs +++ b/fuzz/fuzz_targets/fuzz_ed.rs @@ -2,16 +2,15 @@ #[macro_use] extern crate libfuzzer_sys; use diffutilslib::ed_diff; -use diffutilslib::ed_diff::DiffError; use diffutilslib::params::Params; use std::fs::{self, File}; use std::io::Write; use std::process::Command; -fn diff_w(expected: &[u8], actual: &[u8], filename: &str) -> Result, DiffError> { - let mut output = ed_diff::diff(expected, actual, &Params::default())?; +fn diff_w(expected: &[u8], actual: &[u8], filename: &str) -> Vec { + let mut output = ed_diff::diff(expected, actual, &Params::default()); writeln!(&mut output, "w {filename}").unwrap(); - Ok(output) + output } fuzz_target!(|x: (Vec, Vec)| { @@ -39,7 +38,7 @@ fuzz_target!(|x: (Vec, Vec)| { return; } fs::create_dir_all("target").unwrap(); - let diff = diff_w(&from, &to, "target/fuzz.file").unwrap(); + let diff = diff_w(&from, &to, "target/fuzz.file"); File::create("target/fuzz.file.original") .unwrap() .write_all(&from) diff --git a/src/diff.rs b/src/diff.rs index 55fd30e5..7931d4c1 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -159,13 +159,7 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { Format::Normal => normal_diff::diff(&from_content, &to_content, params), Format::Unified => unified_diff::diff(&from_content, &to_content, params), Format::Context => context_diff::diff(&from_content, &to_content, params), - Format::Ed => match ed_diff::diff(&from_content, &to_content, params) { - Ok(result) => result, - Err(error) => { - eprintln!("{error}"); - return Outcome::Trouble; - } - }, + Format::Ed => ed_diff::diff(&from_content, &to_content, params), // Side by side output is streamed, unless a header may have to come // first. Format::SideBySide => match header { @@ -210,6 +204,25 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { return Outcome::Trouble; } } + // An ed script cannot say that a file's last line lacks its newline, so, + // like GNU diff, write the script anyway, then warn about each such file + // and report trouble. + if params.format == Format::Ed { + let mut missing_newline = false; + for (name, content) in [(¶ms.from, &from_content), (¶ms.to, &to_content)] { + if content.last().is_some_and(|&byte| byte != b'\n') { + eprintln!( + "{}: {}: No newline at end of file\n", + params.executable.to_string_lossy(), + name.to_string_lossy() + ); + missing_newline = true; + } + } + if missing_newline { + return Outcome::Trouble; + } + } if result.is_empty() { maybe_report_identical_files(); Outcome::Identical diff --git a/src/ed_diff.rs b/src/ed_diff.rs index 6ec73568..716fe2f9 100644 --- a/src/ed_diff.rs +++ b/src/ed_diff.rs @@ -16,23 +16,6 @@ struct Mismatch { pub actual: Vec>, } -#[derive(Debug, PartialEq, Eq)] -pub enum DiffError { - MissingNL, -} - -impl std::fmt::Display for DiffError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - std::fmt::Display::fmt("No newline at end of file", f) - } -} - -impl From for String { - fn from(_: DiffError) -> String { - "No newline at end of file".into() - } -} - impl Mismatch { fn new(line_number_expected: usize, line_number_actual: usize) -> Mismatch { Mismatch { @@ -45,7 +28,7 @@ impl Mismatch { } // Produces a diff between the expected output and actual output. -fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result, DiffError> { +fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Vec { let mut line_number_expected = 1; let mut line_number_actual = 1; let mut results = Vec::new(); @@ -59,16 +42,14 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result Result Result Result, DiffError> { +pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { let mut output = Vec::new(); - let diff_results = make_diff(expected, actual, params.brief)?; + let diff_results = make_diff(expected, actual, params.brief); if params.brief && !diff_results.is_empty() { write!(&mut output, "\0").unwrap(); - return Ok(output); + return output; } // Like GNU diff, write the hunks last to first. Each command then refers // to the original line numbers, since editing later lines first leaves @@ -168,7 +149,7 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Result, } } } - Ok(output) + output } #[cfg(test)] @@ -178,24 +159,24 @@ mod tests { use crate::utils::testcmds::ED_CMD; - pub fn diff_w(expected: &[u8], actual: &[u8], filename: &str) -> Result, DiffError> { - let mut output = diff(expected, actual, &Params::default())?; + pub fn diff_w(expected: &[u8], actual: &[u8], filename: &str) -> Vec { + let mut output = diff(expected, actual, &Params::default()); writeln!(&mut output, "w {filename}").unwrap(); - Ok(output) + output } #[test] fn test_basic() { let from = b"a\n"; let to = b"b\n"; - let diff = diff(from, to, &Params::default()).unwrap(); + let diff = diff(from, to, &Params::default()); let expected = ["1c", "b", ".", ""].join("\n"); assert_eq!(diff, expected.as_bytes()); } #[test] fn test_single_line_deleted() { - let diff = diff(b"a\nb\n", b"a\n", &Params::default()).unwrap(); + let diff = diff(b"a\nb\n", b"a\n", &Params::default()); assert_eq!(diff, b"2d\n"); } @@ -246,7 +227,7 @@ mod tests { } // This test diff is intentionally reversed. // We want it to turn the alef into bet. - let diff = diff_w(&alef, &bet, &format!("{target}/alef")).unwrap(); + let diff = diff_w(&alef, &bet, &format!("{target}/alef")); File::create(format!("{target}/ab.ed")) .unwrap() .write_all(&diff) @@ -320,7 +301,7 @@ mod tests { } // This test diff is intentionally reversed. // We want it to turn the alef into bet. - let diff = diff_w(&alef, &bet, &format!("{target}/alef_")).unwrap(); + let diff = diff_w(&alef, &bet, &format!("{target}/alef_")); File::create(format!("{target}/ab_.ed")) .unwrap() .write_all(&diff) @@ -400,7 +381,7 @@ mod tests { } // This test diff is intentionally reversed. // We want it to turn the alef into bet. - let diff = diff_w(&alef, &bet, &format!("{target}/alefr")).unwrap(); + let diff = diff_w(&alef, &bet, &format!("{target}/alefr")); File::create(format!("{target}/abr.ed")) .unwrap() .write_all(&diff) @@ -438,7 +419,7 @@ mod tests { let from = ["a", "b", "c", ""].join("\n"); let to = ["a", "d", "c", ""].join("\n"); - let diff_full = diff(from.as_bytes(), to.as_bytes(), &Params::default()).unwrap(); + let diff_full = diff(from.as_bytes(), to.as_bytes(), &Params::default()); let expected_full = ["2c", "d", ".", ""].join("\n"); assert_eq!(diff_full, expected_full.as_bytes()); @@ -449,12 +430,11 @@ mod tests { brief: true, ..Default::default() }, - ) - .unwrap(); + ); let expected_brief = "\0".as_bytes(); assert_eq!(diff_brief, expected_brief); - let nodiff_full = diff(from.as_bytes(), from.as_bytes(), &Params::default()).unwrap(); + let nodiff_full = diff(from.as_bytes(), from.as_bytes(), &Params::default()); assert!(nodiff_full.is_empty()); let nodiff_brief = diff( @@ -464,8 +444,7 @@ mod tests { brief: true, ..Default::default() }, - ) - .unwrap(); + ); assert!(nodiff_brief.is_empty()); } } diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 6173c199..6f011cce 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -60,15 +60,46 @@ fn differences_brief(#[values(&[][..], &["-u"], &["-c"], &["-e"])] format: &[&st .stdout_is("Files file1 and file2 differ\n"); } -#[test] -fn missing_newline() { +// An ed script cannot say that a newline is missing, so `-e` writes the +// script anyway, treating the last line as if it had its newline, then warns +// about each file that lacks one (#332). +#[rstest] +#[case::both("foo", "bar", "1c\nbar\n.\n", "file1 file2")] +#[case::from("a\nb", "a\nc\n", "2c\nc\n.\n", "file1")] +#[case::to("a\nb\n", "a\nc", "2c\nc\n.\n", "file2")] +#[case::only_the_newline("a\n", "a", "", "file2")] +#[case::same_content("a\nb", "a\nb", "", "file1 file2")] +#[case::from_empty("", "a\nb", "0a\na\nb\n.\n", "file2")] +fn missing_newline_ed( + #[case] from: &str, + #[case] to: &str, + #[case] script: &str, + #[case] warned: &str, +) { let (at, ts) = at_and_ts!(); - at.write("file1", "foo"); - at.write("file2", "bar"); + at.write("file1", from); + at.write("file2", to); + let warnings: String = warned + .split(' ') + .map(|name| format!("diff: {name}: No newline at end of file\n\n")) + .collect(); ts.ucmd() .args(&["-e", "file1", "file2"]) .fails_with_code(2) - .stderr_str_check(|s| s.starts_with("No newline at end of file")); + .stdout_is(script) + .stderr_is(warnings); +} + +#[test] +fn missing_newline_ed_brief() { + let (at, ts) = at_and_ts!(); + at.write("file1", "a\nb"); + at.write("file2", "a\nc\n"); + ts.ucmd() + .args(&["-e", "-q", "file1", "file2"]) + .fails_with_code(2) + .stdout_is("Files file1 and file2 differ\n") + .stderr_is("diff: file1: No newline at end of file\n\n"); } #[test]