菜谱项目

run.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. error_reporting(E_ALL | E_STRICT);
  3. ini_set('short_open_tag', false);
  4. if ('cli' !== php_sapi_name()) {
  5. die('This script is designed for running on the command line.');
  6. }
  7. function showHelp($error) {
  8. die($error . "\n\n" .
  9. <<<OUTPUT
  10. This script has to be called with the following signature:
  11. php run.php [--no-progress] testType pathToTestFiles
  12. The test type must be one of: PHP5, PHP7 or Symfony.
  13. The following options are available:
  14. --no-progress Disables showing which file is currently tested.
  15. OUTPUT
  16. );
  17. }
  18. $options = array();
  19. $arguments = array();
  20. // remove script name from argv
  21. array_shift($argv);
  22. foreach ($argv as $arg) {
  23. if ('-' === $arg[0]) {
  24. $options[] = $arg;
  25. } else {
  26. $arguments[] = $arg;
  27. }
  28. }
  29. if (count($arguments) !== 2) {
  30. showHelp('Too little arguments passed!');
  31. }
  32. $showProgress = true;
  33. $verbose = false;
  34. foreach ($options as $option) {
  35. if ($option === '--no-progress') {
  36. $showProgress = false;
  37. } elseif ($option === '--verbose') {
  38. $verbose = true;
  39. } else {
  40. showHelp('Invalid option passed!');
  41. }
  42. }
  43. $testType = $arguments[0];
  44. $dir = $arguments[1];
  45. switch ($testType) {
  46. case 'Symfony':
  47. $version = 'Php5';
  48. $fileFilter = function($path) {
  49. return preg_match('~\.php(?:\.cache)?$~', $path) && false === strpos($path, 'skeleton');
  50. };
  51. $codeExtractor = function($file, $code) {
  52. return $code;
  53. };
  54. break;
  55. case 'PHP5':
  56. case 'PHP7':
  57. $version = $testType === 'PHP5' ? 'Php5' : 'Php7';
  58. $fileFilter = function($path) {
  59. return preg_match('~\.phpt$~', $path);
  60. };
  61. $codeExtractor = function($file, $code) {
  62. if (preg_match('~(?:
  63. # skeleton files
  64. ext.gmp.tests.001
  65. | ext.skeleton.tests.001
  66. # multibyte encoded files
  67. | ext.mbstring.tests.zend_multibyte-01
  68. | Zend.tests.multibyte.multibyte_encoding_001
  69. | Zend.tests.multibyte.multibyte_encoding_004
  70. | Zend.tests.multibyte.multibyte_encoding_005
  71. # pretty print difference due to INF vs 1e1000
  72. | ext.standard.tests.general_functions.bug27678
  73. | tests.lang.bug24640
  74. # pretty print differences due to negative LNumbers
  75. | Zend.tests.neg_num_string
  76. | Zend.tests.bug72918
  77. # pretty print difference due to nop statements
  78. | ext.mbstring.tests.htmlent
  79. | ext.standard.tests.file.fread_basic
  80. )\.phpt$~x', $file)) {
  81. return null;
  82. }
  83. if (!preg_match('~--FILE--\s*(.*?)--[A-Z]+--~s', $code, $matches)) {
  84. return null;
  85. }
  86. if (preg_match('~--EXPECT(?:F|REGEX)?--\s*(?:Parse|Fatal) error~', $code)) {
  87. return null;
  88. }
  89. return $matches[1];
  90. };
  91. break;
  92. default:
  93. showHelp('Test type must be one of: PHP5, PHP7 or Symfony');
  94. }
  95. require_once dirname(__FILE__) . '/../lib/PhpParser/Autoloader.php';
  96. PhpParser\Autoloader::register();
  97. $parserName = 'PhpParser\Parser\\' . $version;
  98. $parser = new $parserName(new PhpParser\Lexer\Emulative);
  99. $prettyPrinter = new PhpParser\PrettyPrinter\Standard;
  100. $nodeDumper = new PhpParser\NodeDumper;
  101. $parseFail = $ppFail = $compareFail = $count = 0;
  102. $readTime = $parseTime = $ppTime = $reparseTime = $compareTime = 0;
  103. $totalStartTime = microtime(true);
  104. foreach (new RecursiveIteratorIterator(
  105. new RecursiveDirectoryIterator($dir),
  106. RecursiveIteratorIterator::LEAVES_ONLY)
  107. as $file) {
  108. if (!$fileFilter($file)) {
  109. continue;
  110. }
  111. $startTime = microtime(true);
  112. $code = file_get_contents($file);
  113. $readTime += microtime(true) - $startTime;
  114. if (null === $code = $codeExtractor($file, $code)) {
  115. continue;
  116. }
  117. set_time_limit(10);
  118. ++$count;
  119. if ($showProgress) {
  120. echo substr(str_pad('Testing file ' . $count . ': ' . substr($file, strlen($dir)), 79), 0, 79), "\r";
  121. }
  122. try {
  123. $startTime = microtime(true);
  124. $stmts = $parser->parse($code);
  125. $parseTime += microtime(true) - $startTime;
  126. $startTime = microtime(true);
  127. $code = '<?php' . "\n" . $prettyPrinter->prettyPrint($stmts);
  128. $ppTime += microtime(true) - $startTime;
  129. try {
  130. $startTime = microtime(true);
  131. $ppStmts = $parser->parse($code);
  132. $reparseTime += microtime(true) - $startTime;
  133. $startTime = microtime(true);
  134. $same = $nodeDumper->dump($stmts) == $nodeDumper->dump($ppStmts);
  135. $compareTime += microtime(true) - $startTime;
  136. if (!$same) {
  137. echo $file, ":\n Result of initial parse and parse after pretty print differ\n";
  138. if ($verbose) {
  139. echo "Pretty printer output:\n=====\n$code\n=====\n\n";
  140. }
  141. ++$compareFail;
  142. }
  143. } catch (PhpParser\Error $e) {
  144. echo $file, ":\n Parse of pretty print failed with message: {$e->getMessage()}\n";
  145. if ($verbose) {
  146. echo "Pretty printer output:\n=====\n$code\n=====\n\n";
  147. }
  148. ++$ppFail;
  149. }
  150. } catch (PhpParser\Error $e) {
  151. echo $file, ":\n Parse failed with message: {$e->getMessage()}\n";
  152. ++$parseFail;
  153. }
  154. }
  155. if (0 === $parseFail && 0 === $ppFail && 0 === $compareFail) {
  156. $exit = 0;
  157. echo "\n\n", 'All tests passed.', "\n";
  158. } else {
  159. $exit = 1;
  160. echo "\n\n", '==========', "\n\n", 'There were: ', "\n";
  161. if (0 !== $parseFail) {
  162. echo ' ', $parseFail, ' parse failures.', "\n";
  163. }
  164. if (0 !== $ppFail) {
  165. echo ' ', $ppFail, ' pretty print failures.', "\n";
  166. }
  167. if (0 !== $compareFail) {
  168. echo ' ', $compareFail, ' compare failures.', "\n";
  169. }
  170. }
  171. echo "\n",
  172. 'Tested files: ', $count, "\n",
  173. "\n",
  174. 'Reading files took: ', $readTime, "\n",
  175. 'Parsing took: ', $parseTime, "\n",
  176. 'Pretty printing took: ', $ppTime, "\n",
  177. 'Reparsing took: ', $reparseTime, "\n",
  178. 'Comparing took: ', $compareTime, "\n",
  179. "\n",
  180. 'Total time: ', microtime(true) - $totalStartTime, "\n",
  181. 'Maximum memory usage: ', memory_get_peak_usage(true), "\n";
  182. exit($exit);