MediaWiki REL1_30
parserTests.php
Go to the documentation of this file.
1<?php
27// Some methods which are discouraged for normal code throw exceptions unless
28// we declare this is just a test.
29define( 'MW_PARSER_TEST', true );
30
31require __DIR__ . '/../../maintenance/Maintenance.php';
32
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',
72 'Use the wiki\'s Tidy configuration instead of known-good' .
73 'defaults.' );
74 }
75
76 public function finalSetup() {
77 parent::finalSetup();
80 }
81
82 public function execute() {
83 global $wgDBtype;
84
85 // Cases of weird db corruption were encountered when running tests on earlyish
86 // versions of SQLite
87 if ( $wgDBtype == 'sqlite' ) {
88 $db = wfGetDB( DB_MASTER );
89 $version = $db->getServerVersion();
90 if ( version_compare( $version, '3.6' ) < 0 ) {
91 die( "Parser tests require SQLite version 3.6 or later, you have $version\n" );
92 }
93 }
94
95 // Print out software version to assist with locating regressions
96 $version = SpecialVersion::getVersion( 'nodb' );
97 echo "This is MediaWiki version {$version}.\n\n";
98
99 // Only colorize output if stdout is a terminal.
100 $color = !wfIsWindows() && Maintenance::posix_isatty( 1 );
101
102 if ( $this->hasOption( 'color' ) ) {
103 switch ( $this->getOption( 'color' ) ) {
104 case 'no':
105 $color = false;
106 break;
107 case 'yes':
108 default:
109 $color = true;
110 break;
111 }
112 }
113
114 $record = $this->hasOption( 'record' );
115 $compare = $this->hasOption( 'compare' );
116
117 $regex = $this->getOption( 'filter', $this->getOption( 'regex', false ) );
118 if ( $regex !== false ) {
119 $regex = "/$regex/i";
120
121 if ( $record ) {
122 echo "Warning: --record cannot be used with --regex, disabling --record\n";
123 $record = false;
124 }
125 }
126
127 $term = $color
128 ? new AnsiTermColorer()
129 : new DummyTermColorer();
130
131 $recorder = new MultiTestRecorder;
132
133 $recorder->addRecorder( new ParserTestPrinter(
134 $term,
135 [
136 'showDiffs' => !$this->hasOption( 'quick' ),
137 'showProgress' => !$this->hasOption( 'quiet' ),
138 'showFailure' => !$this->hasOption( 'quiet' )
139 || ( !$record && !$compare ), // redundant output
140 'showOutput' => $this->hasOption( 'show-output' ),
141 'useDwdiff' => $this->hasOption( 'dwdiff' ),
142 'markWhitespace' => $this->hasOption( 'mark-ws' ),
143 ]
144 ) );
145
146 $recorderLB = false;
147 if ( $record || $compare ) {
148 $recorderLB = wfGetLBFactory()->newMainLB();
149 // This connection will have the wiki's table prefix, not parsertest_
150 $recorderDB = $recorderLB->getConnection( DB_MASTER );
151
152 // Add recorder before previewer because recorder will create the
153 // DB table if it doesn't exist
154 if ( $record ) {
155 $recorder->addRecorder( new DbTestRecorder( $recorderDB ) );
156 }
157 $recorder->addRecorder( new DbTestPreviewer(
158 $recorderDB,
159 function ( $name ) use ( $regex ) {
160 // Filter reports of old tests by the filter regex
161 if ( $regex === false ) {
162 return true;
163 } else {
164 return (bool)preg_match( $regex, $name );
165 }
166 } ) );
167 }
168
169 // Default parser tests and any set from extensions or local config
170 $files = $this->getOption( 'file', ParserTestRunner::getParserTestFiles() );
171
172 $norm = $this->hasOption( 'norm' ) ? explode( ',', $this->getOption( 'norm' ) ) : [];
173
174 $tester = new ParserTestRunner( $recorder, [
175 'norm' => $norm,
176 'regex' => $regex,
177 'keep-uploads' => $this->hasOption( 'keep-uploads' ),
178 'run-disabled' => $this->hasOption( 'run-disabled' ),
179 'run-parsoid' => $this->hasOption( 'run-parsoid' ),
180 'use-tidy-config' => $this->hasOption( 'use-tidy-config' ),
181 'file-backend' => $this->getOption( 'file-backend' ),
182 'upload-dir' => $this->getOption( 'upload-dir' ),
183 ] );
184
185 $ok = $tester->runTestsFromFiles( $files );
186 if ( $recorderLB ) {
187 $recorderLB->closeAll();
188 }
189 if ( !$ok ) {
190 exit( 1 );
191 }
192 }
193}
194
195$maintClass = 'ParserTestsMaintenance';
196require_once RUN_MAINTENANCE_IF_MAIN;
$wgDBtype
Database type.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfGetLBFactory()
Get the load balancer factory object.
wfIsWindows()
Check if the operating system is Windows.
Terminal that supports ANSI escape sequences.
Definition MWTerm.php:31
A colour-less terminal.
Definition MWTerm.php:64
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
static requireTestsAutoloader()
Call this to set up the autoloader to allow classes to be used from the tests directory.
hasOption( $name)
Checks to see if a particular param exists.
static posix_isatty( $fd)
Wrapper for posix_isatty() We default as considering stdin a tty (for nice readline methods) but trea...
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
This is a TestRecorder representing a collection of other TestRecorders.
addRecorder(TestRecorder $recorder)
This is a TestRecorder responsible for printing information about progress, success and failure to th...
static getParserTestFiles()
Get list of filenames to extension and core parser tests.
__construct()
Default constructor.
execute()
Do the actual work.
finalSetup()
Handle some last-minute setup here.
static getVersion( $flags='', $lang=null)
Return a string of the MediaWiki version with Git revision if available.
static applyInitialConfig()
This should be called before Setup.php, e.g.
Definition TestSetup.php:11
external whereas SearchGetNearMatch runs after $term
Definition hooks.txt:2814
require_once RUN_MAINTENANCE_IF_MAIN
$maintClass
const DB_MASTER
Definition defines.php:26