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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
|
<?php
use MediaWiki\Maintenance\Maintenance;
use MediaWiki\Settings\SettingsBuilder;
use MediaWiki\Tests\AnsiTermColorer;
use Wikimedia\Diff\Diff;
use Wikimedia\Diff\UnifiedDiffFormatter;
use Wikimedia\Parsoid\ParserTests\Test as ParserTest;
use Wikimedia\Parsoid\ParserTests\TestFileReader;
use Wikimedia\Parsoid\ParserTests\TestMode as ParserTestMode;
use Wikimedia\ScopedCallback;
require_once __DIR__ . '/../../maintenance/Maintenance.php';
define( 'MW_AUTOLOAD_TEST_CLASSES', true );
/**
* Interactive parser test runner and test file editor
*/
class ParserEditTests extends Maintenance {
/** @var int */
private $termWidth;
/** @var TestFileReader[] */
private $testFiles;
/** @var int */
private $testCount;
/** @var TestRecorder */
private $recorder;
/** @var ParserTestRunner */
private $runner;
/** @var int */
private $numExecuted;
/** @var int */
private $numSkipped;
/** @var int */
private $numFailed;
/** @var array */
private $results;
/** @var array */
private $session;
public function __construct() {
parent::__construct();
$this->addOption( 'session-data', 'internal option, do not use', false, true );
}
public function finalSetup( SettingsBuilder $settingsBuilder ) {
// Some methods which are discouraged for normal code throw exceptions unless
// we declare this is just a test.
define( 'MW_PARSER_TEST', true );
parent::finalSetup( $settingsBuilder );
TestSetup::applyInitialConfig();
}
public function execute() {
$this->termWidth = $this->getTermSize()[0] - 1;
$this->recorder = new TestRecorder();
$this->setupFileData();
if ( $this->hasOption( 'session-data' ) ) {
$this->session = json_decode( $this->getOption( 'session-data' ), true );
} else {
$this->session = [ 'options' => [] ];
}
// @phan-suppress-next-line PhanTypeArraySuspiciousNullable options always set
$this->runner = new ParserTestRunner( $this->recorder, $this->session['options'] );
$this->runTests();
if ( $this->numFailed === 0 ) {
if ( $this->numSkipped === 0 ) {
print "All tests passed!\n";
} else {
print "All tests passed (but skipped {$this->numSkipped})\n";
}
return;
}
print "{$this->numFailed} test(s) failed.\n";
$this->showResults();
}
protected function setupFileData() {
$this->testFiles = [];
$this->testCount = 0;
foreach ( ParserTestRunner::getParserTestFiles() as $file ) {
$fileInfo = TestFileReader::read( $file );
$this->testFiles[$file] = $fileInfo;
$this->testCount += count( $fileInfo->testCases );
}
}
protected function getTestDesc( ParserTest $test ) {
return $test->testName; // could include mode here too
}
protected function getResultSection( ParserTest $test ) {
// This used to switch between html and html+tidy, but we
// got rid of the "notidy" support some time ago.
// This should probably eventually support html+standalone
// and html+integrated in a similar way, but for now just
// walk the legacy HTML fallback set
$legacyHtmlKeys = [
'html/php', 'html/*', 'html',
# deprecated
'result',
'html/php+tidy',
'html/*+tidy',
'html+tidy',
];
foreach ( $legacyHtmlKeys as $key ) {
if ( $test->sections[$key] ?? false ) {
return $key;
}
}
return 'html';
}
protected function runTests() {
$teardownGuard = null;
$teardownGuard = $this->runner->setupDatabase( $teardownGuard );
$teardownGuard = $this->runner->staticSetup( $teardownGuard );
$teardownGuard = $this->runner->setupUploads( $teardownGuard );
print "Running tests...\n";
$this->results = [];
$this->numExecuted = 0;
$this->numSkipped = 0;
$this->numFailed = 0;
$mode = new ParserTestMode( 'legacy' );
foreach ( $this->testFiles as $fileName => $fileInfo ) {
$this->runner->addArticles( $fileInfo->articles );
foreach ( $fileInfo->testCases as $testInfo ) {
$result = $this->runner->runTest( $testInfo, $mode );
if ( $result === false ) {
$this->numSkipped++;
} elseif ( !$result->isSuccess() ) {
$desc = $this->getTestDesc( $testInfo );
$this->results[$fileName][$desc] = $result;
$this->numFailed++;
}
$this->numExecuted++;
$this->showProgress();
}
}
print "\n";
ScopedCallback::consume( $teardownGuard );
}
protected function showProgress() {
$done = $this->numExecuted;
$total = $this->testCount;
$width = $this->termWidth - 9;
$pos = (int)round( $width * $done / $total );
printf( '│' . str_repeat( '█', $pos ) . str_repeat( '-', $width - $pos ) .
"│ %5.1f%%\r", $done / $total * 100 );
}
protected function showResults() {
if ( isset( $this->session['startFile'] ) ) {
$startFile = $this->session['startFile'];
$startTest = $this->session['startTest'];
$foundStart = false;
} else {
$startFile = false;
$startTest = false;
$foundStart = true;
}
$testIndex = 0;
foreach ( $this->testFiles as $fileName => $fileInfo ) {
if ( !isset( $this->results[$fileName] ) ) {
continue;
}
if ( !$foundStart && $startFile !== false && $fileName !== $startFile ) {
$testIndex += count( $this->results[$fileName] );
continue;
}
foreach ( $fileInfo->testCases as $testInfo ) {
$desc = $this->getTestDesc( $testInfo );
if ( !isset( $this->results[$fileName][$desc] ) ) {
continue;
}
$result = $this->results[$fileName][$desc];
$testIndex++;
if ( !$foundStart && $startTest !== false ) {
if ( $desc !== $startTest ) {
continue;
}
$foundStart = true;
}
$this->handleFailure( $testIndex, $testInfo, $result );
}
}
if ( !$foundStart ) {
print "Could not find the test after a restart, did you rename it?";
unset( $this->session['startFile'] );
unset( $this->session['startTest'] );
// @phan-suppress-next-line PhanPossiblyInfiniteRecursionSameParams
$this->showResults();
}
print "All done\n";
}
protected function heading( $text ) {
$term = new AnsiTermColorer;
$heading = "─── $text ";
$heading .= str_repeat( '─', $this->termWidth - mb_strlen( $heading ) );
$heading = $term->color( '34' ) . $heading . $term->reset() . "\n";
return $heading;
}
protected function unifiedDiff( $left, $right ) {
$fromLines = explode( "\n", $left );
$toLines = explode( "\n", $right );
$formatter = new UnifiedDiffFormatter;
return $formatter->format( new Diff( $fromLines, $toLines ) );
}
protected function handleFailure( $index, $testInfo, $result ) {
$term = new AnsiTermColorer;
$div1 = $term->color( '34' ) . str_repeat( '━', $this->termWidth ) .
$term->reset() . "\n";
$div2 = $term->color( '34' ) . str_repeat( '─', $this->termWidth ) .
$term->reset() . "\n";
$desc = $this->getTestDesc( $testInfo );
print $div1;
print "Failure $index/{$this->numFailed}: {$testInfo->filename} line {$testInfo->lineNumStart}\n" .
"{$desc}\n";
print $this->heading( 'Input' );
print "{$testInfo->wikitext}\n";
print $this->heading( 'Alternating expected/actual output' );
print $this->alternatingAligned( $result->expected, $result->actual );
print $this->heading( 'Diff' );
$dwdiff = $this->dwdiff( $result->expected, $result->actual );
if ( $dwdiff !== false ) {
$diff = $dwdiff;
} else {
$diff = $this->unifiedDiff( $result->expected, $result->actual );
}
print $diff;
if ( $testInfo->options || $testInfo->config ) {
print $this->heading( 'Options / Config' );
if ( $testInfo->options ) {
print json_encode( $testInfo->options ) . "\n";
}
if ( $testInfo->config ) {
print json_encode( $testInfo->config ) . "\n";
}
}
print $div2;
print "What do you want to do?\n";
$specs = [
'[R]eload code and run again',
'[U]pdate source file, copy actual to expected',
'[I]gnore' ];
# XXX originally isSubtest was a way to edit the +tidy vs +untidy
# portions of the test separately (I believe)
// @phan-suppress-next-line PhanUndeclaredProperty
if ( !empty( $testInfo->isSubtest ) ) {
# FIXME: this is orphan code, will never be true
$specs[] = 'Delete [s]ubtest';
}
$specs[] = '[D]elete test';
$specs[] = '[Q]uit';
$options = [];
foreach ( $specs as $spec ) {
if ( !preg_match( '/^(.*\[)(.)(\].*)$/', $spec, $m ) ) {
throw new LogicException( 'Invalid option spec: ' . $spec );
}
print '* ' . $m[1] . $term->color( '35' ) . $m[2] . $term->color( '0' ) . $m[3] . "\n";
$options[strtoupper( $m[2] )] = true;
}
do {
$response = self::readconsole();
$cmdResult = false;
if ( $response === false ) {
exit( 0 );
}
$response = strtoupper( trim( $response ) );
if ( !isset( $options[$response] ) ) {
print "Invalid response, please enter a single letter from the list above\n";
continue;
}
switch ( strtoupper( trim( $response ) ) ) {
case 'R':
$cmdResult = $this->reload( $testInfo );
break;
case 'U':
$cmdResult = $this->update( $testInfo, $result );
break;
case 'I':
return;
case 'T':
$cmdResult = $this->switchTidy( $testInfo );
break;
case 'S':
$cmdResult = $this->deleteSubtest( $testInfo );
break;
case 'D':
$cmdResult = $this->deleteTest( $testInfo );
break;
case 'Q':
exit( 0 );
}
} while ( !$cmdResult );
}
protected function dwdiff( $expected, $actual ) {
if ( !is_executable( '/usr/bin/dwdiff' ) ) {
return false;
}
$markers = [
"\n" => '¶',
' ' => '·',
"\t" => '→'
];
$markedExpected = strtr( $expected, $markers );
$markedActual = strtr( $actual, $markers );
$diff = $this->unifiedDiff( $markedExpected, $markedActual );
$tempFile = tmpfile();
fwrite( $tempFile, $diff );
fseek( $tempFile, 0 );
$pipes = [];
$proc = proc_open( '/usr/bin/dwdiff -Pc --diff-input',
[ 0 => $tempFile, 1 => [ 'pipe', 'w' ], 2 => STDERR ],
$pipes );
if ( !$proc ) {
return false;
}
$result = stream_get_contents( $pipes[1] );
proc_close( $proc );
fclose( $tempFile );
return $result;
}
protected function alternatingAligned( $expectedStr, $actualStr ) {
$expectedLines = explode( "\n", $expectedStr );
$actualLines = explode( "\n", $actualStr );
$maxLines = max( count( $expectedLines ), count( $actualLines ) );
$result = '';
for ( $i = 0; $i < $maxLines; $i++ ) {
if ( $i < count( $expectedLines ) ) {
$expectedLine = $expectedLines[$i];
$expectedChunks = str_split( $expectedLine, $this->termWidth - 3 );
} else {
$expectedChunks = [];
}
if ( $i < count( $actualLines ) ) {
$actualLine = $actualLines[$i];
$actualChunks = str_split( $actualLine, $this->termWidth - 3 );
} else {
$actualChunks = [];
}
$maxChunks = max( count( $expectedChunks ), count( $actualChunks ) );
for ( $j = 0; $j < $maxChunks; $j++ ) {
if ( isset( $expectedChunks[$j] ) ) {
$result .= "E: " . $expectedChunks[$j];
if ( $j === count( $expectedChunks ) - 1 ) {
$result .= "¶";
}
$result .= "\n";
} else {
$result .= "E:\n";
}
$result .= "\33[4m" . // underline
"A: ";
if ( isset( $actualChunks[$j] ) ) {
$result .= $actualChunks[$j];
if ( $j === count( $actualChunks ) - 1 ) {
$result .= "¶";
}
}
$result .= "\33[0m\n"; // reset
}
}
return $result;
}
protected function reload( $testInfo ) {
global $argv;
pcntl_exec( PHP_BINARY, [
$argv[0],
'--session-data',
json_encode( [
'startFile' => $testInfo->filename,
'startTest' => $this->getTestDesc( $testInfo ),
] + $this->session ) ] );
print "pcntl_exec() failed\n";
return false;
}
protected function findTest( $file, $testInfo ) {
$initialPart = '';
for ( $i = 1; $i < $testInfo->lineNumStart; $i++ ) {
$line = fgets( $file );
if ( $line === false ) {
print "Error reading from file\n";
return false;
}
$initialPart .= $line;
}
$line = fgets( $file );
if ( !preg_match( '/^!!\s*test/', $line ) ) {
print "Test has moved, cannot edit\n";
return false;
}
$testPart = $line;
$desc = fgets( $file );
if ( trim( $desc ) !== $this->getTestDesc( $testInfo ) ) {
print "Description does not match, cannot edit\n";
return false;
}
$testPart .= $desc;
return [ $initialPart, $testPart ];
}
protected function getOutputFileName( $inputFileName ) {
if ( is_writable( $inputFileName ) ) {
$outputFileName = $inputFileName;
} else {
$outputFileName = wfTempDir() . '/' . basename( $inputFileName );
print "Cannot write to input file, writing to $outputFileName instead\n";
}
return $outputFileName;
}
protected function editTest( $fileName, $deletions, $changes ) {
$text = file_get_contents( $fileName );
if ( $text === false ) {
print "Unable to open test file!";
return false;
}
$result = TestFileEditor::edit( $text, $deletions, $changes,
static function ( $msg ) {
print "$msg\n";
}
);
if ( is_writable( $fileName ) ) {
file_put_contents( $fileName, $result );
print "Wrote updated file\n";
} else {
print "Cannot write updated file, here is a patch you can paste:\n\n";
print "--- {$fileName}\n" .
"+++ {$fileName}~\n" .
$this->unifiedDiff( $text, $result ) .
"\n";
}
}
protected function update( $testInfo, $result ) {
$resultSection = $this->getResultSection( $testInfo );
$this->editTest( $testInfo->filename,
[], // deletions
[ // changes
$testInfo->testName => [
$resultSection => [
'op' => 'update',
'value' => $result->actual . "\n"
]
]
]
);
return false;
}
protected function deleteTest( $testInfo ) {
$this->editTest( $testInfo->filename,
[ $testInfo->testName ], // deletions
[] // changes
);
return false;
}
protected function switchTidy( $testInfo ) {
$resultSection = $this->getResultSection( $testInfo );
if ( in_array( $resultSection, [ 'html/php' ] ) ) {
$newSection = 'html/php';
} elseif ( in_array( $resultSection, [ 'html/*', 'html', 'result' ] ) ) {
$newSection = 'html';
} else {
print "Unrecognised result section name \"$resultSection\"";
return true;
}
$this->editTest( $testInfo->filename,
[], // deletions
[ // changes
$testInfo->testName => [
$resultSection => [
'op' => 'rename',
'value' => $newSection
]
]
]
);
return false;
}
protected function deleteSubtest( $testInfo ) {
$resultSection = $this->getResultSection( $testInfo );
$this->editTest( $testInfo->filename,
[], // deletions
[ // changes
$testInfo->testName => [
$resultSection => [
'op' => 'delete'
]
]
]
);
return false;
}
}
$maintClass = ParserEditTests::class;
require_once RUN_MAINTENANCE_IF_MAIN;
|