3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 use MediaWiki\Shell\Shell
;
25 * This is a TestRecorder responsible for printing information about progress,
26 * success and failure to the console. It is specific to the parserTests.php
29 class ParserTestPrinter
extends TestRecorder
{
35 private $showProgress;
39 private $markWhitespace;
42 function __construct( $term, $options ) {
46 'showProgress' => true,
47 'showFailure' => true,
48 'showOutput' => false,
50 'markWhitespace' => false,
52 $this->showDiffs
= $options['showDiffs'];
53 $this->showProgress
= $options['showProgress'];
54 $this->showFailure
= $options['showFailure'];
55 $this->showOutput
= $options['showOutput'];
56 $this->useDwdiff
= $options['useDwdiff'];
57 $this->markWhitespace
= $options['markWhitespace'];
60 public function start() {
66 public function startTest( $test ) {
67 if ( $this->showProgress
) {
68 $this->showTesting( $test['desc'] );
72 private function showTesting( $desc ) {
73 print "Running test $desc... ";
77 * Show "Reading tests from ..."
81 public function startSuite( $path ) {
82 print $this->term
->color( 1 ) .
83 "Running parser tests from \"$path\"..." .
84 $this->term
->reset() .
88 public function endSuite( $path ) {
92 public function record( $test, ParserTestResult
$result ) {
94 $this->success +
= ( $result->isSuccess() ?
1 : 0 );
96 if ( $result->isSuccess() ) {
97 $this->showSuccess( $result );
99 $this->showFailure( $result );
104 * Print a happy success message.
106 * @param ParserTestResult $testResult
109 private function showSuccess( ParserTestResult
$testResult ) {
110 if ( $this->showProgress
) {
111 print $this->term
->color( '1;32' ) . 'PASSED' . $this->term
->reset() . "\n";
116 * Print a failure message and provide some explanatory output
117 * about what went wrong if so configured.
119 * @param ParserTestResult $testResult
122 private function showFailure( ParserTestResult
$testResult ) {
123 if ( $this->showFailure
) {
124 if ( !$this->showProgress
) {
125 # In quiet mode we didn't show the 'Testing' message before the
126 # test, in case it succeeded. Show it now:
127 $this->showTesting( $testResult->getDescription() );
130 print $this->term
->color( '31' ) . 'FAILED!' . $this->term
->reset() . "\n";
132 if ( $this->showOutput
) {
133 print "--- Expected ---\n{$testResult->expected}\n";
134 print "--- Actual ---\n{$testResult->actual}\n";
137 if ( $this->showDiffs
) {
138 print $this->quickDiff( $testResult->expected
, $testResult->actual
);
139 if ( !$this->wellFormed( $testResult->actual
) ) {
140 print "XML error: $this->xmlError\n";
149 * Run given strings through a diff and return the (colorized) output.
150 * Requires writable /tmp directory and a 'diff' command in the PATH.
152 * @param string $input
153 * @param string $output
154 * @param string $inFileTail Tailing for the input file name
155 * @param string $outFileTail Tailing for the output file name
158 private function quickDiff( $input, $output,
159 $inFileTail = 'expected', $outFileTail = 'actual'
161 if ( $this->markWhitespace
) {
167 $input = strtr( $input, $pairs );
168 $output = strtr( $output, $pairs );
171 # Windows, or at least the fc utility, is retarded
172 $slash = wfIsWindows() ?
'\\' : '/';
173 $prefix = wfTempDir() . "{$slash}mwParser-" . mt_rand();
175 $infile = "$prefix-$inFileTail";
176 $this->dumpToFile( $input, $infile );
178 $outfile = "$prefix-$outFileTail";
179 $this->dumpToFile( $output, $outfile );
182 // we assume that people with diff3 also have usual diff
183 if ( $this->useDwdiff
) {
184 $shellCommand = 'dwdiff -Pc';
186 $shellCommand = ( wfIsWindows() && !$wgDiff3 ) ?
'fc' : 'diff -au';
189 $result = Shell
::command()
190 ->unsafeParams( $shellCommand )
191 ->params( $infile, $outfile )
193 $diff = $result->getStdout();
198 if ( $this->useDwdiff
) {
201 return $this->colorDiff( $diff );
206 * Write the given string to a file, adding a final newline.
208 * @param string $data
209 * @param string $filename
211 private function dumpToFile( $data, $filename ) {
212 $file = fopen( $filename, "wt" );
213 fwrite( $file, $data . "\n" );
218 * Colorize unified diff output if set for ANSI color output.
219 * Subtractions are colored blue, additions red.
221 * @param string $text
224 private function colorDiff( $text ) {
226 [ '/^(-.*)$/m', '/^(\+.*)$/m' ],
227 [ $this->term
->color( 34 ) . '$1' . $this->term
->reset(),
228 $this->term
->color( 31 ) . '$1' . $this->term
->reset() ],
232 private function wellFormed( $text ) {
234 Sanitizer
::hackDocType() .
239 $parser = xml_parser_create( "UTF-8" );
241 # case folding violates XML standard, turn it off
242 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
244 if ( !xml_parse( $parser, $html, true ) ) {
245 $err = xml_error_string( xml_get_error_code( $parser ) );
246 $position = xml_get_current_byte_index( $parser );
247 $fragment = $this->extractFragment( $html, $position );
248 $this->xmlError
= "$err at byte $position:\n$fragment";
249 xml_parser_free( $parser );
254 xml_parser_free( $parser );
259 private function extractFragment( $text, $position ) {
260 $start = max( 0, $position - 10 );
261 $before = $position - $start;
263 $this->term
->color( 34 ) .
264 substr( $text, $start, $before ) .
265 $this->term
->color( 0 ) .
266 $this->term
->color( 31 ) .
267 $this->term
->color( 1 ) .
268 substr( $text, $position, 1 ) .
269 $this->term
->color( 0 ) .
270 $this->term
->color( 34 ) .
271 substr( $text, $position +
1, 9 ) .
272 $this->term
->color( 0 ) .
274 $display = str_replace( "\n", ' ', $fragment );
276 str_repeat( ' ', $before ) .
277 $this->term
->color( 31 ) .
279 $this->term
->color( 0 );
281 return "$display\n$caret";
285 * Show a warning to the user
286 * @param string $message
288 public function warning( $message ) {
293 * Mark a test skipped
294 * @param string $test
295 * @param string $subtest
297 public function skipped( $test, $subtest ) {
298 if ( $this->showProgress
) {
299 print $this->term
->color( '1;33' ) . 'SKIPPED' . $this->term
->reset() . "\n";
304 public function report() {
305 if ( $this->total
> 0 ) {
306 $this->reportPercentage( $this->success
, $this->total
);
308 print $this->term
->color( 31 ) . "No tests found." . $this->term
->reset() . "\n";
312 private function reportPercentage( $success, $total ) {
313 $ratio = wfPercent( 100 * $success / $total );
314 print $this->term
->color( 1 ) . "Passed $success of $total tests ($ratio)";
315 if ( $this->skipped
) {
316 print ", skipped {$this->skipped}";
320 if ( $success == $total ) {
321 print $this->term
->color( 32 ) . "ALL TESTS PASSED!";
323 $failed = $total - $success;
324 print $this->term
->color( 31 ) . "$failed tests failed!";
327 print $this->term
->reset() . "\n";
329 return ( $success == $total );