Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions docs/tools/tui.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
37 changes: 37 additions & 0 deletions src/Console/EditCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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);

Expand All @@ -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
{
Expand Down
58 changes: 58 additions & 0 deletions tests/Feature/TuiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<?php
require $argv[1];
putenv('APP_ENV=local');
$_ENV['APP_ENV'] = $_SERVER['APP_ENV'] = 'local';
$app = \Orchestra\Testbench\Foundation\Application::create(options: ['extra' => ['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");
});
Loading