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