|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Test runner for scanner_debug utility tests |
| 4 | +# This script must be run via 'make check' to ensure proper environment setup |
| 5 | + |
| 6 | +# Check that we're running in the proper test environment |
| 7 | +if [ -z "$top_srcdir" ] || [ -z "$top_builddir" ]; then |
| 8 | + echo "Error: This test must be run via 'make check'" |
| 9 | + echo "The top_srcdir and top_builddir variables must be set by the build system" |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +# Convert top_srcdir to absolute path before common.sh changes working directory |
| 14 | +top_srcdir="$(cd "$top_srcdir" && pwd)" |
| 15 | + |
| 16 | +# Source the common test environment |
| 17 | +. "$top_srcdir/tests/common.sh" |
| 18 | + |
| 19 | +# Set up scanner_debug binary path |
| 20 | +SCANNER_DEBUG="$top_builddir/src/scanner_debug" |
| 21 | + |
| 22 | +# Check if scanner_debug exists |
| 23 | +if [ ! -x "$SCANNER_DEBUG" ]; then |
| 24 | + echo "Error: scanner_debug binary not found at $SCANNER_DEBUG" |
| 25 | + echo "Make sure to build with --enable-scanner-lsdiff" |
| 26 | + exit 77 # Skip test |
| 27 | +fi |
| 28 | + |
| 29 | +# Test counter |
| 30 | +test_count=0 |
| 31 | +failed_tests=0 |
| 32 | + |
| 33 | +# Helper function to run a test |
| 34 | +run_test() { |
| 35 | + local test_name="$1" |
| 36 | + local test_description="$2" |
| 37 | + shift 2 |
| 38 | + |
| 39 | + test_count=$((test_count + 1)) |
| 40 | + echo "Test $test_count: $test_description" |
| 41 | + |
| 42 | + if "$@"; then |
| 43 | + echo "✓ $test_name passed" |
| 44 | + else |
| 45 | + echo "✗ $test_name failed" |
| 46 | + failed_tests=$((failed_tests + 1)) |
| 47 | + fi |
| 48 | + echo |
| 49 | +} |
| 50 | + |
| 51 | +# Test 1: Basic help functionality |
| 52 | +test_help() { |
| 53 | + "$SCANNER_DEBUG" --help >/dev/null 2>&1 |
| 54 | +} |
| 55 | + |
| 56 | +# Test 2: Basic functionality with simple patch |
| 57 | +test_simple_patch() { |
| 58 | + cat > simple.patch << 'EOF' |
| 59 | +--- old.txt 2024-01-01 12:00:00.000000000 +0000 |
| 60 | ++++ new.txt 2024-01-01 12:00:01.000000000 +0000 |
| 61 | +@@ -1,3 +1,3 @@ |
| 62 | + line1 |
| 63 | +-old line |
| 64 | ++new line |
| 65 | + line3 |
| 66 | +EOF |
| 67 | + "$SCANNER_DEBUG" simple.patch >/dev/null 2>&1 |
| 68 | +} |
| 69 | + |
| 70 | +# Test 3: Stdin input |
| 71 | +test_stdin_input() { |
| 72 | + cat > stdin.patch << 'EOF' |
| 73 | +--- a.txt |
| 74 | ++++ b.txt |
| 75 | +@@ -1 +1 @@ |
| 76 | +-old |
| 77 | ++new |
| 78 | +EOF |
| 79 | + "$SCANNER_DEBUG" < stdin.patch >/dev/null 2>&1 |
| 80 | +} |
| 81 | + |
| 82 | +# Test 4: Verbose output |
| 83 | +test_verbose_output() { |
| 84 | + cat > verbose.patch << 'EOF' |
| 85 | +--- file.txt |
| 86 | ++++ file.txt |
| 87 | +@@ -1,2 +1,2 @@ |
| 88 | + context |
| 89 | +-removed |
| 90 | ++added |
| 91 | +EOF |
| 92 | + "$SCANNER_DEBUG" --verbose verbose.patch | grep -q "HEADERS" |
| 93 | +} |
| 94 | + |
| 95 | +# Test 5: Content option |
| 96 | +test_content_option() { |
| 97 | + cat > content.patch << 'EOF' |
| 98 | +--- test.txt |
| 99 | ++++ test.txt |
| 100 | +@@ -1 +1 @@ |
| 101 | +-old content |
| 102 | ++new content |
| 103 | +EOF |
| 104 | + "$SCANNER_DEBUG" -v -c content.patch | grep -q "Content:" |
| 105 | +} |
| 106 | + |
| 107 | +# Test 6: Positions option |
| 108 | +test_positions_option() { |
| 109 | + cat > positions.patch << 'EOF' |
| 110 | +--- pos.txt |
| 111 | ++++ pos.txt |
| 112 | +@@ -1 +1 @@ |
| 113 | +-old |
| 114 | ++new |
| 115 | +EOF |
| 116 | + "$SCANNER_DEBUG" -v -p positions.patch | grep -q "pos" |
| 117 | +} |
| 118 | + |
| 119 | +# Test 7: Color output (check it doesn't crash) |
| 120 | +test_color_output() { |
| 121 | + cat > color.patch << 'EOF' |
| 122 | +--- color.txt |
| 123 | ++++ color.txt |
| 124 | +@@ -1 +1 @@ |
| 125 | +-old |
| 126 | ++new |
| 127 | +EOF |
| 128 | + "$SCANNER_DEBUG" --color color.patch >/dev/null 2>&1 |
| 129 | +} |
| 130 | + |
| 131 | +# Test 8: Git extended patch |
| 132 | +test_git_patch() { |
| 133 | + cat > git.patch << 'EOF' |
| 134 | +diff --git a/file.txt b/file.txt |
| 135 | +index abc123..def456 100644 |
| 136 | +--- a/file.txt |
| 137 | ++++ b/file.txt |
| 138 | +@@ -1,3 +1,4 @@ |
| 139 | + line 1 |
| 140 | + line 2 |
| 141 | ++added line |
| 142 | + line 3 |
| 143 | +EOF |
| 144 | + "$SCANNER_DEBUG" git.patch | grep -q "HEADERS" |
| 145 | +} |
| 146 | + |
| 147 | +# Test 9: Context diff |
| 148 | +test_context_diff() { |
| 149 | + cat > context.patch << 'EOF' |
| 150 | +*** old.txt 2024-01-01 10:00:00 |
| 151 | +--- new.txt 2024-01-01 11:00:00 |
| 152 | +*************** |
| 153 | +*** 1,2 **** |
| 154 | + line1 |
| 155 | +! old_line |
| 156 | +--- 1,2 ---- |
| 157 | + line1 |
| 158 | +! new_line |
| 159 | +EOF |
| 160 | + "$SCANNER_DEBUG" context.patch | grep -q "HEADERS" |
| 161 | +} |
| 162 | + |
| 163 | +# Test 10: Non-patch content |
| 164 | +test_non_patch() { |
| 165 | + cat > non_patch.txt << 'EOF' |
| 166 | +This is not a patch |
| 167 | +Just some random text |
| 168 | +Nothing to see here |
| 169 | +EOF |
| 170 | + "$SCANNER_DEBUG" non_patch.txt | grep -q "NON-PATCH" |
| 171 | +} |
| 172 | + |
| 173 | +# Test 11: Mixed content |
| 174 | +test_mixed_content() { |
| 175 | + cat > mixed.patch << 'EOF' |
| 176 | +Some header comment |
| 177 | +--- old.txt |
| 178 | ++++ new.txt |
| 179 | +@@ -1,1 +1,1 @@ |
| 180 | +-old |
| 181 | ++new |
| 182 | +Some footer comment |
| 183 | +EOF |
| 184 | + output=$("$SCANNER_DEBUG" mixed.patch) |
| 185 | + echo "$output" | grep -q "NON-PATCH" && echo "$output" | grep -q "HEADERS" |
| 186 | +} |
| 187 | + |
| 188 | +# Test 12: Binary patch detection |
| 189 | +test_binary_patch() { |
| 190 | + cat > binary.patch << 'EOF' |
| 191 | +diff --git a/image.png b/image.png |
| 192 | +new file mode 100644 |
| 193 | +index 0000000..abc123 |
| 194 | +Binary files /dev/null and b/image.png differ |
| 195 | +EOF |
| 196 | + "$SCANNER_DEBUG" binary.patch >/dev/null 2>&1 |
| 197 | +} |
| 198 | + |
| 199 | +# Test 13: No newline handling |
| 200 | +test_no_newline() { |
| 201 | + cat > no_newline.patch << 'EOF' |
| 202 | +--- file.txt |
| 203 | ++++ file.txt |
| 204 | +@@ -1 +1 @@ |
| 205 | +-old_line |
| 206 | +\ No newline at end of file |
| 207 | ++new_line |
| 208 | +\ No newline at end of file |
| 209 | +EOF |
| 210 | + "$SCANNER_DEBUG" no_newline.patch >/dev/null 2>&1 |
| 211 | +} |
| 212 | + |
| 213 | +# Test 14: Error condition - nonexistent file |
| 214 | +test_nonexistent_file() { |
| 215 | + ! "$SCANNER_DEBUG" nonexistent_file.patch >/dev/null 2>&1 |
| 216 | +} |
| 217 | + |
| 218 | +# Test 15: Error condition - invalid options |
| 219 | +test_invalid_option() { |
| 220 | + ! "$SCANNER_DEBUG" --invalid-option >/dev/null 2>&1 |
| 221 | +} |
| 222 | + |
| 223 | +# Test 16: Empty file |
| 224 | +test_empty_file() { |
| 225 | + touch empty.patch |
| 226 | + "$SCANNER_DEBUG" empty.patch >/dev/null 2>&1 |
| 227 | +} |
| 228 | + |
| 229 | +# Test 17: Large patch file (performance test) |
| 230 | +test_large_patch() { |
| 231 | + # Create a patch with many hunks |
| 232 | + { |
| 233 | + echo "--- large.txt" |
| 234 | + echo "+++ large.txt" |
| 235 | + for i in $(seq 1 100); do |
| 236 | + echo "@@ -$i,1 +$i,1 @@" |
| 237 | + echo "-old line $i" |
| 238 | + echo "+new line $i" |
| 239 | + done |
| 240 | + } > large.patch |
| 241 | + "$SCANNER_DEBUG" large.patch >/dev/null 2>&1 |
| 242 | +} |
| 243 | + |
| 244 | +# Test 18: Compact vs verbose output comparison |
| 245 | +test_output_formats() { |
| 246 | + cat > format.patch << 'EOF' |
| 247 | +--- test.txt |
| 248 | ++++ test.txt |
| 249 | +@@ -1,2 +1,2 @@ |
| 250 | + context |
| 251 | +-old |
| 252 | ++new |
| 253 | +EOF |
| 254 | + compact_lines=$("$SCANNER_DEBUG" format.patch | wc -l) |
| 255 | + verbose_lines=$("$SCANNER_DEBUG" -v format.patch | wc -l) |
| 256 | + [ "$verbose_lines" -gt "$compact_lines" ] |
| 257 | +} |
| 258 | + |
| 259 | +# Test 19: Multiple files in single patch |
| 260 | +test_multiple_files() { |
| 261 | + cat > multi.patch << 'EOF' |
| 262 | +--- file1.txt |
| 263 | ++++ file1.txt |
| 264 | +@@ -1 +1 @@ |
| 265 | +-old1 |
| 266 | ++new1 |
| 267 | +--- file2.txt |
| 268 | ++++ file2.txt |
| 269 | +@@ -1 +1 @@ |
| 270 | +-old2 |
| 271 | ++new2 |
| 272 | +EOF |
| 273 | + output=$("$SCANNER_DEBUG" multi.patch) |
| 274 | + # Should have two HEADERS events |
| 275 | + [ "$(echo "$output" | grep -c "HEADERS")" -eq 2 ] |
| 276 | +} |
| 277 | + |
| 278 | +# Test 20: All options combined |
| 279 | +test_all_options() { |
| 280 | + cat > all_opts.patch << 'EOF' |
| 281 | +--- test.txt |
| 282 | ++++ test.txt |
| 283 | +@@ -1,2 +1,2 @@ |
| 284 | + context line |
| 285 | +-removed line |
| 286 | ++added line |
| 287 | +EOF |
| 288 | + "$SCANNER_DEBUG" -v -c -p -x --color all_opts.patch >/dev/null 2>&1 |
| 289 | +} |
| 290 | + |
| 291 | +# Run all tests |
| 292 | +echo "Running scanner_debug utility tests..." |
| 293 | +echo "Scanner debug binary: $SCANNER_DEBUG" |
| 294 | +echo |
| 295 | + |
| 296 | +run_test "help" "Basic help functionality" test_help |
| 297 | +run_test "simple_patch" "Simple unified patch processing" test_simple_patch |
| 298 | +run_test "stdin_input" "Standard input processing" test_stdin_input |
| 299 | +run_test "verbose_output" "Verbose output format" test_verbose_output |
| 300 | +run_test "content_option" "Content display option" test_content_option |
| 301 | +run_test "positions_option" "Position display option" test_positions_option |
| 302 | +run_test "color_output" "Colored output option" test_color_output |
| 303 | +run_test "git_patch" "Git extended patch processing" test_git_patch |
| 304 | +run_test "context_diff" "Context diff processing" test_context_diff |
| 305 | +run_test "non_patch" "Non-patch content detection" test_non_patch |
| 306 | +run_test "mixed_content" "Mixed patch and non-patch content" test_mixed_content |
| 307 | +run_test "binary_patch" "Binary patch detection" test_binary_patch |
| 308 | +run_test "no_newline" "No newline marker handling" test_no_newline |
| 309 | +run_test "nonexistent_file" "Error handling for nonexistent files" test_nonexistent_file |
| 310 | +run_test "invalid_option" "Error handling for invalid options" test_invalid_option |
| 311 | +run_test "empty_file" "Empty file handling" test_empty_file |
| 312 | +run_test "large_patch" "Large patch file processing" test_large_patch |
| 313 | +run_test "output_formats" "Compact vs verbose output formats" test_output_formats |
| 314 | +run_test "multiple_files" "Multiple files in single patch" test_multiple_files |
| 315 | +run_test "all_options" "All command line options combined" test_all_options |
| 316 | + |
| 317 | +# Summary |
| 318 | +echo "==========================================" |
| 319 | +echo "Test Summary:" |
| 320 | +echo "Total tests: $test_count" |
| 321 | +echo "Passed: $((test_count - failed_tests))" |
| 322 | +echo "Failed: $failed_tests" |
| 323 | + |
| 324 | +if [ "$failed_tests" -eq 0 ]; then |
| 325 | + echo "✓ All scanner_debug tests passed!" |
| 326 | + exit 0 |
| 327 | +else |
| 328 | + echo "✗ $failed_tests scanner_debug test(s) failed" |
| 329 | + exit 1 |
| 330 | +fi |
0 commit comments