From 6b71a896155893d49b340189dc490febd1696825 Mon Sep 17 00:00:00 2001 From: Laurent VOULLEMIER Date: Thu, 28 May 2020 22:44:39 +0200 Subject: [PATCH 1/3] Add meaningful message when Process is not installed (ProcessHelper) --- Helper/ProcessHelper.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Helper/ProcessHelper.php b/Helper/ProcessHelper.php index 666f114a2..951bb854e 100644 --- a/Helper/ProcessHelper.php +++ b/Helper/ProcessHelper.php @@ -37,6 +37,10 @@ class ProcessHelper extends Helper */ public function run(OutputInterface $output, $cmd, $error = null, callable $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE) { + if (!class_exists(Process::class)) { + throw new \LogicException('The Process helper requires the "Process" component. Install "symfony/process" to use it.'); + } + if ($output instanceof ConsoleOutputInterface) { $output = $output->getErrorOutput(); } From 6ad3319ec09d333ca9e19c3d18ccd3eecf0d3d8c Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Fri, 29 May 2020 16:03:43 +0200 Subject: [PATCH 2/3] [Console] Fix QuestionHelper::disableStty() --- Helper/QuestionHelper.php | 6 +++--- Tests/Helper/QuestionHelperTest.php | 30 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Helper/QuestionHelper.php b/Helper/QuestionHelper.php index 93e221d36..80f6048b8 100644 --- a/Helper/QuestionHelper.php +++ b/Helper/QuestionHelper.php @@ -32,7 +32,7 @@ class QuestionHelper extends Helper { private $inputStream; private static $shell; - private static $stty; + private static $stty = true; /** * Asks a question to the user. @@ -158,7 +158,7 @@ private function doAsk(OutputInterface $output, Question $question) $inputStream = $this->inputStream ?: STDIN; $autocomplete = $question->getAutocompleterValues(); - if (null === $autocomplete || !Terminal::hasSttyAvailable()) { + if (null === $autocomplete || !self::$stty || !Terminal::hasSttyAvailable()) { $ret = false; if ($question->isHidden()) { try { @@ -424,7 +424,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream) return $value; } - if (Terminal::hasSttyAvailable()) { + if (self::$stty && Terminal::hasSttyAvailable()) { $sttyMode = shell_exec('stty -g'); shell_exec('stty -echo'); diff --git a/Tests/Helper/QuestionHelperTest.php b/Tests/Helper/QuestionHelperTest.php index 4303c020a..93b762c26 100644 --- a/Tests/Helper/QuestionHelperTest.php +++ b/Tests/Helper/QuestionHelperTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Console\Tests\Helper; +use Symfony\Component\Console\Exception\InvalidArgumentException; use Symfony\Component\Console\Formatter\OutputFormatter; use Symfony\Component\Console\Helper\FormatterHelper; use Symfony\Component\Console\Helper\HelperSet; @@ -1013,6 +1014,35 @@ public function testTraversableAutocomplete() $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); + + // + // 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() { // From bfe29ead7e7b1cc9ce74c6a40d06ad1f96fced13 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sat, 30 May 2020 20:58:05 +0200 Subject: [PATCH 3/3] Various cleanups --- Helper/ProcessHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Helper/ProcessHelper.php b/Helper/ProcessHelper.php index 951bb854e..6cafb1fac 100644 --- a/Helper/ProcessHelper.php +++ b/Helper/ProcessHelper.php @@ -38,7 +38,7 @@ class ProcessHelper extends Helper public function run(OutputInterface $output, $cmd, $error = null, callable $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE) { if (!class_exists(Process::class)) { - throw new \LogicException('The Process helper requires the "Process" component. Install "symfony/process" to use it.'); + throw new \LogicException('The ProcessHelper cannot be run as the Process component is not installed. Try running "compose require symfony/process".'); } if ($output instanceof ConsoleOutputInterface) {