From 9081dd1217c574f77a43beab36b436ba43f4188e Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Fri, 7 Jul 2017 15:26:21 -0700 Subject: [PATCH] Autodiscover parser tests for extensions, deprecate $wgParserTestFiles 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 | 13 ++--- tests/parser/ParserTestRunner.php | 49 +++++++++++++++++++ tests/parser/parserTests.php | 4 +- .../suites/ParserTestTopLevelSuite.php | 5 +- 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 0548d8bacf..11f08b2bb5 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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). diff --git a/tests/parser/ParserTestRunner.php b/tests/parser/ParserTestRunner.php index 9dce73f0bb..9255733e74 100644 --- a/tests/parser/ParserTestRunner.php +++ b/tests/parser/ParserTestRunner.php @@ -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; } diff --git a/tests/parser/parserTests.php b/tests/parser/parserTests.php index 1d0867abf0..2735f93e0d 100644 --- a/tests/parser/parserTests.php +++ b/tests/parser/parserTests.php @@ -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' ) ) : []; diff --git a/tests/phpunit/suites/ParserTestTopLevelSuite.php b/tests/phpunit/suites/ParserTestTopLevelSuite.php index 5d5d693571..09052f3c61 100644 --- a/tests/phpunit/suites/ParserTestTopLevelSuite.php +++ b/tests/phpunit/suites/ParserTestTopLevelSuite.php @@ -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 ) { -- 2.20.1