diff --git a/CHANGELOG.md b/CHANGELOG.md index e440ce6..f646ddd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- **`laranail::env-kit.edit` with no terminal failed with `Required.`** With stdin not a + terminal, laravel/prompts returns each prompt's default instead of asking, and the editor's + menu has none, so the first `select()` threw `NonInteractiveValidationException: Required.` + The editor now checks first: under `--no-interaction`, or with no terminal, it writes a + message naming `laranail::env-kit.set`, `laranail::env-kit.unset` and + `laranail::env-kit.rename`, changes nothing, and exits `2` (usage). The interactive path is + unchanged. + - The `--force-production` editor test no longer depends on test order. Switching the app to `production` turns off Laravel's fakeable prompt fallback, so run alone (as the mutation job can) the editor rendered real prompts with no TTY and `select()` threw diff --git a/docs/tools/tui.md b/docs/tools/tui.md index ccb6a01..7450581 100644 --- a/docs/tools/tui.md +++ b/docs/tools/tui.md @@ -27,6 +27,13 @@ editor never crashes on a guarded action. When the app environment is `production`, the editor shows a warning banner before the menu. +## Without a terminal + +The editor needs a person at a terminal. Run with `--no-interaction`, or with stdin that +is not a terminal (a cron job, a CI step, a pipe), it changes nothing and exits with code +`2`, naming the commands that work without one: `laranail::env-kit.set`, +`laranail::env-kit.unset` and `laranail::env-kit.rename`. + --- [← Docs index](../../README.md#documentation) diff --git a/src/Console/EditCommand.php b/src/Console/EditCommand.php index af9318f..0642286 100644 --- a/src/Console/EditCommand.php +++ b/src/Console/EditCommand.php @@ -6,6 +6,10 @@ use Closure; +use function defined; + +use Laravel\Prompts\SelectPrompt; + use function Laravel\Prompts\text; use function Laravel\Prompts\select; use function Laravel\Prompts\confirm; @@ -18,6 +22,9 @@ * UI layer of laranail/console). Pure orchestration — every mutation goes * through the same {@see EnvKit} engine, so atomicity/guards/audit/events all * apply. Engine errors are shown inline and the loop keeps running. + * + * Without a terminal to prompt on it exits with {@see self::EXIT_USAGE} and names + * the non-interactive commands, rather than letting the first prompt fail. */ final class EditCommand extends AbstractEnvCommand { @@ -37,6 +44,14 @@ final class EditCommand extends AbstractEnvCommand public function handle(EnvKit $env): int { + if (! $this->canPrompt()) { + return $this->failWith( + 'laranail::env-kit.edit needs an interactive terminal. To change keys without one, use ' + . 'laranail::env-kit.set, laranail::env-kit.unset or laranail::env-kit.rename.', + self::EXIT_USAGE, + ); + } + $this->force = (bool) $this->option('force-production'); $target = $this->targetEnv($env); @@ -59,6 +74,28 @@ public function handle(EnvKit $env): int return self::EXIT_OK; } + /** + * Whether the editor's prompts can reach a person. + * + * Not when input is non-interactive: `--no-interaction`, or Symfony found no + * terminal on stdin. Yes when Laravel answers prompts through its question-helper + * fallback (Windows, and the test harness). Otherwise only when stdin is a terminal, + * the check laravel/prompts makes before it falls back to each prompt's default -- + * which for the editor's menu is empty, and fails as "Required.". + */ + private function canPrompt(): bool + { + if (! $this->input->isInteractive()) { + return false; + } + + if (SelectPrompt::shouldFallback()) { + return true; + } + + return defined('STDIN') && stream_isatty(STDIN); + } + /** Re-arm the production override before each mutation (each commit consumes it). */ private function allowing(EnvKit $env): EnvKit { diff --git a/tests/Feature/TuiTest.php b/tests/Feature/TuiTest.php index 633cd52..ab4e7cb 100644 --- a/tests/Feature/TuiTest.php +++ b/tests/Feature/TuiTest.php @@ -126,3 +126,61 @@ expect(EnvKit::get('A'))->toBe('10') ->and(EnvKit::get('B'))->toBe('20'); }); + +it('exits with a usage error and names the non-interactive commands under --no-interaction', function () { + $path = $this->bindEnv("A=1\n"); + + $this->artisan('laranail::env-kit.edit', ['--no-interaction' => true]) + // One expectation: each written line satisfies at most one of them. + ->expectsOutputToContain('laranail::env-kit.edit needs an interactive terminal. To change keys without one, use laranail::env-kit.set, laranail::env-kit.unset or laranail::env-kit.rename.') + ->doesntExpectOutputToContain('Closed EnvKit editor.') + ->assertExitCode(2); + + expect(file_get_contents($path))->toBe("A=1\n"); +}); + +it('exits with a usage error, not "Required.", when run with no TTY', function () { + if (DIRECTORY_SEPARATOR === '\\' || ! function_exists('proc_open')) { + $this->markTestSkipped('Needs a POSIX shell with proc_open.'); + } + + $path = envkit_temp(); + file_put_contents($path, "A=1\n"); + $autoload = dirname(__DIR__, 2) . '/vendor/autoload.php'; + + // A real `artisan` run outside the test harness: stdin is /dev/null, so it is not a + // terminal, and the app is not in the `testing` environment, so Laravel answers no + // prompt on the user's behalf -- the shape of a cron job or a CI step. + $worker = dirname($path) . '/edit-worker.php'; + file_put_contents($worker, <<<'PHP' + ['providers' => [ + \Simtabi\Laranail\EnvKit\Headless\Providers\EnvKitServiceProvider::class, + ]]]); + $app['config']->set('laranail.env-kit.backup_path', dirname($argv[2]) . '/backups'); + $kernel = $app->make(\Illuminate\Contracts\Console\Kernel::class); + $input = new \Symfony\Component\Console\Input\ArgvInput(['artisan', 'laranail::env-kit.edit', '--file=' . $argv[2]]); + exit($kernel->handle($input, new \Symfony\Component\Console\Output\StreamOutput(STDOUT))); + PHP); + + $process = proc_open( + [PHP_BINARY, $worker, $autoload, $path], + [['file', '/dev/null', 'r'], ['pipe', 'w'], ['pipe', 'w']], + $pipes, + ); + expect($process)->toBeResource(); + + $output = stream_get_contents($pipes[1]) . stream_get_contents($pipes[2]); + fclose($pipes[1]); + fclose($pipes[2]); + $status = proc_close($process); + + expect($output)->toContain('laranail::env-kit.edit needs an interactive terminal') + ->and($output)->toContain('laranail::env-kit.set, laranail::env-kit.unset or laranail::env-kit.rename') + ->and($output)->not->toContain('Required.') + ->and($status)->toBe(2) + ->and(file_get_contents($path))->toBe("A=1\n"); +});