Autodiscover parser tests for extensions, deprecate $wgParserTestFiles
authorKunal Mehta <legoktm@member.fsf.org>
Fri, 7 Jul 2017 22:26:21 +0000 (15:26 -0700)
committerReedy <reedy@wikimedia.org>
Sat, 8 Jul 2017 00:47:02 +0000 (00:47 +0000)
This implements autodiscovery of extension parser tests that are located
in the tests/parser/ directory. Any *.txt file in that directory tree
will be treated as a parser test.

Core parser tests are now defined in ParserTestRunner::$coreTestFiles,
and $wgParserTestFiles is marked as deprecated.

Bug: T143976
Change-Id: Ia24fd8ef52e6732c698153b17bb679a5f511a2a7

includes/DefaultSettings.php
tests/parser/ParserTestRunner.php
tests/parser/parserTests.php
tests/phpunit/suites/ParserTestTopLevelSuite.php

index 0548d8b..11f08b2 100644 (file)
@@ -6333,15 +6333,16 @@ $wgSiteStatsAsyncFactor = false;
  * Parser test suite files to be run by parserTests.php when no specific
  * filename is passed to it.
  *
- * Extensions may add their own tests to this array, or site-local tests
- * may be added via LocalSettings.php
+ * Extensions using extension.json will have any *.txt file in a
+ * tests/parser/ directory automatically run.
+ *
+ * Core tests can be added to ParserTestRunner::$coreTestFiles.
  *
  * Use full paths.
+ *
+ * @deprecated since 1.30
  */
-$wgParserTestFiles = [
-       "$IP/tests/parser/parserTests.txt",
-       "$IP/tests/parser/extraParserTests.txt"
-];
+$wgParserTestFiles = [];
 
 /**
  * Allow running of javascript test suites via [[Special:JavaScriptTest]] (such as QUnit).
index 9dce73f..9255733 100644 (file)
@@ -34,6 +34,18 @@ use Wikimedia\TestingAccessWrapper;
  * @ingroup Testing
  */
 class ParserTestRunner {
+
+       /**
+        * MediaWiki core parser test files, paths
+        * will be prefixed with __DIR__ . '/'
+        *
+        * @var array
+        */
+       private static $coreTestFiles = [
+               'parserTests.txt',
+               'extraParserTests.txt',
+       ];
+
        /**
         * @var bool $useTemporaryTables Use temporary tables for the temporary database
         */
@@ -147,6 +159,43 @@ class ParserTestRunner {
                }
        }
 
+       /**
+        * Get list of filenames to extension and core parser tests
+        *
+        * @return array
+        */
+       public static function getParserTestFiles() {
+               global $wgParserTestFiles;
+
+               // Add core test files
+               $files = array_map( function( $item ) {
+                       return __DIR__ . "/$item";
+               }, self::$coreTestFiles );
+
+               // Plus legacy global files
+               $files = array_merge( $files, $wgParserTestFiles );
+
+               // Auto-discover extension parser tests
+               $registry = ExtensionRegistry::getInstance();
+               foreach ( $registry->getAllThings() as $info ) {
+                       $dir = dirname( $info['path'] ) . '/tests/parser';
+                       if ( !file_exists( $dir ) ) {
+                               continue;
+                       }
+                       $dirIterator = new RecursiveIteratorIterator(
+                               new RecursiveDirectoryIterator( $dir )
+                       );
+                       foreach ( $dirIterator as $fileInfo ) {
+                               /** @var SplFileInfo $fileInfo */
+                               if ( substr( $fileInfo->getFilename(), -4 ) === '.txt' ) {
+                                       $files[] = $fileInfo->getPathname();
+                               }
+                       }
+               }
+
+               return array_unique( $files );
+       }
+
        public function getRecorder() {
                return $this->recorder;
        }
index 1d0867a..2735f93 100644 (file)
@@ -80,7 +80,7 @@ class ParserTestsMaintenance extends Maintenance {
        }
 
        public function execute() {
-               global $wgParserTestFiles, $wgDBtype;
+               global $wgDBtype;
 
                // Cases of weird db corruption were encountered when running tests on earlyish
                // versions of SQLite
@@ -167,7 +167,7 @@ class ParserTestsMaintenance extends Maintenance {
                }
 
                // Default parser tests and any set from extensions or local config
-               $files = $this->getOption( 'file', $wgParserTestFiles );
+               $files = $this->getOption( 'file', ParserTestRunner::getParserTestFiles() );
 
                $norm = $this->hasOption( 'norm' ) ? explode( ',', $this->getOption( 'norm' ) ) : [];
 
index 5d5d693..09052f3 100644 (file)
@@ -69,7 +69,7 @@ class ParserTestTopLevelSuite extends PHPUnit_Framework_TestSuite {
                if ( is_string( $flags ) ) {
                        $flags = self::CORE_ONLY;
                }
-               global $wgParserTestFiles, $IP;
+               global $IP;
 
                $mwTestDir = $IP . '/tests/';
 
@@ -81,7 +81,8 @@ class ParserTestTopLevelSuite extends PHPUnit_Framework_TestSuite {
                $filesToTest = [];
 
                # Filter out .txt files
-               foreach ( $wgParserTestFiles as $parserTestFile ) {
+               $files = ParserTestRunner::getParserTestFiles();
+               foreach ( $files as $parserTestFile ) {
                        $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) );
 
                        if ( $isCore && $wantsCore ) {