parserTests.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * MediaWiki parser test suite
  4. *
  5. * Copyright © 2004 Brion Vibber <brion@pobox.com>
  6. * https://www.mediawiki.org/
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. * @ingroup Testing
  25. */
  26. // Some methods which are discouraged for normal code throw exceptions unless
  27. // we declare this is just a test.
  28. define( 'MW_PARSER_TEST', true );
  29. require __DIR__ . '/../../maintenance/Maintenance.php';
  30. use MediaWiki\MediaWikiServices;
  31. class ParserTestsMaintenance extends Maintenance {
  32. function __construct() {
  33. parent::__construct();
  34. $this->addDescription( 'Run parser tests' );
  35. $this->addOption( 'quick', 'Suppress diff output of failed tests' );
  36. $this->addOption( 'quiet', 'Suppress notification of passed tests (shows only failed tests)' );
  37. $this->addOption( 'show-output', 'Show expected and actual output' );
  38. $this->addOption( 'color', '[=yes|no] Override terminal detection and force ' .
  39. 'color output on or off. Use wgCommandLineDarkBg = true; if your term is dark',
  40. false, true );
  41. $this->addOption( 'regex', 'Only run tests whose descriptions which match given regex',
  42. false, true );
  43. $this->addOption( 'filter', 'Alias for --regex', false, true );
  44. $this->addOption( 'file', 'Run test cases from a custom file instead of parserTests.txt',
  45. false, true, false, true );
  46. $this->addOption( 'record', 'Record tests in database' );
  47. $this->addOption( 'compare', 'Compare with recorded results, without updating the database.' );
  48. $this->addOption( 'setversion', 'When using --record, set the version string to use (useful' .
  49. 'with "git rev-parse HEAD" to get the exact revision)',
  50. false, true );
  51. $this->addOption( 'keep-uploads', 'Re-use the same upload directory for each ' .
  52. 'test, don\'t delete it' );
  53. $this->addOption( 'file-backend', 'Use the file backend with the given name,' .
  54. 'and upload files to it, instead of creating a mock file backend.', false, true );
  55. $this->addOption( 'upload-dir', 'Specify the upload directory to use. Useful in ' .
  56. 'conjunction with --keep-uploads. Causes a real (non-mock) file backend to ' .
  57. 'be used.', false, true );
  58. $this->addOption( 'run-disabled', 'run disabled tests' );
  59. $this->addOption( 'run-parsoid', 'run parsoid tests (normally disabled)' );
  60. $this->addOption( 'disable-save-parse', 'Don\'t run the parser when ' .
  61. 'inserting articles into the database' );
  62. $this->addOption( 'dwdiff', 'Use dwdiff to display diff output' );
  63. $this->addOption( 'mark-ws', 'Mark whitespace in diffs by replacing it with symbols' );
  64. $this->addOption( 'norm', 'Apply a comma-separated list of normalization functions to ' .
  65. 'both the expected and actual output in order to resolve ' .
  66. 'irrelevant differences. The accepted normalization functions ' .
  67. 'are: removeTbody to remove <tbody> tags; and trimWhitespace ' .
  68. 'to trim whitespace from the start and end of text nodes.',
  69. false, true );
  70. $this->addOption( 'use-tidy-config',
  71. 'Use the wiki\'s Tidy configuration instead of known-good' .
  72. 'defaults.' );
  73. }
  74. public function finalSetup() {
  75. parent::finalSetup();
  76. self::requireTestsAutoloader();
  77. TestSetup::applyInitialConfig();
  78. }
  79. public function execute() {
  80. global $wgDBtype;
  81. // Cases of weird db corruption were encountered when running tests on earlyish
  82. // versions of SQLite
  83. if ( $wgDBtype == 'sqlite' ) {
  84. $db = wfGetDB( DB_MASTER );
  85. $version = $db->getServerVersion();
  86. if ( version_compare( $version, '3.6' ) < 0 ) {
  87. die( "Parser tests require SQLite version 3.6 or later, you have $version\n" );
  88. }
  89. }
  90. // Print out software version to assist with locating regressions
  91. $version = SpecialVersion::getVersion( 'nodb' );
  92. echo "This is MediaWiki version {$version}.\n\n";
  93. // Only colorize output if stdout is a terminal.
  94. $color = !wfIsWindows() && Maintenance::posix_isatty( 1 );
  95. if ( $this->hasOption( 'color' ) ) {
  96. switch ( $this->getOption( 'color' ) ) {
  97. case 'no':
  98. $color = false;
  99. break;
  100. case 'yes':
  101. default:
  102. $color = true;
  103. break;
  104. }
  105. }
  106. $record = $this->hasOption( 'record' );
  107. $compare = $this->hasOption( 'compare' );
  108. $regex = $this->getOption( 'filter', $this->getOption( 'regex', false ) );
  109. if ( $regex !== false ) {
  110. $regex = "/$regex/i";
  111. if ( $record ) {
  112. echo "Warning: --record cannot be used with --regex, disabling --record\n";
  113. $record = false;
  114. }
  115. }
  116. $term = $color
  117. ? new AnsiTermColorer()
  118. : new DummyTermColorer();
  119. $recorder = new MultiTestRecorder;
  120. $recorder->addRecorder( new ParserTestPrinter(
  121. $term,
  122. [
  123. 'showDiffs' => !$this->hasOption( 'quick' ),
  124. 'showProgress' => !$this->hasOption( 'quiet' ),
  125. 'showFailure' => !$this->hasOption( 'quiet' )
  126. || ( !$record && !$compare ), // redundant output
  127. 'showOutput' => $this->hasOption( 'show-output' ),
  128. 'useDwdiff' => $this->hasOption( 'dwdiff' ),
  129. 'markWhitespace' => $this->hasOption( 'mark-ws' ),
  130. ]
  131. ) );
  132. $recorderLB = false;
  133. if ( $record || $compare ) {
  134. $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
  135. $recorderLB = $lbFactory->newMainLB();
  136. // This connection will have the wiki's table prefix, not parsertest_
  137. $recorderDB = $recorderLB->getConnection( DB_MASTER );
  138. // Add recorder before previewer because recorder will create the
  139. // DB table if it doesn't exist
  140. if ( $record ) {
  141. $recorder->addRecorder( new DbTestRecorder( $recorderDB ) );
  142. }
  143. $recorder->addRecorder( new DbTestPreviewer(
  144. $recorderDB,
  145. function ( $name ) use ( $regex ) {
  146. // Filter reports of old tests by the filter regex
  147. if ( $regex === false ) {
  148. return true;
  149. } else {
  150. return (bool)preg_match( $regex, $name );
  151. }
  152. } ) );
  153. }
  154. // Default parser tests and any set from extensions or local config
  155. $files = $this->getOption( 'file', ParserTestRunner::getParserTestFiles() );
  156. $norm = $this->hasOption( 'norm' ) ? explode( ',', $this->getOption( 'norm' ) ) : [];
  157. $tester = new ParserTestRunner( $recorder, [
  158. 'norm' => $norm,
  159. 'regex' => $regex,
  160. 'keep-uploads' => $this->hasOption( 'keep-uploads' ),
  161. 'run-disabled' => $this->hasOption( 'run-disabled' ),
  162. 'run-parsoid' => $this->hasOption( 'run-parsoid' ),
  163. 'disable-save-parse' => $this->hasOption( 'disable-save-parse' ),
  164. 'use-tidy-config' => $this->hasOption( 'use-tidy-config' ),
  165. 'file-backend' => $this->getOption( 'file-backend' ),
  166. 'upload-dir' => $this->getOption( 'upload-dir' ),
  167. ] );
  168. $ok = $tester->runTestsFromFiles( $files );
  169. if ( $recorderLB ) {
  170. $recorderLB->closeAll();
  171. }
  172. if ( !$ok ) {
  173. exit( 1 );
  174. }
  175. }
  176. }
  177. $maintClass = 'ParserTestsMaintenance';
  178. require_once RUN_MAINTENANCE_IF_MAIN;