Repository navigation
Add Prompter::canPrompt() and withPromptFallback() - #83
Merged
Merged
Conversation
A package that runs a loop of prompts needs to know first whether any of them can reach a person: without a terminal laravel/prompts returns each prompt's default instead of asking, and a prompt with no usable default fails with "Required.". The only way to ask was to reach into Laravel\Prompts\SelectPrompt::shouldFallback() and stream_isatty(STDIN), which is what laranail/env-kit does today. canPrompt() answers it in one place: false under a non-interactive input, true when prompts go through Laravel's question-helper fallback, and otherwise whether stdin is a terminal. withPromptFallback() is the test-side counterpart, for a test that moves the app out of the testing environment and still answers a command's prompts with expectsQuestion().
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Prompter::canPrompt(?InputInterface $input = null): boolsays whether a prompt asked now can reach a person:falseunder a non-interactive input (--no-interaction, or no terminal on stdin);truewhen prompts are answered through Laravel's question-helper fallback (Windows, the test harness);InteractsWithConsole::withPromptFallback()turns on that fallback for a test that switches the app out of thetestingenvironment and still answers a command's prompts withexpectsQuestion().Why
Without a terminal, laravel/prompts returns each prompt's default instead of asking, so a prompt with no usable default fails with "Required.". The only way for a package to check first was to reach into
Laravel\Prompts\SelectPrompt::shouldFallback()itself. laranail/env-kit does exactly that today, without declaring laravel/prompts. This gives it, and anything else, the check through console's own API.Tests
tests/Prompter/PrompterCanPromptTest.php: each branch, including a fresh subprocess with stdin on/dev/null(returnsfalse). The prompts fallback statics are snapshotted and restored around each case. Mutating the method (dropping the input check, returningtruefor the TTY branch) turns 4 of the 5 cases red.InteractsWithConsoleTest:withPromptFallback()sets the flag.Local: pest 573 passed, phpstan clean, rector dry-run clean, pint (package-tools config) passed.
Release
laranail/env-kit's refactor (separate PR) requires
laranail/console ^0.1.7for this method, so it waits on a release that carries this change. The CHANGELOG entry is under[Unreleased].