修复 Windows 运行测试 (#1)

Co-authored-by: Yurun <admin@yurunsoft.com>
Reviewed-on: #1
Co-authored-by: yurunsoft <admin@yurunsoft.com>
Co-committed-by: yurunsoft <admin@yurunsoft.com>
pull/2/head
yurunsoft 2 months ago committed by 韩天峰
parent 29236bb66a
commit dfb6f693c3
  1. 4
      composer.json
  2. 14
      run-tests.php
  3. 2
      src/Php/CompilerBase.php
  4. 8
      tests/aot/basic/file_operations.phpt

@ -1,7 +1,6 @@
{ {
"require": { "require": {
"php": ">=8.2 <8.6", "php": ">=8.2 <8.6",
"ext-pcntl": "*",
"nikic/php-parser": "5.6.1", "nikic/php-parser": "5.6.1",
"league/climate": "^3.10", "league/climate": "^3.10",
"marcj/topsort": "^2.0", "marcj/topsort": "^2.0",
@ -22,5 +21,8 @@
"cs-fix": "php-cs-fixer fix $1", "cs-fix": "php-cs-fixer fix $1",
"analyse": "phpstan analyse --memory-limit 512M -l 5 -c phpstan.neon", "analyse": "phpstan analyse --memory-limit 512M -l 5 -c phpstan.neon",
"rector": "rector process --clear-cache" "rector": "rector process --clear-cache"
},
"suggest": {
"ext-pcntl": "Required for process control features."
} }
} }

@ -2461,7 +2461,7 @@ $message
return 'FAILED'; return 'FAILED';
} }
$args = substr($args, strlen(' -- ')); $args = substr($args, strlen(' -- '));
$cmd = './' . $bin_file . ' ' . $args . $cmdRedirect; $cmd = (IS_WINDOWS ? '.\\' : './') . $bin_file . ' ' . $args . $cmdRedirect;
} else { } else {
$content = file_get_contents($test_file); $content = file_get_contents($test_file);
if (preg_match('/function main\(\)/', $content)) { if (preg_match('/function main\(\)/', $content)) {
@ -2667,7 +2667,7 @@ COMMAND $cmd
$file = substr($test_file, 0, strlen($test_file) - 4); $file = substr($test_file, 0, strlen($test_file) - 4);
@unlink($file . '.cc'); @unlink($file . '.cc');
@unlink($file . '.cc.o'); @unlink($file . '.cc.o');
@unlink('./' . $bin_file); @unlink($bin_file);
} }
} }
@unlink($tmp_post); @unlink($tmp_post);
@ -4249,6 +4249,9 @@ function compile_php_file(string $file): string
throw new CompilationFailureException('Invalid PHP file'); throw new CompilationFailureException('Invalid PHP file');
} }
$binary_file = str_replace('-', '_', basename($file, '.php')); $binary_file = str_replace('-', '_', basename($file, '.php'));
if (IS_WINDOWS) {
$binary_file .= '.exe';
}
if (!str_contains($data, 'function main()')) { if (!str_contains($data, 'function main()')) {
file_put_contents($file, "<?php\nfunction main() {\n" . substr($data, 5, -2) . "\n}\n"); file_put_contents($file, "<?php\nfunction main() {\n" . substr($data, 5, -2) . "\n}\n");
@ -4261,7 +4264,12 @@ function compile_php_file(string $file): string
$output = []; $output = [];
$exitCode = 0; $exitCode = 0;
exec($compiler_path . ' ' . escapeshellarg($file) . ' 2>&1', $output, $exitCode); $cmd = $compiler_path;
// On Windows, .php files need to be run through the PHP interpreter
if (IS_WINDOWS && str_ends_with($cmd, '.php')) {
$cmd = escapeshellarg(PHP_BINARY) . ' ' . $cmd;
}
exec($cmd . ' ' . escapeshellarg($file) . ' 2>&1', $output, $exitCode);
clearstatcache(true, $binary_file); clearstatcache(true, $binary_file);
if ($exitCode !== 0 || !file_exists($binary_file)) { if ($exitCode !== 0 || !file_exists($binary_file)) {

@ -1168,7 +1168,7 @@ class CompilerBase extends \PhpAot\Core\Translator
$key = $this->parseIdentifier($expr); $key = $this->parseIdentifier($expr);
if (str_starts_with($key, self::LITERAL_STRINGS)) { if (str_starts_with($key, self::LITERAL_STRINGS)) {
$key = "{$key}.str()"; $key = "{$key}.str()";
} elseif ($key === '0L') { } elseif ($key === '0L' || $key === '0LL') {
// 0 在 C++ 中是一个特殊的值,存在二义性,既是空指针,也是整数,这会导致产生 ambiguous 错误 // 0 在 C++ 中是一个特殊的值,存在二义性,既是空指针,也是整数,这会导致产生 ambiguous 错误
// 必须转为 php::zero 常量,保证作为 key 时正确匹配到 IntKeyMap // 必须转为 php::zero 常量,保证作为 key 时正确匹配到 IntKeyMap
$key = self::VALUE_ZERO; $key = self::VALUE_ZERO;

@ -97,8 +97,8 @@ rmdir($tempDir);
echo "Cleanup completed\n"; echo "Cleanup completed\n";
?> ?>
--EXPECTF-- --EXPECTF--
Directory created: /tmp/aot_test_%s Directory created: %s
File created: /tmp/aot_test_%s/test.txt File created: %s/test.txt
File content: File content:
Hello, World! Hello, World!
This is a test file. This is a test file.
@ -114,8 +114,8 @@ Directory contents:
- second_test.txt - second_test.txt
- test.txt - test.txt
First line from handle: Hello, World! First line from handle: Hello, World!
File copied to: /tmp/aot_test_%s/copied_test.txt File copied to: %s/copied_test.txt
File moved to: /tmp/aot_test_%s/moved_test.txt File moved to: %s/moved_test.txt
Is test file a file: yes Is test file a file: yes
Is temp dir a directory: yes Is temp dir a directory: yes
File modification time: %d-%d-%d %d:%d:%d File modification time: %d-%d-%d %d:%d:%d

Loading…
Cancel
Save