Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 64e5a9d

Browse files
committed
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](https://github.com/symfony/symfony/blob/ee7fc5544ef6bf9f410f91ea0aeb45546a0db740/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 ------- ee7fc55 [Console] Default hidden question to 1 attempt for non-tty session
2 parents 0a7fa8f + ee7fc55 commit 64e5a9d

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream, bool $
437437

438438
if (false !== $shell = $this->getShell()) {
439439
$readCmd = 'csh' === $shell ? 'set mypassword = $<' : 'read -r mypassword';
440-
$command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd);
440+
$command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword' 2> /dev/null", $shell, $readCmd);
441441
$sCommand = shell_exec($command);
442442
$value = $trimmable ? rtrim($sCommand) : $sCommand;
443443
$output->writeln('');
@@ -461,6 +461,11 @@ private function validateAttempts(callable $interviewer, OutputInterface $output
461461
{
462462
$error = null;
463463
$attempts = $question->getMaxAttempts();
464+
465+
if (null === $attempts && !$this->isTty()) {
466+
$attempts = 1;
467+
}
468+
464469
while (null === $attempts || $attempts--) {
465470
if (null !== $error) {
466471
$this->writeError($output, $error);
@@ -503,4 +508,19 @@ private function getShell()
503508

504509
return self::$shell;
505510
}
511+
512+
private function isTty(): bool
513+
{
514+
$inputStream = !$this->inputStream && \defined('STDIN') ? STDIN : $this->inputStream;
515+
516+
if (\function_exists('stream_isatty')) {
517+
return stream_isatty($inputStream);
518+
}
519+
520+
if (!\function_exists('posix_isatty')) {
521+
return posix_isatty($inputStream);
522+
}
523+
524+
return true;
525+
}
506526
}

src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,23 @@ public function testAskThrowsExceptionOnMissingInputWithValidator()
726726
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), $question);
727727
}
728728

729+
public function testAskThrowsExceptionFromValidatorEarlyWhenTtyIsMissing()
730+
{
731+
$this->expectException('Exception');
732+
$this->expectExceptionMessage('Bar, not Foo');
733+
734+
$output = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')->getMock();
735+
$output->expects($this->once())->method('writeln');
736+
737+
(new QuestionHelper())->ask(
738+
$this->createStreamableInputInterfaceMock($this->getInputStream('Foo'), true),
739+
$output,
740+
(new Question('Q?'))->setHidden(true)->setValidator(function ($input) {
741+
throw new \Exception("Bar, not $input");
742+
})
743+
);
744+
}
745+
729746
public function testEmptyChoices()
730747
{
731748
$this->expectException('LogicException');

0 commit comments

Comments
 (0)