parserTest: delay hooks execution to the point we really need them
authorAntoine Musso <hashar@users.mediawiki.org>
Tue, 29 Nov 2011 13:54:17 +0000 (13:54 +0000)
committerAntoine Musso <hashar@users.mediawiki.org>
Tue, 29 Nov 2011 13:54:17 +0000 (13:54 +0000)
disabled and filtered out tests were still triggering hooks run. This patch
let us delay hooks running run a little bit until we know we will really need
their execution (i.e. when test is not filtered out and not disabled).

Saving all that unneeded code execution makes running a subset of parser tests
a bit faster when one has many extension enabled.

NOTE: '!!article' sections are still parsed regardless of their usage.
 We would need to add an article/test dependency system to really filter them

tests/testHelpers.inc

index e9a6cb8..1cd611f 100644 (file)
@@ -365,6 +365,10 @@ class TestFileIterator implements Iterator {
        function readNextTest() {
                $this->clearSection();
 
+               # Create a fake parser tests which never run anything unless
+               # asked to do so. This will avoid running hooks for a disabled test
+               $delayedParserTest = new DelayedParserTest();
+
                while ( false !== ( $line = fgets( $this->fh ) ) ) {
                        $this->lineNum++;
                        $matches = array();
@@ -390,9 +394,7 @@ class TestFileIterator implements Iterator {
                                                $line = trim( $line );
 
                                                if ( $line ) {
-                                                       if ( !$this->parserTest->requireHook( $line ) ) {
-                                                               return false;
-                                                       }
+                                                       $delayedParserTest->requireHook( $line );
                                                }
                                        }
 
@@ -408,9 +410,7 @@ class TestFileIterator implements Iterator {
                                                $line = trim( $line );
 
                                                if ( $line ) {
-                                                       if ( !$this->parserTest->requireFunctionHook( $line ) ) {
-                                                               return false;
-                                                       }
+                                                       $delayedParserTest->requireFunctionHook( $line );
                                                }
                                        }
 
@@ -437,9 +437,20 @@ class TestFileIterator implements Iterator {
                                                # disabled test
                                                $this->clearSection();
 
+                                               # Forget any pending hooks call since test is disabled
+                                               $delayedParserTest->reset();
+
                                                continue;
                                        }
 
+                                       # We are really going to run the test, run pending hooks and hooks function
+                                       wfDebug( __METHOD__ . " unleashing delayed test for: {$this->sectionData['test']}" );
+                                       $hooksResult = $delayedParserTest->unleash( $this->parserTest );
+                                       if( !$hooksResult ) {
+                                               # Some hook reported an issue. Abort.
+                                               return false;
+                                       }
+
                                        $this->test = array(
                                                'test'    => ParserTest::chomp( $this->sectionData['test']    ),
                                                'input'   => ParserTest::chomp( $this->sectionData['input']   ),
@@ -503,3 +514,69 @@ class TestFileIterator implements Iterator {
                return true;
        }
 }
+
+
+/**
+ * A class to delay execution of a parser test hooks.
+ *   
+ */
+class DelayedParserTest {
+
+       /** Initialized on construction */
+       private $hooks;
+       private $fnHooks;
+
+       public function __construct() {
+               $this->reset();
+       }
+
+       /**
+        * Init/reset or forgot about the current delayed test.
+        * Call to this will erase any hooks function that were pending.
+        */
+       public function reset() {
+               $this->hooks   = array();
+               $this->fnHooks = array();
+       }
+
+       /**
+        * Called whenever we actually want to run the hook.
+        * Should be the case if we found the parserTest is not disabled 
+        */
+       public function unleash( ParserTest &$parserTest ) {
+               # Trigger delayed hooks. Any failure will make us abort
+               foreach( $this->hooks as $hook ) {
+                       $ret = $parserTest->requireHook( $hook );
+                       if( !$ret ) {
+                               return false;
+                       }
+               }
+
+               # Trigger delayed function hooks. Any failure will make us abort
+               foreach( $this->fnHooks as $fnHook ) {
+                       $ret = $parserTest->requireFunctionHook( $fnHook );
+                       if( !$ret ) {
+                               return false;
+                       }
+               }
+
+               # Delayed execution was successful.
+               return true;
+       }
+
+       /**
+        * Similar to ParserTest object but does not run anything
+        * Use unleash() to really execute the hook
+        */
+       public function requireHook( $hook ) {
+               $this->hooks[] = $hook;
+       }
+       /**
+        * Similar to ParserTest object but does not run anything
+        * Use unleash() to really execute the hook function
+        */
+       public function requireFunctionHook( $fnHook ) {
+               $this->fnHooks[] = $fnHook;
+       }
+
+}