From 4e94de6620e5701b60e33e8e6ed3e41a2589a073 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 12 Jun 2026 12:16:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(tests):=20=E4=BF=AE=E5=A4=8D=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=96=87=E4=BB=B6=E7=BC=96=E8=AF=91=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了对 compile_php_file 函数抛出异常的捕获和处理 - 引入 CompilationFailureException 类来封装编译失败信息 - 实现了编译失败时将输出写入日志、差异和输出文件的功能 - 在编译失败时正确标记 JUnit 测试状态并返回 FAILED - 重构了 PHP 文件编译逻辑以捕获编译器输出和错误码 - 添加了对现有二进制文件的清理和缓存清除机制 --- run-tests.php | 70 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/run-tests.php b/run-tests.php index 0b74873d..e9b7cb0b 100755 --- a/run-tests.php +++ b/run-tests.php @@ -2444,7 +2444,40 @@ TEST $file global $no_aot; if (!$no_aot) { - $bin_file = compile_php_file($test_file); + try { + $bin_file = compile_php_file($test_file); + } catch (Throwable $e) { + $compileOutput = trim($e instanceof CompilationFailureException ? $e->getCompilerOutput() : ''); + $message = $e->getMessage(); + $compileInfo = ' (compilation failed: ' . $message . ')'; + + if (strpos($log_format, 'O') !== false && file_put_contents($output_filename, $compileOutput !== '' ? $compileOutput : $message) === false) { + error("Cannot create test output - $output_filename"); + } + if (strpos($log_format, 'D') !== false && file_put_contents($diff_filename, $compileInfo . PHP_EOL) === false) { + error("Cannot create test diff - $diff_filename"); + } + if (strpos($log_format, 'L') !== false && file_put_contents($log_filename, " +---- AOT COMPILATION FAILED +$message +---- COMPILER OUTPUT +" . ($compileOutput !== '' ? $compileOutput : '(no compiler output)') . " +---- FAILED +") === false) { + error("Cannot create test log - $log_filename"); + } + + show_result('FAIL', $tested, $tested_file, 'reason: ' . $message); + $PHP_FAILED_TESTS['FAILED'][] = [ + 'name' => $file, + 'test_name' => (is_array($IN_REDIRECT) ? $IN_REDIRECT['via'] : '') . $tested . " [$tested_file]", + 'output' => $output_filename, + 'diff' => $diff_filename, + 'info' => $compileInfo, + ]; + $junit->markTestAs('FAIL', $shortname, $tested, null, $message, $compileOutput); + return 'FAILED'; + } $args = substr($args, strlen(' -- ')); $cmd = './' . $bin_file . ' ' . $args . $cmdRedirect; } else { @@ -3303,6 +3336,19 @@ class BorkageException extends Exception { } +class CompilationFailureException extends RuntimeException +{ + public function __construct(string $message, private string $compilerOutput = '') + { + parent::__construct($message); + } + + public function getCompilerOutput(): string + { + return $this->compilerOutput; + } +} + class JUnit { private bool $enabled = true; @@ -4218,16 +4264,28 @@ function compile_php_file(string $file): string $data = trim(file_get_contents($file)); if (!str_starts_with($data, '')) { - throw new Exception('Invalid PHP file'); + throw new CompilationFailureException('Invalid PHP file'); } + $binary_file = str_replace('-', '_', basename($file, '.php')); + if (!str_contains($data, 'function main()')) { file_put_contents($file, "&1', $output, $exitCode); + clearstatcache(true, $binary_file); + + if ($exitCode !== 0 || !file_exists($binary_file)) { + throw new CompilationFailureException('Compilation failed', implode(PHP_EOL, $output)); } + return $binary_file; }