php-parser/test_old/run.php

276 lines
8.1 KiB
PHP
Raw Normal View History

<?php
2011-10-20 22:36:10 +02:00
error_reporting(E_ALL | E_STRICT);
ini_set('short_open_tag', false);
if ('cli' !== php_sapi_name()) {
die('This script is designed for running on the command line.');
}
function showHelp($error) {
die($error . "\n\n" .
<<<OUTPUT
This script has to be called with the following signature:
php run.php [--no-progress] testType pathToTestFiles
The test type must be one of: PHP, Symfony
The following options are available:
--no-progress Disables showing which file is currently tested.
--verbose Print more information for failures.
--php-version=VERSION PHP version to use for lexing/parsing.
2015-07-09 12:27:15 +02:00
OUTPUT
);
}
$options = array();
$arguments = array();
// remove script name from argv
array_shift($argv);
foreach ($argv as $arg) {
if ('-' === $arg[0]) {
$parts = explode('=', $arg);
$options[$parts[0]] = $parts[1] ?? true;
} else {
$arguments[] = $arg;
}
}
if (count($arguments) !== 2) {
2020-08-05 16:27:47 +02:00
showHelp('Too few arguments passed!');
}
$showProgress = !isset($options['--no-progress']);
$verbose = isset($options['--verbose']);
$phpVersion = $options['--php-version'] ?? '8.0';
$testType = $arguments[0];
$dir = $arguments[1];
2020-08-05 16:27:47 +02:00
require_once __DIR__ . '/../vendor/autoload.php';
switch ($testType) {
case 'Symfony':
$fileFilter = function($path) {
2018-09-22 10:41:58 +02:00
if (!preg_match('~\.php$~', $path)) {
return false;
}
if (preg_match('~(?:
# invalid php code
dependency-injection.Tests.Fixtures.xml.xml_with_wrong_ext
# difference in nop statement
| framework-bundle.Resources.views.Form.choice_widget_options\.html
# difference due to INF
| yaml.Tests.InlineTest
)\.php$~x', $path)) {
return false;
}
return true;
};
$codeExtractor = function($file, $code) {
return $code;
};
break;
case 'PHP':
$fileFilter = function($path) {
return preg_match('~\.phpt$~', $path);
};
$codeExtractor = function($file, $code) {
if (preg_match('~(?:
# skeleton files
ext.gmp.tests.001
| ext.skeleton.tests.00\d
# multibyte encoded files
| ext.mbstring.tests.zend_multibyte-01
| Zend.tests.multibyte.multibyte_encoding_001
| Zend.tests.multibyte.multibyte_encoding_004
| Zend.tests.multibyte.multibyte_encoding_005
# invalid code due to missing WS after opening tag
| tests.run-test.bug75042-3
2019-10-19 10:44:01 +02:00
# contains invalid chars, which we treat as parse error
| Zend.tests.warning_during_heredoc_scan_ahead
# pretty print difference due to INF vs 1e1000
| ext.standard.tests.general_functions.bug27678
| tests.lang.bug24640
| Zend.tests.bug74947
2016-12-11 16:47:47 +01:00
# pretty print differences due to negative LNumbers
| Zend.tests.neg_num_string
| Zend.tests.numeric_strings.neg_num_string
2016-12-11 16:47:47 +01:00
| Zend.tests.bug72918
# pretty print difference due to nop statements
| ext.mbstring.tests.htmlent
| ext.standard.tests.file.fread_basic
# its too hard to emulate these on old PHP versions
| Zend.tests.flexible-heredoc-complex-test[1-4]
# whitespace in namespaced name
| Zend.tests.bug55086
| Zend.tests.grammar.regression_010
)\.phpt$~x', $file)) {
return null;
}
if (!preg_match('~--FILE--\s*(.*?)\n--[A-Z]+--~s', $code, $matches)) {
return null;
}
if (preg_match('~--EXPECT(?:F|REGEX)?--\s*(?:Parse|Fatal) error~', $code)) {
return null;
}
return $matches[1];
};
break;
default:
showHelp('Test type must be one of: PHP or Symfony');
}
2020-08-05 16:27:47 +02:00
$lexer = new PhpParser\Lexer\Emulative([
'usedAttributes' => [
'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos',
],
'phpVersion' => $phpVersion,
2020-08-05 16:27:47 +02:00
]);
if (version_compare($phpVersion, '7.0', '>=')) {
$parser = new PhpParser\Parser\Php7($lexer);
} else {
$parser = new PhpParser\Parser\Php5($lexer);
}
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
2017-10-29 14:19:01 +01:00
$nodeDumper = new PhpParser\NodeDumper;
2017-10-29 14:19:01 +01:00
$cloningTraverser = new PhpParser\NodeTraverser;
$cloningTraverser->addVisitor(new PhpParser\NodeVisitor\CloningVisitor);
2017-10-29 14:19:01 +01:00
$parseFail = $fpppFail = $ppFail = $compareFail = $count = 0;
$readTime = $parseTime = $cloneTime = 0;
$fpppTime = $ppTime = $reparseTime = $compareTime = 0;
$totalStartTime = microtime(true);
foreach (new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::LEAVES_ONLY)
as $file) {
if (!$fileFilter($file)) {
continue;
}
$startTime = microtime(true);
2017-10-29 14:19:01 +01:00
$origCode = file_get_contents($file);
$readTime += microtime(true) - $startTime;
2017-10-29 14:19:01 +01:00
if (null === $origCode = $codeExtractor($file, $origCode)) {
continue;
}
set_time_limit(10);
++$count;
if ($showProgress) {
echo substr(str_pad('Testing file ' . $count . ': ' . substr($file, strlen($dir)), 79), 0, 79), "\r";
}
try {
$startTime = microtime(true);
2017-10-29 14:19:01 +01:00
$origStmts = $parser->parse($origCode);
$parseTime += microtime(true) - $startTime;
2017-10-29 14:19:01 +01:00
$origTokens = $lexer->getTokens();
$startTime = microtime(true);
$stmts = $cloningTraverser->traverse($origStmts);
$cloneTime += microtime(true) - $startTime;
$startTime = microtime(true);
$code = $prettyPrinter->printFormatPreserving($stmts, $origStmts, $origTokens);
$fpppTime += microtime(true) - $startTime;
if ($code !== $origCode) {
echo $file, ":\n Result of format-preserving pretty-print differs\n";
if ($verbose) {
echo "FPPP output:\n=====\n$code\n=====\n\n";
}
++$fpppFail;
}
$startTime = microtime(true);
2017-10-29 14:19:01 +01:00
$code = "<?php\n" . $prettyPrinter->prettyPrint($stmts);
$ppTime += microtime(true) - $startTime;
try {
$startTime = microtime(true);
$ppStmts = $parser->parse($code);
$reparseTime += microtime(true) - $startTime;
$startTime = microtime(true);
$same = $nodeDumper->dump($stmts) == $nodeDumper->dump($ppStmts);
$compareTime += microtime(true) - $startTime;
if (!$same) {
echo $file, ":\n Result of initial parse and parse after pretty print differ\n";
if ($verbose) {
echo "Pretty printer output:\n=====\n$code\n=====\n\n";
}
2011-05-29 20:38:36 +02:00
++$compareFail;
}
} catch (PhpParser\Error $e) {
echo $file, ":\n Parse of pretty print failed with message: {$e->getMessage()}\n";
if ($verbose) {
echo "Pretty printer output:\n=====\n$code\n=====\n\n";
}
2011-05-29 20:38:36 +02:00
++$ppFail;
}
} catch (PhpParser\Error $e) {
echo $file, ":\n Parse failed with message: {$e->getMessage()}\n";
2011-05-29 20:38:36 +02:00
++$parseFail;
} catch (Throwable $e) {
echo $file, ":\n Unknown error occurred: $e\n";
}
}
if (0 === $parseFail && 0 === $ppFail && 0 === $compareFail) {
2016-04-07 06:37:47 +02:00
$exit = 0;
echo "\n\n", 'All tests passed.', "\n";
} else {
2016-04-07 06:37:47 +02:00
$exit = 1;
echo "\n\n", '==========', "\n\n", 'There were: ', "\n";
if (0 !== $parseFail) {
echo ' ', $parseFail, ' parse failures.', "\n";
}
if (0 !== $ppFail) {
echo ' ', $ppFail, ' pretty print failures.', "\n";
}
2017-10-29 14:19:01 +01:00
if (0 !== $fpppFail) {
echo ' ', $fpppFail, ' FPPP failures.', "\n";
}
if (0 !== $compareFail) {
echo ' ', $compareFail, ' compare failures.', "\n";
}
}
echo "\n",
'Tested files: ', $count, "\n",
"\n",
'Reading files took: ', $readTime, "\n",
'Parsing took: ', $parseTime, "\n",
2017-10-29 14:19:01 +01:00
'Cloning took: ', $cloneTime, "\n",
'FPPP took: ', $fpppTime, "\n",
'Pretty printing took: ', $ppTime, "\n",
'Reparsing took: ', $reparseTime, "\n",
'Comparing took: ', $compareTime, "\n",
"\n",
'Total time: ', microtime(true) - $totalStartTime, "\n",
'Maximum memory usage: ', memory_get_peak_usage(true), "\n";
2016-04-07 06:37:47 +02:00
exit($exit);