7 function __construct( $parent ) {
8 $this->parent = $parent;
9 $this->term = $parent->term;
17 function record( $test, $result ) {
19 $this->success += ( $result ? 1 : 0 );
27 if ( $this->total > 0 ) {
28 $this->reportPercentage( $this->success, $this->total );
30 throw new MWException( "No tests found.\n" );
34 function reportPercentage( $success, $total ) {
35 $ratio = wfPercent( 100 * $success / $total );
36 print $this->term->color( 1 ) . "Passed $success of $total tests ($ratio)... ";
38 if ( $success == $total ) {
39 print $this->term->color( 32 ) . "ALL TESTS PASSED!";
41 $failed = $total - $success ;
42 print $this->term->color( 31 ) . "$failed tests failed!";
45 print $this->term->reset() . "\n";
47 return ( $success == $total );
51 class DbTestPreviewer extends TestRecorder {
52 protected $lb; // /< Database load balancer
53 protected $db; // /< Database connection to the main DB
54 protected $curRun; // /< run ID number for the current run
55 protected $prevRun; // /< run ID number for the previous run, if any
56 protected $results; // /< Result array
59 * This should be called before the table prefix is changed
61 function __construct( $parent ) {
62 parent::__construct( $parent );
64 $this->lb = wfGetLBFactory()->newMainLB();
65 // This connection will have the wiki's table prefix, not parsertest_
66 $this->db = $this->lb->getConnection( DB_MASTER );
70 * Set up result recording; insert a record for the run with the date
71 * and all that fun stuff
76 if ( ! $this->db->tableExists( 'testrun', __METHOD__ )
77 || ! $this->db->tableExists( 'testitem', __METHOD__ ) )
79 print "WARNING> `testrun` table not found in database.\n";
80 $this->prevRun = false;
82 // We'll make comparisons against the previous run later...
83 $this->prevRun = $this->db->selectField( 'testrun', 'MAX(tr_id)' );
86 $this->results = array();
89 function record( $test, $result ) {
90 parent::record( $test, $result );
91 $this->results[$test] = $result;
95 if ( $this->prevRun ) {
96 // f = fail, p = pass, n = nonexistent
97 // codes show before then after
99 'fp' => 'previously failing test(s) now PASSING! :)',
100 'pn' => 'previously PASSING test(s) removed o_O',
101 'np' => 'new PASSING test(s) :)',
103 'pf' => 'previously passing test(s) now FAILING! :(',
104 'fn' => 'previously FAILING test(s) removed O_o',
105 'nf' => 'new FAILING test(s) :(',
106 'ff' => 'still FAILING test(s) :(',
109 $prevResults = array();
111 $res = $this->db->select( 'testitem', array( 'ti_name', 'ti_success' ),
112 array( 'ti_run' => $this->prevRun ), __METHOD__ );
114 foreach ( $res as $row ) {
115 if ( !$this->parent->regex
116 || preg_match( "/{$this->parent->regex}/i", $row->ti_name ) )
118 $prevResults[$row->ti_name] = $row->ti_success;
122 $combined = array_keys( $this->results + $prevResults );
124 # Determine breakdown by change type
125 $breakdown = array();
126 foreach ( $combined as $test ) {
127 if ( !isset( $prevResults[$test] ) ) {
129 } elseif ( $prevResults[$test] == 1 ) {
131 } else /* if ( $prevResults[$test] == 0 )*/ {
135 if ( !isset( $this->results[$test] ) ) {
137 } elseif ( $this->results[$test] == 1 ) {
139 } else /*if ( $this->results[$test] == 0 ) */ {
143 $code = $before . $after;
145 if ( isset( $table[$code] ) ) {
146 $breakdown[$code][$test] = $this->getTestStatusInfo( $test, $after );
151 foreach ( $table as $code => $label ) {
152 if ( !empty( $breakdown[$code] ) ) {
153 $count = count( $breakdown[$code] );
154 printf( "\n%4d %s\n", $count, $label );
156 foreach ( $breakdown[$code] as $differing_test_name => $statusInfo ) {
157 print " * $differing_test_name [$statusInfo]\n";
162 print "No previous test runs to compare against.\n";
170 * Returns a string giving information about when a test last had a status change.
171 * Could help to track down when regressions were introduced, as distinct from tests
172 * which have never passed (which are more change requests than regressions).
174 private function getTestStatusInfo( $testname, $after ) {
175 // If we're looking at a test that has just been removed, then say when it first appeared.
176 if ( $after == 'n' ) {
177 $changedRun = $this->db->selectField ( 'testitem',
179 array( 'ti_name' => $testname ),
181 $appear = $this->db->selectRow ( 'testrun',
182 array( 'tr_date', 'tr_mw_version' ),
183 array( 'tr_id' => $changedRun ),
186 return "First recorded appearance: "
187 . date( "d-M-Y H:i:s", strtotime ( $appear->tr_date ) )
188 . ", " . $appear->tr_mw_version;
191 // Otherwise, this test has previous recorded results.
192 // See when this test last had a different result to what we're seeing now.
194 'ti_name' => $testname,
195 'ti_success' => ( $after == 'f' ? "1" : "0" ) );
197 if ( $this->curRun ) {
198 $conds[] = "ti_run != " . $this->db->addQuotes ( $this->curRun );
201 $changedRun = $this->db->selectField ( 'testitem', 'MAX(ti_run)', $conds, __METHOD__ );
203 // If no record of ever having had a different result.
204 if ( is_null ( $changedRun ) ) {
205 if ( $after == "f" ) {
206 return "Has never passed";
208 return "Has never failed";
212 // Otherwise, we're looking at a test whose status has changed.
213 // (i.e. it used to work, but now doesn't; or used to fail, but is now fixed.)
214 // In this situation, give as much info as we can as to when it changed status.
215 $pre = $this->db->selectRow ( 'testrun',
216 array( 'tr_date', 'tr_mw_version' ),
217 array( 'tr_id' => $changedRun ),
219 $post = $this->db->selectRow ( 'testrun',
220 array( 'tr_date', 'tr_mw_version' ),
221 array( "tr_id > " . $this->db->addQuotes ( $changedRun ) ),
223 array( "LIMIT" => 1, "ORDER BY" => 'tr_id' )
227 $postDate = date( "d-M-Y H:i:s", strtotime ( $post->tr_date ) ) . ", {$post->tr_mw_version}";
232 return ( $after == "f" ? "Introduced" : "Fixed" ) . " between "
233 . date( "d-M-Y H:i:s", strtotime ( $pre->tr_date ) ) . ", " . $pre->tr_mw_version
239 * Commit transaction and clean up for result recording
242 $this->lb->commitMasterChanges();
243 $this->lb->closeAll();
249 class DbTestRecorder extends DbTestPreviewer {
253 * Set up result recording; insert a record for the run with the date
254 * and all that fun stuff
259 if ( ! $this->db->tableExists( 'testrun' )
260 || ! $this->db->tableExists( 'testitem' ) )
262 print "WARNING> `testrun` table not found in database. Trying to create table.\n";
263 $this->db->sourceFile( $this->db->patchPath( 'patch-testrun.sql' ) );
264 echo "OK, resuming.\n";
269 $this->db->insert( 'testrun',
271 'tr_date' => $this->db->timestamp(),
272 'tr_mw_version' => $this->version,
273 'tr_php_version' => phpversion(),
274 'tr_db_version' => $this->db->getServerVersion(),
275 'tr_uname' => php_uname()
278 if ( $this->db->getType() === 'postgres' ) {
279 $this->curRun = $this->db->currentSequenceValue( 'testrun_id_seq' );
281 $this->curRun = $this->db->insertId();
286 * Record an individual test item's success or failure to the db
288 * @param $test String
289 * @param $result Boolean
291 function record( $test, $result ) {
292 parent::record( $test, $result );
294 $this->db->insert( 'testitem',
296 'ti_run' => $this->curRun,
298 'ti_success' => $result ? 1 : 0,
304 class TestFileIterator implements Iterator {
307 private $parserTest; /* An instance of ParserTest (parserTests.php) or MediaWikiParserTest (phpunit) */
310 private $section = null; /** String|null: current test section being analyzed */
311 private $sectionData = array();
315 function __construct( $file, $parserTest ) {
319 $this->fh = fopen( $this->file, "rt" );
322 throw new MWException( "Couldn't open file '$file'\n" );
325 $this->parserTest = $parserTest;
326 $this->parserTest->showRunFile( wfRelativePath( $this->file, $IP ) );
328 $this->lineNum = $this->index = 0;
332 if ( fseek( $this->fh, 0 ) ) {
333 throw new MWException( "Couldn't fseek to the start of '$this->file'\n" );
353 if ( $this->readNextTest() ) {
362 return $this->eof != true;
365 function readNextTest() {
366 $this->clearSection();
368 # Create a fake parser tests which never run anything unless
369 # asked to do so. This will avoid running hooks for a disabled test
370 $delayedParserTest = new DelayedParserTest();
372 while ( false !== ( $line = fgets( $this->fh ) ) ) {
376 if ( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
377 $this->section = strtolower( $matches[1] );
379 if ( $this->section == 'endarticle' ) {
380 $this->checkSection( 'text' );
381 $this->checkSection( 'article' );
383 $this->parserTest->addArticle( ParserTest::chomp( $this->sectionData['article'] ), $this->sectionData['text'], $this->lineNum );
385 $this->clearSection();
390 if ( $this->section == 'endhooks' ) {
391 $this->checkSection( 'hooks' );
393 foreach ( explode( "\n", $this->sectionData['hooks'] ) as $line ) {
394 $line = trim( $line );
397 $delayedParserTest->requireHook( $line );
401 $this->clearSection();
406 if ( $this->section == 'endfunctionhooks' ) {
407 $this->checkSection( 'functionhooks' );
409 foreach ( explode( "\n", $this->sectionData['functionhooks'] ) as $line ) {
410 $line = trim( $line );
413 $delayedParserTest->requireFunctionHook( $line );
417 $this->clearSection();
422 if ( $this->section == 'end' ) {
423 $this->checkSection( 'test' );
424 $this->checkSection( 'input' );
425 $this->checkSection( 'result' );
427 if ( !isset( $this->sectionData['options'] ) ) {
428 $this->sectionData['options'] = '';
431 if ( !isset( $this->sectionData['config'] ) ) {
432 $this->sectionData['config'] = '';
435 if ( ( ( preg_match( '/\\bdisabled\\b/i', $this->sectionData['options'] ) && !$this->parserTest->runDisabled )
436 || !preg_match( "/" . $this->parserTest->regex . "/i", $this->sectionData['test'] ) ) ) {
438 $this->clearSection();
440 # Forget any pending hooks call since test is disabled
441 $delayedParserTest->reset();
446 # We are really going to run the test, run pending hooks and hooks function
447 wfDebug( __METHOD__ . " unleashing delayed test for: {$this->sectionData['test']}" );
448 $hooksResult = $delayedParserTest->unleash( $this->parserTest );
449 if( !$hooksResult ) {
450 # Some hook reported an issue. Abort.
455 'test' => ParserTest::chomp( $this->sectionData['test'] ),
456 'input' => ParserTest::chomp( $this->sectionData['input'] ),
457 'result' => ParserTest::chomp( $this->sectionData['result'] ),
458 'options' => ParserTest::chomp( $this->sectionData['options'] ),
459 'config' => ParserTest::chomp( $this->sectionData['config'] ),
465 if ( isset ( $this->sectionData[$this->section] ) ) {
466 throw new MWException( "duplicate section '$section' at line {$this->lineNum} of $this->file\n" );
469 $this->sectionData[$this->section] = '';
474 if ( $this->section ) {
475 $this->sectionData[$this->section] .= $line;
484 * Clear section name and its data
486 private function clearSection() {
487 $this->sectionData = array();
488 $this->section = null;
493 * Verify the current section data has some value for the given token
494 * name (first parameter).
495 * Throw an exception if it is not set, referencing current section
496 * and adding the current file name and line number
498 * @param $token String: expected token that should have been mentionned before closing this section
500 private function checkSection( $token ) {
501 if( is_null( $this->section ) ) {
502 throw new MWException( __METHOD__ . " can not verify a null section!\n" );
505 if( !isset($this->sectionData[$token]) ) {
506 throw new MWException( sprintf(
507 "'%s' without '%s' at line %s of %s\n",
520 * A class to delay execution of a parser test hooks.
523 class DelayedParserTest {
525 /** Initialized on construction */
529 public function __construct() {
534 * Init/reset or forgot about the current delayed test.
535 * Call to this will erase any hooks function that were pending.
537 public function reset() {
538 $this->hooks = array();
539 $this->fnHooks = array();
543 * Called whenever we actually want to run the hook.
544 * Should be the case if we found the parserTest is not disabled
546 public function unleash( &$parserTest ) {
547 if( !($parserTest instanceof ParserTest || $parserTest instanceof NewParserTest
549 throw new MWException( __METHOD__ . " must be passed an instance of ParserTest or NewParserTest classes\n" );
552 # Trigger delayed hooks. Any failure will make us abort
553 foreach( $this->hooks as $hook ) {
554 $ret = $parserTest->requireHook( $hook );
560 # Trigger delayed function hooks. Any failure will make us abort
561 foreach( $this->fnHooks as $fnHook ) {
562 $ret = $parserTest->requireFunctionHook( $fnHook );
568 # Delayed execution was successful.
573 * Similar to ParserTest object but does not run anything
574 * Use unleash() to really execute the hook
576 public function requireHook( $hook ) {
577 $this->hooks[] = $hook;
580 * Similar to ParserTest object but does not run anything
581 * Use unleash() to really execute the hook function
583 public function requireFunctionHook( $fnHook ) {
584 $this->fnHooks[] = $fnHook;