From dc73eecacfd50dcd8cb6ee0f3efd8c022a85131a Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 5 Jul 2024 23:52:24 +0900 Subject: feat: handle runtime errors in tools/json2phpunit.php --- tools/json2phpunit.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tools/json2phpunit.php b/tools/json2phpunit.php index 025c613..9688afe 100644 --- a/tools/json2phpunit.php +++ b/tools/json2phpunit.php @@ -28,16 +28,28 @@ $wastJsonBaseDir = __DIR__ . '/../tests/fixtures/spec_testsuites/core'; $phpUnitBaseDir = __DIR__ . '/../tests/src/SpecTestsuites/Core'; $wastJsonFiles = glob("$wastJsonBaseDir/*.json", GLOB_ERR); +if ($wastJsonFiles === false) { + exit("Failed to list wast json files (base dir: $wastJsonBaseDir).\n"); +} foreach ($wastJsonFiles as $wastJsonFile) { $wastJsonFileContent = file_get_contents($wastJsonFile); + if ($wastJsonFileContent === false) { + exit("Failed to read wast json file (path: $wastJsonFile).\n"); + } $wastJson = json_decode($wastJsonFileContent, associative: true); + if (!is_array($wastJson)) { + exit("Failed to decode wast json file (path: $wastJsonFile).\n"); + } $sourceFilename = $wastJson['source_filename'] ?? null; $commands = $wastJson['commands'] ?? null; $className = wastJsonFileToPascalCase($wastJsonFile) . 'Test'; $fp = fopen("$phpUnitBaseDir/$className.php", 'w'); + if ($fp === false) { + exit("Failed to open file to write (path: $phpUnitBaseDir/$className.php).\n"); + } fwrite($fp, " buildAssertUnlinkableCommandTest($command), 'action' => buildActionCommandTest($command), 'register' => buildRegisterCommandTest($command), + default => exit("Unknown command type: $type\n"), }; if ($commandTest === null) { fwrite($fp, " #[DoesNotPerformAssertions]\n"); @@ -240,7 +253,7 @@ function toPhpLiteral(mixed $value): string if (is_null($value)) { return 'null'; } elseif (is_int($value)) { - return $value; + return (string)$value; } elseif (is_string($value)) { $value = strtr($value, ["\\" => "\\\\", "'" => "\\'"]); return "'$value'"; -- cgit v1.2.3-70-g09d2