From f18db1ddd416a6b6cd956de9ab2b526e2cfd53fd Mon Sep 17 00:00:00 2001 From: Milan Pramod Date: Sun, 15 Nov 2026 19:28:00 +0530 Subject: [PATCH] sdiff: implementation of the `sdiff` command sdiff: add support for left-column and suppress-common-lines options test(side_diff): add unit tests for side diff features sdiff: add sdiff utility implementation - register the new sdiff module in lib.rs and main.rs - update binary dispatch and usage text to support the sdiff command side_diff: render `(` when --left-column is set sdiff: change diff return type from Vec to bool refactor: extract file reading and binary detection utilities - Move `read_file_contents` and `is_binary` functions from `src/diff.rs` to `src/utils.rs` to improve code reuse - Add the `thiserror` dependency to `Cargo.toml` sdiff: add test suite for sdiff utility --- Cargo.lock | 1 + Cargo.toml | 1 + src/diff.rs | 67 +++-- src/lib.rs | 1 + src/main.rs | 4 +- src/params.rs | 6 + src/sdiff.rs | 442 ++++++++++++++++++++++++++++++++ src/side_diff.rs | 136 ++++++++-- src/utils.rs | 21 +- tests/by-util/test_diff.rs | 21 ++ tests/by-util/test_sdiff.rs | 498 ++++++++++++++++++++++++++++++++++++ tests/tests.rs | 3 + 12 files changed, 1146 insertions(+), 55 deletions(-) create mode 100644 src/sdiff.rs create mode 100644 tests/by-util/test_sdiff.rs diff --git a/Cargo.lock b/Cargo.lock index e12563a4..e37d3f09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -309,6 +309,7 @@ dependencies = [ "rstest", "same-file", "tempfile", + "thiserror", "unicode-width", "uutests", ] diff --git a/Cargo.toml b/Cargo.toml index 5a433257..3c69b566 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ diff = "0.1.13" itoa = "1.0.11" regex = "1.10.4" same-file = "1.0.6" +thiserror = "2.0.21" unicode-width = "0.2.0" [dev-dependencies] diff --git a/src/diff.rs b/src/diff.rs index 55fd30e5..5d99da91 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -5,12 +5,13 @@ use crate::color::{colorize, ColorWhen, Palette, PaletteError}; use crate::params::{parse_params, Format, MissingFile, Params}; -use crate::utils::report_failure_to_read_input_file; +use crate::utils::{is_binary, read_file_contents, report_failure_to_read_input_file}; use crate::{context_diff, dir_diff, ed_diff, normal_diff, side_diff, unified_diff}; use std::env::ArgsOs; use std::ffi::OsString; +#[cfg(any(target_os = "linux", target_os = "android"))] use std::fs; -use std::io::{self, stdout, IsTerminal, Read, Write}; +use std::io::{self, stdout, IsTerminal, Write}; use std::iter::Peekable; use std::path::Path; use std::process::ExitCode; @@ -88,15 +89,6 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { return Outcome::Identical; } - // read files - fn read_file_contents(filepath: &OsString) -> io::Result> { - if filepath == "-" { - let mut content = Vec::new(); - io::stdin().read_to_end(&mut content).and(Ok(content)) - } else { - fs::read(filepath) - } - } let mut io_error = false; let mut from_missing = false; let mut to_missing = false; @@ -154,6 +146,32 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { ..params.clone() }; + if params.format == Format::SideBySide { + let has_diff = match header { + None => side_diff::diff(&from_content, &to_content, &mut stdout().lock(), params), + Some(header) => { + let mut output = Vec::new(); + let has_diff = side_diff::diff(&from_content, &to_content, &mut output, params); + if has_diff { + output.splice(0..0, header.iter().copied()); + } + if stdout().write_all(&output).is_err() { + return Outcome::Trouble; + } + has_diff + } + }; + return if has_diff { + if params.brief { + print_files_differ("Files", params); + } + Outcome::Different + } else { + maybe_report_identical_files(); + Outcome::Identical + }; + } + // run diff let result: Vec = match params.format { Format::Normal => normal_diff::diff(&from_content, &to_content, params), @@ -166,26 +184,11 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { return Outcome::Trouble; } }, - // Side by side output is streamed, unless a header may have to come - // first. - Format::SideBySide => match header { - None => side_diff::diff(&from_content, &to_content, &mut stdout().lock(), params), - Some(header) => { - let mut output = Vec::new(); - let result = side_diff::diff(&from_content, &to_content, &mut output, params); - if !result.is_empty() { - output.splice(0..0, header.iter().copied()); - } - if stdout().write_all(&output).is_err() { - return Outcome::Trouble; - } - result - } - }, + Format::SideBySide => unreachable!(), }; if params.brief && !result.is_empty() { print_files_differ("Files", params); - } else if params.format != Format::SideBySide { + } else { let header = if result.is_empty() { &[][..] } else { @@ -249,14 +252,6 @@ fn output_palette(params: &Params) -> Option { } } -/// Number of leading bytes inspected when deciding whether an input is binary. -const BINARY_PROBE_LEN: usize = 16 * 1024; - -/// An input is treated as binary when a NUL byte occurs near its start. -fn is_binary(content: &[u8]) -> bool { - content[..content.len().min(BINARY_PROBE_LEN)].contains(&0) -} - /// Print the one-line summary used instead of the differences, `kind` naming /// what was compared. fn print_files_differ(kind: &str, params: &Params) { diff --git a/src/lib.rs b/src/lib.rs index e1e43b6e..b29c0781 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ pub mod ed_diff; pub mod macros; pub mod normal_diff; pub mod params; +pub mod sdiff; pub mod side_diff; pub mod unified_diff; pub mod utils; diff --git a/src/main.rs b/src/main.rs index 2be28099..e474d7bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ mod ed_diff; mod macros; mod normal_diff; mod params; +mod sdiff; mod side_diff; mod unified_diff; mod utils; @@ -46,7 +47,7 @@ fn usage(name: &str) { println!("{name} {VERSION} (multi-call binary)\n"); println!("Usage: {name} [function [arguments...]]\n"); println!("Currently defined functions:\n"); - println!(" cmp, diff\n"); + println!(" cmp, diff, sdiff\n"); } fn second_arg_error(name: &OsStr) -> ! { @@ -73,6 +74,7 @@ fn main() -> ExitCode { }; match util_name.as_encoded_bytes() { + name if name.ends_with(b"sdiff") => sdiff::main(args), name if name.ends_with(b"diff") => diff::main(args), name if name.ends_with(b"cmp") => cmp::main(args), name => { diff --git a/src/params.rs b/src/params.rs index b7239181..303b5208 100644 --- a/src/params.rs +++ b/src/params.rs @@ -70,6 +70,10 @@ pub struct Params { pub to_missing: bool, pub from_label: Option, pub to_label: Option, + /// Print only the left column of two common lines. + pub left_column: bool, + /// Do not print common lines + pub suppress_common_lines: bool, } impl Params { @@ -113,6 +117,8 @@ impl Default for Params { to_missing: false, from_label: None, to_label: None, + left_column: false, + suppress_common_lines: false, } } } diff --git a/src/sdiff.rs b/src/sdiff.rs new file mode 100644 index 00000000..fdb37e86 --- /dev/null +++ b/src/sdiff.rs @@ -0,0 +1,442 @@ +// This file is part of the uutils diffutils package. +// +// For the full copyright and license information, please view the LICENSE-* +// files that was distributed with this source code. + +use crate::params::Params; +use crate::side_diff; +use crate::utils::{is_binary, read_file_contents, report_failure_to_read_input_file}; +use std::env::ArgsOs; +use std::ffi::OsString; +use std::io::{stdout, Write}; +use std::iter::Peekable; +use std::process::ExitCode; + +#[derive(thiserror::Error, Debug, PartialEq, Eq)] +pub enum SdiffError { + #[error("Usage: {0} ")] + Usage(String), + #[error("option '{0}' requires an argument")] + MissingArgument(String), + #[error("invalid width '{0}'")] + InvalidWidth(String), + #[error("invalid tabsize '{0}'")] + InvalidTabSize(String), + #[error("unrecognized option '{0}'")] + UnrecognizedOption(String), +} + +fn parse_width(val: &str) -> Result { + match val.parse::() { + Ok(w) if w >= 7 => Ok(w), + _ => Err(SdiffError::InvalidWidth(val.to_string())), + } +} + +fn parse_tabsize(val: &str) -> Result { + match val.parse::() { + Ok(num) if num != 0 && num <= crate::params::MAX_TABSIZE => Ok(num), + _ => Err(SdiffError::InvalidTabSize(val.to_string())), + } +} + +/// parses command line arguments for `sdiff`. +/// this is necessary since the options differ for `diff` and `sdiff` in GNU +/// for example: `-w` sets the columns in `sdiff` but in `diff` it ignores whitespace +/// https://www.gnu.org/software/diffutils/manual/html_node/diff-Options.html +/// https://www.gnu.org/software/diffutils/manual/html_node/sdiff-Options.html +pub fn parse_params>( + mut opts: Peekable, +) -> Result { + let executable = opts + .next() + .ok_or_else(|| SdiffError::Usage("sdiff".to_string()))?; + let mut params = Params { + executable, + ..Default::default() + }; + + let mut operands = Vec::new(); + + while let Some(param) = opts.next() { + let param_str = param.to_string_lossy(); + if param == "--" { + operands.extend(opts); + break; + } + + if param == "-s" || param == "--suppress-common-lines" { + params.suppress_common_lines = true; + continue; + } + + if param == "-l" || param == "--left-column" { + params.left_column = true; + continue; + } + + if param == "-t" || param == "--expand-tabs" { + params.expand_tabs = true; + continue; + } + + if param == "-a" || param == "--text" { + params.text = true; + continue; + } + + if param == "-w" || param == "--width" { + let val = opts + .next() + .ok_or_else(|| SdiffError::MissingArgument(param_str.to_string()))?; + params.width = parse_width(&val.to_string_lossy())?; + continue; + } + + if let Some(rest) = param_str.strip_prefix("--width=") { + params.width = parse_width(rest)?; + continue; + } + + if param_str.starts_with("-w") && param_str.len() > 2 { + params.width = parse_width(¶m_str[2..])?; + continue; + } + + if param == "--tabsize" { + let val = opts + .next() + .ok_or_else(|| SdiffError::MissingArgument(param_str.to_string()))?; + params.tabsize = parse_tabsize(&val.to_string_lossy())?; + continue; + } + + if let Some(rest) = param_str.strip_prefix("--tabsize=") { + params.tabsize = parse_tabsize(rest)?; + continue; + } + + if param_str.starts_with('-') && param_str != "-" { + return Err(SdiffError::UnrecognizedOption(param_str.to_string())); + } + + operands.push(param); + } + + if operands.len() != 2 { + return Err(SdiffError::Usage( + params.executable.to_string_lossy().into_owned(), + )); + } + + params.from = operands.remove(0); + params.to = operands.remove(0); + + Ok(params) +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum Outcome { + Identical, + Different, + Trouble, +} + +impl From for ExitCode { + fn from(outcome: Outcome) -> Self { + match outcome { + Outcome::Identical => Self::SUCCESS, + Outcome::Different => Self::from(1), + Outcome::Trouble => Self::from(2), + } + } +} + +pub fn compare_files(params: &Params) -> Outcome { + if params.from == "-" && params.to == "-" + || same_file::is_same_file(¶ms.from, ¶ms.to).unwrap_or(false) + { + if params.suppress_common_lines { + return Outcome::Identical; + } + let content = match read_file_contents(¶ms.from) { + Ok(c) => c, + Err(e) => { + report_failure_to_read_input_file(¶ms.executable, ¶ms.from, &e); + return Outcome::Trouble; + } + }; + if !params.text && is_binary(&content) { + return Outcome::Identical; + } + let mut stdout = stdout().lock(); + side_diff::diff(&content, &content, &mut stdout, params); + let _ = stdout.flush(); + return Outcome::Identical; + } + + let mut io_error = false; + let from_content = match read_file_contents(¶ms.from) { + Ok(c) => c, + Err(e) => { + report_failure_to_read_input_file(¶ms.executable, ¶ms.from, &e); + io_error = true; + vec![] + } + }; + + let to_content = match read_file_contents(¶ms.to) { + Ok(c) => c, + Err(e) => { + report_failure_to_read_input_file(¶ms.executable, ¶ms.to, &e); + io_error = true; + vec![] + } + }; + + if io_error { + return Outcome::Trouble; + } + + if !params.text && (is_binary(&from_content) || is_binary(&to_content)) { + if from_content == to_content { + return Outcome::Identical; + } + println!( + "Binary files {} and {} differ", + params.from.to_string_lossy(), + params.to.to_string_lossy() + ); + return Outcome::Different; + } + + let mut stdout = stdout().lock(); + let has_diff = side_diff::diff(&from_content, &to_content, &mut stdout, params); + let _ = stdout.flush(); + + if has_diff { + Outcome::Different + } else { + Outcome::Identical + } +} + +pub fn main(opts: Peekable) -> ExitCode { + let params = match parse_params(opts) { + Ok(p) => p, + Err(e) => { + eprintln!("{e}"); + return ExitCode::from(2); + } + }; + + compare_files(¶ms).into() +} + +#[cfg(test)] +mod tests { + use super::*; + use std::ffi::OsString; + use std::fs; + use tempfile::NamedTempFile; + + fn parse(args: &[&str]) -> Result { + let os_args: Vec = args.iter().map(OsString::from).collect(); + parse_params(os_args.into_iter().peekable()) + } + + #[test] + fn test_parse_defaults() { + let p = parse(&["sdiff", "file1", "file2"]).unwrap(); + assert_eq!(p.from, OsString::from("file1")); + assert_eq!(p.to, OsString::from("file2")); + assert_eq!(p.width, 130); + assert_eq!(p.tabsize, 8); + assert!(!p.left_column); + assert!(!p.suppress_common_lines); + assert!(!p.expand_tabs); + assert!(!p.text); + } + + #[test] + fn test_parse_all_flags() { + let p = parse(&[ + "sdiff", + "-s", + "-l", + "-t", + "-a", + "-w", + "80", + "--tabsize=4", + "f1", + "f2", + ]) + .unwrap(); + assert!(p.suppress_common_lines); + assert!(p.left_column); + assert!(p.expand_tabs); + assert!(p.text); + assert_eq!(p.width, 80); + assert_eq!(p.tabsize, 4); + } + + #[test] + fn test_parse_attached_width() { + let p1 = parse(&["sdiff", "--width=100", "f1", "f2"]).unwrap(); + assert_eq!(p1.width, 100); + + let p2 = parse(&["sdiff", "-w95", "f1", "f2"]).unwrap(); + assert_eq!(p2.width, 95); + } + + #[test] + fn test_invalid_width_rejected() { + assert_eq!( + parse(&["sdiff", "-w", "6", "f1", "f2"]).unwrap_err(), + SdiffError::InvalidWidth("6".to_string()) + ); + assert_eq!( + parse(&["sdiff", "-w", "not_a_num", "f1", "f2"]).unwrap_err(), + SdiffError::InvalidWidth("not_a_num".to_string()) + ); + assert_eq!( + parse(&["sdiff", "-w"]).unwrap_err(), + SdiffError::MissingArgument("-w".to_string()) + ); + assert_eq!( + parse(&["sdiff", "--width"]).unwrap_err(), + SdiffError::MissingArgument("--width".to_string()) + ); + } + + #[test] + fn test_tabsize_parsing() { + let p1 = parse(&["sdiff", "--tabsize=4", "f1", "f2"]).unwrap(); + assert_eq!(p1.tabsize, 4); + + let p2 = parse(&["sdiff", "--tabsize", "2", "f1", "f2"]).unwrap(); + assert_eq!(p2.tabsize, 2); + + assert_eq!( + parse(&["sdiff", "--tabsize=0", "f1", "f2"]).unwrap_err(), + SdiffError::InvalidTabSize("0".to_string()) + ); + assert_eq!( + parse(&["sdiff", "--tabsize", "0", "f1", "f2"]).unwrap_err(), + SdiffError::InvalidTabSize("0".to_string()) + ); + assert_eq!( + parse(&["sdiff", "--tabsize=invalid", "f1", "f2"]).unwrap_err(), + SdiffError::InvalidTabSize("invalid".to_string()) + ); + assert_eq!( + parse(&["sdiff", "--tabsize"]).unwrap_err(), + SdiffError::MissingArgument("--tabsize".to_string()) + ); + } + + #[test] + fn test_missing_operands() { + assert_eq!( + parse(&["sdiff", "f1"]).unwrap_err(), + SdiffError::Usage("sdiff".to_string()) + ); + assert_eq!( + parse(&["sdiff"]).unwrap_err(), + SdiffError::Usage("sdiff".to_string()) + ); + } + + #[test] + fn test_extra_operands() { + assert_eq!( + parse(&["sdiff", "f1", "f2", "f3"]).unwrap_err(), + SdiffError::Usage("sdiff".to_string()) + ); + } + + #[test] + fn test_double_dash_operand_separator() { + let p = parse(&["sdiff", "--", "-weird-file1", "-weird-file2"]).unwrap(); + assert_eq!(p.from, OsString::from("-weird-file1")); + assert_eq!(p.to, OsString::from("-weird-file2")); + } + + #[test] + fn test_unrecognized_option() { + let res = parse(&["sdiff", "-unknown", "f1", "f2"]); + assert_eq!( + res.unwrap_err(), + SdiffError::UnrecognizedOption("-unknown".to_string()) + ); + } + + #[test] + fn test_outcome_exit_codes() { + assert_eq!(ExitCode::from(Outcome::Identical), ExitCode::SUCCESS); + assert_eq!(ExitCode::from(Outcome::Different), ExitCode::from(1)); + assert_eq!(ExitCode::from(Outcome::Trouble), ExitCode::from(2)); + } + + #[test] + fn test_compare_files_identical() { + let f1 = NamedTempFile::new().unwrap(); + fs::write(f1.path(), b"hello\nworld\n").unwrap(); + + let params = Params { + executable: OsString::from("sdiff"), + from: f1.path().as_os_str().to_os_string(), + to: f1.path().as_os_str().to_os_string(), + ..Default::default() + }; + + assert_eq!(compare_files(¶ms), Outcome::Identical); + } + + #[test] + fn test_compare_files_different() { + let f1 = NamedTempFile::new().unwrap(); + let f2 = NamedTempFile::new().unwrap(); + fs::write(f1.path(), b"hello\nleft\n").unwrap(); + fs::write(f2.path(), b"hello\nright\n").unwrap(); + + let params = Params { + executable: OsString::from("sdiff"), + from: f1.path().as_os_str().to_os_string(), + to: f2.path().as_os_str().to_os_string(), + ..Default::default() + }; + + assert_eq!(compare_files(¶ms), Outcome::Different); + } + + #[test] + fn test_compare_files_missing() { + let params = Params { + executable: OsString::from("sdiff"), + from: OsString::from("/path/to/nonexistent_file_a"), + to: OsString::from("/path/to/nonexistent_file_b"), + ..Default::default() + }; + + assert_eq!(compare_files(¶ms), Outcome::Trouble); + } + + #[test] + fn test_compare_files_binary_different() { + let f1 = NamedTempFile::new().unwrap(); + let f2 = NamedTempFile::new().unwrap(); + fs::write(f1.path(), b"\0binary1").unwrap(); + fs::write(f2.path(), b"\0binary2").unwrap(); + + let params = Params { + executable: OsString::from("sdiff"), + from: f1.path().as_os_str().to_os_string(), + to: f2.path().as_os_str().to_os_string(), + ..Default::default() + }; + + assert_eq!(compare_files(¶ms), Outcome::Different); + } +} diff --git a/src/side_diff.rs b/src/side_diff.rs index faebfd19..33328a92 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -5,7 +5,7 @@ use core::cmp::{max, min}; use diff::Result; -use std::{io::Write, vec}; +use std::io::Write; use unicode_width::UnicodeWidthStr; use crate::params::Params; @@ -22,6 +22,8 @@ struct Config { tab_size: usize, expanded: bool, separator_pos: usize, + left_column: bool, + suppress_common_lines: bool, } impl<'a> From<&'a [u8]> for CharIter<'a> { @@ -73,7 +75,13 @@ impl<'a> Iterator for CharIter<'a> { } impl Config { - pub fn new(full_width: usize, tab_size: usize, expanded: bool) -> Self { + pub fn new( + full_width: usize, + tab_size: usize, + expanded: bool, + left_column: bool, + suppress_common_lines: bool, + ) -> Self { // diff uses this calculation to calculate the size of a half line // based on the options passed (like -w, -t, etc.). It's actually // pretty useless, because we (actually) don't have any size modifiers @@ -94,6 +102,8 @@ impl Config { tab_size, sdiff_half_width: hw, separator_pos: ((hw + c2o - 1) >> 1), + left_column, + suppress_common_lines, } } } @@ -308,17 +318,18 @@ fn push_output( Ok(()) } -pub fn diff( - from_file: &[u8], - to_file: &[u8], - output: &mut T, - params: &Params, -) -> Vec { +pub fn diff(from_file: &[u8], to_file: &[u8], output: &mut T, params: &Params) -> bool { // ^ The left file ^ The right file let mut left_lines: Vec<&[u8]> = from_file.split_inclusive(|&c| c == b'\n').collect(); let mut right_lines: Vec<&[u8]> = to_file.split_inclusive(|&c| c == b'\n').collect(); - let config = Config::new(params.width, params.tabsize, params.expand_tabs); + let config = Config::new( + params.width, + params.tabsize, + params.expand_tabs, + params.left_column, + params.suppress_common_lines, + ); if left_lines.last() == Some(&&b""[..]) { left_lines.pop(); @@ -350,17 +361,33 @@ pub fn diff( More studies are needed to cover GNU diff side by side with 100% accuracy, which is one of the goals of this project : ) */ + + let mut has_diff = false; + for result in diff::slice(&left_lines, &right_lines) { match result { - Result::Left(left_ln) => push_output(left_ln, b"", b'<', output, &config).unwrap(), - Result::Right(right_ln) => push_output(b"", right_ln, b'>', output, &config).unwrap(), + Result::Left(left_ln) => { + has_diff = true; + push_output(left_ln, b"", b'<', output, &config).unwrap() + } + Result::Right(right_ln) => { + has_diff = true; + push_output(b"", right_ln, b'>', output, &config).unwrap() + } Result::Both(left_ln, right_ln) => { - push_output(left_ln, right_ln, b' ', output, &config).unwrap() + if config.suppress_common_lines { + continue; + } + if config.left_column { + push_output(left_ln, b"", b'(', output, &config).unwrap(); + } else { + push_output(left_ln, right_ln, b' ', output, &config).unwrap(); + } } } } - vec![] + has_diff } #[cfg(test)] @@ -378,6 +405,8 @@ mod tests { expanded: true, sdiff_column_two_offset: 0, separator_pos: 0, + left_column: false, + suppress_common_lines: false, }; const CONFIG_E_F: Config = Config { @@ -386,6 +415,8 @@ mod tests { expanded: false, sdiff_column_two_offset: 0, separator_pos: 0, + left_column: false, + suppress_common_lines: false, }; #[test] @@ -476,6 +507,8 @@ mod tests { tab_size, expanded, separator_pos: 15, + left_column: false, + suppress_common_lines: false, } } @@ -825,7 +858,7 @@ mod tests { impl Default for Config { fn default() -> Self { - Config::new(130, 8, false) + Config::new(130, 8, false, false, false) } } @@ -1112,10 +1145,10 @@ mod tests { let params = generate_params(); let from_file = b""; let to_file = b""; - let mut output = vec![]; + let mut output: Vec = vec![]; diff(from_file, to_file, &mut output, ¶ms); - assert_eq!(output, vec![]); + assert!(output.is_empty()); } #[test] @@ -1223,7 +1256,7 @@ mod tests { use super::*; fn create_config(full_width: usize, tab_size: usize, expanded: bool) -> Config { - Config::new(full_width, tab_size, expanded) + Config::new(full_width, tab_size, expanded, false, false) } #[test] @@ -1290,4 +1323,73 @@ mod tests { assert_eq!(config.separator_pos, 2); } } + + mod sdiff_feature_tests { + use super::*; + + #[test] + fn test_suppress_common_lines_omits_identical_lines() { + let left = b"common header\nleft unique\ncommon footer\n"; + let right = b"common header\nright unique\ncommon footer\n"; + let mut out = Vec::new(); + let params = Params { + width: 80, + suppress_common_lines: true, + ..Default::default() + }; + let result = diff(left, right, &mut out, ¶ms); + assert!(result, "Differences should be reported (true)"); + let text = String::from_utf8(out).unwrap(); + assert!( + !text.contains("common header"), + "Common lines must be suppressed" + ); + assert!( + !text.contains("common footer"), + "Common lines must be suppressed" + ); + assert!(text.contains("<"), "Left difference must be displayed"); + assert!(text.contains(">"), "Right difference must be displayed"); + } + + #[test] + fn test_left_column_prints_gutter_marker_on_match() { + let left = b"common line\n"; + let right = b"common line\n"; + let mut out = Vec::new(); + let params = Params { + width: 80, + left_column: true, + expand_tabs: true, + ..Default::default() + }; + let result = diff(left, right, &mut out, ¶ms); + assert!(!result, "Identical files should return false"); + let text = String::from_utf8(out).unwrap(); + // With width 80 and expand_tabs, half-width is 38 and separator '(' sits at col 39 + assert_eq!( + text, "common line (\n", + "GNU sdiff/diff -y --left-column formats common lines with '(' in the gutter" + ); + } + + #[test] + fn test_identical_files_reports_false() { + let content = b"line 1\nline 2\n"; + let mut out = Vec::new(); + let params = Params::default(); + let result = diff(content, content, &mut out, ¶ms); + assert!(!result, "Identical files must return false"); + } + + #[test] + fn test_differing_files_reports_true() { + let left = b"line a\n"; + let right = b"line b\n"; + let mut out = Vec::new(); + let params = Params::default(); + let result = diff(left, right, &mut out, ¶ms); + assert!(result, "Differing files must return true"); + } + } } diff --git a/src/utils.rs b/src/utils.rs index 645fece6..b7ca0563 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -8,7 +8,8 @@ use regex::Regex; use std::{ borrow::Cow, ffi::{OsStr, OsString}, - io::Write, + fs, + io::{Read, Write}, }; use unicode_width::UnicodeWidthStr; @@ -314,6 +315,24 @@ pub fn report_failure_to_read_input_file( ); } +/// Number of leading bytes inspected when deciding whether an input is binary. +pub const BINARY_PROBE_LEN: usize = 16 * 1024; + +/// An input is treated as binary when a NUL byte occurs near its start. +pub fn is_binary(content: &[u8]) -> bool { + content[..content.len().min(BINARY_PROBE_LEN)].contains(&0) +} + +/// Reads the entire contents of a file, or standard input if `filepath` is `"-"`. +pub fn read_file_contents(filepath: &OsStr) -> std::io::Result> { + if filepath == "-" { + let mut content = Vec::new(); + std::io::stdin().read_to_end(&mut content).and(Ok(content)) + } else { + fs::read(filepath) + } +} + #[cfg(test)] pub mod testcmds { // Command construction wrapper that provides some validation and non-obscure, "fail fast" diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index c251c39b..c8a7c7d5 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -1054,3 +1054,24 @@ fn label_errors(#[case] args: &[&str], #[case] error: &str) { .fails_with_code(2) .stderr_contains(error); } + +#[test] +fn side_by_side_different_files_exit_code_and_directory_headers() { + let (at, ts) = at_and_ts!(); + at.write("file1", "foo\n"); + at.write("file2", "bar\n"); + ts.ucmd().args(&["-y", "file1", "file2"]).fails_with_code(1); + + at.mkdir("a"); + at.mkdir("b"); + at.write("a/f", "left\n"); + at.write("b/f", "right\n"); + ts.ucmd() + .args(&["-y", "-r", "a", "b"]) + .fails_with_code(1) + .stdout_contains("diff -y -r a/f b/f\n"); + + at.write("file3", "same\n"); + at.write("file4", "same\n"); + ts.ucmd().args(&["-y", "file3", "file4"]).succeeds(); +} diff --git a/tests/by-util/test_sdiff.rs b/tests/by-util/test_sdiff.rs new file mode 100644 index 00000000..495af23f --- /dev/null +++ b/tests/by-util/test_sdiff.rs @@ -0,0 +1,498 @@ +// This file is part of the uutils diffutils package. +// +// For the full copyright and license information, please view the LICENSE-* +// files that was distributed with this source code. + +use uutests::{at_and_ts, new_ucmd}; + +#[test] +fn test_sdiff_missing_operands() { + new_ucmd!() + .fails_with_code(2) + .stderr_contains("Usage: sdiff "); + + new_ucmd!() + .arg("only_one_file") + .fails_with_code(2) + .stderr_contains("Usage: sdiff "); +} + +#[test] +fn test_sdiff_extra_operands() { + let (at, ts) = at_and_ts!(); + at.touch("a"); + at.touch("b"); + at.touch("c"); + + ts.ucmd() + .args(&["a", "b", "c"]) + .fails_with_code(2) + .stderr_contains("Usage: sdiff "); +} + +#[test] +fn test_sdiff_unrecognized_option() { + let (at, ts) = at_and_ts!(); + at.touch("a"); + at.touch("b"); + + ts.ucmd() + .args(&["--unknown-flag", "a", "b"]) + .fails_with_code(2) + .stderr_contains("unrecognized option '--unknown-flag'"); + + ts.ucmd() + .args(&["-z", "a", "b"]) + .fails_with_code(2) + .stderr_contains("unrecognized option '-z'"); +} + +#[test] +fn test_sdiff_double_dash_operand_separator() { + let (at, ts) = at_and_ts!(); + at.write("-odd-name1", "test content\n"); + at.write("-odd-name2", "test content\n"); + + ts.ucmd() + .args(&["--", "-odd-name1", "-odd-name2"]) + .succeeds() + .stdout_contains("test content"); +} + +#[test] +fn test_sdiff_nonexistent_file() { + new_ucmd!() + .args(&["no_such_file1", "no_such_file2"]) + .fails_with_code(2); +} + +#[test] +fn test_sdiff_identical_files() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "alpha\nbeta\ngamma\n"); + at.write("f2.txt", "alpha\nbeta\ngamma\n"); + + let result = ts.ucmd().args(&["f1.txt", "f2.txt"]).succeeds(); + let out = result.stdout_str(); + assert!(out.contains("alpha")); + assert!(out.contains("beta")); + assert!(out.contains("gamma")); + // Identical lines have whitespace gutter (no < or > markers) + assert!(!out.contains("<")); + assert!(!out.contains(">")); +} + +#[test] +fn test_sdiff_different_files() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "apple\nbanana\n"); + at.write("f2.txt", "apple\ncherry\n"); + + let result = ts.ucmd().args(&["f1.txt", "f2.txt"]).fails_with_code(1); + let out = result.stdout_str(); + assert!(out.contains("apple")); + assert!(out.contains("<")); + assert!(out.contains(">")); + assert!(out.contains("banana")); + assert!(out.contains("cherry")); +} + +#[test] +fn test_sdiff_empty_files() { + let (at, ts) = at_and_ts!(); + at.touch("empty1.txt"); + at.touch("empty2.txt"); + + // Both empty: identical -> exit 0 + let res = ts.ucmd().args(&["empty1.txt", "empty2.txt"]).succeeds(); + assert_eq!(res.stdout_str(), ""); + + // One empty, one non-empty: different -> exit 1 + at.write("non_empty.txt", "hello\n"); + let res_diff = ts + .ucmd() + .args(&["empty1.txt", "non_empty.txt"]) + .fails_with_code(1); + assert!(res_diff.stdout_str().contains(">")); + assert!(res_diff.stdout_str().contains("hello")); +} + +#[test] +fn test_sdiff_suppress_common_lines_flag() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "same_line\nleft_only\n"); + at.write("f2.txt", "same_line\nright_only\n"); + + let result = ts + .ucmd() + .args(&["-s", "f1.txt", "f2.txt"]) + .fails_with_code(1); + let out = result.stdout_str(); + assert!( + !out.contains("same_line"), + "Common lines must be omitted with -s" + ); + assert!(out.contains("left_only")); + assert!(out.contains("right_only")); + + let result_long = ts + .ucmd() + .args(&["--suppress-common-lines", "f1.txt", "f2.txt"]) + .fails_with_code(1); + assert!(!result_long.stdout_str().contains("same_line")); +} + +#[test] +fn test_sdiff_suppress_common_lines_identical_files() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "identical\n"); + at.write("f2.txt", "identical\n"); + + let result = ts.ucmd().args(&["-s", "f1.txt", "f2.txt"]).succeeds(); + assert_eq!( + result.stdout_str(), + "", + "Output should be empty for identical files with -s" + ); +} + +#[test] +fn test_sdiff_left_column_flag() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "shared_line\n"); + at.write("f2.txt", "shared_line\n"); + + // GNU sdiff -l formats matching lines with '(' in the midpoint gutter + let result = ts + .ucmd() + .args(&["-l", "--width=80", "--expand-tabs", "f1.txt", "f2.txt"]) + .succeeds(); + assert_eq!( + result.stdout_str(), + "shared_line (\n" + ); + + let result_long = ts + .ucmd() + .args(&[ + "--left-column", + "--width=80", + "--expand-tabs", + "f1.txt", + "f2.txt", + ]) + .succeeds(); + assert_eq!( + result_long.stdout_str(), + "shared_line (\n" + ); +} + +#[test] +fn test_sdiff_left_column_with_differences() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "common\nleft_only\n"); + at.write("f2.txt", "common\nright_only\n"); + + let res = ts + .ucmd() + .args(&["-l", "--width=40", "--expand-tabs", "f1.txt", "f2.txt"]) + .fails_with_code(1); + let out = res.stdout_str(); + assert!(out.contains("(")); + assert!(out.contains("<")); + assert!(out.contains(">")); +} + +#[test] +fn test_sdiff_custom_width_flags() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "a\n"); + at.write("f2.txt", "b\n"); + + // Detached short: -w 80 + ts.ucmd() + .args(&["-w", "80", "f1.txt", "f2.txt"]) + .fails_with_code(1); + + // Attached short: -w80 + ts.ucmd() + .args(&["-w80", "f1.txt", "f2.txt"]) + .fails_with_code(1); + + // Long flag with =: --width=80 + ts.ucmd() + .args(&["--width=80", "f1.txt", "f2.txt"]) + .fails_with_code(1); + + // Long flag with space: --width 80 + ts.ucmd() + .args(&["--width", "80", "f1.txt", "f2.txt"]) + .fails_with_code(1); +} + +#[test] +fn test_sdiff_invalid_width() { + let (at, ts) = at_and_ts!(); + at.touch("f1.txt"); + at.touch("f2.txt"); + + // Width less than GNU minimum (7) + ts.ucmd() + .args(&["-w", "5", "f1.txt", "f2.txt"]) + .fails_with_code(2) + .stderr_contains("invalid width '5'"); + + ts.ucmd() + .args(&["-w0", "f1.txt", "f2.txt"]) + .fails_with_code(2) + .stderr_contains("invalid width '0'"); + + // Non-numeric width + ts.ucmd() + .args(&["--width=abc", "f1.txt", "f2.txt"]) + .fails_with_code(2) + .stderr_contains("invalid width 'abc'"); + + // Missing width argument + ts.ucmd() + .args(&["-w"]) + .fails_with_code(2) + .stderr_contains("option '-w' requires an argument"); + + ts.ucmd() + .args(&["--width"]) + .fails_with_code(2) + .stderr_contains("option '--width' requires an argument"); +} + +#[test] +fn test_sdiff_expand_tabs_flag() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "a\tb\n"); + at.write("f2.txt", "a\tb\n"); + + let result = ts + .ucmd() + .args(&["-t", "--width=80", "-l", "f1.txt", "f2.txt"]) + .succeeds(); + assert_eq!( + result.stdout_str(), + "a b (\n" + ); + + let result_long = ts + .ucmd() + .args(&["--expand-tabs", "--width=80", "-l", "f1.txt", "f2.txt"]) + .succeeds(); + assert_eq!( + result_long.stdout_str(), + "a b (\n" + ); +} + +#[test] +fn test_sdiff_tabsize() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "a\tb\n"); + at.write("f2.txt", "a\tb\n"); + + // --tabsize=4 with tab expansion + let res = ts + .ucmd() + .args(&["-t", "--tabsize=4", "--width=40", "-l", "f1.txt", "f2.txt"]) + .succeeds(); + assert_eq!(res.stdout_str(), "a b (\n"); + + // --tabsize 4 (detached) + let res_detached = ts + .ucmd() + .args(&[ + "-t", + "--tabsize", + "4", + "--width=40", + "-l", + "f1.txt", + "f2.txt", + ]) + .succeeds(); + assert_eq!(res_detached.stdout_str(), "a b (\n"); + + // Invalid tabsize 0 + ts.ucmd() + .args(&["--tabsize=0", "f1.txt", "f2.txt"]) + .fails_with_code(2) + .stderr_contains("invalid tabsize '0'"); + + ts.ucmd() + .args(&["--tabsize", "0", "f1.txt", "f2.txt"]) + .fails_with_code(2) + .stderr_contains("invalid tabsize '0'"); + + // Missing argument + ts.ucmd() + .args(&["--tabsize"]) + .fails_with_code(2) + .stderr_contains("option '--tabsize' requires an argument"); +} + +#[test] +fn test_sdiff_binary_files() { + let (at, ts) = at_and_ts!(); + at.write_bytes("b1.bin", b"\0first binary\n"); + at.write_bytes("b2.bin", b"\0second binary\n"); + + // GNU diffutils outputs "Binary files ... differ" to stdout + ts.ucmd() + .args(&["b1.bin", "b2.bin"]) + .fails_with_code(1) + .stdout_contains("Binary files b1.bin and b2.bin differ"); +} + +#[test] +fn test_sdiff_binary_files_identical() { + let (at, ts) = at_and_ts!(); + at.write_bytes("b1.bin", b"\0identical binary data\n"); + at.write_bytes("b2.bin", b"\0identical binary data\n"); + + ts.ucmd().args(&["b1.bin", "b2.bin"]).succeeds(); +} + +#[test] +fn test_sdiff_text_flag_overrides_binary() { + let (at, ts) = at_and_ts!(); + at.write_bytes("b1.bin", b"\0first binary\n"); + at.write_bytes("b2.bin", b"\0second binary\n"); + + // Short flag -a + ts.ucmd() + .args(&["-a", "b1.bin", "b2.bin"]) + .fails_with_code(1) + .stdout_contains("<"); + + // Long flag --text + ts.ucmd() + .args(&["--text", "b1.bin", "b2.bin"]) + .fails_with_code(1) + .stdout_contains("<"); +} + +#[test] +fn test_sdiff_stdin_left_operand() { + let (at, ts) = at_and_ts!(); + at.write("f2.txt", "line from disk\n"); + + let res = ts + .ucmd() + .args(&["-", "f2.txt"]) + .pipe_in("line from stdin\n") + .fails_with_code(1); + let out = res.stdout_str(); + assert!(out.contains("line from stdin")); + assert!(out.contains("line from disk")); + assert!(out.contains("<")); + assert!(out.contains(">")); +} + +#[test] +fn test_sdiff_stdin_right_operand() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "line from disk\n"); + + let res = ts + .ucmd() + .args(&["f1.txt", "-"]) + .pipe_in("line from stdin\n") + .fails_with_code(1); + let out = res.stdout_str(); + assert!(out.contains("line from disk")); + assert!(out.contains("line from stdin")); +} + +#[test] +fn test_sdiff_stdin_identical() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "same content\n"); + + let res = ts + .ucmd() + .args(&["-", "f1.txt"]) + .pipe_in("same content\n") + .succeeds(); + assert!(res.stdout_str().contains("same content")); + assert!(!res.stdout_str().contains("<")); + assert!(!res.stdout_str().contains(">")); +} + +#[test] +fn test_sdiff_both_from_stdin() { + // Both operands as "-" denotes comparing stdin with itself (identical) + new_ucmd!().args(&["-", "-"]).succeeds(); +} + +#[test] +fn test_sdiff_missing_trailing_newline() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "no newline"); + at.write("f2.txt", "no newline"); + + ts.ucmd().args(&["f1.txt", "f2.txt"]).succeeds(); + + at.write("f3.txt", "different"); + let res = ts.ucmd().args(&["f1.txt", "f3.txt"]).fails_with_code(1); + assert!(res.stdout_str().contains("<")); + assert!(res.stdout_str().contains(">")); +} + +#[test] +fn test_sdiff_crlf_line_endings() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "hello\r\nworld\r\n"); + at.write("f2.txt", "hello\r\nworld\r\n"); + + ts.ucmd().args(&["f1.txt", "f2.txt"]).succeeds(); + + at.write("f3.txt", "hello\r\nthere\r\n"); + ts.ucmd().args(&["f1.txt", "f3.txt"]).fails_with_code(1); +} + +#[test] +fn test_sdiff_multibyte_unicode_alignment() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "こんにちは\n世界\n"); + at.write("f2.txt", "こんばんは\n世界\n"); + + let res = ts + .ucmd() + .args(&["--width=60", "f1.txt", "f2.txt"]) + .fails_with_code(1); + let out = res.stdout_str(); + assert!(out.contains("こんにちは")); + assert!(out.contains("こんばんは")); + assert!(out.contains("世界")); +} + +#[test] +fn test_sdiff_gutter_marker_column() { + let (at, ts) = at_and_ts!(); + at.write("f1.txt", "aa\n"); + at.write("f2.txt", "bb\n"); + + // With --expand-tabs and --width=40: matches GNU exact column spacing (#269) + let res = ts + .ucmd() + .args(&["--width=40", "--expand-tabs", "f1.txt", "f2.txt"]) + .fails_with_code(1); + assert_eq!( + res.stdout_str(), + "aa <\n > bb\n" + ); + + // Default tabbed padding with --width=40: matches GNU exact column spacing + let res_tabs = ts + .ucmd() + .args(&["--width=40", "f1.txt", "f2.txt"]) + .fails_with_code(1); + assert_eq!(res_tabs.stdout_str(), "aa\t\t <\n\t\t >\tbb\n"); +} diff --git a/tests/tests.rs b/tests/tests.rs index 2a1076f6..0ce87c9d 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -22,3 +22,6 @@ mod test_cmp; #[path = "by-util/test_diff.rs"] mod test_diff; + +#[path = "by-util/test_sdiff.rs"] +mod test_sdiff;