fix(tests): 修复测试文件编译失败时的异常处理

- 添加了对 compile_php_file 函数抛出异常的捕获和处理
- 引入 CompilationFailureException 类来封装编译失败信息
- 实现了编译失败时将输出写入日志、差异和输出文件的功能
- 在编译失败时正确标记 JUnit 测试状态并返回 FAILED
- 重构了 PHP 文件编译逻辑以捕获编译器输出和错误码
- 添加了对现有二进制文件的清理和缓存清除机制
pull/1/head
韩天峰 2 months ago
parent e996dc8b43
commit 4e94de6620
  1. 70
      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, '<?php') or !str_ends_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, "<?php\nfunction main() {\n" . substr($data, 5, -2) . "\n}\n");
}
system($compiler_path . ' ' . $file);
$binary_file = str_replace('-', '_', basename($file, '.php'));
if (!file_exists($binary_file)) {
throw new Exception('Compilation failed');
if (file_exists($binary_file)) {
@unlink($binary_file);
}
clearstatcache(true, $binary_file);
$output = [];
$exitCode = 0;
exec($compiler_path . ' ' . escapeshellarg($file) . ' 2>&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;
}

Loading…
Cancel
Save