MediaWiki
REL1_21
|
00001 <?php 00024 class TestRecorder { 00025 var $parent; 00026 var $term; 00027 00028 function __construct( $parent ) { 00029 $this->parent = $parent; 00030 $this->term = $parent->term; 00031 } 00032 00033 function start() { 00034 $this->total = 0; 00035 $this->success = 0; 00036 } 00037 00038 function record( $test, $result ) { 00039 $this->total++; 00040 $this->success += ( $result ? 1 : 0 ); 00041 } 00042 00043 function end() { 00044 // dummy 00045 } 00046 00047 function report() { 00048 if ( $this->total > 0 ) { 00049 $this->reportPercentage( $this->success, $this->total ); 00050 } else { 00051 throw new MWException( "No tests found.\n" ); 00052 } 00053 } 00054 00055 function reportPercentage( $success, $total ) { 00056 $ratio = wfPercent( 100 * $success / $total ); 00057 print $this->term->color( 1 ) . "Passed $success of $total tests ($ratio)... "; 00058 00059 if ( $success == $total ) { 00060 print $this->term->color( 32 ) . "ALL TESTS PASSED!"; 00061 } else { 00062 $failed = $total - $success; 00063 print $this->term->color( 31 ) . "$failed tests failed!"; 00064 } 00065 00066 print $this->term->reset() . "\n"; 00067 00068 return ( $success == $total ); 00069 } 00070 } 00071 00072 class DbTestPreviewer extends TestRecorder { 00073 protected $lb; // /< Database load balancer 00074 protected $db; // /< Database connection to the main DB 00075 protected $curRun; // /< run ID number for the current run 00076 protected $prevRun; // /< run ID number for the previous run, if any 00077 protected $results; // /< Result array 00078 00082 function __construct( $parent ) { 00083 parent::__construct( $parent ); 00084 00085 $this->lb = wfGetLBFactory()->newMainLB(); 00086 // This connection will have the wiki's table prefix, not parsertest_ 00087 $this->db = $this->lb->getConnection( DB_MASTER ); 00088 } 00089 00094 function start() { 00095 parent::start(); 00096 00097 if ( !$this->db->tableExists( 'testrun', __METHOD__ ) 00098 || !$this->db->tableExists( 'testitem', __METHOD__ ) 00099 ) { 00100 print "WARNING> `testrun` table not found in database.\n"; 00101 $this->prevRun = false; 00102 } else { 00103 // We'll make comparisons against the previous run later... 00104 $this->prevRun = $this->db->selectField( 'testrun', 'MAX(tr_id)' ); 00105 } 00106 00107 $this->results = array(); 00108 } 00109 00110 function record( $test, $result ) { 00111 parent::record( $test, $result ); 00112 $this->results[$test] = $result; 00113 } 00114 00115 function report() { 00116 if ( $this->prevRun ) { 00117 // f = fail, p = pass, n = nonexistent 00118 // codes show before then after 00119 $table = array( 00120 'fp' => 'previously failing test(s) now PASSING! :)', 00121 'pn' => 'previously PASSING test(s) removed o_O', 00122 'np' => 'new PASSING test(s) :)', 00123 00124 'pf' => 'previously passing test(s) now FAILING! :(', 00125 'fn' => 'previously FAILING test(s) removed O_o', 00126 'nf' => 'new FAILING test(s) :(', 00127 'ff' => 'still FAILING test(s) :(', 00128 ); 00129 00130 $prevResults = array(); 00131 00132 $res = $this->db->select( 'testitem', array( 'ti_name', 'ti_success' ), 00133 array( 'ti_run' => $this->prevRun ), __METHOD__ ); 00134 00135 foreach ( $res as $row ) { 00136 if ( !$this->parent->regex 00137 || preg_match( "/{$this->parent->regex}/i", $row->ti_name ) 00138 ) { 00139 $prevResults[$row->ti_name] = $row->ti_success; 00140 } 00141 } 00142 00143 $combined = array_keys( $this->results + $prevResults ); 00144 00145 # Determine breakdown by change type 00146 $breakdown = array(); 00147 foreach ( $combined as $test ) { 00148 if ( !isset( $prevResults[$test] ) ) { 00149 $before = 'n'; 00150 } elseif ( $prevResults[$test] == 1 ) { 00151 $before = 'p'; 00152 } else /* if ( $prevResults[$test] == 0 )*/ { 00153 $before = 'f'; 00154 } 00155 00156 if ( !isset( $this->results[$test] ) ) { 00157 $after = 'n'; 00158 } elseif ( $this->results[$test] == 1 ) { 00159 $after = 'p'; 00160 } else /*if ( $this->results[$test] == 0 ) */ { 00161 $after = 'f'; 00162 } 00163 00164 $code = $before . $after; 00165 00166 if ( isset( $table[$code] ) ) { 00167 $breakdown[$code][$test] = $this->getTestStatusInfo( $test, $after ); 00168 } 00169 } 00170 00171 # Write out results 00172 foreach ( $table as $code => $label ) { 00173 if ( !empty( $breakdown[$code] ) ) { 00174 $count = count( $breakdown[$code] ); 00175 printf( "\n%4d %s\n", $count, $label ); 00176 00177 foreach ( $breakdown[$code] as $differing_test_name => $statusInfo ) { 00178 print " * $differing_test_name [$statusInfo]\n"; 00179 } 00180 } 00181 } 00182 } else { 00183 print "No previous test runs to compare against.\n"; 00184 } 00185 00186 print "\n"; 00187 parent::report(); 00188 } 00189 00195 private function getTestStatusInfo( $testname, $after ) { 00196 // If we're looking at a test that has just been removed, then say when it first appeared. 00197 if ( $after == 'n' ) { 00198 $changedRun = $this->db->selectField( 'testitem', 00199 'MIN(ti_run)', 00200 array( 'ti_name' => $testname ), 00201 __METHOD__ ); 00202 $appear = $this->db->selectRow( 'testrun', 00203 array( 'tr_date', 'tr_mw_version' ), 00204 array( 'tr_id' => $changedRun ), 00205 __METHOD__ ); 00206 00207 return "First recorded appearance: " 00208 . date( "d-M-Y H:i:s", strtotime( $appear->tr_date ) ) 00209 . ", " . $appear->tr_mw_version; 00210 } 00211 00212 // Otherwise, this test has previous recorded results. 00213 // See when this test last had a different result to what we're seeing now. 00214 $conds = array( 00215 'ti_name' => $testname, 00216 'ti_success' => ( $after == 'f' ? "1" : "0" ) ); 00217 00218 if ( $this->curRun ) { 00219 $conds[] = "ti_run != " . $this->db->addQuotes( $this->curRun ); 00220 } 00221 00222 $changedRun = $this->db->selectField( 'testitem', 'MAX(ti_run)', $conds, __METHOD__ ); 00223 00224 // If no record of ever having had a different result. 00225 if ( is_null( $changedRun ) ) { 00226 if ( $after == "f" ) { 00227 return "Has never passed"; 00228 } else { 00229 return "Has never failed"; 00230 } 00231 } 00232 00233 // Otherwise, we're looking at a test whose status has changed. 00234 // (i.e. it used to work, but now doesn't; or used to fail, but is now fixed.) 00235 // In this situation, give as much info as we can as to when it changed status. 00236 $pre = $this->db->selectRow( 'testrun', 00237 array( 'tr_date', 'tr_mw_version' ), 00238 array( 'tr_id' => $changedRun ), 00239 __METHOD__ ); 00240 $post = $this->db->selectRow( 'testrun', 00241 array( 'tr_date', 'tr_mw_version' ), 00242 array( "tr_id > " . $this->db->addQuotes( $changedRun ) ), 00243 __METHOD__, 00244 array( "LIMIT" => 1, "ORDER BY" => 'tr_id' ) 00245 ); 00246 00247 if ( $post ) { 00248 $postDate = date( "d-M-Y H:i:s", strtotime( $post->tr_date ) ) . ", {$post->tr_mw_version}"; 00249 } else { 00250 $postDate = 'now'; 00251 } 00252 00253 return ( $after == "f" ? "Introduced" : "Fixed" ) . " between " 00254 . date( "d-M-Y H:i:s", strtotime( $pre->tr_date ) ) . ", " . $pre->tr_mw_version 00255 . " and $postDate"; 00256 } 00257 00261 function end() { 00262 $this->lb->commitMasterChanges(); 00263 $this->lb->closeAll(); 00264 parent::end(); 00265 } 00266 } 00267 00268 class DbTestRecorder extends DbTestPreviewer { 00269 var $version; 00270 00275 function start() { 00276 $this->db->begin( __METHOD__ ); 00277 00278 if ( !$this->db->tableExists( 'testrun' ) 00279 || !$this->db->tableExists( 'testitem' ) 00280 ) { 00281 print "WARNING> `testrun` table not found in database. Trying to create table.\n"; 00282 $this->db->sourceFile( $this->db->patchPath( 'patch-testrun.sql' ) ); 00283 echo "OK, resuming.\n"; 00284 } 00285 00286 parent::start(); 00287 00288 $this->db->insert( 'testrun', 00289 array( 00290 'tr_date' => $this->db->timestamp(), 00291 'tr_mw_version' => $this->version, 00292 'tr_php_version' => phpversion(), 00293 'tr_db_version' => $this->db->getServerVersion(), 00294 'tr_uname' => php_uname() 00295 ), 00296 __METHOD__ ); 00297 if ( $this->db->getType() === 'postgres' ) { 00298 $this->curRun = $this->db->currentSequenceValue( 'testrun_id_seq' ); 00299 } else { 00300 $this->curRun = $this->db->insertId(); 00301 } 00302 } 00303 00310 function record( $test, $result ) { 00311 parent::record( $test, $result ); 00312 00313 $this->db->insert( 'testitem', 00314 array( 00315 'ti_run' => $this->curRun, 00316 'ti_name' => $test, 00317 'ti_success' => $result ? 1 : 0, 00318 ), 00319 __METHOD__ ); 00320 } 00321 } 00322 00323 class TestFileIterator implements Iterator { 00324 private $file; 00325 private $fh; 00326 private $parserTest; /* An instance of ParserTest (parserTests.php) or MediaWikiParserTest (phpunit) */ 00327 private $index = 0; 00328 private $test; 00329 private $section = null; 00331 private $sectionData = array(); 00332 private $lineNum; 00333 private $eof; 00334 00335 function __construct( $file, $parserTest ) { 00336 $this->file = $file; 00337 $this->fh = fopen( $this->file, "rt" ); 00338 00339 if ( !$this->fh ) { 00340 throw new MWException( "Couldn't open file '$file'\n" ); 00341 } 00342 00343 $this->parserTest = $parserTest; 00344 00345 $this->lineNum = $this->index = 0; 00346 } 00347 00348 function rewind() { 00349 if ( fseek( $this->fh, 0 ) ) { 00350 throw new MWException( "Couldn't fseek to the start of '$this->file'\n" ); 00351 } 00352 00353 $this->index = -1; 00354 $this->lineNum = 0; 00355 $this->eof = false; 00356 $this->next(); 00357 00358 return true; 00359 } 00360 00361 function current() { 00362 return $this->test; 00363 } 00364 00365 function key() { 00366 return $this->index; 00367 } 00368 00369 function next() { 00370 if ( $this->readNextTest() ) { 00371 $this->index++; 00372 return true; 00373 } else { 00374 $this->eof = true; 00375 } 00376 } 00377 00378 function valid() { 00379 return $this->eof != true; 00380 } 00381 00382 function readNextTest() { 00383 $this->clearSection(); 00384 00385 # Create a fake parser tests which never run anything unless 00386 # asked to do so. This will avoid running hooks for a disabled test 00387 $delayedParserTest = new DelayedParserTest(); 00388 00389 while ( false !== ( $line = fgets( $this->fh ) ) ) { 00390 $this->lineNum++; 00391 $matches = array(); 00392 00393 if ( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) { 00394 $this->section = strtolower( $matches[1] ); 00395 00396 if ( $this->section == 'endarticle' ) { 00397 $this->checkSection( 'text' ); 00398 $this->checkSection( 'article' ); 00399 00400 $this->parserTest->addArticle( ParserTest::chomp( $this->sectionData['article'] ), $this->sectionData['text'], $this->lineNum ); 00401 00402 $this->clearSection(); 00403 00404 continue; 00405 } 00406 00407 if ( $this->section == 'endhooks' ) { 00408 $this->checkSection( 'hooks' ); 00409 00410 foreach ( explode( "\n", $this->sectionData['hooks'] ) as $line ) { 00411 $line = trim( $line ); 00412 00413 if ( $line ) { 00414 $delayedParserTest->requireHook( $line ); 00415 } 00416 } 00417 00418 $this->clearSection(); 00419 00420 continue; 00421 } 00422 00423 if ( $this->section == 'endfunctionhooks' ) { 00424 $this->checkSection( 'functionhooks' ); 00425 00426 foreach ( explode( "\n", $this->sectionData['functionhooks'] ) as $line ) { 00427 $line = trim( $line ); 00428 00429 if ( $line ) { 00430 $delayedParserTest->requireFunctionHook( $line ); 00431 } 00432 } 00433 00434 $this->clearSection(); 00435 00436 continue; 00437 } 00438 00439 if ( $this->section == 'end' ) { 00440 $this->checkSection( 'test' ); 00441 $this->checkSection( 'input' ); 00442 $this->checkSection( 'result' ); 00443 00444 if ( !isset( $this->sectionData['options'] ) ) { 00445 $this->sectionData['options'] = ''; 00446 } 00447 00448 if ( !isset( $this->sectionData['config'] ) ) { 00449 $this->sectionData['config'] = ''; 00450 } 00451 00452 if ( ( ( preg_match( '/\\bdisabled\\b/i', $this->sectionData['options'] ) && !$this->parserTest->runDisabled ) 00453 || ( preg_match( '/\\bparsoid\\b/i', $this->sectionData['options'] ) && !$this->parserTest->runParsoid ) 00454 || !preg_match( "/" . $this->parserTest->regex . "/i", $this->sectionData['test'] ) ) 00455 ) { 00456 # disabled test 00457 $this->clearSection(); 00458 00459 # Forget any pending hooks call since test is disabled 00460 $delayedParserTest->reset(); 00461 00462 continue; 00463 } 00464 00465 # We are really going to run the test, run pending hooks and hooks function 00466 wfDebug( __METHOD__ . " unleashing delayed test for: {$this->sectionData['test']}" ); 00467 $hooksResult = $delayedParserTest->unleash( $this->parserTest ); 00468 if ( !$hooksResult ) { 00469 # Some hook reported an issue. Abort. 00470 return false; 00471 } 00472 00473 $this->test = array( 00474 'test' => ParserTest::chomp( $this->sectionData['test'] ), 00475 'input' => ParserTest::chomp( $this->sectionData['input'] ), 00476 'result' => ParserTest::chomp( $this->sectionData['result'] ), 00477 'options' => ParserTest::chomp( $this->sectionData['options'] ), 00478 'config' => ParserTest::chomp( $this->sectionData['config'] ), 00479 ); 00480 00481 return true; 00482 } 00483 00484 if ( isset ( $this->sectionData[$this->section] ) ) { 00485 throw new MWException( "duplicate section '$this->section' at line {$this->lineNum} of $this->file\n" ); 00486 } 00487 00488 $this->sectionData[$this->section] = ''; 00489 00490 continue; 00491 } 00492 00493 if ( $this->section ) { 00494 $this->sectionData[$this->section] .= $line; 00495 } 00496 } 00497 00498 return false; 00499 } 00500 00501 00505 private function clearSection() { 00506 $this->sectionData = array(); 00507 $this->section = null; 00508 00509 } 00510 00519 private function checkSection( $token ) { 00520 if ( is_null( $this->section ) ) { 00521 throw new MWException( __METHOD__ . " can not verify a null section!\n" ); 00522 } 00523 00524 if ( !isset( $this->sectionData[$token] ) ) { 00525 throw new MWException( sprintf( 00526 "'%s' without '%s' at line %s of %s\n", 00527 $this->section, 00528 $token, 00529 $this->lineNum, 00530 $this->file 00531 ) ); 00532 } 00533 return true; 00534 } 00535 } 00536 00540 class DelayedParserTest { 00541 00543 private $hooks; 00544 private $fnHooks; 00545 00546 public function __construct() { 00547 $this->reset(); 00548 } 00549 00554 public function reset() { 00555 $this->hooks = array(); 00556 $this->fnHooks = array(); 00557 } 00558 00563 public function unleash( &$parserTest ) { 00564 if ( !( $parserTest instanceof ParserTest || $parserTest instanceof NewParserTest ) ) { 00565 throw new MWException( __METHOD__ . " must be passed an instance of ParserTest or NewParserTest classes\n" ); 00566 } 00567 00568 # Trigger delayed hooks. Any failure will make us abort 00569 foreach ( $this->hooks as $hook ) { 00570 $ret = $parserTest->requireHook( $hook ); 00571 if ( !$ret ) { 00572 return false; 00573 } 00574 } 00575 00576 # Trigger delayed function hooks. Any failure will make us abort 00577 foreach ( $this->fnHooks as $fnHook ) { 00578 $ret = $parserTest->requireFunctionHook( $fnHook ); 00579 if ( !$ret ) { 00580 return false; 00581 } 00582 } 00583 00584 # Delayed execution was successful. 00585 return true; 00586 } 00587 00592 public function requireHook( $hook ) { 00593 $this->hooks[] = $hook; 00594 } 00595 00600 public function requireFunctionHook( $fnHook ) { 00601 $this->fnHooks[] = $fnHook; 00602 } 00603 00604 }