feat(package): check Platform Requirements with --runtime before writing the zipUnverified
4e340ed parent: 79f167d modified
bin/scaleway-package +17 -4 | @@ -8,13 +8,26 @@ use JDecool\ScalewayRuntime\Packager; | ||
| 8 | 8 | |
| 9 | 9 | require $_composer_autoload_path ?? __DIR__.'/../vendor/autoload.php'; |
| 10 | 10 | |
| 11 | -if (in_array($argv[1] ?? null, ['-h', '--help'], true)) { | |
| 12 | - echo 'Usage: scaleway-package [<zip-file>] (default: function.zip)', \PHP_EOL; | |
| 13 | - exit(0); | |
| 11 | +$usage = 'Usage: scaleway-package [--runtime=<function-runtime>] [<zip-file>] (default: function.zip; --runtime checks the Platform Requirements against php84 or php85 first)'; | |
| 12 | +$functionRuntime = null; | |
| 13 | +$zipFiles = []; | |
| 14 | +foreach (array_slice($argv ?? [], 1) as $argument) { | |
| 15 | + if (in_array($argument, ['-h', '--help'], true)) { | |
| 16 | + echo $usage, \PHP_EOL; | |
| 17 | + exit(0); | |
| 18 | + } | |
| 19 | + if (1 === preg_match('/^--runtime=(.+)$/', $argument, $match)) { | |
| 20 | + $functionRuntime = $match[1]; | |
| 21 | + } elseif (str_starts_with($argument, '-') || [] !== $zipFiles) { | |
| 22 | + fwrite(\STDERR, $usage.\PHP_EOL); | |
| 23 | + exit(1); | |
| 24 | + } else { | |
| 25 | + $zipFiles[] = $argument; | |
| 26 | + } | |
| 14 | 27 | } |
| 15 | 28 | |
| 16 | 29 | try { |
| 17 | - (new Packager((string) realpath(InstalledVersions::getRootPackage()['install_path']), \STDERR))->package($argv[1] ?? 'function.zip'); | |
| 30 | + (new Packager((string) realpath(InstalledVersions::getRootPackage()['install_path']), \STDERR))->package($zipFiles[0] ?? 'function.zip', $functionRuntime); | |
| 18 | 31 | } catch (Throwable $e) { |
| 19 | 32 | fwrite(\STDERR, 'Packaging failed: '.$e->getMessage().\PHP_EOL); |
| 20 | 33 | exit(1); |
| @@ -8,13 +8,26 @@ use JDecool\ScalewayRuntime\Packager; | |||
| 8 | 8 | ||
| 9 | require $_composer_autoload_path ?? __DIR__.'/../vendor/autoload.php'; | 9 | require $_composer_autoload_path ?? __DIR__.'/../vendor/autoload.php'; |
| 10 | 10 | ||
| 11 | -if (in_array($argv[1] ?? null, ['-h', '--help'], true)) { | 11 | +$usage = 'Usage: scaleway-package [--runtime=<function-runtime>] [<zip-file>] (default: function.zip; --runtime checks the Platform Requirements against php84 or php85 first)'; |
| 12 | - echo 'Usage: scaleway-package [<zip-file>] (default: function.zip)', \PHP_EOL; | 12 | +$functionRuntime = null; |
| 13 | - exit(0); | 13 | +$zipFiles = []; |
| 14 | +foreach (array_slice($argv ?? [], 1) as $argument) { | ||
| 15 | + if (in_array($argument, ['-h', '--help'], true)) { | ||
| 16 | + echo $usage, \PHP_EOL; | ||
| 17 | + exit(0); | ||
| 18 | + } | ||
| 19 | + if (1 === preg_match('/^--runtime=(.+)$/', $argument, $match)) { | ||
| 20 | + $functionRuntime = $match[1]; | ||
| 21 | + } elseif (str_starts_with($argument, '-') || [] !== $zipFiles) { | ||
| 22 | + fwrite(\STDERR, $usage.\PHP_EOL); | ||
| 23 | + exit(1); | ||
| 24 | + } else { | ||
| 25 | + $zipFiles[] = $argument; | ||
| 26 | + } | ||
| 14 | } | 27 | } |
| 15 | 28 | ||
| 16 | try { | 29 | try { |
| 17 | - (new Packager((string) realpath(InstalledVersions::getRootPackage()['install_path']), \STDERR))->package($argv[1] ?? 'function.zip'); | 30 | + (new Packager((string) realpath(InstalledVersions::getRootPackage()['install_path']), \STDERR))->package($zipFiles[0] ?? 'function.zip', $functionRuntime); |
| 18 | } catch (Throwable $e) { | 31 | } catch (Throwable $e) { |
| 19 | fwrite(\STDERR, 'Packaging failed: '.$e->getMessage().\PHP_EOL); | 32 | fwrite(\STDERR, 'Packaging failed: '.$e->getMessage().\PHP_EOL); |
| 20 | exit(1); | 33 | exit(1); |
modified
src/Packager.php +7 -1 | @@ -36,7 +36,10 @@ final readonly class Packager | ||
| 36 | 36 | ) { |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - public function package(string $zipFile): void | |
| 39 | + /** | |
| 40 | + * @param ?string $functionRuntime checks the Platform Requirements against it first, as "php84" | |
| 41 | + */ | |
| 42 | + public function package(string $zipFile, ?string $functionRuntime = null): void | |
| 40 | 43 | { |
| 41 | 44 | if (!class_exists(\ZipArchive::class)) { |
| 42 | 45 | throw new \RuntimeException('The zip PHP extension is required to build the deployment zip.'); |
| @@ -47,6 +50,9 @@ final readonly class Packager | ||
| 47 | 50 | } |
| 48 | 51 | } |
| 49 | 52 | self::checkLock((string) file_get_contents($this->appDir.'/composer.json'), (string) file_get_contents($this->appDir.'/composer.lock')); |
| 53 | + if (null !== $functionRuntime && !new Doctor(FunctionRuntimeCatalogue::load(), $this->log)->check($this->appDir, $functionRuntime)) { | |
| 54 | + throw new \RuntimeException(\sprintf('%s does not meet the Platform Requirements above: Scaleway\'s "composer install" would fail, and every Invocation answer a 500.', $functionRuntime)); | |
| 55 | + } | |
| 50 | 56 | |
| 51 | 57 | $this->log('Writing '.$zipFile); |
| 52 | 58 | [$zipSize, $codeSize] = $this->zip($zipFile, $this->relativeToApp($zipFile)); |
| @@ -36,7 +36,10 @@ final readonly class Packager | |||
| 36 | ) { | 36 | ) { |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | - public function package(string $zipFile): void | 39 | + /** |
| 40 | + * @param ?string $functionRuntime checks the Platform Requirements against it first, as "php84" | ||
| 41 | + */ | ||
| 42 | + public function package(string $zipFile, ?string $functionRuntime = null): void | ||
| 40 | { | 43 | { |
| 41 | if (!class_exists(\ZipArchive::class)) { | 44 | if (!class_exists(\ZipArchive::class)) { |
| 42 | throw new \RuntimeException('The zip PHP extension is required to build the deployment zip.'); | 45 | throw new \RuntimeException('The zip PHP extension is required to build the deployment zip.'); |
| @@ -47,6 +50,9 @@ final readonly class Packager | |||
| 47 | } | 50 | } |
| 48 | } | 51 | } |
| 49 | self::checkLock((string) file_get_contents($this->appDir.'/composer.json'), (string) file_get_contents($this->appDir.'/composer.lock')); | 52 | self::checkLock((string) file_get_contents($this->appDir.'/composer.json'), (string) file_get_contents($this->appDir.'/composer.lock')); |
| 53 | + if (null !== $functionRuntime && !new Doctor(FunctionRuntimeCatalogue::load(), $this->log)->check($this->appDir, $functionRuntime)) { | ||
| 54 | + throw new \RuntimeException(\sprintf('%s does not meet the Platform Requirements above: Scaleway\'s "composer install" would fail, and every Invocation answer a 500.', $functionRuntime)); | ||
| 55 | + } | ||
| 50 | 56 | ||
| 51 | $this->log('Writing '.$zipFile); | 57 | $this->log('Writing '.$zipFile); |
| 52 | [$zipSize, $codeSize] = $this->zip($zipFile, $this->relativeToApp($zipFile)); | 58 | [$zipSize, $codeSize] = $this->zip($zipFile, $this->relativeToApp($zipFile)); |
modified
tests/PackagerTest.php +56 -2 | @@ -171,9 +171,63 @@ final class PackagerTest extends TestCase | ||
| 171 | 171 | self::assertFileDoesNotExist($this->dir.'/function.zip'); |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - private function package(): void | |
| 174 | + public function testPlatformRequirementsAreCheckedAgainstTheFunctionRuntimeBeforeZipping(): void | |
| 175 | 175 | { |
| 176 | - new Packager($this->appDir, $this->log)->package($this->dir.'/function.zip'); | |
| 176 | + $this->requireInTheLock(['php' => '>=8.4']); | |
| 177 | + | |
| 178 | + $this->package('php84'); | |
| 179 | + | |
| 180 | + self::assertFileExists($this->dir.'/function.zip'); | |
| 181 | + self::assertStringContainsString('1 satisfied, 0 unknown, 0 incompatible on php84', $this->log()); | |
| 182 | + } | |
| 183 | + | |
| 184 | + public function testIncompatiblePlatformRequirementLeavesNoZip(): void | |
| 185 | + { | |
| 186 | + $this->requireInTheLock(['ext-not-on-scaleway' => '*']); | |
| 187 | + | |
| 188 | + try { | |
| 189 | + $this->package('php84'); | |
| 190 | + self::fail('The application must be refused.'); | |
| 191 | + } catch (\RuntimeException $e) { | |
| 192 | + self::assertStringStartsWith('php84 does not meet the Platform Requirements above', $e->getMessage()); | |
| 193 | + } | |
| 194 | + self::assertStringContainsString('INCOMPATIBLE ext-not-on-scaleway * required by acme/lib 1.0.0', $this->log()); | |
| 195 | + self::assertFileDoesNotExist($this->dir.'/function.zip'); | |
| 196 | + } | |
| 197 | + | |
| 198 | + public function testPlatformRequirementsAreNotCheckedWithoutAFunctionRuntime(): void | |
| 199 | + { | |
| 200 | + $this->requireInTheLock(['ext-not-on-scaleway' => '*']); | |
| 201 | + | |
| 202 | + $this->package(); | |
| 203 | + | |
| 204 | + self::assertFileExists($this->dir.'/function.zip'); | |
| 205 | + self::assertStringNotContainsString('ext-not-on-scaleway', $this->log()); | |
| 206 | + } | |
| 207 | + | |
| 208 | + private function package(?string $functionRuntime = null): void | |
| 209 | + { | |
| 210 | + new Packager($this->appDir, $this->log)->package($this->dir.'/function.zip', $functionRuntime); | |
| 211 | + } | |
| 212 | + | |
| 213 | + /** | |
| 214 | + * @param array<string, string> $require of the locked acme/lib, which leaves the lock in sync with composer.json | |
| 215 | + */ | |
| 216 | + private function requireInTheLock(array $require): void | |
| 217 | + { | |
| 218 | + $lock = json_decode((string) file_get_contents($this->appDir.'/composer.lock'), true); | |
| 219 | + self::assertIsArray($lock); | |
| 220 | + self::assertIsArray($lock['packages']); | |
| 221 | + self::assertIsArray($lock['packages'][0]); | |
| 222 | + $lock['packages'][0]['require'] = $require; | |
| 223 | + $this->filesystem->dumpFile($this->appDir.'/composer.lock', (string) json_encode($lock)); | |
| 224 | + } | |
| 225 | + | |
| 226 | + private function log(): string | |
| 227 | + { | |
| 228 | + rewind($this->log); | |
| 229 | + | |
| 230 | + return (string) stream_get_contents($this->log); | |
| 177 | 231 | } |
| 178 | 232 | |
| 179 | 233 | /** |
| @@ -171,9 +171,63 @@ final class PackagerTest extends TestCase | |||
| 171 | self::assertFileDoesNotExist($this->dir.'/function.zip'); | 171 | self::assertFileDoesNotExist($this->dir.'/function.zip'); |
| 172 | } | 172 | } |
| 173 | 173 | ||
| 174 | - private function package(): void | 174 | + public function testPlatformRequirementsAreCheckedAgainstTheFunctionRuntimeBeforeZipping(): void |
| 175 | { | 175 | { |
| 176 | - new Packager($this->appDir, $this->log)->package($this->dir.'/function.zip'); | 176 | + $this->requireInTheLock(['php' => '>=8.4']); |
| 177 | + | ||
| 178 | + $this->package('php84'); | ||
| 179 | + | ||
| 180 | + self::assertFileExists($this->dir.'/function.zip'); | ||
| 181 | + self::assertStringContainsString('1 satisfied, 0 unknown, 0 incompatible on php84', $this->log()); | ||
| 182 | + } | ||
| 183 | + | ||
| 184 | + public function testIncompatiblePlatformRequirementLeavesNoZip(): void | ||
| 185 | + { | ||
| 186 | + $this->requireInTheLock(['ext-not-on-scaleway' => '*']); | ||
| 187 | + | ||
| 188 | + try { | ||
| 189 | + $this->package('php84'); | ||
| 190 | + self::fail('The application must be refused.'); | ||
| 191 | + } catch (\RuntimeException $e) { | ||
| 192 | + self::assertStringStartsWith('php84 does not meet the Platform Requirements above', $e->getMessage()); | ||
| 193 | + } | ||
| 194 | + self::assertStringContainsString('INCOMPATIBLE ext-not-on-scaleway * required by acme/lib 1.0.0', $this->log()); | ||
| 195 | + self::assertFileDoesNotExist($this->dir.'/function.zip'); | ||
| 196 | + } | ||
| 197 | + | ||
| 198 | + public function testPlatformRequirementsAreNotCheckedWithoutAFunctionRuntime(): void | ||
| 199 | + { | ||
| 200 | + $this->requireInTheLock(['ext-not-on-scaleway' => '*']); | ||
| 201 | + | ||
| 202 | + $this->package(); | ||
| 203 | + | ||
| 204 | + self::assertFileExists($this->dir.'/function.zip'); | ||
| 205 | + self::assertStringNotContainsString('ext-not-on-scaleway', $this->log()); | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + private function package(?string $functionRuntime = null): void | ||
| 209 | + { | ||
| 210 | + new Packager($this->appDir, $this->log)->package($this->dir.'/function.zip', $functionRuntime); | ||
| 211 | + } | ||
| 212 | + | ||
| 213 | + /** | ||
| 214 | + * @param array<string, string> $require of the locked acme/lib, which leaves the lock in sync with composer.json | ||
| 215 | + */ | ||
| 216 | + private function requireInTheLock(array $require): void | ||
| 217 | + { | ||
| 218 | + $lock = json_decode((string) file_get_contents($this->appDir.'/composer.lock'), true); | ||
| 219 | + self::assertIsArray($lock); | ||
| 220 | + self::assertIsArray($lock['packages']); | ||
| 221 | + self::assertIsArray($lock['packages'][0]); | ||
| 222 | + $lock['packages'][0]['require'] = $require; | ||
| 223 | + $this->filesystem->dumpFile($this->appDir.'/composer.lock', (string) json_encode($lock)); | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + private function log(): string | ||
| 227 | + { | ||
| 228 | + rewind($this->log); | ||
| 229 | + | ||
| 230 | + return (string) stream_get_contents($this->log); | ||
| 177 | } | 231 | } |
| 178 | 232 | ||
| 179 | /** | 233 | /** |