diff --git a/src/unified_diff.rs b/src/unified_diff.rs index bd8ea43e..3045f160 100644 --- a/src/unified_diff.rs +++ b/src/unified_diff.rs @@ -33,6 +33,15 @@ impl Mismatch { lines: Vec::new(), } } + + /// Returns the index where expected (deletion) lines should be inserted so that + /// all deletions in the current change block precede insertions. + fn expected_insert_pos(&self) -> usize { + self.lines + .iter() + .rposition(|line| !matches!(line, DiffLine::Actual(_) | DiffLine::MissingNL)) + .map_or(0, |pos| pos + 1) + } } // Produces a diff between the expected output and actual output. @@ -83,26 +92,12 @@ 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 insert_pos = mismatch.expected_insert_pos(); + mismatch + .lines + .insert(insert_pos, DiffLine::Expected(str.to_vec())); + if line_number_expected > expected_lines_count { + mismatch.lines.insert(insert_pos + 1, DiffLine::MissingNL); } line_number_expected += 1; lines_since_mismatch = 0; @@ -151,7 +146,10 @@ 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())); + let insert_pos = mismatch.expected_insert_pos(); + mismatch + .lines + .insert(insert_pos, DiffLine::Expected(str.to_vec())); mismatch.lines.push(DiffLine::Actual(str.to_vec())); mismatch.lines.push(DiffLine::MissingNL); lines_since_mismatch = 0; @@ -167,8 +165,11 @@ 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); + let insert_pos = mismatch.expected_insert_pos(); + mismatch + .lines + .insert(insert_pos, DiffLine::Expected(str.to_vec())); + mismatch.lines.insert(insert_pos + 1, DiffLine::MissingNL); mismatch.lines.push(DiffLine::Actual(str.to_vec())); lines_since_mismatch = 0; } else { diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 6173c199..0e1c1017 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -71,6 +71,25 @@ fn missing_newline() { .stderr_str_check(|s| s.starts_with("No newline at end of file")); } +#[test] +fn unified_missing_newline_deletions_first() { + let (at, ts) = at_and_ts!(); + at.write("n1", "1\n"); + at.write("n2", "0\n1"); + at.write("n3", "1"); + at.write("n4", "0\n1\n"); + + ts.ucmd() + .args(&["-u", "n1", "n2"]) + .fails_with_code(1) + .stdout_contains("@@ -1 +1,2 @@\n-1\n+0\n+1\n\\ No newline at end of file\n"); + + ts.ucmd() + .args(&["-u", "n3", "n4"]) + .fails_with_code(1) + .stdout_contains("@@ -1 +1,2 @@\n-1\n\\ No newline at end of file\n+0\n+1\n"); +} + #[test] fn huge_tabsize() { new_ucmd!()