diff --git a/src/context_diff.rs b/src/context_diff.rs index 0762bcb8..86f12857 100644 --- a/src/context_diff.rs +++ b/src/context_diff.rs @@ -181,19 +181,14 @@ fn make_diff( && (line_number_expected > expected_lines_count) { ChangeBlock::close(&mut block, &mut mismatch); - if context_queue.len() < context_size { - while let Some(line) = context_queue.pop_front() { - mismatch.expected.push(DiffLine::Context(line.to_vec())); - mismatch.actual.push(DiffLine::Context(line.to_vec())); - } - if lines_since_mismatch < context_size { - mismatch.expected.push(DiffLine::Context(str.to_vec())); - mismatch.actual.push(DiffLine::Context(str.to_vec())); - mismatch.expected_missing_nl = true; - mismatch.actual_missing_nl = true; - } + // 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_missing_nl = true; + mismatch.actual_missing_nl = true; } - lines_since_mismatch = 0; } else if line_number_actual > actual_lines_count { if lines_since_mismatch > max_gap { results.push(mismatch); diff --git a/src/unified_diff.rs b/src/unified_diff.rs index 28eaf8f9..14f4384b 100644 --- a/src/unified_diff.rs +++ b/src/unified_diff.rs @@ -133,17 +133,12 @@ fn make_diff( if (line_number_actual > actual_lines_count) && (line_number_expected > expected_lines_count) { - if context_queue.len() < context_size { - while let Some(line) = context_queue.pop_front() { - debug_assert!(mismatch.lines.last() != Some(&DiffLine::MissingNL)); - mismatch.lines.push(DiffLine::Context(line.to_vec())); - } - if lines_since_mismatch < context_size { - mismatch.lines.push(DiffLine::Context(str.to_vec())); - mismatch.lines.push(DiffLine::MissingNL); - } + // 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::MissingNL); } - lines_since_mismatch = 0; } else if line_number_actual > actual_lines_count { if lines_since_mismatch > max_gap { results.push(mismatch); diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 2f5b5dd9..337ad397 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -1237,3 +1237,51 @@ fn extra_operand_without_double_dash() -> Result<(), Box> .stderr("diff: extra operand 'extra'\ndiff: Try 'diff --help' for more information.\n"); Ok(()) } + +#[test] +fn trailing_context_without_final_newlines() -> Result<(), Box> { + let mut file1 = NamedTempFile::new()?; + file1.write_all(b"1\n2\n3\n4\n5\n6\n7\n8")?; + let mut file2 = NamedTempFile::new()?; + file2.write_all(b"1\nX\n3\n4\n5\n6\n7\n8")?; + + for (format, hunk) in [ + ("-u", "@@ -1,5 +1,5 @@\n 1\n-2\n+X\n 3\n 4\n 5\n"), + ( + "-c", + "*** 1,5 ****\n 1\n! 2\n 3\n 4\n 5\n--- 1,5 ----\n 1\n! X\n 3\n 4\n 5\n", + ), + ] { + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.arg("diff") + .arg(format) + .arg(file1.path()) + .arg(file2.path()); + cmd.assert() + .code(predicate::eq(1)) + .stdout(predicate::str::ends_with(hunk)); + } + + // the last line is still shown when it is within the trailing context + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.arg("diff") + .arg("-U5") + .arg(file1.path()) + .arg(file2.path()); + cmd.assert() + .code(predicate::eq(1)) + .stdout(predicate::str::ends_with( + "@@ -1,7 +1,7 @@\n 1\n-2\n+X\n 3\n 4\n 5\n 6\n 7\n", + )); + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.arg("diff") + .arg("-U6") + .arg(file1.path()) + .arg(file2.path()); + cmd.assert() + .code(predicate::eq(1)) + .stdout(predicate::str::ends_with( + "@@ -1,8 +1,8 @@\n 1\n-2\n+X\n 3\n 4\n 5\n 6\n 7\n 8\n\\ No newline at end of file\n", + )); + Ok(()) +}