Debian Codegraph @

tests/parser/parserTests.php

raw symbols SCIP
1 <?php
2 /**
3 * MediaWiki parser test suite
4 *
5 * Copyright © 2004 Brooke Vibber <[email protected]>
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 require_once __DIR__ . '/../../maintenance/Maintenance.php';
28
29 use MediaWiki\Maintenance\Maintenance;
30 use MediaWiki\MediaWikiServices;
31 use MediaWiki\Settings\SettingsBuilder;
32 use MediaWiki\Specials\SpecialVersion;
33 use MediaWiki\Tests\AnsiTermColorer;
34 use MediaWiki\Tests\DummyTermColorer;
35 use Wikimedia\Parsoid\Utils\ScriptUtils;
36
37 define( 'MW_AUTOLOAD_TEST_CLASSES', true );
38
39 class ParserTestsMaintenance extends Maintenance {
40
41 public function __construct() {
42 parent::__construct();
43 $this->addDescription( 'Run parser tests' );
44
45 $this->addOption( 'quick', 'Suppress diff output of failed tests' );
46 $this->addOption( 'quiet', 'Suppress notification of passed tests (shows only failed tests)' );
47 $this->addOption( 'show-output', 'Show expected and actual output' );
48 $this->addOption( 'color', '[=yes|no] Override terminal detection and force ' .
49 'color output on or off. Use wgCommandLineDarkBg = true; if your term is dark',
50 false, true );
51 $this->addOption( 'regex', 'Only run tests whose descriptions which match given regex',
52 false, true );
53 $this->addOption( 'filter', 'Alias for --regex', false, true );
54 $this->addOption( 'file', 'Run test cases from a custom file instead of parserTests.txt',
55 false, true, false, true );
56 $this->addOption( 'dir', 'Run test cases for all *.txt files in a directory',
57 false, true, false, true );
58 $this->addOption( 'record', 'Record tests in database' );
59 $this->addOption( 'compare', 'Compare with recorded results, without updating the database.' );
60 $this->addOption( 'setversion', 'When using --record, set the version string to use (useful' .
61 'with "git rev-parse HEAD" to get the exact revision)',
62 false, true );
63 $this->addOption( 'keep-uploads', 'Re-use the same upload directory for each ' .
64 'test, don\'t delete it' );
65 $this->addOption( 'file-backend', 'Use the file backend with the given name,' .
66 'and upload files to it, instead of creating a mock file backend.', false, true );
67 $this->addOption( 'upload-dir', 'Specify the upload directory to use. Useful in ' .
68 'conjunction with --keep-uploads. Causes a real (non-mock) file backend to ' .
69 'be used.', false, true );
70 $this->addOption( 'run-disabled', 'run disabled tests' );
71 $this->addOption( 'disable-save-parse', 'Don\'t run the parser when ' .
72 'inserting articles into the database' );
73 $this->addOption( 'dwdiff', 'Use dwdiff to display diff output' );
74 $this->addOption( 'mark-ws', 'Mark whitespace in diffs by replacing it with symbols' );
75 $this->addOption( 'norm', 'Apply a comma-separated list of normalization functions to ' .
76 'both the expected and actual output in order to resolve ' .
77 'irrelevant differences. The accepted normalization functions ' .
78 'are: removeTbody to remove <tbody> tags; and trimWhitespace ' .
79 'to trim whitespace from the start and end of text nodes.',
80 false, true );
81 $this->addOption( 'wt2html', 'Parsoid: Wikitext -> HTML' );
82 $this->addOption( 'wt2wt',
83 'Parsoid Roundtrip testing: Wikitext -> HTML(DOM) -> Wikitext' );
84 $this->addOption( 'html2wt', 'Parsoid: HTML -> Wikitext' );
85 $this->addOption( 'numchanges',
86 'Max different selser edit tests to generate from the Parsoid DOM' );
87 $this->addOption( 'html2html',
88 'Parsoid Roundtrip testing: HTML -> Wikitext -> HTML' );
89 $this->addOption( 'selser',
90 'Parsoid Roundtrip testing: Wikitext -> DOM(HTML) -> Wikitext (with selective serialization). ' .
91 'Set to "noauto" to just run the tests with manual selser changes.',
92 false, true );
93 $this->addOption( 'changetree',
94 'Changes to apply to Parsoid HTML to generate new HTML to be serialized (use with selser)',
95 false, true );
96 $this->addOption( 'parsoid', 'Run Parsoid tests' );
97 $this->addOption( 'trace', 'Use --trace=help for supported options (Parsoid only)', false, true );
98 $this->addOption( 'dump', 'Use --dump=help for supported options (Parsoid only)', false, true );
99 $this->addOption( 'updateKnownFailures', 'Update knownFailures.json with failing tests' );
100 $this->addOption( 'knownFailures',
101 'Compare against known failures (default: true). If false, ignores knownFailures.json file',
102 false, true );
103 $this->addOption( 'update-tests',
104 'Update parserTests.txt with results from wt2html fails. Note that editTests.php exists ' .
105 'for finer grained editing of tests.' );
106 }
107
108 public function finalSetup( SettingsBuilder $settingsBuilder ) {
109 // Some methods which are discouraged for normal code throw exceptions unless
110 // we declare this is just a test.
111 define( 'MW_PARSER_TEST', true );
112
113 parent::finalSetup( $settingsBuilder );
114 TestSetup::applyInitialConfig();
115 }
116
117 public function execute() {
118 global $wgDBtype;
119
120 // Cases of weird db corruption were encountered when running tests on earlyish
121 // versions of SQLite
122 if ( $wgDBtype == 'sqlite' ) {
123 $dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
124 $version = $dbw->getServerVersion();
125 if ( version_compare( $version, '3.6' ) < 0 ) {
126 die( "Parser tests require SQLite version 3.6 or later, you have $version\n" );
127 }
128 }
129
130 // Print out software version to assist with locating regressions
131 $version = SpecialVersion::getVersion( 'nodb' );
132 echo "This is MediaWiki version {$version}.\n\n";
133
134 // Only colorize output if stdout is a terminal.
135 $color = !wfIsWindows() && Maintenance::posix_isatty( 1 );
136
137 if ( $this->hasOption( 'color' ) ) {
138 switch ( $this->getOption( 'color' ) ) {
139 case 'no':
140 $color = false;
141 break;
142 case 'yes':
143 default:
144 $color = true;
145 break;
146 }
147 }
148
149 $record = $this->hasOption( 'record' );
150 $compare = $this->hasOption( 'compare' );
151
152 $regex = $this->getOption( 'filter', $this->getOption( 'regex', false ) );
153 if ( $regex !== false ) {
154 $regex = "/$regex/i";
155
156 if ( $record ) {
157 echo "Warning: --record cannot be used with --regex, disabling --record\n";
158 $record = false;
159 }
160 }
161
162 $term = $color
163 ? new AnsiTermColorer()
164 : new DummyTermColorer();
165
166 $recorder = new MultiTestRecorder;
167
168 $recorder->addRecorder( new ParserTestPrinter(
169 $term,
170 [
171 'showDiffs' => !$this->hasOption( 'quick' ),
172 'showProgress' => !$this->hasOption( 'quiet' ),
173 'showFailure' => !$this->hasOption( 'quiet' )
174 || ( !$record && !$compare ), // redundant output
175 'showOutput' => $this->hasOption( 'show-output' ),
176 'useDwdiff' => $this->hasOption( 'dwdiff' ),
177 'markWhitespace' => $this->hasOption( 'mark-ws' ),
178 ]
179 ) );
180
181 $traceFlags = array_fill_keys( explode( ',', $this->getOption( 'trace', '' ) ), true );
182 $dumpFlags = array_fill_keys( explode( ',', $this->getOption( 'dump', '' ) ), true );
183 if ( $traceFlags['help'] ?? false ) {
184 print "-------------- PARSOID ONLY --------------\n";
185 print ScriptUtils::traceUsageHelp();
186 exit( 1 );
187 }
188 if ( $dumpFlags['help'] ?? false ) {
189 print "-------------- PARSOID ONLY --------------\n";
190 print ScriptUtils::dumpUsageHelp();
191 exit( 1 );
192 }
193
194 $recorderLB = false;
195 if ( $record || $compare ) {
196 // Make an untracked DB_PRIMARY connection (wiki's table prefix, not parsertest_)
197 $recorderLB = $this->getServiceContainer()->getDBLoadBalancerFactory()->newMainLB();
198 $recorderDB = $recorderLB->getMaintenanceConnectionRef( DB_PRIMARY );
199 // Add recorder before previewer because recorder will create the
200 // DB table if it doesn't exist
201 if ( $record ) {
202 $recorder->addRecorder( new DbTestRecorder( $recorderDB ) );
203 }
204 $recorder->addRecorder( new DbTestPreviewer(
205 $recorderDB,
206 static function ( $name ) use ( $regex ) {
207 // Filter reports of old tests by the filter regex
208 return $regex === false || (bool)preg_match( $regex, $name );
209 } ) );
210 }
211
212 // Default parser tests and any set from extensions or local config
213 $dirs = $this->getOption( 'dir', [] );
214 $files = $this->getOption( 'file', ParserTestRunner::getParserTestFiles( $dirs ) );
215 $norm = $this->hasOption( 'norm' ) ? explode( ',', $this->getOption( 'norm' ) ) : [];
216
217 $selserOpt = $this->getOption( 'selser', false ); /* can also be 'noauto' */
218 if ( $selserOpt !== 'noauto' ) {
219 $selserOpt = ScriptUtils::booleanOption( $selserOpt );
220 }
221 $tester = new ParserTestRunner( $recorder, [
222 'norm' => $norm,
223 'regex' => $regex,
224 'keep-uploads' => $this->hasOption( 'keep-uploads' ),
225 'run-disabled' => $this->hasOption( 'run-disabled' ),
226 'disable-save-parse' => $this->hasOption( 'disable-save-parse' ),
227 'file-backend' => $this->getOption( 'file-backend' ),
228 'upload-dir' => $this->getOption( 'upload-dir' ),
229 // Passing a parsoid-specific option implies --parsoid
230 'parsoid' => (
231 $this->hasOption( 'parsoid' ) ||
232 $this->hasOption( 'wt2html' ) ||
233 $this->hasOption( 'wt2wt' ) ||
234 $this->hasOption( 'html2wt' ) ||
235 $this->hasOption( 'html2html' ) ||
236 $selserOpt ),
237 'wt2html' => $this->hasOption( 'wt2html' ),
238 'wt2wt' => $this->hasOption( 'wt2wt' ),
239 'html2wt' => $this->hasOption( 'html2wt' ),
240 'html2html' => $this->hasOption( 'html2html' ),
241 'numchanges' => $this->getOption( 'numchanges', 20 ),
242 'selser' => $selserOpt,
243 'changetree' => json_decode( $this->getOption( 'changetree', '' ), true ),
244 'knownFailures' => ScriptUtils::booleanOption( $this->getOption( 'knownFailures', true ) ),
245 'updateKnownFailures' => $this->hasOption( 'updateKnownFailures' ),
246 'traceFlags' => $traceFlags,
247 'dumpFlags' => $dumpFlags,
248 'update-tests' => $this->hasOption( 'update-tests' ),
249 ] );
250
251 $ok = $tester->runTestsFromFiles( $files );
252 if ( $recorderLB ) {
253 $recorderLB->closeAll( __METHOD__ );
254 }
255 if ( $tester->unexpectedTestPasses ) {
256 $recorder->warning( "There were some unexpected passing tests. " .
257 "Please rerun with --updateKnownFailures option." );
258 $ok = false;
259 }
260 if ( !$ok ) {
261 exit( 1 );
262 }
263 }
264 }
265
266 $maintClass = ParserTestsMaintenance::class;
267 require_once RUN_MAINTENANCE_IF_MAIN;