38923f0494f08b8e21b6b1ed9634ed4c398be19b
[lhc/web/wiklou.git] / tests / parser / parserTests.php
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
27 // Some methods which are discouraged for normal code throw exceptions unless
28 // we declare this is just a test.
29 define( 'MW_PARSER_TEST', true );
30
31 require __DIR__ . '/../../maintenance/Maintenance.php';
32
33 class ParserTestsMaintenance extends Maintenance {
34 function __construct() {
35 parent::__construct();
36 $this->addDescription( 'Run parser tests' );
37
38 $this->addOption( 'quick', 'Suppress diff output of failed tests' );
39 $this->addOption( 'quiet', 'Suppress notification of passed tests (shows only failed tests)' );
40 $this->addOption( 'show-output', 'Show expected and actual output' );
41 $this->addOption( 'color', '[=yes|no] Override terminal detection and force ' .
42 'color output on or off. Use wgCommandLineDarkBg = true; if your term is dark',
43 false, true );
44 $this->addOption( 'regex', 'Only run tests whose descriptions which match given regex',
45 false, true );
46 $this->addOption( 'filter', 'Alias for --regex', false, true );
47 $this->addOption( 'file', 'Run test cases from a custom file instead of parserTests.txt',
48 false, true, false, true );
49 $this->addOption( 'record', 'Record tests in database' );
50 $this->addOption( 'compare', 'Compare with recorded results, without updating the database.' );
51 $this->addOption( 'setversion', 'When using --record, set the version string to use (useful' .
52 'with "git rev-parse HEAD" to get the exact revision)',
53 false, true );
54 $this->addOption( 'keep-uploads', 'Re-use the same upload directory for each ' .
55 'test, don\'t delete it' );
56 $this->addOption( 'file-backend', 'Use the file backend with the given name,' .
57 'and upload files to it, instead of creating a mock file backend.', false, true );
58 $this->addOption( 'upload-dir', 'Specify the upload directory to use. Useful in ' .
59 'conjunction with --keep-uploads. Causes a real (non-mock) file backend to ' .
60 'be used.', false, true );
61 $this->addOption( 'run-disabled', 'run disabled tests' );
62 $this->addOption( 'run-parsoid', 'run parsoid tests (normally disabled)' );
63 $this->addOption( 'dwdiff', 'Use dwdiff to display diff output' );
64 $this->addOption( 'mark-ws', 'Mark whitespace in diffs by replacing it with symbols' );
65 $this->addOption( 'norm', 'Apply a comma-separated list of normalization functions to ' .
66 'both the expected and actual output in order to resolve ' .
67 'irrelevant differences. The accepted normalization functions ' .
68 'are: removeTbody to remove <tbody> tags; and trimWhitespace ' .
69 'to trim whitespace from the start and end of text nodes.',
70 false, true );
71 $this->addOption( 'use-tidy-config', 'Use the wiki\'s Tidy configuration instead of known-good' .
72 'defaults.' );
73 }
74
75 public function finalSetup() {
76 parent::finalSetup();
77 self::requireTestsAutoloader();
78 TestSetup::applyInitialConfig();
79 }
80
81 public function execute() {
82 global $wgParserTestFiles, $wgDBtype;
83
84 // Cases of weird db corruption were encountered when running tests on earlyish
85 // versions of SQLite
86 if ( $wgDBtype == 'sqlite' ) {
87 $db = wfGetDB( DB_MASTER );
88 $version = $db->getServerVersion();
89 if ( version_compare( $version, '3.6' ) < 0 ) {
90 die( "Parser tests require SQLite version 3.6 or later, you have $version\n" );
91 }
92 }
93
94 // Print out software version to assist with locating regressions
95 $version = SpecialVersion::getVersion( 'nodb' );
96 echo "This is MediaWiki version {$version}.\n\n";
97
98 // Only colorize output if stdout is a terminal.
99 $color = !wfIsWindows() && Maintenance::posix_isatty( 1 );
100
101 if ( $this->hasOption( 'color' ) ) {
102 switch ( $this->getOption( 'color' ) ) {
103 case 'no':
104 $color = false;
105 break;
106 case 'yes':
107 default:
108 $color = true;
109 break;
110 }
111 }
112
113 $record = $this->hasOption( 'record' );
114 $compare = $this->hasOption( 'compare' );
115
116 $regex = $this->getOption( 'filter', $this->getOption( 'regex', false ) );
117 if ( $regex !== false ) {
118 $regex = "/$regex/i";
119
120 if ( $record ) {
121 echo "Warning: --record cannot be used with --regex, disabling --record\n";
122 $record = false;
123 }
124 }
125
126 $term = $color
127 ? new AnsiTermColorer()
128 : new DummyTermColorer();
129
130 $recorder = new MultiTestRecorder;
131
132 $recorder->addRecorder( new ParserTestPrinter(
133 $term,
134 [
135 'showDiffs' => !$this->hasOption( 'quick' ),
136 'showProgress' => !$this->hasOption( 'quiet' ),
137 'showFailure' => !$this->hasOption( 'quiet' )
138 || ( !$record && !$compare ), // redundant output
139 'showOutput' => $this->hasOption( 'show-output' ),
140 'useDwdiff' => $this->hasOption( 'dwdiff' ),
141 'markWhitespace' => $this->hasOption( 'mark-ws' ),
142 ]
143 ) );
144
145 $recorderLB = false;
146 if ( $record || $compare ) {
147 $recorderLB = wfGetLBFactory()->newMainLB();
148 // This connection will have the wiki's table prefix, not parsertest_
149 $recorderDB = $recorderLB->getConnection( DB_MASTER );
150
151 // Add recorder before previewer because recorder will create the
152 // DB table if it doesn't exist
153 if ( $record ) {
154 $recorder->addRecorder( new DbTestRecorder( $recorderDB ) );
155 }
156 $recorder->addRecorder( new DbTestPreviewer(
157 $recorderDB,
158 function ( $name ) use ( $regex ) {
159 // Filter reports of old tests by the filter regex
160 if ( $regex === false ) {
161 return true;
162 } else {
163 return (bool)preg_match( $regex, $name );
164 }
165 } ) );
166 }
167
168 // Default parser tests and any set from extensions or local config
169 $files = $this->getOption( 'file', $wgParserTestFiles );
170
171 $norm = $this->hasOption( 'norm' ) ? explode( ',', $this->getOption( 'norm' ) ) : [];
172
173 $tester = new ParserTestRunner( $recorder, [
174 'norm' => $norm,
175 'regex' => $regex,
176 'keep-uploads' => $this->hasOption( 'keep-uploads' ),
177 'run-disabled' => $this->hasOption( 'run-disabled' ),
178 'run-parsoid' => $this->hasOption( 'run-parsoid' ),
179 'use-tidy-config' => $this->hasOption( 'use-tidy-config' ),
180 'file-backend' => $this->getOption( 'file-backend' ),
181 'upload-dir' => $this->getOption( 'upload-dir' ),
182 ] );
183
184 $ok = $tester->runTestsFromFiles( $files );
185 if ( $recorderLB ) {
186 $recorderLB->closeAll();
187 }
188 if ( !$ok ) {
189 exit( 1 );
190 }
191 }
192 }
193
194 $maintClass = 'ParserTestsMaintenance';
195 require_once RUN_MAINTENANCE_IF_MAIN;