Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 5 additions & 10 deletions src/unified_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
48 changes: 48 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,3 +1237,51 @@ fn extra_operand_without_double_dash() -> Result<(), Box<dyn std::error::Error>>
.stderr("diff: extra operand 'extra'\ndiff: Try 'diff --help' for more information.\n");
Ok(())
}

#[test]
fn trailing_context_without_final_newlines() -> Result<(), Box<dyn std::error::Error>> {
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")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please also cover -C5 / -C6? only the unified side is tested at the boundary
and it could be a loop like the one above, no?

.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(())
}
Loading