diff --git a/src/context_diff.rs b/src/context_diff.rs index ddc11dd4..db69d521 100644 --- a/src/context_diff.rs +++ b/src/context_diff.rs @@ -7,7 +7,7 @@ use std::collections::VecDeque; use std::io::Write; use crate::params::Params; -use crate::utils::{do_write_line, line_prefix}; +use crate::utils::{diff_lines, do_write_line, line_prefix}; use crate::utils::{ get_modification_time, is_posix_locale, write_file_header, FunctionLineFinder, TimestampStyle, }; @@ -100,10 +100,11 @@ fn make_diff( actual: &[u8], context_size: usize, stop_early: bool, + ignore_case: bool, ) -> Vec { let mut line_number_expected = 1; let mut line_number_actual = 1; - let mut context_queue: VecDeque<&[u8]> = VecDeque::with_capacity(context_size); + let mut context_queue: VecDeque<(&[u8], &[u8])> = VecDeque::with_capacity(context_size); // Like GNU diff, keep changes in one hunk while at most twice the context // size of unchanged lines separates them. let max_gap = 2 * context_size; @@ -111,19 +112,19 @@ fn make_diff( let mut results = Vec::new(); let mut mismatch = Mismatch::new(0, 0); - let mut expected_lines: Vec<&[u8]> = expected.split(|&c| c == b'\n').collect(); - let mut actual_lines: Vec<&[u8]> = actual.split(|&c| c == b'\n').collect(); + let mut expected_lines = diff_lines(expected, ignore_case); + let mut actual_lines = diff_lines(actual, ignore_case); debug_assert_eq!(b"".split(|&c| c == b'\n').count(), 1); // ^ means that underflow here is impossible let expected_lines_count = expected_lines.len() - 1; let actual_lines_count = actual_lines.len() - 1; - if expected_lines.last() == Some(&&b""[..]) { + if expected_lines.last().is_some_and(|l| l.raw.is_empty()) { expected_lines.pop(); } - if actual_lines.last() == Some(&&b""[..]) { + if actual_lines.last().is_some_and(|l| l.raw.is_empty()) { actual_lines.pop(); } @@ -140,9 +141,9 @@ fn make_diff( ); } - while let Some(line) = context_queue.pop_front() { - mismatch.expected.push(DiffLine::Context(line.to_vec())); - mismatch.actual.push(DiffLine::Context(line.to_vec())); + while let Some((exp_line, act_line)) = context_queue.pop_front() { + mismatch.expected.push(DiffLine::Context(exp_line.to_vec())); + mismatch.actual.push(DiffLine::Context(act_line.to_vec())); } ChangeBlock::mark(&mut block, &mismatch, true, false); @@ -162,9 +163,9 @@ fn make_diff( ); } - while let Some(line) = context_queue.pop_front() { - mismatch.expected.push(DiffLine::Context(line.to_vec())); - mismatch.actual.push(DiffLine::Context(line.to_vec())); + while let Some((exp_line, act_line)) = context_queue.pop_front() { + mismatch.expected.push(DiffLine::Context(exp_line.to_vec())); + mismatch.actual.push(DiffLine::Context(act_line.to_vec())); } ChangeBlock::mark(&mut block, &mismatch, false, true); @@ -175,7 +176,7 @@ fn make_diff( line_number_actual += 1; lines_since_mismatch = 0; } - diff::Result::Both(str, _) => { + diff::Result::Both(str1, str2) => { // if one of them is missing a newline and the other isn't, then they don't actually match if (line_number_actual > actual_lines_count) && (line_number_expected > expected_lines_count) @@ -184,8 +185,8 @@ fn make_diff( // this is the last line: lines still in the queue are past // the trailing context and there is no next hunk to lead into if lines_since_mismatch < context_size { - mismatch.expected.push(DiffLine::Context(str.to_vec())); - mismatch.actual.push(DiffLine::Context(str.to_vec())); + mismatch.expected.push(DiffLine::Context(str1.to_vec())); + mismatch.actual.push(DiffLine::Context(str2.to_vec())); mismatch.expected_missing_nl = true; mismatch.actual_missing_nl = true; } @@ -197,13 +198,13 @@ fn make_diff( line_number_actual - context_queue.len(), ); } - while let Some(line) = context_queue.pop_front() { - mismatch.expected.push(DiffLine::Context(line.to_vec())); - mismatch.actual.push(DiffLine::Context(line.to_vec())); + while let Some((exp_line, act_line)) = context_queue.pop_front() { + mismatch.expected.push(DiffLine::Context(exp_line.to_vec())); + mismatch.actual.push(DiffLine::Context(act_line.to_vec())); } ChangeBlock::mark(&mut block, &mismatch, true, true); - mismatch.expected.push(DiffLine::Change(str.to_vec())); - mismatch.actual.push(DiffLine::Change(str.to_vec())); + mismatch.expected.push(DiffLine::Change(str1.to_vec())); + mismatch.actual.push(DiffLine::Change(str2.to_vec())); mismatch.actual_missing_nl = true; lines_since_mismatch = 0; } else if line_number_expected > expected_lines_count { @@ -214,14 +215,14 @@ fn make_diff( line_number_actual - context_queue.len(), ); } - while let Some(line) = context_queue.pop_front() { - mismatch.expected.push(DiffLine::Context(line.to_vec())); - mismatch.actual.push(DiffLine::Context(line.to_vec())); + while let Some((exp_line, act_line)) = context_queue.pop_front() { + mismatch.expected.push(DiffLine::Context(exp_line.to_vec())); + mismatch.actual.push(DiffLine::Context(act_line.to_vec())); } ChangeBlock::mark(&mut block, &mismatch, true, true); - mismatch.expected.push(DiffLine::Change(str.to_vec())); + mismatch.expected.push(DiffLine::Change(str1.to_vec())); mismatch.expected_missing_nl = true; - mismatch.actual.push(DiffLine::Change(str.to_vec())); + mismatch.actual.push(DiffLine::Change(str2.to_vec())); lines_since_mismatch = 0; } else { ChangeBlock::close(&mut block, &mut mismatch); @@ -230,10 +231,10 @@ fn make_diff( let _ = context_queue.pop_front(); } if lines_since_mismatch < context_size { - mismatch.expected.push(DiffLine::Context(str.to_vec())); - mismatch.actual.push(DiffLine::Context(str.to_vec())); + mismatch.expected.push(DiffLine::Context(str1.to_vec())); + mismatch.actual.push(DiffLine::Context(str2.to_vec())); } else if context_size > 0 { - context_queue.push_back(str); + context_queue.push_back((str1.raw, str2.raw)); } lines_since_mismatch += 1; } @@ -337,7 +338,13 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { params.to_label.as_deref(), &to_modified_time, ); - let diff_results = make_diff(expected, actual, params.context_count, params.brief); + let diff_results = make_diff( + expected, + actual, + params.context_count, + params.brief, + params.ignore_case, + ); if diff_results.is_empty() { return Vec::new(); } diff --git a/src/ed_diff.rs b/src/ed_diff.rs index 6ec73568..6e738000 100644 --- a/src/ed_diff.rs +++ b/src/ed_diff.rs @@ -6,7 +6,7 @@ use std::io::Write; use crate::params::Params; -use crate::utils::do_write_line; +use crate::utils::{diff_lines, do_write_line}; #[derive(Debug, PartialEq)] struct Mismatch { @@ -45,27 +45,32 @@ 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, + ignore_case: bool, +) -> Result, DiffError> { let mut line_number_expected = 1; let mut line_number_actual = 1; let mut results = Vec::new(); let mut mismatch = Mismatch::new(line_number_expected, line_number_actual); - let mut expected_lines: Vec<&[u8]> = expected.split(|&c| c == b'\n').collect(); - let mut actual_lines: Vec<&[u8]> = actual.split(|&c| c == b'\n').collect(); + let mut expected_lines = diff_lines(expected, ignore_case); + let mut actual_lines = diff_lines(actual, ignore_case); debug_assert_eq!(b"".split(|&c| c == b'\n').count(), 1); // ^ means that underflow here is impossible let _expected_lines_count = expected_lines.len() - 1; let _actual_lines_count = actual_lines.len() - 1; - if expected_lines.last() == Some(&&b""[..]) { + if expected_lines.last().is_some_and(|l| l.raw.is_empty()) { expected_lines.pop(); } else { return Err(DiffError::MissingNL); } - if actual_lines.last() == Some(&&b""[..]) { + if actual_lines.last().is_some_and(|l| l.raw.is_empty()) { actual_lines.pop(); } else { return Err(DiffError::MissingNL); @@ -112,7 +117,7 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result Result, DiffError> { let mut output = Vec::new(); - let diff_results = make_diff(expected, actual, params.brief)?; + let diff_results = make_diff(expected, actual, params.brief, params.ignore_case)?; if params.brief && !diff_results.is_empty() { write!(&mut output, "\0").unwrap(); return Ok(output); diff --git a/src/normal_diff.rs b/src/normal_diff.rs index f5a833f2..85d6ac86 100644 --- a/src/normal_diff.rs +++ b/src/normal_diff.rs @@ -6,7 +6,7 @@ use std::io::Write; use crate::params::Params; -use crate::utils::{do_write_line, line_prefix}; +use crate::utils::{diff_lines, do_write_line, line_prefix}; #[derive(Debug, PartialEq)] struct Mismatch { @@ -32,25 +32,25 @@ impl Mismatch { } // Produces a diff between the expected output and actual output. -fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Vec { +fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool, ignore_case: bool) -> Vec { let mut line_number_expected = 1; let mut line_number_actual = 1; let mut results = Vec::new(); let mut mismatch = Mismatch::new(line_number_expected, line_number_actual); - let mut expected_lines: Vec<&[u8]> = expected.split(|&c| c == b'\n').collect(); - let mut actual_lines: Vec<&[u8]> = actual.split(|&c| c == b'\n').collect(); + let mut expected_lines = diff_lines(expected, ignore_case); + let mut actual_lines = diff_lines(actual, ignore_case); debug_assert_eq!(b"".split(|&c| c == b'\n').count(), 1); // ^ means that underflow here is impossible let expected_lines_count = expected_lines.len() - 1; let actual_lines_count = actual_lines.len() - 1; - if expected_lines.last() == Some(&&b""[..]) { + if expected_lines.last().is_some_and(|l| l.raw.is_empty()) { expected_lines.pop(); } - if actual_lines.last() == Some(&&b""[..]) { + if actual_lines.last().is_some_and(|l| l.raw.is_empty()) { actual_lines.pop(); } @@ -70,7 +70,7 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Vec mismatch.actual_missing_nl = line_number_actual > actual_lines_count; line_number_actual += 1; } - diff::Result::Both(str, _) => { + diff::Result::Both(str1, str2) => { match ( line_number_expected > expected_lines_count, line_number_actual > actual_lines_count, @@ -78,16 +78,16 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Vec (true, false) => { line_number_expected += 1; line_number_actual += 1; - mismatch.expected.push(str.to_vec()); + mismatch.expected.push(str1.to_vec()); mismatch.expected_missing_nl = true; - mismatch.actual.push(str.to_vec()); + mismatch.actual.push(str2.to_vec()); } (false, true) => { line_number_expected += 1; line_number_actual += 1; - mismatch.actual.push(str.to_vec()); + mismatch.actual.push(str2.to_vec()); mismatch.actual_missing_nl = true; - mismatch.expected.push(str.to_vec()); + mismatch.expected.push(str1.to_vec()); } (true, true) | (false, false) => { line_number_expected += 1; @@ -121,7 +121,7 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { // See https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Normal.html // for details on the syntax of the normal format. let mut output = Vec::new(); - let diff_results = make_diff(expected, actual, params.brief); + let diff_results = make_diff(expected, actual, params.brief, params.ignore_case); if params.brief && !diff_results.is_empty() { write!(&mut output, "\0").unwrap(); return output; @@ -618,4 +618,22 @@ mod tests { ); assert!(nodiff_brief.is_empty()); } + + #[test] + fn test_ignore_case() { + let from = b"Hello World\nLine 2\n"; + let to = b"hello world\nline 2\n"; + let res = diff( + from, + to, + &Params { + ignore_case: true, + ..Default::default() + }, + ); + assert!(res.is_empty()); + + let res_diff = diff(from, to, &Params::default()); + assert!(!res_diff.is_empty()); + } } diff --git a/src/params.rs b/src/params.rs index 73ea45e0..81cab2ad 100644 --- a/src/params.rs +++ b/src/params.rs @@ -63,6 +63,7 @@ pub struct Params { /// The name from which to start comparing the top-level directories /// (`-S`). pub starting_file: Option, + pub ignore_case: bool, pub ignore_file_name_case: bool, pub no_dereference: bool, /// Set when `from` did not exist and was compared as an empty file. @@ -109,6 +110,7 @@ impl Default for Params { function_line_patterns: Vec::new(), recursive: false, starting_file: None, + ignore_case: false, ignore_file_name_case: false, no_dereference: false, from_missing: false, @@ -253,6 +255,14 @@ pub fn parse_params>( params.no_dereference = true; continue; } + if param == "-i" || param == "--ignore-case" { + params.ignore_case = true; + continue; + } + if param == "--no-ignore-case" { + params.ignore_case = false; + continue; + } if param == "--ignore-file-name-case" { params.ignore_file_name_case = true; continue; @@ -1487,6 +1497,24 @@ mod tests { ) .is_err()); } + #[test] + fn ignore_case() { + for (args, expected) in [ + (&[][..], false), + (&["-i"], true), + (&["--ignore-case"], true), + (&["--no-ignore-case"], false), + (&["-i", "--no-ignore-case"], false), + (&["--no-ignore-case", "-i"], true), + ] { + let args = std::iter::once("diff") + .chain(args.iter().copied()) + .chain(["foo", "bar"]); + let params = parse_params(args.map(os).peekable()).unwrap(); + assert_eq!(params.ignore_case, expected); + } + } + #[test] fn ignore_file_name_case() { // The last of --ignore-file-name-case and --no-ignore-file-name-case diff --git a/src/side_diff.rs b/src/side_diff.rs index faebfd19..2f38f771 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -9,6 +9,7 @@ use std::{io::Write, vec}; use unicode_width::UnicodeWidthStr; use crate::params::Params; +use crate::utils::diff_lines_inclusive; const GUTTER_WIDTH_MIN: usize = 3; @@ -316,15 +317,15 @@ pub fn diff( ) -> Vec { // ^ 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 mut left_lines = diff_lines_inclusive(from_file, params.ignore_case); + let mut right_lines = diff_lines_inclusive(to_file, params.ignore_case); let config = Config::new(params.width, params.tabsize, params.expand_tabs); - if left_lines.last() == Some(&&b""[..]) { + if left_lines.last().is_some_and(|l| l.raw.is_empty()) { left_lines.pop(); } - if right_lines.last() == Some(&&b""[..]) { + if right_lines.last().is_some_and(|l| l.raw.is_empty()) { right_lines.pop(); } @@ -352,10 +353,12 @@ pub fn diff( */ 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) => push_output(left_ln.raw, b"", b'<', output, &config).unwrap(), + Result::Right(right_ln) => { + push_output(b"", right_ln.raw, b'>', output, &config).unwrap() + } Result::Both(left_ln, right_ln) => { - push_output(left_ln, right_ln, b' ', output, &config).unwrap() + push_output(left_ln.raw, right_ln.raw, b' ', output, &config).unwrap() } } } diff --git a/src/unified_diff.rs b/src/unified_diff.rs index bd8ea43e..07b9c5d4 100644 --- a/src/unified_diff.rs +++ b/src/unified_diff.rs @@ -7,7 +7,7 @@ use std::collections::VecDeque; use std::io::Write; use crate::params::Params; -use crate::utils::{do_write_line, line_prefix}; +use crate::utils::{diff_lines, do_write_line, line_prefix}; use crate::utils::{get_modification_time, write_file_header, FunctionLineFinder, TimestampStyle}; #[derive(Debug, PartialEq)] @@ -41,6 +41,7 @@ fn make_diff( actual: &[u8], context_size: usize, stop_early: bool, + ignore_case: bool, ) -> Vec { let mut line_number_expected = 1; let mut line_number_actual = 1; @@ -52,19 +53,19 @@ fn make_diff( let mut results = Vec::new(); let mut mismatch = Mismatch::new(0, 0); - let mut expected_lines: Vec<&[u8]> = expected.split(|&c| c == b'\n').collect(); - let mut actual_lines: Vec<&[u8]> = actual.split(|&c| c == b'\n').collect(); + let mut expected_lines = diff_lines(expected, ignore_case); + let mut actual_lines = diff_lines(actual, ignore_case); debug_assert_eq!(b"".split(|&c| c == b'\n').count(), 1); // ^ means that underflow here is impossible let expected_lines_count = expected_lines.len() as u32 - 1; let actual_lines_count = actual_lines.len() as u32 - 1; - if expected_lines.last() == Some(&&b""[..]) { + if expected_lines.last().is_some_and(|l| l.raw.is_empty()) { expected_lines.pop(); } - if actual_lines.last() == Some(&&b""[..]) { + if actual_lines.last().is_some_and(|l| l.raw.is_empty()) { actual_lines.pop(); } @@ -128,7 +129,7 @@ fn make_diff( line_number_actual += 1; lines_since_mismatch = 0; } - diff::Result::Both(str, _) => { + diff::Result::Both(str1, str2) => { // if one of them is missing a newline and the other isn't, then they don't actually match if (line_number_actual > actual_lines_count) && (line_number_expected > expected_lines_count) @@ -136,7 +137,7 @@ fn make_diff( // this is the last line: lines still in the queue are past // the trailing context and there is no next hunk to lead into if lines_since_mismatch < context_size { - mismatch.lines.push(DiffLine::Context(str.to_vec())); + mismatch.lines.push(DiffLine::Context(str1.to_vec())); mismatch.lines.push(DiffLine::MissingNL); } } else if line_number_actual > actual_lines_count { @@ -151,8 +152,8 @@ fn make_diff( debug_assert!(mismatch.lines.last() != Some(&DiffLine::MissingNL)); mismatch.lines.push(DiffLine::Context(line.to_vec())); } - mismatch.lines.push(DiffLine::Expected(str.to_vec())); - mismatch.lines.push(DiffLine::Actual(str.to_vec())); + mismatch.lines.push(DiffLine::Expected(str1.to_vec())); + mismatch.lines.push(DiffLine::Actual(str2.to_vec())); mismatch.lines.push(DiffLine::MissingNL); lines_since_mismatch = 0; } else if line_number_expected > expected_lines_count { @@ -167,9 +168,9 @@ fn make_diff( debug_assert!(mismatch.lines.last() != Some(&DiffLine::MissingNL)); mismatch.lines.push(DiffLine::Context(line.to_vec())); } - mismatch.lines.push(DiffLine::Expected(str.to_vec())); + mismatch.lines.push(DiffLine::Expected(str1.to_vec())); mismatch.lines.push(DiffLine::MissingNL); - mismatch.lines.push(DiffLine::Actual(str.to_vec())); + mismatch.lines.push(DiffLine::Actual(str2.to_vec())); lines_since_mismatch = 0; } else { debug_assert!(context_queue.len() <= context_size); @@ -177,9 +178,9 @@ fn make_diff( let _ = context_queue.pop_front(); } if lines_since_mismatch < context_size { - mismatch.lines.push(DiffLine::Context(str.to_vec())); + mismatch.lines.push(DiffLine::Context(str1.to_vec())); } else if context_size > 0 { - context_queue.push_back(str); + context_queue.push_back(str1.raw); } lines_since_mismatch += 1; } @@ -262,7 +263,13 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { params.to_label.as_deref(), &to_modified_time, ); - let diff_results = make_diff(expected, actual, params.context_count, params.brief); + let diff_results = make_diff( + expected, + actual, + params.context_count, + params.brief, + params.ignore_case, + ); if diff_results.is_empty() { return Vec::new(); } diff --git a/src/utils.rs b/src/utils.rs index 8bd2d18b..d440650a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -462,10 +462,118 @@ pub mod testcmds { }; } +fn lowercase_key<'a>(raw: &'a [u8], ignore_case: bool, posix: bool) -> Cow<'a, [u8]> { + if !ignore_case { + return Cow::Borrowed(raw); + } + if posix { + return Cow::Owned(raw.to_ascii_lowercase()); + } + if let Ok(s) = std::str::from_utf8(raw) { + Cow::Owned(s.to_lowercase().into_bytes()) + } else { + Cow::Owned(raw.to_ascii_lowercase()) + } +} + +/// A reference to a line of text for diff comparison, optionally ignoring letter case. +#[derive(Debug)] +pub struct DiffLineRef<'a> { + pub raw: &'a [u8], + key: Cow<'a, [u8]>, +} + +impl<'a> DiffLineRef<'a> { + pub fn new(raw: &'a [u8], ignore_case: bool, posix: bool) -> Self { + Self { + raw, + key: lowercase_key(raw, ignore_case, posix), + } + } +} + +impl<'a> std::ops::Deref for DiffLineRef<'a> { + type Target = [u8]; + fn deref(&self) -> &Self::Target { + self.raw + } +} + +impl<'a> PartialEq for DiffLineRef<'a> { + fn eq(&self, other: &Self) -> bool { + self.key == other.key + } +} + +/// Splits `content` into lines wrapped for comparison according to `ignore_case`. +pub fn diff_lines<'a>(content: &'a [u8], ignore_case: bool) -> Vec> { + let posix = ignore_case && is_posix_locale("LC_CTYPE"); + content + .split(|&c| c == b'\n') + .map(|raw| DiffLineRef::new(raw, ignore_case, posix)) + .collect() +} + +/// Splits `content` into inclusive lines (preserving trailing newlines) wrapped for comparison. +pub fn diff_lines_inclusive<'a>(content: &'a [u8], ignore_case: bool) -> Vec> { + let posix = ignore_case && is_posix_locale("LC_CTYPE"); + content + .split_inclusive(|&c| c == b'\n') + .map(|raw| DiffLineRef::new(raw, ignore_case, posix)) + .collect() +} + #[cfg(test)] mod tests { use super::*; + mod ignore_case { + use super::*; + + #[test] + fn basics() { + assert_eq!( + lowercase_key(b"Funky Stuff", true, false), + lowercase_key(b"fUNKy stuFf", true, false) + ); + assert_eq!( + lowercase_key(b"hello", true, false), + lowercase_key(b"HELLO", true, false) + ); + assert_ne!( + lowercase_key(b"hello", true, false), + lowercase_key(b"world", true, false) + ); + assert_eq!( + lowercase_key(b"", true, false), + lowercase_key(b"", true, false) + ); + } + + #[test] + fn unicode_case_folding() { + let s1 = "AĀȀΆΑАӐḀἈA𐐀-Δ".as_bytes(); + let s2 = "aāȁάαаӑḁἀa𐐨-δ".as_bytes(); + assert_eq!( + lowercase_key(s1, true, false), + lowercase_key(s2, true, false) + ); + assert_ne!(lowercase_key(s1, true, true), lowercase_key(s2, true, true)); + } + + #[test] + fn non_utf8() { + assert_eq!( + lowercase_key(b"\xffa", true, false), + lowercase_key(b"\xffA", true, false) + ); + assert_ne!( + lowercase_key(b"\xffa", true, false), + lowercase_key(b"\xfeA", true, false) + ); + } + } + mod expand_tabs { use super::*; use pretty_assertions::assert_eq; diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 6173c199..f89f5b40 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -1131,3 +1131,74 @@ fn label_errors(#[case] args: &[&str], #[case] error: &str) { .fails_with_code(2) .stderr_contains(error); } + +#[test] +fn ignore_case() { + let (at, ts) = at_and_ts!(); + at.write("i1", "Funky Stuff\n"); + at.write("i2", "fUNKy stuFf\n"); + for option in ["-i", "--ignore-case"] { + ts.ucmd() + .args(&[option, "i1", "i2"]) + .succeeds() + .stdout_is(""); + } + // Without -i the files still differ. + ts.ucmd().args(&["i1", "i2"]).fails_with_code(1); + + // Overriding with --no-ignore-case + ts.ucmd() + .args(&["-i", "--no-ignore-case", "i1", "i2"]) + .fails_with_code(1); + + // Formats: unified, context, ed, side-by-side + ts.ucmd() + .args(&["-i", "-u", "i1", "i2"]) + .succeeds() + .stdout_is(""); + ts.ucmd() + .args(&["-i", "-c", "i1", "i2"]) + .succeeds() + .stdout_is(""); + ts.ucmd() + .args(&["-i", "-e", "i1", "i2"]) + .succeeds() + .stdout_is(""); + ts.ucmd().args(&["-i", "-y", "i1", "i2"]).succeeds(); + + // Unicode case folding: in UTF-8 locale, Unicode characters are folded + let (at, ts) = at_and_ts!(); + at.write("u1", "AĀȀΆΑАӐḀἈA𐐀-Δ\n"); + at.write("u2", "aāȁάαаӑḁἀa𐐨-δ\n"); + ts.ucmd() + .env("LC_ALL", "en_US.UTF-8") + .args(&["-i", "u1", "u2"]) + .succeeds() + .stdout_is(""); + + // In POSIX locale (LC_ALL=C), GNU diff only folds ASCII, so Unicode characters differ + ts.ucmd() + .env("LC_ALL", "C") + .args(&["-i", "u1", "u2"]) + .fails_with_code(1); + ts.ucmd().args(&["u1", "u2"]).fails_with_code(1); +} + +#[test] +fn ignore_case_context_lines() { + let (at, ts) = at_and_ts!(); + at.write("c1", "COMMON LINE\ndiff1\n"); + at.write("c2", "common line\ndiff2\n"); + + // Unified diff: common line differing only in case is emitted as context from from-file + ts.ucmd() + .args(&["-i", "-u", "c1", "c2"]) + .fails_with_code(1) + .stdout_contains(" COMMON LINE\n-diff1\n+diff2\n"); + + // Context diff: from-file hunk shows COMMON LINE, to-file hunk shows common line + ts.ucmd() + .args(&["-i", "-c", "c1", "c2"]) + .fails_with_code(1) + .stdout_contains(" COMMON LINE\n! diff1\n--- 1,2 ----\n common line\n! diff2\n"); +}