Ignore leading and trailing blank lines for comparison
```git-check-assertions run test/mechanicaldiff.bats assert_success git checkout HEAD~ bin run test/mechanicaldiff.bats assert_failure assert_output --partial "-- command failed --" ```
This commit is contained in:
parent
3d814b5b15
commit
0b3d0d8f46
2 changed files with 29 additions and 1 deletions
|
|
@ -4,6 +4,16 @@ import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def trim_blank_ends(lines: list[str]) -> list[str]:
|
||||||
|
start = 0
|
||||||
|
end = len(lines)
|
||||||
|
while start < end and lines[start] == "\n":
|
||||||
|
start += 1
|
||||||
|
while end > start and lines[end - 1] == "\n":
|
||||||
|
end -= 1
|
||||||
|
return lines[start:end]
|
||||||
|
|
||||||
|
|
||||||
def filter_change_block(
|
def filter_change_block(
|
||||||
change_lines: list[str], search: str, replace: str
|
change_lines: list[str], search: str, replace: str
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
|
|
@ -27,7 +37,7 @@ def filter_change_block(
|
||||||
transformed_removed = [
|
transformed_removed = [
|
||||||
re.sub(search, replace, line) for line in removed_lines
|
re.sub(search, replace, line) for line in removed_lines
|
||||||
]
|
]
|
||||||
if transformed_removed == added_lines:
|
if trim_blank_ends(transformed_removed) == trim_blank_ends(added_lines):
|
||||||
return change_lines
|
return change_lines
|
||||||
|
|
||||||
if len(removed_lines) != len(added_lines):
|
if len(removed_lines) != len(added_lines):
|
||||||
|
|
|
||||||
|
|
@ -143,6 +143,24 @@ setup() {
|
||||||
assert_output "$(cat "$BATS_TEST_TMPDIR/diff")"
|
assert_output "$(cat "$BATS_TEST_TMPDIR/diff")"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "ignores leading and trailing blank lines for comparison" {
|
||||||
|
cat >"$BATS_TEST_TMPDIR/old" <<-'EOF'
|
||||||
|
|
||||||
|
prefix foo suffix
|
||||||
|
|
||||||
|
EOF
|
||||||
|
cat >"$BATS_TEST_TMPDIR/new" <<-'EOF'
|
||||||
|
prefix bar suffix
|
||||||
|
EOF
|
||||||
|
|
||||||
|
git diff --no-index "$BATS_TEST_TMPDIR/old" "$BATS_TEST_TMPDIR/new" \
|
||||||
|
>"$BATS_TEST_TMPDIR/diff" || true
|
||||||
|
|
||||||
|
run mechanicaldiff.py foo bar <"$BATS_TEST_TMPDIR/diff"
|
||||||
|
assert_success
|
||||||
|
assert_output "$(cat "$BATS_TEST_TMPDIR/diff")"
|
||||||
|
}
|
||||||
|
|
||||||
@test "keeps only matching line in a consecutive change block" {
|
@test "keeps only matching line in a consecutive change block" {
|
||||||
cat >"$BATS_TEST_TMPDIR/old" <<-'EOF'
|
cat >"$BATS_TEST_TMPDIR/old" <<-'EOF'
|
||||||
foo
|
foo
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue