| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# Test for GitHub issue #18: interdiff does not reverse a patch correctly |
| 4 |
# According to man interdiff: "To reverse a patch, use /dev/null for diff2." |
| 5 |
# However, interdiff file.patch /dev/null does not always produce a valid reversed patch. |
| 6 |
|
| 7 |
. ${top_srcdir-.}/tests/common.sh |
| 8 |
|
| 9 |
# Test 1: Simple case |
| 10 |
echo "Testing simple patch reversal..." |
| 11 |
|
| 12 |
# Create test files |
| 13 |
cat << 'EOF' > file1.php |
| 14 |
<?php |
| 15 |
// Original file content |
| 16 |
function example() { |
| 17 |
$var = "original"; |
| 18 |
return $var; |
| 19 |
} |
| 20 |
|
| 21 |
class TestClass { |
| 22 |
public function method1() { |
| 23 |
return "original method"; |
| 24 |
} |
| 25 |
} |
| 26 |
?> |
| 27 |
EOF |
| 28 |
|
| 29 |
cat << 'EOF' > file2.php |
| 30 |
<?php |
| 31 |
// Modified file content |
| 32 |
function example() { |
| 33 |
$var = "modified"; |
| 34 |
$extra = "new variable"; |
| 35 |
return $var . $extra; |
| 36 |
} |
| 37 |
|
| 38 |
class TestClass { |
| 39 |
public function method1() { |
| 40 |
return "modified method"; |
| 41 |
} |
| 42 |
|
| 43 |
public function method2() { |
| 44 |
return "new method"; |
| 45 |
} |
| 46 |
} |
| 47 |
?> |
| 48 |
EOF |
| 49 |
|
| 50 |
# Create forward patch (file1.php -> file2.php) |
| 51 |
diff -u file1.php file2.php > forward.patch |
| 52 |
|
| 53 |
# Generate reversed patch using interdiff with /dev/null |
| 54 |
${INTERDIFF} forward.patch /dev/null > reversed.patch 2>errors || exit 1 |
| 55 |
[ -s errors ] && exit 1 |
| 56 |
|
| 57 |
# Copy file2.php to test file for applying the reversed patch |
| 58 |
cp file2.php test_file.php |
| 59 |
|
| 60 |
# Apply the reversed patch to file2.php - this should give us file1.php |
| 61 |
${PATCH} -p0 test_file.php < reversed.patch 2>patch_errors |
| 62 |
|
| 63 |
# Check if patch application succeeded |
| 64 |
if [ $? -ne 0 ]; then |
| 65 |
echo "ERROR: Simple reversed patch failed to apply cleanly" >&2 |
| 66 |
echo "Patch errors:" >&2 |
| 67 |
cat patch_errors >&2 |
| 68 |
echo "Generated reversed patch:" >&2 |
| 69 |
cat reversed.patch >&2 |
| 70 |
exit 1 |
| 71 |
fi |
| 72 |
|
| 73 |
# Check if the result matches the original file1.php |
| 74 |
if ! diff -q file1.php test_file.php > /dev/null; then |
| 75 |
echo "ERROR: Simple reversed patch did not produce the original file" >&2 |
| 76 |
echo "Expected (file1.php):" >&2 |
| 77 |
cat file1.php >&2 |
| 78 |
echo "Got (test_file.php after applying reversed patch):" >&2 |
| 79 |
cat test_file.php >&2 |
| 80 |
echo "Differences:" >&2 |
| 81 |
diff -u file1.php test_file.php >&2 |
| 82 |
exit 1 |
| 83 |
fi |
| 84 |
|
| 85 |
echo "Simple test passed." |
| 86 |
|
| 87 |
# Test 2: Complex case with more intricate changes |
| 88 |
echo "Testing complex patch reversal..." |
| 89 |
|
| 90 |
# Create complex test files inline |
| 91 |
cat << 'EOF' > complex_orig.php |
| 92 |
<?php |
| 93 |
/** |
| 94 |
* Original complex file |
| 95 |
*/ |
| 96 |
|
| 97 |
class DatabaseManager { |
| 98 |
private $connection; |
| 99 |
private $host = 'localhost'; |
| 100 |
private $database = 'test_db'; |
| 101 |
|
| 102 |
public function __construct() { |
| 103 |
$this->connection = null; |
| 104 |
} |
| 105 |
|
| 106 |
public function connect() { |
| 107 |
try { |
| 108 |
$this->connection = new PDO( |
| 109 |
"mysql:host={$this->host};dbname={$this->database}", |
| 110 |
'user', |
| 111 |
'password' |
| 112 |
); |
| 113 |
return true; |
| 114 |
} catch (PDOException $e) { |
| 115 |
error_log("Connection failed: " . $e->getMessage()); |
| 116 |
return false; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
public function query($sql) { |
| 121 |
if (!$this->connection) { |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
$stmt = $this->connection->prepare($sql); |
| 126 |
return $stmt->execute(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
function helper_function($data) { |
| 131 |
return array_map('trim', $data); |
| 132 |
} |
| 133 |
?> |
| 134 |
EOF |
| 135 |
|
| 136 |
cat << 'EOF' > complex_modified.php |
| 137 |
<?php |
| 138 |
/** |
| 139 |
* Modified complex file with various changes |
| 140 |
*/ |
| 141 |
|
| 142 |
class DatabaseManager { |
| 143 |
private $connection; |
| 144 |
private $host = 'production_host'; |
| 145 |
private $database = 'production_db'; |
| 146 |
private $timeout = 30; |
| 147 |
|
| 148 |
public function __construct($config = []) { |
| 149 |
$this->connection = null; |
| 150 |
if (isset($config['host'])) { |
| 151 |
$this->host = $config['host']; |
| 152 |
} |
| 153 |
if (isset($config['database'])) { |
| 154 |
$this->database = $config['database']; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
public function connect() { |
| 159 |
try { |
| 160 |
$dsn = "mysql:host={$this->host};dbname={$this->database};timeout={$this->timeout}"; |
| 161 |
$this->connection = new PDO( |
| 162 |
$dsn, |
| 163 |
'production_user', |
| 164 |
'secure_password', |
| 165 |
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] |
| 166 |
); |
| 167 |
return true; |
| 168 |
} catch (PDOException $e) { |
| 169 |
error_log("Database connection failed: " . $e->getMessage()); |
| 170 |
throw new Exception("Cannot connect to database"); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
public function query($sql, $params = []) { |
| 175 |
if (!$this->connection) { |
| 176 |
throw new Exception("No database connection"); |
| 177 |
} |
| 178 |
|
| 179 |
$stmt = $this->connection->prepare($sql); |
| 180 |
if (!empty($params)) { |
| 181 |
return $stmt->execute($params); |
| 182 |
} |
| 183 |
return $stmt->execute(); |
| 184 |
} |
| 185 |
|
| 186 |
public function disconnect() { |
| 187 |
$this->connection = null; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
function helper_function($data) { |
| 192 |
// Enhanced helper with validation |
| 193 |
if (!is_array($data)) { |
| 194 |
return []; |
| 195 |
} |
| 196 |
return array_map(function($item) { |
| 197 |
return trim($item); |
| 198 |
}, $data); |
| 199 |
} |
| 200 |
|
| 201 |
function new_helper($input) { |
| 202 |
return strtolower(trim($input)); |
| 203 |
} |
| 204 |
?> |
| 205 |
EOF |
| 206 |
|
| 207 |
# Create forward patch for complex files |
| 208 |
diff -u complex_orig.php complex_modified.php > complex_forward.patch |
| 209 |
|
| 210 |
# Generate reversed patch using interdiff with /dev/null |
| 211 |
${INTERDIFF} complex_forward.patch /dev/null > complex_reversed.patch 2>complex_errors || exit 1 |
| 212 |
[ -s complex_errors ] && exit 1 |
| 213 |
|
| 214 |
# Copy complex_modified.php to test file for applying the reversed patch |
| 215 |
cp complex_modified.php complex_test.php |
| 216 |
|
| 217 |
# Apply the reversed patch - this should give us complex_orig.php |
| 218 |
${PATCH} -p0 complex_test.php < complex_reversed.patch 2>complex_patch_errors |
| 219 |
|
| 220 |
# Check if patch application succeeded |
| 221 |
if [ $? -ne 0 ]; then |
| 222 |
echo "ERROR: Complex reversed patch failed to apply cleanly" >&2 |
| 223 |
echo "Patch errors:" >&2 |
| 224 |
cat complex_patch_errors >&2 |
| 225 |
echo "Generated reversed patch:" >&2 |
| 226 |
cat complex_reversed.patch >&2 |
| 227 |
exit 1 |
| 228 |
fi |
| 229 |
|
| 230 |
# Check if the result matches the original complex_orig.php |
| 231 |
if ! diff -q complex_orig.php complex_test.php > /dev/null; then |
| 232 |
echo "ERROR: Complex reversed patch did not produce the original file" >&2 |
| 233 |
echo "Expected (complex_orig.php):" >&2 |
| 234 |
cat complex_orig.php >&2 |
| 235 |
echo "Got (complex_test.php after applying reversed patch):" >&2 |
| 236 |
cat complex_test.php >&2 |
| 237 |
echo "Differences:" >&2 |
| 238 |
diff -u complex_orig.php complex_test.php >&2 |
| 239 |
exit 1 |
| 240 |
fi |
| 241 |
|
| 242 |
echo "Complex test passed." |
| 243 |
|
| 244 |
# Test 3: Check that the reversed patch is actually the inverse |
| 245 |
echo "Testing patch inversion correctness..." |
| 246 |
|
| 247 |
# Apply forward patch to original to get modified |
| 248 |
cp file1.php forward_test.php |
| 249 |
${PATCH} -p0 forward_test.php < forward.patch 2>/dev/null |
| 250 |
|
| 251 |
# Apply reversed patch to the result to get back to original |
| 252 |
${PATCH} -p0 forward_test.php < reversed.patch 2>/dev/null |
| 253 |
|
| 254 |
# This should match the original |
| 255 |
if ! diff -q file1.php forward_test.php > /dev/null; then |
| 256 |
echo "ERROR: Forward + reversed patch sequence did not return to original" >&2 |
| 257 |
echo "Expected (file1.php):" >&2 |
| 258 |
cat file1.php >&2 |
| 259 |
echo "Got (forward_test.php after forward+reverse):" >&2 |
| 260 |
cat forward_test.php >&2 |
| 261 |
exit 1 |
| 262 |
fi |
| 263 |
|
| 264 |
echo "Patch inversion test passed." |
| 265 |
|
| 266 |
# If we get here, all tests passed |
| 267 |
# Test 4: Multi-hunk patch test with proper format |
| 268 |
echo "Testing multi-hunk patch reversal..." |
| 269 |
|
| 270 |
# Create a simple multi-hunk patch to test the fix |
| 271 |
cat << 'EOF' > simple_orig.txt |
| 272 |
line1 |
| 273 |
line2 |
| 274 |
line3 |
| 275 |
|
| 276 |
section2_line1 |
| 277 |
section2_line2 |
| 278 |
section2_line3 |
| 279 |
EOF |
| 280 |
|
| 281 |
cat << 'EOF' > simple_modified.txt |
| 282 |
line1 |
| 283 |
modified_line2 |
| 284 |
line3 |
| 285 |
|
| 286 |
section2_line1 |
| 287 |
modified_section2_line2 |
| 288 |
new_line |
| 289 |
section2_line3 |
| 290 |
EOF |
| 291 |
|
| 292 |
# Create the forward patch |
| 293 |
diff -u simple_orig.txt simple_modified.txt > simple_multi_hunk.patch |
| 294 |
|
| 295 |
# Generate reversed patch using interdiff with /dev/null |
| 296 |
${INTERDIFF} --quiet simple_multi_hunk.patch /dev/null > simple_reversed.patch 2>simple_errors || { |
| 297 |
echo "ERROR: interdiff failed to generate reversed patch" >&2 |
| 298 |
cat simple_errors >&2 |
| 299 |
exit 1 |
| 300 |
} |
| 301 |
|
| 302 |
# Check if there were errors |
| 303 |
if [ -s simple_errors ]; then |
| 304 |
echo "ERROR: interdiff reported errors:" >&2 |
| 305 |
cat simple_errors >&2 |
| 306 |
exit 1 |
| 307 |
fi |
| 308 |
|
| 309 |
echo "Generated reversed patch:" |
| 310 |
cat simple_reversed.patch |
| 311 |
|
| 312 |
# Apply the reversed patch to the modified file to get back the original |
| 313 |
cp simple_modified.txt test_simple_reversed.txt |
| 314 |
${PATCH} -p0 test_simple_reversed.txt < simple_reversed.patch 2>simple_patch_errors || { |
| 315 |
echo "ERROR: Failed to apply reversed patch" >&2 |
| 316 |
cat simple_patch_errors >&2 |
| 317 |
exit 1 |
| 318 |
} |
| 319 |
|
| 320 |
# Check if the result matches the original |
| 321 |
if ! diff -q simple_orig.txt test_simple_reversed.txt > /dev/null; then |
| 322 |
echo "ERROR: Multi-hunk reversed patch did not correctly restore original file" >&2 |
| 323 |
echo "Expected:" >&2 |
| 324 |
cat simple_orig.txt >&2 |
| 325 |
echo "Got:" >&2 |
| 326 |
cat test_simple_reversed.txt >&2 |
| 327 |
echo "Differences:" >&2 |
| 328 |
diff -u simple_orig.txt test_simple_reversed.txt >&2 |
| 329 |
exit 1 |
| 330 |
fi |
| 331 |
|
| 332 |
echo "Multi-hunk test passed." |
| 333 |
|
| 334 |
echo "All tests passed: interdiff correctly reverses patches" |
| 335 |
exit 0 |