Merge "RCFilters: display timestamp of new changes in refresh link"
[lhc/web/wiklou.git] / tests / parser / ParserTestPrinter.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Testing
20 */
21
22 use MediaWiki\Shell\Shell;
23
24 /**
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
27 * frontend.
28 */
29 class ParserTestPrinter extends TestRecorder {
30 private $total;
31 private $success;
32 private $skipped;
33 private $term;
34 private $showDiffs;
35 private $showProgress;
36 private $showFailure;
37 private $showOutput;
38 private $useDwdiff;
39 private $markWhitespace;
40 private $xmlError;
41
42 function __construct( $term, $options ) {
43 $this->term = $term;
44 $options += [
45 'showDiffs' => true,
46 'showProgress' => true,
47 'showFailure' => true,
48 'showOutput' => false,
49 'useDwdiff' => false,
50 'markWhitespace' => false,
51 ];
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'];
58 }
59
60 public function start() {
61 $this->total = 0;
62 $this->success = 0;
63 $this->skipped = 0;
64 }
65
66 public function startTest( $test ) {
67 if ( $this->showProgress ) {
68 $this->showTesting( $test['desc'] );
69 }
70 }
71
72 private function showTesting( $desc ) {
73 print "Running test $desc... ";
74 }
75
76 /**
77 * Show "Reading tests from ..."
78 *
79 * @param string $path
80 */
81 public function startSuite( $path ) {
82 print $this->term->color( 1 ) .
83 "Running parser tests from \"$path\"..." .
84 $this->term->reset() .
85 "\n";
86 }
87
88 public function endSuite( $path ) {
89 print "\n";
90 }
91
92 public function record( $test, ParserTestResult $result ) {
93 $this->total++;
94 $this->success += ( $result->isSuccess() ? 1 : 0 );
95
96 if ( $result->isSuccess() ) {
97 $this->showSuccess( $result );
98 } else {
99 $this->showFailure( $result );
100 }
101 }
102
103 /**
104 * Print a happy success message.
105 *
106 * @param ParserTestResult $testResult
107 * @return bool
108 */
109 private function showSuccess( ParserTestResult $testResult ) {
110 if ( $this->showProgress ) {
111 print $this->term->color( '1;32' ) . 'PASSED' . $this->term->reset() . "\n";
112 }
113 }
114
115 /**
116 * Print a failure message and provide some explanatory output
117 * about what went wrong if so configured.
118 *
119 * @param ParserTestResult $testResult
120 * @return bool
121 */
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() );
128 }
129
130 print $this->term->color( '31' ) . 'FAILED!' . $this->term->reset() . "\n";
131
132 if ( $this->showOutput ) {
133 print "--- Expected ---\n{$testResult->expected}\n";
134 print "--- Actual ---\n{$testResult->actual}\n";
135 }
136
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";
141 }
142 }
143 }
144
145 return false;
146 }
147
148 /**
149 * Run given strings through a diff and return the (colorized) output.
150 * Requires writable /tmp directory and a 'diff' command in the PATH.
151 *
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
156 * @return string
157 */
158 private function quickDiff( $input, $output,
159 $inFileTail = 'expected', $outFileTail = 'actual'
160 ) {
161 if ( $this->markWhitespace ) {
162 $pairs = [
163 "\n" => '¶',
164 ' ' => '·',
165 "\t" => '→'
166 ];
167 $input = strtr( $input, $pairs );
168 $output = strtr( $output, $pairs );
169 }
170
171 # Windows, or at least the fc utility, is retarded
172 $slash = wfIsWindows() ? '\\' : '/';
173 $prefix = wfTempDir() . "{$slash}mwParser-" . mt_rand();
174
175 $infile = "$prefix-$inFileTail";
176 $this->dumpToFile( $input, $infile );
177
178 $outfile = "$prefix-$outFileTail";
179 $this->dumpToFile( $output, $outfile );
180
181 global $wgDiff3;
182 // we assume that people with diff3 also have usual diff
183 if ( $this->useDwdiff ) {
184 $shellCommand = 'dwdiff -Pc';
185 } else {
186 $shellCommand = ( wfIsWindows() && !$wgDiff3 ) ? 'fc' : 'diff -au';
187 }
188
189 $result = Shell::command()
190 ->unsafeParams( $shellCommand )
191 ->params( $infile, $outfile )
192 ->execute();
193 $diff = $result->getStdout();
194
195 unlink( $infile );
196 unlink( $outfile );
197
198 if ( $this->useDwdiff ) {
199 return $diff;
200 } else {
201 return $this->colorDiff( $diff );
202 }
203 }
204
205 /**
206 * Write the given string to a file, adding a final newline.
207 *
208 * @param string $data
209 * @param string $filename
210 */
211 private function dumpToFile( $data, $filename ) {
212 $file = fopen( $filename, "wt" );
213 fwrite( $file, $data . "\n" );
214 fclose( $file );
215 }
216
217 /**
218 * Colorize unified diff output if set for ANSI color output.
219 * Subtractions are colored blue, additions red.
220 *
221 * @param string $text
222 * @return string
223 */
224 private function colorDiff( $text ) {
225 return preg_replace(
226 [ '/^(-.*)$/m', '/^(\+.*)$/m' ],
227 [ $this->term->color( 34 ) . '$1' . $this->term->reset(),
228 $this->term->color( 31 ) . '$1' . $this->term->reset() ],
229 $text );
230 }
231
232 private function wellFormed( $text ) {
233 $html =
234 Sanitizer::hackDocType() .
235 '<html>' .
236 $text .
237 '</html>';
238
239 $parser = xml_parser_create( "UTF-8" );
240
241 # case folding violates XML standard, turn it off
242 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
243
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 );
250
251 return false;
252 }
253
254 xml_parser_free( $parser );
255
256 return true;
257 }
258
259 private function extractFragment( $text, $position ) {
260 $start = max( 0, $position - 10 );
261 $before = $position - $start;
262 $fragment = '...' .
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 ) .
273 '...';
274 $display = str_replace( "\n", ' ', $fragment );
275 $caret = ' ' .
276 str_repeat( ' ', $before ) .
277 $this->term->color( 31 ) .
278 '^' .
279 $this->term->color( 0 );
280
281 return "$display\n$caret";
282 }
283
284 /**
285 * Show a warning to the user
286 * @param string $message
287 */
288 public function warning( $message ) {
289 echo "$message\n";
290 }
291
292 /**
293 * Mark a test skipped
294 * @param string $test
295 * @param string $subtest
296 */
297 public function skipped( $test, $subtest ) {
298 if ( $this->showProgress ) {
299 print $this->term->color( '1;33' ) . 'SKIPPED' . $this->term->reset() . "\n";
300 }
301 $this->skipped++;
302 }
303
304 public function report() {
305 if ( $this->total > 0 ) {
306 $this->reportPercentage( $this->success, $this->total );
307 } else {
308 print $this->term->color( 31 ) . "No tests found." . $this->term->reset() . "\n";
309 }
310 }
311
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}";
317 }
318 print "... ";
319
320 if ( $success == $total ) {
321 print $this->term->color( 32 ) . "ALL TESTS PASSED!";
322 } else {
323 $failed = $total - $success;
324 print $this->term->color( 31 ) . "$failed tests failed!";
325 }
326
327 print $this->term->reset() . "\n";
328
329 return ( $success == $total );
330 }
331 }