Skip to content
Open
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
50 changes: 26 additions & 24 deletions src/unified_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ impl Mismatch {
lines: Vec::new(),
}
}

/// Pushes an `Expected` deletion line before any trailing `Actual` insertion
/// lines in the current change, so deletions always precede insertions.
fn push_expected(&mut self, line: Vec<u8>, missing_nl: bool) {
let mut insert_pos = self.lines.len();
while insert_pos > 0 {
if matches!(self.lines[insert_pos - 1], DiffLine::Actual(_)) {
insert_pos -= 1;
} else if self.lines[insert_pos - 1] == DiffLine::MissingNL
&& insert_pos >= 2
&& matches!(self.lines[insert_pos - 2], DiffLine::Actual(_))
{
insert_pos -= 2;
} else {
break;
}
}
self.lines.insert(insert_pos, DiffLine::Expected(line));
if missing_nl {
self.lines.insert(insert_pos + 1, DiffLine::MissingNL);
}
}
}

// Produces a diff between the expected output and actual output.
Expand Down Expand Up @@ -83,27 +105,8 @@ fn make_diff(
mismatch.lines.push(DiffLine::Context(line.to_vec()));
}

if mismatch.lines.last() == Some(&DiffLine::MissingNL) {
mismatch.lines.pop();
match mismatch.lines.pop() {
Some(DiffLine::Actual(res)) => {
// We have to make sure that Actual (the + lines)
// always come after Expected (the - lines)
mismatch.lines.push(DiffLine::Expected(str.to_vec()));
if line_number_expected > expected_lines_count {
mismatch.lines.push(DiffLine::MissingNL);
}
mismatch.lines.push(DiffLine::Actual(res));
mismatch.lines.push(DiffLine::MissingNL);
}
_ => unreachable!("unterminated Left and Common lines shouldn't be followed by more Left lines"),
}
} else {
mismatch.lines.push(DiffLine::Expected(str.to_vec()));
if line_number_expected > expected_lines_count {
mismatch.lines.push(DiffLine::MissingNL);
}
}
let missing_nl = line_number_expected > expected_lines_count;
mismatch.push_expected(str.to_vec(), missing_nl);
line_number_expected += 1;
lines_since_mismatch = 0;
}
Expand Down Expand Up @@ -151,7 +154,7 @@ 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.push_expected(str.to_vec(), false);
mismatch.lines.push(DiffLine::Actual(str.to_vec()));
mismatch.lines.push(DiffLine::MissingNL);
lines_since_mismatch = 0;
Expand All @@ -167,8 +170,7 @@ 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::MissingNL);
mismatch.push_expected(str.to_vec(), true);
mismatch.lines.push(DiffLine::Actual(str.to_vec()));
lines_since_mismatch = 0;
} else {
Expand Down
22 changes: 22 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,3 +1069,25 @@ fn label_errors(#[case] args: &[&str], #[case] error: &str) {
.fails_with_code(2)
.stderr_contains(error);
}

#[test]
fn unified_missing_newline_deletions_first() {
let (at, ts) = at_and_ts!();
at.write("n1", "1\n");
at.write("n2", "0\n1");
ts.ucmd()
.args(&["-u", "n1", "n2"])
.fails_with_code(1)
.stdout_str_check(|s| {
s.ends_with("@@ -1 +1,2 @@\n-1\n+0\n+1\n\\ No newline at end of file\n")
});

at.write("n3", "1");
at.write("n4", "0\n1\n");
ts.ucmd()
.args(&["-u", "n3", "n4"])
.fails_with_code(1)
.stdout_str_check(|s| {
s.ends_with("@@ -1 +1,2 @@\n-1\n\\ No newline at end of file\n+0\n+1\n")
});
}
Loading