diff --git a/src/Actions/GetNodeVersionAction.php b/src/Actions/GetNodeVersionAction.php index 7ef197b..7a9a93f 100644 --- a/src/Actions/GetNodeVersionAction.php +++ b/src/Actions/GetNodeVersionAction.php @@ -49,6 +49,10 @@ private function resolveVersion(): ?string protected function resolveVersionFromBinary(): ?string { + if (! $this->canUseShellExec()) { + return null; + } + $output = shell_exec('node --version 2>/dev/null'); if (blank($output)) { @@ -59,4 +63,9 @@ protected function resolveVersionFromBinary(): ?string return preg_match('/^\d+\.\d+/', $version) ? $version : null; } + + protected function canUseShellExec(): bool + { + return function_exists('shell_exec'); + } } diff --git a/tests/GetNodeVersionActionTest.php b/tests/GetNodeVersionActionTest.php index c453d09..09a1d25 100644 --- a/tests/GetNodeVersionActionTest.php +++ b/tests/GetNodeVersionActionTest.php @@ -31,6 +31,27 @@ protected function resolveVersionFromBinary(): ?string rmdir($dir); }); +test('reads version from .nvmrc when shell_exec is disabled', function () { + $dir = sys_get_temp_dir().'/kite-sdk-test-'.uniqid(); + mkdir($dir); + file_put_contents($dir.'/.nvmrc', '20.11.0'); + + $action = new class($dir) extends GetNodeVersionAction + { + protected function canUseShellExec(): bool + { + return false; + } + }; + + $result = $action->handle(new Collection, fn ($data) => $data); + + expect($result[0]['value'])->toBe('20.11.0'); + + unlink($dir.'/.nvmrc'); + rmdir($dir); +}); + test('strips leading v from .nvmrc version', function () { $dir = sys_get_temp_dir().'/kite-sdk-test-'.uniqid(); mkdir($dir);