bug #36590 [Console] Default hidden question to 1 attempt for non-tty session (ostrolucky)
This PR was merged into the 4.4 branch. Discussion ---------- [Console] Default hidden question to 1 attempt for non-tty session | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #36565 | License | MIT | Doc PR | ### Problem 1 `validateAttempts()` method repeats validation forever by default, until exception extending `RuntimeException` isn't thrown. This currently happens disregarding if user is in tty session where they can actually type input, or non-tty session. This presents a problem when user code throws custom exceptions for hidden questions -> loop doesn't stop. As far as I can tell this issue is in all Symfony versions, but it was uncovered only after we stopped marking interactive flag to false automatically ourselves. Actually, all 3 problems were already existing problems, just hidden until now. ### Problem 2 Infinite loop problem is related to hidden questions, but this one isn't. If validation fails, another attempt to read & validate happens. This means user will get two prompts: 2x same question with 2 different error messages. One error message coming from validator, second error message about inability to read input (because this loop repeats until this kind of error happens, so last output will always be this error). As an example, output in practice would look like following ``` What do you want to do: > [ERROR] Action must not be empty. What do you want to do: > Aborted. ``` So even if loop stops, output is more than expected. ### Problem 3 This is purely cosmetic issue, but currently user gets `stty: stdin isn't a terminal` printed additionally when question helper tries to ask a hidden question without having tty. I have fixed this in same fashion as was already done for [getShell() method](ee7fc5544e/src/Symfony/Component/Console/Helper/QuestionHelper.php (L500)
). ### More details Well root of the first problem is that `\Symfony\Component\Console\Helper\QuestionHelper::getHiddenResponse` is inconsistent. In some cases it does throw `MissingInputException` (which extends `RuntimeException`), in others doesn't. This is because in others, `shell_exec` is used, which won't return `false` even in non-tty sessions. Initially I attempted to fix this and make them consistent by checking for empty result + `isTty` call, but during my testing I found that at least last, `bash -c` method returns `\n` as output both when passing empty input and when passing newline as input. This means we cannot differentiate with this technique when input is really empty, or at least I can't currently tell how, maybe someone does. I had also idea to use proc_open and check if `STDERR` cotains message about stdin not being a terminal, but I realized these functions might not be available. In future we should modernize this method to use less hacky techniques. Other solutions, eg. Inquirer.js or [hoa/console](https://github.com/hoaproject/Console/blob/master/Source/Readline/Readline.php) have much more elegant solutions. Anyway, since I encountered this issue and additionally this doesn't solve Problem 2, I stopped trying to fix this on this level. ### Alternative solution Alternative solution to problem 1 and 3 would be to fallback to default in case of hidden questions when tty is missing. But this still doesn't solve problem 2 and I can't think about solution right now which would fix problem 2 separately. We also didn't really reach consensus if reading passwords via stdin is desired. I tried this in `Inquirer.js` and this library *does read password from stdin* Commits -------ee7fc5544e
[Console] Default hidden question to 1 attempt for non-tty session
This commit is contained in:
commit
64e5a9dee8
@ -437,7 +437,7 @@ class QuestionHelper extends Helper
|
|||||||
|
|
||||||
if (false !== $shell = $this->getShell()) {
|
if (false !== $shell = $this->getShell()) {
|
||||||
$readCmd = 'csh' === $shell ? 'set mypassword = $<' : 'read -r mypassword';
|
$readCmd = 'csh' === $shell ? 'set mypassword = $<' : 'read -r mypassword';
|
||||||
$command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd);
|
$command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword' 2> /dev/null", $shell, $readCmd);
|
||||||
$sCommand = shell_exec($command);
|
$sCommand = shell_exec($command);
|
||||||
$value = $trimmable ? rtrim($sCommand) : $sCommand;
|
$value = $trimmable ? rtrim($sCommand) : $sCommand;
|
||||||
$output->writeln('');
|
$output->writeln('');
|
||||||
@ -461,6 +461,11 @@ class QuestionHelper extends Helper
|
|||||||
{
|
{
|
||||||
$error = null;
|
$error = null;
|
||||||
$attempts = $question->getMaxAttempts();
|
$attempts = $question->getMaxAttempts();
|
||||||
|
|
||||||
|
if (null === $attempts && !$this->isTty()) {
|
||||||
|
$attempts = 1;
|
||||||
|
}
|
||||||
|
|
||||||
while (null === $attempts || $attempts--) {
|
while (null === $attempts || $attempts--) {
|
||||||
if (null !== $error) {
|
if (null !== $error) {
|
||||||
$this->writeError($output, $error);
|
$this->writeError($output, $error);
|
||||||
@ -503,4 +508,19 @@ class QuestionHelper extends Helper
|
|||||||
|
|
||||||
return self::$shell;
|
return self::$shell;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function isTty(): bool
|
||||||
|
{
|
||||||
|
$inputStream = !$this->inputStream && \defined('STDIN') ? STDIN : $this->inputStream;
|
||||||
|
|
||||||
|
if (\function_exists('stream_isatty')) {
|
||||||
|
return stream_isatty($inputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!\function_exists('posix_isatty')) {
|
||||||
|
return posix_isatty($inputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -726,6 +726,23 @@ class QuestionHelperTest extends AbstractQuestionHelperTest
|
|||||||
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), $question);
|
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), $question);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAskThrowsExceptionFromValidatorEarlyWhenTtyIsMissing()
|
||||||
|
{
|
||||||
|
$this->expectException('Exception');
|
||||||
|
$this->expectExceptionMessage('Bar, not Foo');
|
||||||
|
|
||||||
|
$output = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')->getMock();
|
||||||
|
$output->expects($this->once())->method('writeln');
|
||||||
|
|
||||||
|
(new QuestionHelper())->ask(
|
||||||
|
$this->createStreamableInputInterfaceMock($this->getInputStream('Foo'), true),
|
||||||
|
$output,
|
||||||
|
(new Question('Q?'))->setHidden(true)->setValidator(function ($input) {
|
||||||
|
throw new \Exception("Bar, not $input");
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testEmptyChoices()
|
public function testEmptyChoices()
|
||||||
{
|
{
|
||||||
$this->expectException('LogicException');
|
$this->expectException('LogicException');
|
||||||
|
Reference in New Issue
Block a user