Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Console] Default hidden question to 1 attempt for non-tty session #36590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2020
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
[Console] Default hidden question to 1 attempt for non-tty session
  • Loading branch information
ostrolucky committed Apr 27, 2020
commit ee7fc5544ef6bf9f410f91ea0aeb45546a0db740
22 changes: 21 additions & 1 deletion 22 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()) {
fabpot marked this conversation as resolved.
Show resolved Hide resolved
$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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.