[Console] Fix QuestionHelper::disableStty()

This commit is contained in:
Robin Chalas 2020-05-29 16:03:43 +02:00
parent b7cd22cbc0
commit 5d93b61278
2 changed files with 33 additions and 3 deletions

View File

@ -32,7 +32,7 @@ class QuestionHelper extends Helper
{ {
private $inputStream; private $inputStream;
private static $shell; private static $shell;
private static $stty; private static $stty = true;
/** /**
* Asks a question to the user. * Asks a question to the user.
@ -158,7 +158,7 @@ class QuestionHelper extends Helper
$inputStream = $this->inputStream ?: STDIN; $inputStream = $this->inputStream ?: STDIN;
$autocomplete = $question->getAutocompleterValues(); $autocomplete = $question->getAutocompleterValues();
if (null === $autocomplete || !Terminal::hasSttyAvailable()) { if (null === $autocomplete || !self::$stty || !Terminal::hasSttyAvailable()) {
$ret = false; $ret = false;
if ($question->isHidden()) { if ($question->isHidden()) {
try { try {
@ -424,7 +424,7 @@ class QuestionHelper extends Helper
return $value; return $value;
} }
if (Terminal::hasSttyAvailable()) { if (self::$stty && Terminal::hasSttyAvailable()) {
$sttyMode = shell_exec('stty -g'); $sttyMode = shell_exec('stty -g');
shell_exec('stty -echo'); shell_exec('stty -echo');

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Console\Tests\Helper; namespace Symfony\Component\Console\Tests\Helper;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Formatter\OutputFormatter; use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\FormatterHelper; use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Helper\HelperSet;
@ -1013,6 +1014,35 @@ class QuestionHelperTest extends AbstractQuestionHelperTest
$this->assertEquals('FooBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question)); $this->assertEquals('FooBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
} }
public function testDisableSttby()
{
if (!Terminal::hasSttyAvailable()) {
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
}
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('invalid');
QuestionHelper::disableStty();
$dialog = new QuestionHelper();
$dialog->setHelperSet(new HelperSet([new FormatterHelper()]));
$question = new ChoiceQuestion('Please select a bundle', [1 => 'AcmeDemoBundle', 4 => 'AsseticBundle']);
$question->setMaxAttempts(1);
// <UP ARROW><UP ARROW><NEWLINE><DOWN ARROW><DOWN ARROW><NEWLINE>
// Gives `AcmeDemoBundle` with stty
$inputStream = $this->getInputStream("\033[A\033[A\n\033[B\033[B\n");
try {
$dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question);
} finally {
$reflection = new \ReflectionProperty(QuestionHelper::class, 'stty');
$reflection->setAccessible(true);
$reflection->setValue(null, true);
}
}
public function testTraversableMultiselectAutocomplete() public function testTraversableMultiselectAutocomplete()
{ {
// <NEWLINE> // <NEWLINE>