editTests.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. require __DIR__ . '/../../maintenance/Maintenance.php';
  3. define( 'MW_PARSER_TEST', true );
  4. /**
  5. * Interactive parser test runner and test file editor
  6. */
  7. class ParserEditTests extends Maintenance {
  8. private $termWidth;
  9. private $testFiles;
  10. private $testCount;
  11. private $recorder;
  12. private $runner;
  13. private $numExecuted;
  14. private $numSkipped;
  15. private $numFailed;
  16. function __construct() {
  17. parent::__construct();
  18. $this->addOption( 'session-data', 'internal option, do not use', false, true );
  19. $this->addOption( 'use-tidy-config',
  20. 'Use the wiki\'s Tidy configuration instead of known-good' .
  21. 'defaults.' );
  22. }
  23. public function finalSetup() {
  24. parent::finalSetup();
  25. self::requireTestsAutoloader();
  26. TestSetup::applyInitialConfig();
  27. }
  28. public function execute() {
  29. $this->termWidth = $this->getTermSize()[0] - 1;
  30. $this->recorder = new TestRecorder();
  31. $this->setupFileData();
  32. if ( $this->hasOption( 'session-data' ) ) {
  33. $this->session = json_decode( $this->getOption( 'session-data' ), true );
  34. } else {
  35. $this->session = [ 'options' => [] ];
  36. }
  37. if ( $this->hasOption( 'use-tidy-config' ) ) {
  38. $this->session['options']['use-tidy-config'] = true;
  39. }
  40. $this->runner = new ParserTestRunner( $this->recorder, $this->session['options'] );
  41. $this->runTests();
  42. if ( $this->numFailed === 0 ) {
  43. if ( $this->numSkipped === 0 ) {
  44. print "All tests passed!\n";
  45. } else {
  46. print "All tests passed (but skipped {$this->numSkipped})\n";
  47. }
  48. return;
  49. }
  50. print "{$this->numFailed} test(s) failed.\n";
  51. $this->showResults();
  52. }
  53. protected function setupFileData() {
  54. $this->testFiles = [];
  55. $this->testCount = 0;
  56. foreach ( ParserTestRunner::getParserTestFiles() as $file ) {
  57. $fileInfo = TestFileReader::read( $file );
  58. $this->testFiles[$file] = $fileInfo;
  59. $this->testCount += count( $fileInfo['tests'] );
  60. }
  61. }
  62. protected function runTests() {
  63. $teardown = $this->runner->staticSetup();
  64. $teardown = $this->runner->setupDatabase( $teardown );
  65. $teardown = $this->runner->setupUploads( $teardown );
  66. print "Running tests...\n";
  67. $this->results = [];
  68. $this->numExecuted = 0;
  69. $this->numSkipped = 0;
  70. $this->numFailed = 0;
  71. foreach ( $this->testFiles as $fileName => $fileInfo ) {
  72. $this->runner->addArticles( $fileInfo['articles'] );
  73. foreach ( $fileInfo['tests'] as $testInfo ) {
  74. $result = $this->runner->runTest( $testInfo );
  75. if ( $result === false ) {
  76. $this->numSkipped++;
  77. } elseif ( !$result->isSuccess() ) {
  78. $this->results[$fileName][$testInfo['desc']] = $result;
  79. $this->numFailed++;
  80. }
  81. $this->numExecuted++;
  82. $this->showProgress();
  83. }
  84. }
  85. print "\n";
  86. }
  87. protected function showProgress() {
  88. $done = $this->numExecuted;
  89. $total = $this->testCount;
  90. $width = $this->termWidth - 9;
  91. $pos = round( $width * $done / $total );
  92. printf( '│' . str_repeat( '█', $pos ) . str_repeat( '-', $width - $pos ) .
  93. "│ %5.1f%%\r", $done / $total * 100 );
  94. }
  95. protected function showResults() {
  96. if ( isset( $this->session['startFile'] ) ) {
  97. $startFile = $this->session['startFile'];
  98. $startTest = $this->session['startTest'];
  99. $foundStart = false;
  100. } else {
  101. $startFile = false;
  102. $startTest = false;
  103. $foundStart = true;
  104. }
  105. $testIndex = 0;
  106. foreach ( $this->testFiles as $fileName => $fileInfo ) {
  107. if ( !isset( $this->results[$fileName] ) ) {
  108. continue;
  109. }
  110. if ( !$foundStart && $startFile !== false && $fileName !== $startFile ) {
  111. $testIndex += count( $this->results[$fileName] );
  112. continue;
  113. }
  114. foreach ( $fileInfo['tests'] as $testInfo ) {
  115. if ( !isset( $this->results[$fileName][$testInfo['desc']] ) ) {
  116. continue;
  117. }
  118. $result = $this->results[$fileName][$testInfo['desc']];
  119. $testIndex++;
  120. if ( !$foundStart && $startTest !== false ) {
  121. if ( $testInfo['desc'] !== $startTest ) {
  122. continue;
  123. }
  124. $foundStart = true;
  125. }
  126. $this->handleFailure( $testIndex, $testInfo, $result );
  127. }
  128. }
  129. if ( !$foundStart ) {
  130. print "Could not find the test after a restart, did you rename it?";
  131. unset( $this->session['startFile'] );
  132. unset( $this->session['startTest'] );
  133. $this->showResults();
  134. }
  135. print "All done\n";
  136. }
  137. protected function heading( $text ) {
  138. $term = new AnsiTermColorer;
  139. $heading = "─── $text ";
  140. $heading .= str_repeat( '─', $this->termWidth - mb_strlen( $heading ) );
  141. $heading = $term->color( 34 ) . $heading . $term->reset() . "\n";
  142. return $heading;
  143. }
  144. protected function unifiedDiff( $left, $right ) {
  145. $fromLines = explode( "\n", $left );
  146. $toLines = explode( "\n", $right );
  147. $formatter = new UnifiedDiffFormatter;
  148. return $formatter->format( new Diff( $fromLines, $toLines ) );
  149. }
  150. protected function handleFailure( $index, $testInfo, $result ) {
  151. $term = new AnsiTermColorer;
  152. $div1 = $term->color( 34 ) . str_repeat( '━', $this->termWidth ) .
  153. $term->reset() . "\n";
  154. $div2 = $term->color( 34 ) . str_repeat( '─', $this->termWidth ) .
  155. $term->reset() . "\n";
  156. print $div1;
  157. print "Failure $index/{$this->numFailed}: {$testInfo['file']} line {$testInfo['line']}\n" .
  158. "{$testInfo['desc']}\n";
  159. print $this->heading( 'Input' );
  160. print "{$testInfo['input']}\n";
  161. print $this->heading( 'Alternating expected/actual output' );
  162. print $this->alternatingAligned( $result->expected, $result->actual );
  163. print $this->heading( 'Diff' );
  164. $dwdiff = $this->dwdiff( $result->expected, $result->actual );
  165. if ( $dwdiff !== false ) {
  166. $diff = $dwdiff;
  167. } else {
  168. $diff = $this->unifiedDiff( $result->expected, $result->actual );
  169. }
  170. print $diff;
  171. if ( $testInfo['options'] || $testInfo['config'] ) {
  172. print $this->heading( 'Options / Config' );
  173. if ( $testInfo['options'] ) {
  174. print $testInfo['options'] . "\n";
  175. }
  176. if ( $testInfo['config'] ) {
  177. print $testInfo['config'] . "\n";
  178. }
  179. }
  180. print $div2;
  181. print "What do you want to do?\n";
  182. $specs = [
  183. '[R]eload code and run again',
  184. '[U]pdate source file, copy actual to expected',
  185. '[I]gnore' ];
  186. if ( strpos( $testInfo['options'], ' tidy' ) === false ) {
  187. if ( empty( $testInfo['isSubtest'] ) ) {
  188. $specs[] = "Enable [T]idy";
  189. }
  190. } else {
  191. $specs[] = 'Disable [T]idy';
  192. }
  193. if ( !empty( $testInfo['isSubtest'] ) ) {
  194. $specs[] = 'Delete [s]ubtest';
  195. }
  196. $specs[] = '[D]elete test';
  197. $specs[] = '[Q]uit';
  198. $options = [];
  199. foreach ( $specs as $spec ) {
  200. if ( !preg_match( '/^(.*\[)(.)(\].*)$/', $spec, $m ) ) {
  201. throw new MWException( 'Invalid option spec: ' . $spec );
  202. }
  203. print '* ' . $m[1] . $term->color( 35 ) . $m[2] . $term->color( 0 ) . $m[3] . "\n";
  204. $options[strtoupper( $m[2] )] = true;
  205. }
  206. do {
  207. $response = $this->readconsole();
  208. $cmdResult = false;
  209. if ( $response === false ) {
  210. exit( 0 );
  211. }
  212. $response = strtoupper( trim( $response ) );
  213. if ( !isset( $options[$response] ) ) {
  214. print "Invalid response, please enter a single letter from the list above\n";
  215. continue;
  216. }
  217. switch ( strtoupper( trim( $response ) ) ) {
  218. case 'R':
  219. $cmdResult = $this->reload( $testInfo );
  220. break;
  221. case 'U':
  222. $cmdResult = $this->update( $testInfo, $result );
  223. break;
  224. case 'I':
  225. return;
  226. case 'T':
  227. $cmdResult = $this->switchTidy( $testInfo );
  228. break;
  229. case 'S':
  230. $cmdResult = $this->deleteSubtest( $testInfo );
  231. break;
  232. case 'D':
  233. $cmdResult = $this->deleteTest( $testInfo );
  234. break;
  235. case 'Q':
  236. exit( 0 );
  237. }
  238. } while ( !$cmdResult );
  239. }
  240. protected function dwdiff( $expected, $actual ) {
  241. if ( !is_executable( '/usr/bin/dwdiff' ) ) {
  242. return false;
  243. }
  244. $markers = [
  245. "\n" => '¶',
  246. ' ' => '·',
  247. "\t" => '→'
  248. ];
  249. $markedExpected = strtr( $expected, $markers );
  250. $markedActual = strtr( $actual, $markers );
  251. $diff = $this->unifiedDiff( $markedExpected, $markedActual );
  252. $tempFile = tmpfile();
  253. fwrite( $tempFile, $diff );
  254. fseek( $tempFile, 0 );
  255. $pipes = [];
  256. $proc = proc_open( '/usr/bin/dwdiff -Pc --diff-input',
  257. [ 0 => $tempFile, 1 => [ 'pipe', 'w' ], 2 => STDERR ],
  258. $pipes );
  259. if ( !$proc ) {
  260. return false;
  261. }
  262. $result = stream_get_contents( $pipes[1] );
  263. proc_close( $proc );
  264. fclose( $tempFile );
  265. return $result;
  266. }
  267. protected function alternatingAligned( $expectedStr, $actualStr ) {
  268. $expectedLines = explode( "\n", $expectedStr );
  269. $actualLines = explode( "\n", $actualStr );
  270. $maxLines = max( count( $expectedLines ), count( $actualLines ) );
  271. $result = '';
  272. for ( $i = 0; $i < $maxLines; $i++ ) {
  273. if ( $i < count( $expectedLines ) ) {
  274. $expectedLine = $expectedLines[$i];
  275. $expectedChunks = str_split( $expectedLine, $this->termWidth - 3 );
  276. } else {
  277. $expectedChunks = [];
  278. }
  279. if ( $i < count( $actualLines ) ) {
  280. $actualLine = $actualLines[$i];
  281. $actualChunks = str_split( $actualLine, $this->termWidth - 3 );
  282. } else {
  283. $actualChunks = [];
  284. }
  285. $maxChunks = max( count( $expectedChunks ), count( $actualChunks ) );
  286. for ( $j = 0; $j < $maxChunks; $j++ ) {
  287. if ( isset( $expectedChunks[$j] ) ) {
  288. $result .= "E: " . $expectedChunks[$j];
  289. if ( $j === count( $expectedChunks ) - 1 ) {
  290. $result .= "¶";
  291. }
  292. $result .= "\n";
  293. } else {
  294. $result .= "E:\n";
  295. }
  296. $result .= "\33[4m" . // underline
  297. "A: ";
  298. if ( isset( $actualChunks[$j] ) ) {
  299. $result .= $actualChunks[$j];
  300. if ( $j === count( $actualChunks ) - 1 ) {
  301. $result .= "¶";
  302. }
  303. }
  304. $result .= "\33[0m\n"; // reset
  305. }
  306. }
  307. return $result;
  308. }
  309. protected function reload( $testInfo ) {
  310. global $argv;
  311. pcntl_exec( PHP_BINARY, [
  312. $argv[0],
  313. '--session-data',
  314. json_encode( [
  315. 'startFile' => $testInfo['file'],
  316. 'startTest' => $testInfo['desc']
  317. ] + $this->session ) ] );
  318. print "pcntl_exec() failed\n";
  319. return false;
  320. }
  321. protected function findTest( $file, $testInfo ) {
  322. $initialPart = '';
  323. for ( $i = 1; $i < $testInfo['line']; $i++ ) {
  324. $line = fgets( $file );
  325. if ( $line === false ) {
  326. print "Error reading from file\n";
  327. return false;
  328. }
  329. $initialPart .= $line;
  330. }
  331. $line = fgets( $file );
  332. if ( !preg_match( '/^!!\s*test/', $line ) ) {
  333. print "Test has moved, cannot edit\n";
  334. return false;
  335. }
  336. $testPart = $line;
  337. $desc = fgets( $file );
  338. if ( trim( $desc ) !== $testInfo['desc'] ) {
  339. print "Description does not match, cannot edit\n";
  340. return false;
  341. }
  342. $testPart .= $desc;
  343. return [ $initialPart, $testPart ];
  344. }
  345. protected function getOutputFileName( $inputFileName ) {
  346. if ( is_writable( $inputFileName ) ) {
  347. $outputFileName = $inputFileName;
  348. } else {
  349. $outputFileName = wfTempDir() . '/' . basename( $inputFileName );
  350. print "Cannot write to input file, writing to $outputFileName instead\n";
  351. }
  352. return $outputFileName;
  353. }
  354. protected function editTest( $fileName, $deletions, $changes ) {
  355. $text = file_get_contents( $fileName );
  356. if ( $text === false ) {
  357. print "Unable to open test file!";
  358. return false;
  359. }
  360. $result = TestFileEditor::edit( $text, $deletions, $changes,
  361. function ( $msg ) {
  362. print "$msg\n";
  363. }
  364. );
  365. if ( is_writable( $fileName ) ) {
  366. file_put_contents( $fileName, $result );
  367. print "Wrote updated file\n";
  368. } else {
  369. print "Cannot write updated file, here is a patch you can paste:\n\n";
  370. print "--- {$fileName}\n" .
  371. "+++ {$fileName}~\n" .
  372. $this->unifiedDiff( $text, $result ) .
  373. "\n";
  374. }
  375. }
  376. protected function update( $testInfo, $result ) {
  377. $this->editTest( $testInfo['file'],
  378. [], // deletions
  379. [ // changes
  380. $testInfo['test'] => [
  381. $testInfo['resultSection'] => [
  382. 'op' => 'update',
  383. 'value' => $result->actual . "\n"
  384. ]
  385. ]
  386. ]
  387. );
  388. }
  389. protected function deleteTest( $testInfo ) {
  390. $this->editTest( $testInfo['file'],
  391. [ $testInfo['test'] ], // deletions
  392. [] // changes
  393. );
  394. }
  395. protected function switchTidy( $testInfo ) {
  396. $resultSection = $testInfo['resultSection'];
  397. if ( in_array( $resultSection, [ 'html/php', 'html/*', 'html', 'result' ] ) ) {
  398. $newSection = 'html+tidy';
  399. } elseif ( in_array( $resultSection, [ 'html/php+tidy', 'html+tidy' ] ) ) {
  400. $newSection = 'html';
  401. } else {
  402. print "Unrecognised result section name \"$resultSection\"";
  403. return;
  404. }
  405. $this->editTest( $testInfo['file'],
  406. [], // deletions
  407. [ // changes
  408. $testInfo['test'] => [
  409. $resultSection => [
  410. 'op' => 'rename',
  411. 'value' => $newSection
  412. ]
  413. ]
  414. ]
  415. );
  416. }
  417. protected function deleteSubtest( $testInfo ) {
  418. $this->editTest( $testInfo['file'],
  419. [], // deletions
  420. [ // changes
  421. $testInfo['test'] => [
  422. $testInfo['resultSection'] => [
  423. 'op' => 'delete'
  424. ]
  425. ]
  426. ]
  427. );
  428. }
  429. }
  430. $maintClass = 'ParserEditTests';
  431. require RUN_MAINTENANCE_IF_MAIN;