From ee7fc5544ef6bf9f410f91ea0aeb45546a0db740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Ostroluck=C3=BD?= Date: Mon, 27 Apr 2020 05:08:14 +0200 Subject: [PATCH] [Console] Default hidden question to 1 attempt for non-tty session --- .../Console/Helper/QuestionHelper.php | 22 ++++++++++++++++++- .../Tests/Helper/QuestionHelperTest.php | 17 ++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index b383252c5a..4e0afeae78 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -437,7 +437,7 @@ class QuestionHelper extends Helper if (false !== $shell = $this->getShell()) { $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); $value = $trimmable ? rtrim($sCommand) : $sCommand; $output->writeln(''); @@ -461,6 +461,11 @@ class QuestionHelper extends Helper { $error = null; $attempts = $question->getMaxAttempts(); + + if (null === $attempts && !$this->isTty()) { + $attempts = 1; + } + while (null === $attempts || $attempts--) { if (null !== $error) { $this->writeError($output, $error); @@ -503,4 +508,19 @@ class QuestionHelper extends Helper 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; + } } diff --git a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php index 139aa7290d..f4689bc818 100644 --- a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php @@ -726,6 +726,23 @@ class QuestionHelperTest extends AbstractQuestionHelperTest $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() { $this->expectException('LogicException');