From 81a271b41e61bbad5f3a3285078e551aee97327b Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 7 Nov 2026 13:13:47 -1000 Subject: Check what each side-by-side cell holds The span checks compared page totals, so a span on the wrong side or over the wrong characters passed while the totals agreed. A fill that read cells a longer pair left behind passed as well, since only a literal pair after a longer one shows it. Nothing checked the cell classes the forced-colours border keys on, or the rows for unpaired, overlong, tabbed, binary and cut short lines. --- tests/t0107-diff.sh | 124 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 106 insertions(+), 18 deletions(-) diff --git a/tests/t0107-diff.sh b/tests/t0107-diff.sh index 861ca44..f069894 100755 --- a/tests/t0107-diff.sh +++ b/tests/t0107-diff.sh @@ -114,25 +114,113 @@ test_expect_success 'set up a repo with shrinking changed lines' ' cgitrc_repo lcsrc lcs ' +# Each side of a row is written on a line of its own, the old side first and +# the new side closing the row. test_expect_success 'every changed pair gets both a del and an add span' ' - cgit_rc lcsrc "url=lcs/diff/&dt=1" >tmp && - deletions=$(grep -o "" tmp | wc -l) && - additions=$(grep -o "" tmp | wc -l) && - test "$deletions" -gt 0 && - test "$deletions" -eq "$additions" -' - -test_expect_success 'stripping the highlight leaves every line intact' ' - sed -e "s/<[^>]*>//g" tmp >plain.out && - git -C repos/lcs show HEAD~1:lines.txt >lines && - git -C repos/lcs show HEAD:lines.txt >>lines && - while read -r line - do - grep -qF "$line" plain.out || { - echo "missing line: $line" - return 1 - } - done tmp && + test $(grep -c "^.*.*.*$" tmp) -eq 40 && + test $(grep -c "^.*.*.*$" tmp) -eq 40 +' + +test_expect_success 'stripping the highlight leaves each side its own lines' ' + cgit_ok lcsrc "url=lcs/diff/&dt=1" >tmp && + sed -n "s/^]*>[0-9]*<\/a><\/td>\(.*\)<\/td>$/\1/p" tmp | + sed "s/<[^>]*>//g" >old.out && + sed -n "s/^]*>[0-9]*<\/a><\/td>\(.*\)<\/td><\/tr>$/\1/p" tmp | + sed "s/<[^>]*>//g" >new.out && + git -C repos/lcs show HEAD~1:lines.txt >expect && + test_cmp expect old.out && + git -C repos/lcs show HEAD:lines.txt >expect && + test_cmp expect new.out +' + +# Comparing the second pair below reads cells the first one wrote, and a cell +# left over from it would tip a tie there and mark the wrong characters. +test_expect_success 'a short pair after a longer one marks its own characters' ' + mkrepo repos/pair 1 && + ( + cd repos/pair && + test_write_lines bcde xa >pair.txt && + git add pair.txt && + git commit -m "add pair.txt" && + test_write_lines abcd ax >pair.txt && + git commit -am "change pair.txt" + ) && + cgitrc_repo pairrc pair && + cgit_ok pairrc "url=pair/diff/&dt=1" >tmp && + sed -n "s/^]*>[0-9]*<\/a><\/td>//p" tmp | tr -d "\047" >actual && + cat >expect <<-\EOF && + bcde + abcd + xa + ax + EOF + test_cmp expect actual +' + +# A file added or deleted fills one side, leaving the other a darker blank, +# and a changed pair with nothing in common is marked whole. Forced colours +# drop the backgrounds, so those cells keep a thicker border instead. +test_expect_success 'each side of a row is classed by what it holds' ' + cgit_ok_url "foo/diff&dt=1" >tmp && + grep -x "" tmp && + grep "5" tmp && + cgit_ok hunk1rc "url=hunk1/diff/&dt=1" >tmp && + grep "only$" tmp && + grep "ONLY$" tmp +' + +test_expect_success 'forced colours mark the changed cells with a border' ' + sed -n "/^@media (forced-colors: active) {$/,/^}/p" \ + "$TEST_OUTPUT_DIRECTORY/../assets/cgit.css" >rule && + sed -n "/^ div#cgit table.ssdiff td.add,$/,/^ }$/p" rule >cells && + grep "^ div#cgit table.ssdiff td.del,$" cells && + grep "^ div#cgit table.ssdiff td.changed {$" cells && + grep "^ border-left-width: 4px;$" cells +' + +# Removals and additions that do not pair off are shown without marking, +# with a dark blank where a side runs out. A line past the comparison's +# limit is shown whole, a tab is expanded, and a binary file says so. +test_expect_success 'set up a commit for the remaining side-by-side rows' ' + mkrepo repos/cells 1 && + ( + cd repos/cells && + test_write_lines gone >gone.txt && + test_write_lines one two keep >uneven.txt && + printf "%0130d\n" 0 >long.txt && + printf "a\tb\nx\n" >tab.txt && + printf "\000old" >bin.dat && + git add . && + git commit -m "add the files" && + git rm -q gone.txt && + test_write_lines ONE keep >uneven.txt && + printf "%0129d1\n" 0 >long.txt && + printf "a\tb\ny\n" >tab.txt && + printf "\000new" >bin.dat && + git commit -am "change the files" + ) && + cgitrc_repo cellsrc cells +' + +test_expect_success 'the remaining side-by-side rows' ' + cgit_ok cellsrc "url=cells/diff/&dt=1" >tmp && + grep "gone$" tmp && + grep -x "" tmp && + grep "one$" tmp && + grep "ONE$" tmp && + grep "two$" tmp && + grep -x "" tmp && + grep "$(printf "%0130d" 0)$" tmp && + grep "$(printf "%0129d1" 0)$" tmp && + grep "a b$" tmp && + grep "Binary files differ" tmp +' + +test_expect_success 'a side-by-side diff past max-diff-lines is cut short' ' + cgitrc_repo cellscaprc cells max-diff-lines=1 && + cgit_ok cellscaprc "url=cells/diff/&dt=1" >tmp && + grep "This diff is too large to be rendered inline. View it on its own page." tmp ' # The diffstat links a renamed file by its new name, and so does the log of -- cgit 2.12.1