parserTests.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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( 'dwdiff', 'Use dwdiff to display diff output' );
  61. $this->addOption( 'mark-ws', 'Mark whitespace in diffs by replacing it with symbols' );
  62. $this->addOption( 'norm', 'Apply a comma-separated list of normalization functions to ' .
  63. 'both the expected and actual output in order to resolve ' .
  64. 'irrelevant differences. The accepted normalization functions ' .
  65. 'are: removeTbody to remove <tbody> tags; and trimWhitespace ' .
  66. 'to trim whitespace from the start and end of text nodes.',
  67. false, true );
  68. $this->addOption( 'use-tidy-config',
  69. 'Use the wiki\'s Tidy configuration instead of known-good' .
  70. 'defaults.' );
  71. }
  72. public function finalSetup() {
  73. parent::finalSetup();
  74. self::requireTestsAutoloader();
  75. TestSetup::applyInitialConfig();
  76. }
  77. public function execute() {
  78. global $wgDBtype;
  79. // Cases of weird db corruption were encountered when running tests on earlyish
  80. // versions of SQLite
  81. if ( $wgDBtype == 'sqlite' ) {
  82. $db = wfGetDB( DB_MASTER );
  83. $version = $db->getServerVersion();
  84. if ( version_compare( $version, '3.6' ) < 0 ) {
  85. die( "Parser tests require SQLite version 3.6 or later, you have $version\n" );
  86. }
  87. }
  88. // Print out software version to assist with locating regressions
  89. $version = SpecialVersion::getVersion( 'nodb' );
  90. echo "This is MediaWiki version {$version}.\n\n";
  91. // Only colorize output if stdout is a terminal.
  92. $color = !wfIsWindows() && Maintenance::posix_isatty( 1 );
  93. if ( $this->hasOption( 'color' ) ) {
  94. switch ( $this->getOption( 'color' ) ) {
  95. case 'no':
  96. $color = false;
  97. break;
  98. case 'yes':
  99. default:
  100. $color = true;
  101. break;
  102. }
  103. }
  104. $record = $this->hasOption( 'record' );
  105. $compare = $this->hasOption( 'compare' );
  106. $regex = $this->getOption( 'filter', $this->getOption( 'regex', false ) );
  107. if ( $regex !== false ) {
  108. $regex = "/$regex/i";
  109. if ( $record ) {
  110. echo "Warning: --record cannot be used with --regex, disabling --record\n";
  111. $record = false;
  112. }
  113. }
  114. $term = $color
  115. ? new AnsiTermColorer()
  116. : new DummyTermColorer();
  117. $recorder = new MultiTestRecorder;
  118. $recorder->addRecorder( new ParserTestPrinter(
  119. $term,
  120. [
  121. 'showDiffs' => !$this->hasOption( 'quick' ),
  122. 'showProgress' => !$this->hasOption( 'quiet' ),
  123. 'showFailure' => !$this->hasOption( 'quiet' )
  124. || ( !$record && !$compare ), // redundant output
  125. 'showOutput' => $this->hasOption( 'show-output' ),
  126. 'useDwdiff' => $this->hasOption( 'dwdiff' ),
  127. 'markWhitespace' => $this->hasOption( 'mark-ws' ),
  128. ]
  129. ) );
  130. $recorderLB = false;
  131. if ( $record || $compare ) {
  132. $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
  133. $recorderLB = $lbFactory->newMainLB();
  134. // This connection will have the wiki's table prefix, not parsertest_
  135. $recorderDB = $recorderLB->getConnection( DB_MASTER );
  136. // Add recorder before previewer because recorder will create the
  137. // DB table if it doesn't exist
  138. if ( $record ) {
  139. $recorder->addRecorder( new DbTestRecorder( $recorderDB ) );
  140. }
  141. $recorder->addRecorder( new DbTestPreviewer(
  142. $recorderDB,
  143. function ( $name ) use ( $regex ) {
  144. // Filter reports of old tests by the filter regex
  145. if ( $regex === false ) {
  146. return true;
  147. } else {
  148. return (bool)preg_match( $regex, $name );
  149. }
  150. } ) );
  151. }
  152. // Default parser tests and any set from extensions or local config
  153. $files = $this->getOption( 'file', ParserTestRunner::getParserTestFiles() );
  154. $norm = $this->hasOption( 'norm' ) ? explode( ',', $this->getOption( 'norm' ) ) : [];
  155. $tester = new ParserTestRunner( $recorder, [
  156. 'norm' => $norm,
  157. 'regex' => $regex,
  158. 'keep-uploads' => $this->hasOption( 'keep-uploads' ),
  159. 'run-disabled' => $this->hasOption( 'run-disabled' ),
  160. 'run-parsoid' => $this->hasOption( 'run-parsoid' ),
  161. 'use-tidy-config' => $this->hasOption( 'use-tidy-config' ),
  162. 'file-backend' => $this->getOption( 'file-backend' ),
  163. 'upload-dir' => $this->getOption( 'upload-dir' ),
  164. ] );
  165. $ok = $tester->runTestsFromFiles( $files );
  166. if ( $recorderLB ) {
  167. $recorderLB->closeAll();
  168. }
  169. if ( !$ok ) {
  170. exit( 1 );
  171. }
  172. }
  173. }
  174. $maintClass = 'ParserTestsMaintenance';
  175. require_once RUN_MAINTENANCE_IF_MAIN;