MediaWiki REL1_30
editTests.php
Go to the documentation of this file.
1<?php
2
3require __DIR__.'/../../maintenance/Maintenance.php';
4
5define( 'MW_PARSER_TEST', true );
6
11 private $termWidth;
12 private $testFiles;
13 private $testCount;
14 private $recorder;
15 private $runner;
16 private $numExecuted;
17 private $numSkipped;
18 private $numFailed;
19
20 function __construct() {
21 parent::__construct();
22 $this->addOption( 'session-data', 'internal option, do not use', false, true );
23 $this->addOption( 'use-tidy-config',
24 'Use the wiki\'s Tidy configuration instead of known-good' .
25 'defaults.' );
26 }
27
28 public function finalSetup() {
29 parent::finalSetup();
32 }
33
34 public function execute() {
35 $this->termWidth = $this->getTermSize()[0] - 1;
36
37 $this->recorder = new TestRecorder();
38 $this->setupFileData();
39
40 if ( $this->hasOption( 'session-data' ) ) {
41 $this->session = json_decode( $this->getOption( 'session-data' ), true );
42 } else {
43 $this->session = [ 'options' => [] ];
44 }
45 if ( $this->hasOption( 'use-tidy-config' ) ) {
46 $this->session['options']['use-tidy-config'] = true;
47 }
48 $this->runner = new ParserTestRunner( $this->recorder, $this->session['options'] );
49
50 $this->runTests();
51
52 if ( $this->numFailed === 0 ) {
53 if ( $this->numSkipped === 0 ) {
54 print "All tests passed!\n";
55 } else {
56 print "All tests passed (but skipped {$this->numSkipped})\n";
57 }
58 return;
59 }
60 print "{$this->numFailed} test(s) failed.\n";
61 $this->showResults();
62 }
63
64 protected function setupFileData() {
66 $this->testFiles = [];
67 $this->testCount = 0;
68 foreach ( $wgParserTestFiles as $file ) {
69 $fileInfo = TestFileReader::read( $file );
70 $this->testFiles[$file] = $fileInfo;
71 $this->testCount += count( $fileInfo['tests'] );
72 }
73 }
74
75 protected function runTests() {
76 $teardown = $this->runner->staticSetup();
77 $teardown = $this->runner->setupDatabase( $teardown );
78 $teardown = $this->runner->setupUploads( $teardown );
79
80 print "Running tests...\n";
81 $this->results = [];
82 $this->numExecuted = 0;
83 $this->numSkipped = 0;
84 $this->numFailed = 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 ) {
90 $this->numSkipped++;
91 } elseif ( !$result->isSuccess() ) {
92 $this->results[$fileName][$testInfo['desc']] = $result;
93 $this->numFailed++;
94 }
95 $this->numExecuted++;
96 $this->showProgress();
97 }
98 }
99 print "\n";
100 }
101
102 protected function showProgress() {
103 $done = $this->numExecuted;
104 $total = $this->testCount;
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 );
109 }
110
111 protected function showResults() {
112 if ( isset( $this->session['startFile'] ) ) {
113 $startFile = $this->session['startFile'];
114 $startTest = $this->session['startTest'];
115 $foundStart = false;
116 } else {
117 $startFile = false;
118 $startTest = false;
119 $foundStart = true;
120 }
121
122 $testIndex = 0;
123 foreach ( $this->testFiles as $fileName => $fileInfo ) {
124 if ( !isset( $this->results[$fileName] ) ) {
125 continue;
126 }
127 if ( !$foundStart && $startFile !== false && $fileName !== $startFile ) {
128 $testIndex += count( $this->results[$fileName] );
129 continue;
130 }
131 foreach ( $fileInfo['tests'] as $testInfo ) {
132 if ( !isset( $this->results[$fileName][$testInfo['desc']] ) ) {
133 continue;
134 }
135 $result = $this->results[$fileName][$testInfo['desc']];
136 $testIndex++;
137 if ( !$foundStart && $startTest !== false ) {
138 if ( $testInfo['desc'] !== $startTest ) {
139 continue;
140 }
141 $foundStart = true;
142 }
143
144 $this->handleFailure( $testIndex, $testInfo, $result );
145 }
146 }
147
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'] );
152 $this->showResults();
153 }
154 print "All done\n";
155 }
156
157 protected function heading( $text ) {
159 $heading = "─── $text ";
160 $heading .= str_repeat( '─', $this->termWidth - mb_strlen( $heading ) );
161 $heading = $term->color( 34 ) . $heading . $term->reset() . "\n";
162 return $heading;
163 }
164
165 protected function unifiedDiff( $left, $right ) {
166 $fromLines = explode( "\n", $left );
167 $toLines = explode( "\n", $right );
168 $formatter = new UnifiedDiffFormatter;
169 return $formatter->format( new Diff( $fromLines, $toLines ) );
170 }
171
172 protected function handleFailure( $index, $testInfo, $result ) {
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";
178
179 print $div1;
180 print "Failure $index/{$this->numFailed}: {$testInfo['file']} line {$testInfo['line']}\n" .
181 "{$testInfo['desc']}\n";
182
183 print $this->heading( 'Input' );
184 print "{$testInfo['input']}\n";
185
186 print $this->heading( 'Alternating expected/actual output' );
187 print $this->alternatingAligned( $result->expected, $result->actual );
188
189 print $this->heading( 'Diff' );
190
191 $dwdiff = $this->dwdiff( $result->expected, $result->actual );
192 if ( $dwdiff !== false ) {
193 $diff = $dwdiff;
194 } else {
195 $diff = $this->unifiedDiff( $result->expected, $result->actual );
196 }
197 print $diff;
198
199 if ( $testInfo['options'] || $testInfo['config'] ) {
200 print $this->heading( 'Options / Config' );
201 if ( $testInfo['options'] ) {
202 print $testInfo['options'] . "\n";
203 }
204 if ( $testInfo['config'] ) {
205 print $testInfo['config'] . "\n";
206 }
207 }
208
209 print $div2;
210 print "What do you want to do?\n";
211 $specs = [
212 '[R]eload code and run again',
213 '[U]pdate source file, copy actual to expected',
214 '[I]gnore' ];
215
216 if ( strpos( $testInfo['options'], ' tidy' ) === false ) {
217 if ( empty( $testInfo['isSubtest'] ) ) {
218 $specs[] = "Enable [T]idy";
219 }
220 } else {
221 $specs[] = 'Disable [T]idy';
222 }
223
224 if ( !empty( $testInfo['isSubtest'] ) ) {
225 $specs[] = 'Delete [s]ubtest';
226 }
227 $specs[] = '[D]elete test';
228 $specs[] = '[Q]uit';
229
230 $options = [];
231 foreach ( $specs as $spec ) {
232 if ( !preg_match( '/^(.*\[)(.)(\].*)$/', $spec, $m ) ) {
233 throw new MWException( 'Invalid option spec: ' . $spec );
234 }
235 print '* ' . $m[1] . $term->color( 35 ) . $m[2] . $term->color( 0 ) . $m[3] . "\n";
236 $options[strtoupper( $m[2] )] = true;
237 }
238
239 do {
240 $response = $this->readconsole();
241 $cmdResult = false;
242 if ( $response === false ) {
243 exit( 0 );
244 }
245
246 $response = strtoupper( trim( $response ) );
247 if ( !isset( $options[$response] ) ) {
248 print "Invalid response, please enter a single letter from the list above\n";
249 continue;
250 }
251
252 switch ( strtoupper( trim( $response ) ) ) {
253 case 'R':
254 $cmdResult = $this->reload( $testInfo );
255 break;
256 case 'U':
257 $cmdResult = $this->update( $testInfo, $result );
258 break;
259 case 'I':
260 return;
261 case 'T':
262 $cmdResult = $this->switchTidy( $testInfo );
263 break;
264 case 'S':
265 $cmdResult = $this->deleteSubtest( $testInfo );
266 break;
267 case 'D':
268 $cmdResult = $this->deleteTest( $testInfo );
269 break;
270 case 'Q':
271 exit( 0 );
272 }
273 } while ( !$cmdResult );
274 }
275
276 protected function dwdiff( $expected, $actual ) {
277 if ( !is_executable( '/usr/bin/dwdiff' ) ) {
278 return false;
279 }
280
281 $markers = [
282 "\n" => '¶',
283 ' ' => '·',
284 "\t" => '→'
285 ];
286 $markedExpected = strtr( $expected, $markers );
287 $markedActual = strtr( $actual, $markers );
288 $diff = $this->unifiedDiff( $markedExpected, $markedActual );
289
290 $tempFile = tmpfile();
291 fwrite( $tempFile, $diff );
292 fseek( $tempFile, 0 );
293 $pipes = [];
294 $proc = proc_open( '/usr/bin/dwdiff -Pc --diff-input',
295 [ 0 => $tempFile, 1 => [ 'pipe', 'w' ], 2 => STDERR ],
296 $pipes );
297
298 if ( !$proc ) {
299 return false;
300 }
301
302 $result = stream_get_contents( $pipes[1] );
303 proc_close( $proc );
304 fclose( $tempFile );
305 return $result;
306 }
307
308 protected function alternatingAligned( $expectedStr, $actualStr ) {
309 $expectedLines = explode( "\n", $expectedStr );
310 $actualLines = explode( "\n", $actualStr );
311 $maxLines = max( count( $expectedLines ), count( $actualLines ) );
312 $result = '';
313 for ( $i = 0; $i < $maxLines; $i++ ) {
314 if ( $i < count( $expectedLines ) ) {
315 $expectedLine = $expectedLines[$i];
316 $expectedChunks = str_split( $expectedLine, $this->termWidth - 3 );
317 } else {
318 $expectedChunks = [];
319 }
320
321 if ( $i < count( $actualLines ) ) {
322 $actualLine = $actualLines[$i];
323 $actualChunks = str_split( $actualLine, $this->termWidth - 3 );
324 } else {
325 $actualChunks = [];
326 }
327
328 $maxChunks = max( count( $expectedChunks ), count( $actualChunks ) );
329
330 for ( $j = 0; $j < $maxChunks; $j++ ) {
331 if ( isset( $expectedChunks[$j] ) ) {
332 $result .= "E: " . $expectedChunks[$j];
333 if ( $j === count( $expectedChunks ) - 1 ) {
334 $result .= "¶";
335 }
336 $result .= "\n";
337 } else {
338 $result .= "E:\n";
339 }
340 $result .= "\33[4m" . // underline
341 "A: ";
342 if ( isset( $actualChunks[$j] ) ) {
343 $result .= $actualChunks[$j];
344 if ( $j === count( $actualChunks ) - 1 ) {
345 $result .= "¶";
346 }
347 }
348 $result .= "\33[0m\n"; // reset
349 }
350 }
351 return $result;
352 }
353
354 protected function reload( $testInfo ) {
356 pcntl_exec( PHP_BINARY, [
357 $argv[0],
358 '--session-data',
359 json_encode( [
360 'startFile' => $testInfo['file'],
361 'startTest' => $testInfo['desc']
362 ] + $this->session ) ] );
363
364 print "pcntl_exec() failed\n";
365 return false;
366 }
367
368 protected function findTest( $file, $testInfo ) {
369 $initialPart = '';
370 for ( $i = 1; $i < $testInfo['line']; $i++ ) {
371 $line = fgets( $file );
372 if ( $line === false ) {
373 print "Error reading from file\n";
374 return false;
375 }
376 $initialPart .= $line;
377 }
378
379 $line = fgets( $file );
380 if ( !preg_match( '/^!!\s*test/', $line ) ) {
381 print "Test has moved, cannot edit\n";
382 return false;
383 }
384
385 $testPart = $line;
386
387 $desc = fgets( $file );
388 if ( trim( $desc ) !== $testInfo['desc'] ) {
389 print "Description does not match, cannot edit\n";
390 return false;
391 }
392 $testPart .= $desc;
393 return [ $initialPart, $testPart ];
394 }
395
396 protected function getOutputFileName( $inputFileName ) {
397 if ( is_writable( $inputFileName ) ) {
398 $outputFileName = $inputFileName;
399 } else {
400 $outputFileName = wfTempDir() . '/' . basename( $inputFileName );
401 print "Cannot write to input file, writing to $outputFileName instead\n";
402 }
403 return $outputFileName;
404 }
405
406 protected function editTest( $fileName, $deletions, $changes ) {
407 $text = file_get_contents( $fileName );
408 if ( $text === false ) {
409 print "Unable to open test file!";
410 return false;
411 }
412 $result = TestFileEditor::edit( $text, $deletions, $changes,
413 function ( $msg ) {
414 print "$msg\n";
415 }
416 );
417 if ( is_writable( $fileName ) ) {
418 file_put_contents( $fileName, $result );
419 print "Wrote updated file\n";
420 } else {
421 print "Cannot write updated file, here is a patch you can paste:\n\n";
422 print
423 "--- {$fileName}\n" .
424 "+++ {$fileName}~\n" .
425 $this->unifiedDiff( $text, $result ) .
426 "\n";
427 }
428 }
429
430 protected function update( $testInfo, $result ) {
431 $this->editTest( $testInfo['file'],
432 [], // deletions
433 [ // changes
434 $testInfo['test'] => [
435 $testInfo['resultSection'] => [
436 'op' => 'update',
437 'value' => $result->actual . "\n"
438 ]
439 ]
440 ]
441 );
442 }
443
444 protected function deleteTest( $testInfo ) {
445 $this->editTest( $testInfo['file'],
446 [ $testInfo['test'] ], // deletions
447 [] // changes
448 );
449 }
450
451 protected function switchTidy( $testInfo ) {
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';
457 } else {
458 print "Unrecognised result section name \"$resultSection\"";
459 return;
460 }
461
462 $this->editTest( $testInfo['file'],
463 [], // deletions
464 [ // changes
465 $testInfo['test'] => [
466 $resultSection => [
467 'op' => 'rename',
468 'value' => $newSection
469 ]
470 ]
471 ]
472 );
473 }
474
475 protected function deleteSubtest( $testInfo ) {
476 $this->editTest( $testInfo['file'],
477 [], // deletions
478 [ // changes
479 $testInfo['test'] => [
480 $testInfo['resultSection'] => [
481 'op' => 'delete'
482 ]
483 ]
484 ]
485 );
486 }
487}
488
489$maintClass = 'ParserEditTests';
$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.
global $argv
$line
Definition cdb.php:58
Terminal that supports ANSI escape sequences.
Definition MWTerm.php:31
color( $color)
Return ANSI terminal escape code for changing text attribs/color.
Definition MWTerm.php:41
format( $diff)
Format a diff.
Class representing a 'diff' between two sequences of strings.
MediaWiki exception.
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.
Definition editTests.php:10
reload( $testInfo)
handleFailure( $index, $testInfo, $result)
__construct()
Default constructor.
Definition editTests.php:20
deleteSubtest( $testInfo)
update( $testInfo, $result)
unifiedDiff( $left, $right)
findTest( $file, $testInfo)
dwdiff( $expected, $actual)
alternatingAligned( $expectedStr, $actualStr)
switchTidy( $testInfo)
finalSetup()
Handle some last-minute setup here.
Definition editTests.php:28
deleteTest( $testInfo)
execute()
Do the actual work.
Definition editTests.php:34
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.
Definition TestSetup.php:11
A formatter that outputs unified diffs.
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
$maintClass
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:64
external whereas SearchGetNearMatch runs after $term
Definition hooks.txt:2814
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. & $title:Title object for the current page & $request:WebRequest & $ignoreRedirect:boolean to skip redirect check & $target:Title/string of redirect target & $article:Article object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) & $article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetMagic':DEPRECATED! Use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language & $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetSpecialPageAliases':DEPRECATED! Use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language & $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Array with elements of the form "language:title" in the order that they will be output. & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED! Use HtmlPageLinkRendererBegin instead. Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition hooks.txt:1963
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
Definition hooks.txt:1971
this hook is for auditing only $response
Definition hooks.txt:781
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
require_once RUN_MAINTENANCE_IF_MAIN