3require __DIR__.
'/../../maintenance/Maintenance.php';
5define(
'MW_PARSER_TEST',
true );
21 parent::__construct();
22 $this->
addOption(
'session-data',
'internal option, do not use',
false,
true );
24 'Use the wiki\'s Tidy configuration instead of known-good' .
40 if ( $this->
hasOption(
'session-data' ) ) {
41 $this->session = json_decode( $this->
getOption(
'session-data' ),
true );
43 $this->session = [
'options' => [] ];
45 if ( $this->
hasOption(
'use-tidy-config' ) ) {
46 $this->session[
'options'][
'use-tidy-config'] =
true;
48 $this->runner =
new ParserTestRunner( $this->recorder, $this->session[
'options'] );
52 if ( $this->numFailed === 0 ) {
53 if ( $this->numSkipped === 0 ) {
54 print "All tests passed!\n";
56 print "All tests passed (but skipped {$this->numSkipped})\n";
60 print "{$this->numFailed} test(s) failed.\n";
66 $this->testFiles = [];
70 $this->testFiles[$file] = $fileInfo;
71 $this->testCount += count( $fileInfo[
'tests'] );
76 $teardown = $this->runner->staticSetup();
77 $teardown = $this->runner->setupDatabase( $teardown );
78 $teardown = $this->runner->setupUploads( $teardown );
80 print "Running tests...\n";
82 $this->numExecuted = 0;
83 $this->numSkipped = 0;
85 foreach ( $this->testFiles as $fileName => $fileInfo ) {
86 $this->runner->addArticles( $fileInfo[
'articles'] );
87 foreach ( $fileInfo[
'tests'] as $testInfo ) {
88 $result = $this->runner->runTest( $testInfo );
89 if ( $result ===
false ) {
91 } elseif ( !$result->isSuccess() ) {
92 $this->results[$fileName][$testInfo[
'desc']] =
$result;
105 $width = $this->termWidth - 9;
106 $pos = round( $width * $done / $total );
107 printf(
'│' . str_repeat(
'█', $pos ) . str_repeat(
'-', $width - $pos ) .
108 "│ %5.1f%%\r", $done / $total * 100 );
112 if ( isset( $this->session[
'startFile'] ) ) {
113 $startFile = $this->session[
'startFile'];
114 $startTest = $this->session[
'startTest'];
123 foreach ( $this->testFiles as $fileName => $fileInfo ) {
124 if ( !isset( $this->results[$fileName] ) ) {
127 if ( !$foundStart && $startFile !==
false && $fileName !== $startFile ) {
128 $testIndex += count( $this->results[$fileName] );
131 foreach ( $fileInfo[
'tests'] as $testInfo ) {
132 if ( !isset( $this->results[$fileName][$testInfo[
'desc']] ) ) {
135 $result = $this->results[$fileName][$testInfo[
'desc']];
137 if ( !$foundStart && $startTest !==
false ) {
138 if ( $testInfo[
'desc'] !== $startTest ) {
148 if ( !$foundStart ) {
149 print "Could not find the test after a restart, did you rename it?";
150 unset( $this->session[
'startFile'] );
151 unset( $this->session[
'startTest'] );
159 $heading =
"─── $text ";
160 $heading .= str_repeat(
'─', $this->termWidth - mb_strlen( $heading ) );
161 $heading =
$term->color( 34 ) . $heading .
$term->reset() .
"\n";
166 $fromLines = explode(
"\n", $left );
167 $toLines = explode(
"\n", $right );
169 return $formatter->
format(
new Diff( $fromLines, $toLines ) );
174 $div1 =
$term->color( 34 ) . str_repeat(
'━', $this->termWidth ) .
175 $term->reset() .
"\n";
176 $div2 =
$term->color( 34 ) . str_repeat(
'─', $this->termWidth ) .
177 $term->reset() .
"\n";
180 print "Failure $index/{$this->numFailed}: {$testInfo['file']} line {$testInfo['line']}\n" .
181 "{$testInfo['desc']}\n";
184 print "{$testInfo['input']}\n";
186 print $this->
heading(
'Alternating expected/actual output' );
191 $dwdiff = $this->
dwdiff( $result->expected, $result->actual );
192 if ( $dwdiff !==
false ) {
195 $diff = $this->
unifiedDiff( $result->expected, $result->actual );
199 if ( $testInfo[
'options'] || $testInfo[
'config'] ) {
201 if ( $testInfo[
'options'] ) {
202 print $testInfo[
'options'] .
"\n";
204 if ( $testInfo[
'config'] ) {
205 print $testInfo[
'config'] .
"\n";
210 print "What do you want to do?\n";
212 '[R]eload code and run again',
213 '[U]pdate source file, copy actual to expected',
216 if ( strpos( $testInfo[
'options'],
' tidy' ) ===
false ) {
217 if ( empty( $testInfo[
'isSubtest'] ) ) {
218 $specs[] =
"Enable [T]idy";
221 $specs[] =
'Disable [T]idy';
224 if ( !empty( $testInfo[
'isSubtest'] ) ) {
225 $specs[] =
'Delete [s]ubtest';
227 $specs[] =
'[D]elete test';
231 foreach ( $specs as $spec ) {
232 if ( !preg_match(
'/^(.*\[)(.)(\].*)$/', $spec, $m ) ) {
233 throw new MWException(
'Invalid option spec: ' . $spec );
235 print '* ' . $m[1] .
$term->color( 35 ) . $m[2] .
$term->color( 0 ) . $m[3] .
"\n";
236 $options[strtoupper( $m[2] )] =
true;
248 print "Invalid response, please enter a single letter from the list above\n";
252 switch ( strtoupper( trim(
$response ) ) ) {
254 $cmdResult = $this->
reload( $testInfo );
257 $cmdResult = $this->
update( $testInfo, $result );
273 }
while ( !$cmdResult );
276 protected function dwdiff( $expected, $actual ) {
277 if ( !is_executable(
'/usr/bin/dwdiff' ) ) {
286 $markedExpected = strtr( $expected, $markers );
287 $markedActual = strtr( $actual, $markers );
288 $diff = $this->
unifiedDiff( $markedExpected, $markedActual );
290 $tempFile = tmpfile();
291 fwrite( $tempFile, $diff );
292 fseek( $tempFile, 0 );
294 $proc = proc_open(
'/usr/bin/dwdiff -Pc --diff-input',
295 [ 0 => $tempFile, 1 => [
'pipe',
'w' ], 2 => STDERR ],
302 $result = stream_get_contents( $pipes[1] );
309 $expectedLines = explode(
"\n", $expectedStr );
310 $actualLines = explode(
"\n", $actualStr );
311 $maxLines = max( count( $expectedLines ), count( $actualLines ) );
313 for ( $i = 0; $i < $maxLines; $i++ ) {
314 if ( $i < count( $expectedLines ) ) {
315 $expectedLine = $expectedLines[$i];
316 $expectedChunks = str_split( $expectedLine, $this->termWidth - 3 );
318 $expectedChunks = [];
321 if ( $i < count( $actualLines ) ) {
322 $actualLine = $actualLines[$i];
323 $actualChunks = str_split( $actualLine, $this->termWidth - 3 );
328 $maxChunks = max( count( $expectedChunks ), count( $actualChunks ) );
330 for ( $j = 0; $j < $maxChunks; $j++ ) {
331 if ( isset( $expectedChunks[$j] ) ) {
332 $result .=
"E: " . $expectedChunks[$j];
333 if ( $j === count( $expectedChunks ) - 1 ) {
340 $result .=
"\33[4m" .
342 if ( isset( $actualChunks[$j] ) ) {
343 $result .= $actualChunks[$j];
344 if ( $j === count( $actualChunks ) - 1 ) {
348 $result .=
"\33[0m\n";
356 pcntl_exec( PHP_BINARY, [
360 'startFile' => $testInfo[
'file'],
361 'startTest' => $testInfo[
'desc']
362 ] + $this->session ) ] );
364 print "pcntl_exec() failed\n";
370 for ( $i = 1; $i < $testInfo[
'line']; $i++ ) {
371 $line = fgets( $file );
372 if (
$line ===
false ) {
373 print "Error reading from file\n";
376 $initialPart .=
$line;
379 $line = fgets( $file );
380 if ( !preg_match(
'/^!!\s*test/',
$line ) ) {
381 print "Test has moved, cannot edit\n";
387 $desc = fgets( $file );
388 if ( trim( $desc ) !== $testInfo[
'desc'] ) {
389 print "Description does not match, cannot edit\n";
393 return [ $initialPart, $testPart ];
397 if ( is_writable( $inputFileName ) ) {
398 $outputFileName = $inputFileName;
400 $outputFileName =
wfTempDir() .
'/' . basename( $inputFileName );
401 print "Cannot write to input file, writing to $outputFileName instead\n";
403 return $outputFileName;
406 protected function editTest( $fileName, $deletions, $changes ) {
407 $text = file_get_contents( $fileName );
408 if ( $text ===
false ) {
409 print "Unable to open test file!";
417 if ( is_writable( $fileName ) ) {
418 file_put_contents( $fileName, $result );
419 print "Wrote updated file\n";
421 print "Cannot write updated file, here is a patch you can paste:\n\n";
423 "--- {$fileName}\n" .
424 "+++ {$fileName}~\n" .
430 protected function update( $testInfo, $result ) {
434 $testInfo[
'test'] => [
435 $testInfo[
'resultSection'] => [
437 'value' => $result->actual .
"\n"
446 [ $testInfo[
'test'] ],
452 $resultSection = $testInfo[
'resultSection'];
453 if ( in_array( $resultSection, [
'html/php',
'html/*',
'html',
'result' ] ) ) {
454 $newSection =
'html+tidy';
455 } elseif ( in_array( $resultSection, [
'html/php+tidy',
'html+tidy' ] ) ) {
456 $newSection =
'html';
458 print "Unrecognised result section name \"$resultSection\"";
465 $testInfo[
'test'] => [
468 'value' => $newSection
479 $testInfo[
'test'] => [
480 $testInfo[
'resultSection'] => [
$wgParserTestFiles
Parser test suite files to be run by parserTests.php when no specific filename is passed to it.
wfTempDir()
Tries to get the system directory for temporary files.
Terminal that supports ANSI escape sequences.
Class representing a 'diff' between two sequences of strings.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
static getTermSize()
Get the terminal size as a two-element array where the first element is the width (number of columns)...
static requireTestsAutoloader()
Call this to set up the autoloader to allow classes to be used from the tests directory.
hasOption( $name)
Checks to see if a particular param exists.
static readconsole( $prompt='> ')
Prompt the console for input.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
Interactive parser test runner and test file editor.
handleFailure( $index, $testInfo, $result)
__construct()
Default constructor.
deleteSubtest( $testInfo)
update( $testInfo, $result)
unifiedDiff( $left, $right)
findTest( $file, $testInfo)
dwdiff( $expected, $actual)
alternatingAligned( $expectedStr, $actualStr)
finalSetup()
Handle some last-minute setup here.
execute()
Do the actual work.
getOutputFileName( $inputFileName)
editTest( $fileName, $deletions, $changes)
static edit( $text, array $deletions, array $changes, $warningCallback=null)
static read( $file, array $options=[])
Interface to record parser test results.
static applyInitialConfig()
This should be called before Setup.php, e.g.
external whereas SearchGetNearMatch runs after $term
namespace being checked & $result
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
this hook is for auditing only $response
require_once RUN_MAINTENANCE_IF_MAIN