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
22 changes: 21 additions & 1 deletion src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream, bool $

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('');
Expand All @@ -461,6 +461,11 @@ private function validateAttempts(callable $interviewer, OutputInterface $output
{
$error = null;
$attempts = $question->getMaxAttempts();

if (null === $attempts && !$this->isTty()) {
$attempts = 1;
}

while (null === $attempts || $attempts--) {
if (null !== $error) {
$this->writeError($output, $error);
Expand Down Expand Up @@ -503,4 +508,19 @@ private function getShell()

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,23 @@ public function testAskThrowsExceptionOnMissingInputWithValidator()
$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');
Expand Down