ParserTestPrinter.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. * This is a TestRecorder responsible for printing information about progress,
  23. * success and failure to the console. It is specific to the parserTests.php
  24. * frontend.
  25. */
  26. class ParserTestPrinter extends TestRecorder {
  27. private $total;
  28. private $success;
  29. private $skipped;
  30. private $term;
  31. private $showDiffs;
  32. private $showProgress;
  33. private $showFailure;
  34. private $showOutput;
  35. private $useDwdiff;
  36. private $markWhitespace;
  37. private $xmlError;
  38. function __construct( $term, $options ) {
  39. $this->term = $term;
  40. $options += [
  41. 'showDiffs' => true,
  42. 'showProgress' => true,
  43. 'showFailure' => true,
  44. 'showOutput' => false,
  45. 'useDwdiff' => false,
  46. 'markWhitespace' => false,
  47. ];
  48. $this->showDiffs = $options['showDiffs'];
  49. $this->showProgress = $options['showProgress'];
  50. $this->showFailure = $options['showFailure'];
  51. $this->showOutput = $options['showOutput'];
  52. $this->useDwdiff = $options['useDwdiff'];
  53. $this->markWhitespace = $options['markWhitespace'];
  54. }
  55. public function start() {
  56. $this->total = 0;
  57. $this->success = 0;
  58. $this->skipped = 0;
  59. }
  60. public function startTest( $test ) {
  61. if ( $this->showProgress ) {
  62. $this->showTesting( $test['desc'] );
  63. }
  64. }
  65. private function showTesting( $desc ) {
  66. print "Running test $desc... ";
  67. }
  68. /**
  69. * Show "Reading tests from ..."
  70. *
  71. * @param string $path
  72. */
  73. public function startSuite( $path ) {
  74. print $this->term->color( 1 ) .
  75. "Running parser tests from \"$path\"..." .
  76. $this->term->reset() .
  77. "\n";
  78. }
  79. public function endSuite( $path ) {
  80. print "\n";
  81. }
  82. public function record( $test, ParserTestResult $result ) {
  83. $this->total++;
  84. $this->success += ( $result->isSuccess() ? 1 : 0 );
  85. if ( $result->isSuccess() ) {
  86. $this->showSuccess( $result );
  87. } else {
  88. $this->showFailure( $result );
  89. }
  90. }
  91. /**
  92. * Print a happy success message.
  93. *
  94. * @param ParserTestResult $testResult
  95. * @return bool
  96. */
  97. private function showSuccess( ParserTestResult $testResult ) {
  98. if ( $this->showProgress ) {
  99. print $this->term->color( '1;32' ) . 'PASSED' . $this->term->reset() . "\n";
  100. }
  101. }
  102. /**
  103. * Print a failure message and provide some explanatory output
  104. * about what went wrong if so configured.
  105. *
  106. * @param ParserTestResult $testResult
  107. * @return bool
  108. */
  109. private function showFailure( ParserTestResult $testResult ) {
  110. if ( $this->showFailure ) {
  111. if ( !$this->showProgress ) {
  112. # In quiet mode we didn't show the 'Testing' message before the
  113. # test, in case it succeeded. Show it now:
  114. $this->showTesting( $testResult->getDescription() );
  115. }
  116. print $this->term->color( '31' ) . 'FAILED!' . $this->term->reset() . "\n";
  117. if ( $this->showOutput ) {
  118. print "--- Expected ---\n{$testResult->expected}\n";
  119. print "--- Actual ---\n{$testResult->actual}\n";
  120. }
  121. if ( $this->showDiffs ) {
  122. print $this->quickDiff( $testResult->expected, $testResult->actual );
  123. if ( !$this->wellFormed( $testResult->actual ) ) {
  124. print "XML error: $this->xmlError\n";
  125. }
  126. }
  127. }
  128. return false;
  129. }
  130. /**
  131. * Run given strings through a diff and return the (colorized) output.
  132. * Requires writable /tmp directory and a 'diff' command in the PATH.
  133. *
  134. * @param string $input
  135. * @param string $output
  136. * @param string $inFileTail Tailing for the input file name
  137. * @param string $outFileTail Tailing for the output file name
  138. * @return string
  139. */
  140. private function quickDiff( $input, $output,
  141. $inFileTail = 'expected', $outFileTail = 'actual'
  142. ) {
  143. if ( $this->markWhitespace ) {
  144. $pairs = [
  145. "\n" => '¶',
  146. ' ' => '·',
  147. "\t" => '→'
  148. ];
  149. $input = strtr( $input, $pairs );
  150. $output = strtr( $output, $pairs );
  151. }
  152. # Windows, or at least the fc utility, is retarded
  153. $slash = wfIsWindows() ? '\\' : '/';
  154. $prefix = wfTempDir() . "{$slash}mwParser-" . mt_rand();
  155. $infile = "$prefix-$inFileTail";
  156. $this->dumpToFile( $input, $infile );
  157. $outfile = "$prefix-$outFileTail";
  158. $this->dumpToFile( $output, $outfile );
  159. $shellInfile = wfEscapeShellArg( $infile );
  160. $shellOutfile = wfEscapeShellArg( $outfile );
  161. global $wgDiff3;
  162. // we assume that people with diff3 also have usual diff
  163. if ( $this->useDwdiff ) {
  164. $shellCommand = 'dwdiff -Pc';
  165. } else {
  166. $shellCommand = ( wfIsWindows() && !$wgDiff3 ) ? 'fc' : 'diff -au';
  167. }
  168. $diff = wfShellExec( "$shellCommand $shellInfile $shellOutfile" );
  169. unlink( $infile );
  170. unlink( $outfile );
  171. if ( $this->useDwdiff ) {
  172. return $diff;
  173. } else {
  174. return $this->colorDiff( $diff );
  175. }
  176. }
  177. /**
  178. * Write the given string to a file, adding a final newline.
  179. *
  180. * @param string $data
  181. * @param string $filename
  182. */
  183. private function dumpToFile( $data, $filename ) {
  184. $file = fopen( $filename, "wt" );
  185. fwrite( $file, $data . "\n" );
  186. fclose( $file );
  187. }
  188. /**
  189. * Colorize unified diff output if set for ANSI color output.
  190. * Subtractions are colored blue, additions red.
  191. *
  192. * @param string $text
  193. * @return string
  194. */
  195. private function colorDiff( $text ) {
  196. return preg_replace(
  197. [ '/^(-.*)$/m', '/^(\+.*)$/m' ],
  198. [ $this->term->color( 34 ) . '$1' . $this->term->reset(),
  199. $this->term->color( 31 ) . '$1' . $this->term->reset() ],
  200. $text );
  201. }
  202. private function wellFormed( $text ) {
  203. $html =
  204. Sanitizer::hackDocType() .
  205. '<html>' .
  206. $text .
  207. '</html>';
  208. $parser = xml_parser_create( "UTF-8" );
  209. # case folding violates XML standard, turn it off
  210. xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
  211. if ( !xml_parse( $parser, $html, true ) ) {
  212. $err = xml_error_string( xml_get_error_code( $parser ) );
  213. $position = xml_get_current_byte_index( $parser );
  214. $fragment = $this->extractFragment( $html, $position );
  215. $this->xmlError = "$err at byte $position:\n$fragment";
  216. xml_parser_free( $parser );
  217. return false;
  218. }
  219. xml_parser_free( $parser );
  220. return true;
  221. }
  222. private function extractFragment( $text, $position ) {
  223. $start = max( 0, $position - 10 );
  224. $before = $position - $start;
  225. $fragment = '...' .
  226. $this->term->color( 34 ) .
  227. substr( $text, $start, $before ) .
  228. $this->term->color( 0 ) .
  229. $this->term->color( 31 ) .
  230. $this->term->color( 1 ) .
  231. substr( $text, $position, 1 ) .
  232. $this->term->color( 0 ) .
  233. $this->term->color( 34 ) .
  234. substr( $text, $position + 1, 9 ) .
  235. $this->term->color( 0 ) .
  236. '...';
  237. $display = str_replace( "\n", ' ', $fragment );
  238. $caret = ' ' .
  239. str_repeat( ' ', $before ) .
  240. $this->term->color( 31 ) .
  241. '^' .
  242. $this->term->color( 0 );
  243. return "$display\n$caret";
  244. }
  245. /**
  246. * Show a warning to the user
  247. * @param string $message
  248. */
  249. public function warning( $message ) {
  250. echo "$message\n";
  251. }
  252. /**
  253. * Mark a test skipped
  254. * @param string $test
  255. * @param string $subtest
  256. */
  257. public function skipped( $test, $subtest ) {
  258. if ( $this->showProgress ) {
  259. print $this->term->color( '1;33' ) . 'SKIPPED' . $this->term->reset() . "\n";
  260. }
  261. $this->skipped++;
  262. }
  263. public function report() {
  264. if ( $this->total > 0 ) {
  265. $this->reportPercentage( $this->success, $this->total );
  266. } else {
  267. print $this->term->color( 31 ) . "No tests found." . $this->term->reset() . "\n";
  268. }
  269. }
  270. private function reportPercentage( $success, $total ) {
  271. $ratio = wfPercent( 100 * $success / $total );
  272. print $this->term->color( 1 ) . "Passed $success of $total tests ($ratio)";
  273. if ( $this->skipped ) {
  274. print ", skipped {$this->skipped}";
  275. }
  276. print "... ";
  277. if ( $success == $total ) {
  278. print $this->term->color( 32 ) . "ALL TESTS PASSED!";
  279. } else {
  280. $failed = $total - $success;
  281. print $this->term->color( 31 ) . "$failed tests failed!";
  282. }
  283. print $this->term->reset() . "\n";
  284. return ( $success == $total );
  285. }
  286. }