MediaWiki  REL1_31
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.
29 define( 'MW_PARSER_TEST', true );
30 
31 require __DIR__ . '/../../maintenance/Maintenance.php';
32 
34 
36  function __construct() {
37  parent::__construct();
38  $this->addDescription( 'Run parser tests' );
39 
40  $this->addOption( 'quick', 'Suppress diff output of failed tests' );
41  $this->addOption( 'quiet', 'Suppress notification of passed tests (shows only failed tests)' );
42  $this->addOption( 'show-output', 'Show expected and actual output' );
43  $this->addOption( 'color', '[=yes|no] Override terminal detection and force ' .
44  'color output on or off. Use wgCommandLineDarkBg = true; if your term is dark',
45  false, true );
46  $this->addOption( 'regex', 'Only run tests whose descriptions which match given regex',
47  false, true );
48  $this->addOption( 'filter', 'Alias for --regex', false, true );
49  $this->addOption( 'file', 'Run test cases from a custom file instead of parserTests.txt',
50  false, true, false, true );
51  $this->addOption( 'record', 'Record tests in database' );
52  $this->addOption( 'compare', 'Compare with recorded results, without updating the database.' );
53  $this->addOption( 'setversion', 'When using --record, set the version string to use (useful' .
54  'with "git rev-parse HEAD" to get the exact revision)',
55  false, true );
56  $this->addOption( 'keep-uploads', 'Re-use the same upload directory for each ' .
57  'test, don\'t delete it' );
58  $this->addOption( 'file-backend', 'Use the file backend with the given name,' .
59  'and upload files to it, instead of creating a mock file backend.', false, true );
60  $this->addOption( 'upload-dir', 'Specify the upload directory to use. Useful in ' .
61  'conjunction with --keep-uploads. Causes a real (non-mock) file backend to ' .
62  'be used.', false, true );
63  $this->addOption( 'run-disabled', 'run disabled tests' );
64  $this->addOption( 'run-parsoid', 'run parsoid tests (normally disabled)' );
65  $this->addOption( 'dwdiff', 'Use dwdiff to display diff output' );
66  $this->addOption( 'mark-ws', 'Mark whitespace in diffs by replacing it with symbols' );
67  $this->addOption( 'norm', 'Apply a comma-separated list of normalization functions to ' .
68  'both the expected and actual output in order to resolve ' .
69  'irrelevant differences. The accepted normalization functions ' .
70  'are: removeTbody to remove <tbody> tags; and trimWhitespace ' .
71  'to trim whitespace from the start and end of text nodes.',
72  false, true );
73  $this->addOption( 'use-tidy-config',
74  'Use the wiki\'s Tidy configuration instead of known-good' .
75  'defaults.' );
76  }
77 
78  public function finalSetup() {
79  parent::finalSetup();
82  }
83 
84  public function execute() {
86 
87  // Cases of weird db corruption were encountered when running tests on earlyish
88  // versions of SQLite
89  if ( $wgDBtype == 'sqlite' ) {
90  $db = wfGetDB( DB_MASTER );
91  $version = $db->getServerVersion();
92  if ( version_compare( $version, '3.6' ) < 0 ) {
93  die( "Parser tests require SQLite version 3.6 or later, you have $version\n" );
94  }
95  }
96 
97  // Print out software version to assist with locating regressions
98  $version = SpecialVersion::getVersion( 'nodb' );
99  echo "This is MediaWiki version {$version}.\n\n";
100 
101  // Only colorize output if stdout is a terminal.
102  $color = !wfIsWindows() && Maintenance::posix_isatty( 1 );
103 
104  if ( $this->hasOption( 'color' ) ) {
105  switch ( $this->getOption( 'color' ) ) {
106  case 'no':
107  $color = false;
108  break;
109  case 'yes':
110  default:
111  $color = true;
112  break;
113  }
114  }
115 
116  $record = $this->hasOption( 'record' );
117  $compare = $this->hasOption( 'compare' );
118 
119  $regex = $this->getOption( 'filter', $this->getOption( 'regex', false ) );
120  if ( $regex !== false ) {
121  $regex = "/$regex/i";
122 
123  if ( $record ) {
124  echo "Warning: --record cannot be used with --regex, disabling --record\n";
125  $record = false;
126  }
127  }
128 
129  $term = $color
130  ? new AnsiTermColorer()
131  : new DummyTermColorer();
132 
133  $recorder = new MultiTestRecorder;
134 
135  $recorder->addRecorder( new ParserTestPrinter(
136  $term,
137  [
138  'showDiffs' => !$this->hasOption( 'quick' ),
139  'showProgress' => !$this->hasOption( 'quiet' ),
140  'showFailure' => !$this->hasOption( 'quiet' )
141  || ( !$record && !$compare ), // redundant output
142  'showOutput' => $this->hasOption( 'show-output' ),
143  'useDwdiff' => $this->hasOption( 'dwdiff' ),
144  'markWhitespace' => $this->hasOption( 'mark-ws' ),
145  ]
146  ) );
147 
148  $recorderLB = false;
149  if ( $record || $compare ) {
150  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
151  $recorderLB = $lbFactory->newMainLB();
152  // This connection will have the wiki's table prefix, not parsertest_
153  $recorderDB = $recorderLB->getConnection( DB_MASTER );
154 
155  // Add recorder before previewer because recorder will create the
156  // DB table if it doesn't exist
157  if ( $record ) {
158  $recorder->addRecorder( new DbTestRecorder( $recorderDB ) );
159  }
160  $recorder->addRecorder( new DbTestPreviewer(
161  $recorderDB,
162  function ( $name ) use ( $regex ) {
163  // Filter reports of old tests by the filter regex
164  if ( $regex === false ) {
165  return true;
166  } else {
167  return (bool)preg_match( $regex, $name );
168  }
169  } ) );
170  }
171 
172  // Default parser tests and any set from extensions or local config
173  $files = $this->getOption( 'file', ParserTestRunner::getParserTestFiles() );
174 
175  $norm = $this->hasOption( 'norm' ) ? explode( ',', $this->getOption( 'norm' ) ) : [];
176 
177  $tester = new ParserTestRunner( $recorder, [
178  'norm' => $norm,
179  'regex' => $regex,
180  'keep-uploads' => $this->hasOption( 'keep-uploads' ),
181  'run-disabled' => $this->hasOption( 'run-disabled' ),
182  'run-parsoid' => $this->hasOption( 'run-parsoid' ),
183  'use-tidy-config' => $this->hasOption( 'use-tidy-config' ),
184  'file-backend' => $this->getOption( 'file-backend' ),
185  'upload-dir' => $this->getOption( 'upload-dir' ),
186  ] );
187 
188  $ok = $tester->runTestsFromFiles( $files );
189  if ( $recorderLB ) {
190  $recorderLB->closeAll();
191  }
192  if ( !$ok ) {
193  exit( 1 );
194  }
195  }
196 }
197 
198 $maintClass = 'ParserTestsMaintenance';
199 require_once RUN_MAINTENANCE_IF_MAIN;
DummyTermColorer
A colour-less terminal.
Definition: MWTerm.php:72
ParserTestPrinter
This is a TestRecorder responsible for printing information about progress, success and failure to th...
Definition: ParserTestPrinter.php:27
ParserTestsMaintenance\__construct
__construct()
Default constructor.
Definition: parserTests.php:36
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
ParserTestsMaintenance\execute
execute()
Do the actual work.
Definition: parserTests.php:84
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:291
$wgDBtype
$wgDBtype
Database type.
Definition: DefaultSettings.php:1819
ParserTestRunner\getParserTestFiles
static getParserTestFiles()
Get list of filenames to extension and core parser tests.
Definition: ParserTestRunner.php:167
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$maintClass
$maintClass
Definition: parserTests.php:198
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
$term
For QUnit the mediawiki tests qunit testrunner dependency will be added to any module whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2845
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2812
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:219
TestSetup\applyInitialConfig
static applyInitialConfig()
This should be called before Setup.php, e.g.
Definition: TestSetup.php:11
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
DB_MASTER
const DB_MASTER
Definition: defines.php:26
AnsiTermColorer
Terminal that supports ANSI escape sequences.
Definition: MWTerm.php:39
ParserTestsMaintenance
Definition: parserTests.php:35
SpecialVersion\getVersion
static getVersion( $flags='', $lang=null)
Return a string of the MediaWiki version with Git revision if available.
Definition: SpecialVersion.php:271
DbTestPreviewer
Definition: DbTestPreviewer.php:22
wfIsWindows
wfIsWindows()
Check if the operating system is Windows.
Definition: GlobalFunctions.php:2019
Maintenance\requireTestsAutoloader
static requireTestsAutoloader()
Call this to set up the autoloader to allow classes to be used from the tests directory.
Definition: Maintenance.php:1620
Maintenance\posix_isatty
static posix_isatty( $fd)
Wrapper for posix_isatty() We default as considering stdin a tty (for nice readline methods) but trea...
Definition: Maintenance.php:1511
DbTestRecorder
Definition: DbTestRecorder.php:24
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:254
ParserTestsMaintenance\finalSetup
finalSetup()
Handle some last-minute setup here.
Definition: parserTests.php:78
MultiTestRecorder\addRecorder
addRecorder(TestRecorder $recorder)
Definition: MultiTestRecorder.php:10
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:25
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:240
MultiTestRecorder
This is a TestRecorder representing a collection of other TestRecorders.
Definition: MultiTestRecorder.php:7
ParserTestRunner
Definition: ParserTestRunner.php:36