|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Test lsdiff exclusion mode for --lines and --hunks options |
| 4 | +# Tests the 'x' prefix syntax for excluding ranges |
| 5 | + |
| 6 | +. ${top_srcdir-.}/tests/common.sh |
| 7 | + |
| 8 | +# Create test patch with files having hunks at known line ranges |
| 9 | +cat << EOF > test.diff |
| 10 | +--- file1 |
| 11 | ++++ file1 |
| 12 | +@@ -1,2 +1,3 @@ |
| 13 | + line1 |
| 14 | ++added |
| 15 | + line2 |
| 16 | +@@ -10,2 +11,3 @@ |
| 17 | + line10 |
| 18 | ++added |
| 19 | + line11 |
| 20 | +--- file2 |
| 21 | ++++ file2 |
| 22 | +@@ -5,2 +5,3 @@ |
| 23 | + line5 |
| 24 | ++added |
| 25 | + line6 |
| 26 | +@@ -15,2 +16,3 @@ |
| 27 | + line15 |
| 28 | ++added |
| 29 | + line16 |
| 30 | +@@ -25,2 +27,3 @@ |
| 31 | + line25 |
| 32 | ++added |
| 33 | + line26 |
| 34 | +--- file3 |
| 35 | ++++ file3 |
| 36 | +@@ -8,2 +8,3 @@ |
| 37 | + line8 |
| 38 | ++added |
| 39 | + line9 |
| 40 | +--- file4 |
| 41 | ++++ file4 |
| 42 | +@@ -20,2 +20,3 @@ |
| 43 | + line20 |
| 44 | ++added |
| 45 | + line21 |
| 46 | +@@ -30,2 +31,3 @@ |
| 47 | + line30 |
| 48 | ++added |
| 49 | + line31 |
| 50 | +EOF |
| 51 | + |
| 52 | +# Test 1: Exclude files with hunks touching lines 1-10 (--lines x1-10) |
| 53 | +# file1: has hunks at lines 1,10 -> EXCLUDED |
| 54 | +# file2: has hunks at lines 5,15,25 -> line 5 in excluded range -> EXCLUDED |
| 55 | +# file3: has hunk at line 8 -> EXCLUDED |
| 56 | +# file4: has hunks at lines 20,30 -> NOT EXCLUDED |
| 57 | +${LSDIFF} --lines x1-10 test.diff 2>errors1 >result1 || exit 1 |
| 58 | +[ -s errors1 ] && exit 1 |
| 59 | + |
| 60 | +cat << EOF | cmp - result1 || exit 1 |
| 61 | +file4 |
| 62 | +EOF |
| 63 | + |
| 64 | +# Test 2: Exclude files with hunks touching specific line 15 (--lines x15) |
| 65 | +# file1: no hunks at line 15 -> NOT EXCLUDED |
| 66 | +# file2: has hunk at line 15 -> EXCLUDED |
| 67 | +# file3: no hunks at line 15 -> NOT EXCLUDED |
| 68 | +# file4: no hunks at line 15 -> NOT EXCLUDED |
| 69 | +${LSDIFF} --lines x15 test.diff 2>errors2 >result2 || exit 1 |
| 70 | +[ -s errors2 ] && exit 1 |
| 71 | + |
| 72 | +cat << EOF | cmp - result2 || exit 1 |
| 73 | +file1 |
| 74 | +file3 |
| 75 | +file4 |
| 76 | +EOF |
| 77 | + |
| 78 | +# Test 3: Exclude files with hunk #2 (--hunks x2) |
| 79 | +# file1: has 2 hunks -> has hunk #2 -> EXCLUDED |
| 80 | +# file2: has 3 hunks -> has hunk #2 -> EXCLUDED |
| 81 | +# file3: has 1 hunk -> no hunk #2 -> NOT EXCLUDED |
| 82 | +# file4: has 2 hunks -> has hunk #2 -> EXCLUDED |
| 83 | +${LSDIFF} --hunks x2 test.diff 2>errors3 >result3 || exit 1 |
| 84 | +[ -s errors3 ] && exit 1 |
| 85 | + |
| 86 | +cat << EOF | cmp - result3 || exit 1 |
| 87 | +file3 |
| 88 | +EOF |
| 89 | + |
| 90 | +# Test 4: Exclude files with hunk #1 (--hunks x1) - should exclude all files |
| 91 | +# All files have at least hunk #1 |
| 92 | +${LSDIFF} --hunks x1 test.diff 2>errors4 >result4 || exit 1 |
| 93 | +[ -s errors4 ] && exit 1 |
| 94 | +[ -s result4 ] && exit 1 # Should be empty |
| 95 | + |
| 96 | +# Test 5: Exclude files with hunks in range 2-3 (--hunks x2-3) |
| 97 | +# file1: has hunks 1,2 -> has hunk 2 -> EXCLUDED |
| 98 | +# file2: has hunks 1,2,3 -> has hunks 2,3 -> EXCLUDED |
| 99 | +# file3: has hunk 1 -> no hunks 2-3 -> NOT EXCLUDED |
| 100 | +# file4: has hunks 1,2 -> has hunk 2 -> EXCLUDED |
| 101 | +${LSDIFF} --hunks x2-3 test.diff 2>errors5 >result5 || exit 1 |
| 102 | +[ -s errors5 ] && exit 1 |
| 103 | + |
| 104 | +cat << EOF | cmp - result5 || exit 1 |
| 105 | +file3 |
| 106 | +EOF |
| 107 | + |
| 108 | +# Test 6: Combination exclusion - exclude lines 1-5 AND hunk 3 |
| 109 | +# Only files that have BOTH excluded criteria should be excluded |
| 110 | +# file1: lines 1,10 (line 1 in excluded range) AND no hunk 3 -> EXCLUDED (lines) |
| 111 | +# file2: lines 5,15,25 (line 5 in excluded range) AND has hunk 3 -> EXCLUDED (both) |
| 112 | +# file3: line 8 (not in excluded range) AND no hunk 3 -> NOT EXCLUDED |
| 113 | +# file4: lines 20,30 (not in excluded range) AND no hunk 3 -> NOT EXCLUDED |
| 114 | +${LSDIFF} --lines x1-5 --hunks x3 test.diff 2>errors6 >result6 || exit 1 |
| 115 | +[ -s errors6 ] && exit 1 |
| 116 | + |
| 117 | +cat << EOF | cmp - result6 || exit 1 |
| 118 | +file3 |
| 119 | +file4 |
| 120 | +EOF |
| 121 | + |
| 122 | +# Test 7: Test exclusion with --files range |
| 123 | +# Exclude files #1-2, then apply line exclusion |
| 124 | +${LSDIFF} --files x1-2 --lines x20-30 test.diff 2>errors7 >result7 || exit 1 |
| 125 | +[ -s errors7 ] && exit 1 |
| 126 | + |
| 127 | +# file1,file2 excluded by --files x1-2 |
| 128 | +# file3: not excluded by files, line 8 not in x20-30 -> NOT EXCLUDED |
| 129 | +# file4: not excluded by files, lines 20,30 in x20-30 -> EXCLUDED |
| 130 | +cat << EOF | cmp - result7 || exit 1 |
| 131 | +file3 |
| 132 | +EOF |
| 133 | + |
| 134 | +# Test 8: Test exclusion with status display |
| 135 | +${LSDIFF} -s --lines x1-10 test.diff 2>errors8 >result8 || exit 1 |
| 136 | +[ -s errors8 ] && exit 1 |
| 137 | + |
| 138 | +cat << EOF | cmp - result8 || exit 1 |
| 139 | +! file4 |
| 140 | +EOF |
| 141 | + |
| 142 | +# Test 9: Complex exclusion test with empty ranges |
| 143 | +# Create a patch where exclusion results in no matches |
| 144 | +${LSDIFF} --lines x1-100 test.diff 2>errors9 >result9 || exit 1 |
| 145 | +[ -s errors9 ] && exit 1 |
| 146 | +[ -s result9 ] && exit 1 # Should be empty - all files excluded |
| 147 | + |
| 148 | +# Test 10: Test exclusion range parsing edge cases |
| 149 | +# Exclude single hunk number |
| 150 | +${LSDIFF} --hunks x1 test.diff 2>errors10 >result10 || exit 1 |
| 151 | +[ -s errors10 ] && exit 1 |
| 152 | +[ -s result10 ] && exit 1 # Should be empty |
| 153 | + |
| 154 | +# Test 11: Exclude open-ended range (x10-) |
| 155 | +# file1: lines 1,10 -> line 10 in x10- -> EXCLUDED |
| 156 | +# file2: lines 5,15,25 -> lines 15,25 in x10- -> EXCLUDED |
| 157 | +# file3: line 8 -> not in x10- -> NOT EXCLUDED |
| 158 | +# file4: lines 20,30 -> both in x10- -> EXCLUDED |
| 159 | +${LSDIFF} --lines x10- test.diff 2>errors11 >result11 || exit 1 |
| 160 | +[ -s errors11 ] && exit 1 |
| 161 | + |
| 162 | +cat << EOF | cmp - result11 || exit 1 |
| 163 | +file3 |
| 164 | +EOF |
| 165 | + |
| 166 | +echo "✓ All exclusion mode tests passed" |
| 167 | +exit 0 |
0 commit comments