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