8000 [Console] Fix infinite loop on missing input · symfony/symfony@263fad5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 263fad5

Browse files
committed
[Console] Fix infinite loop on missing input
[Console] Use console exception for missing input
1 parent 46a8ede commit 263fad5

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private function doAsk(OutputInterface $output, Question $question)
149149
if (false === $ret) {
150150
$ret = fgets($inputStream, 4096);
151151
if (false === $ret) {
152-
throw new \RuntimeException('Aborted');
152+
throw new RuntimeException('Aborted');
153153
}
154154
$ret = trim($ret);
155155
}
@@ -412,6 +412,8 @@ private function validateAttempts(callable $interviewer, OutputInterface $output
412412

413413
try {
414414
return call_user_func($question->getValidator(), $interviewer());
415+
} catch (RuntimeException $e) {
416+
throw $e;
415417
} catch (\Exception $error) {
416418
}
417419
}

src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,37 @@ public function testLegacyChoiceOutputFormattingQuestionForUtf8Keys()
744744
$dialog->ask($this->createInputInterfaceMock(), $output, $question);
745745
}
746746

747+
/**
748+
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
749+
* @expectedExceptionMessage Aborted
750+
*/
751+
public function testAskThrowsExceptionOnMissingInput()
752+
{
753+
$dialog = new QuestionHelper();
754+
$dialog->setInputStream($this->getInputStream(''));
755+
756+
$dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), new Question('What\'s your name?'));
757+
}
758+
759+
/**
760+
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
761+
* @expectedExceptionMessage Aborted
762+
*/
763+
public function testAskThrowsExceptionOnMissingInputWithValidator()
764+
{
765+
$dialog = new QuestionHelper();
766+
$dialog->setInputStream($this->getInputStream(''));
767+
768+
$question = new Question('What\'s your name?');
769+
$question->setValidator(function () {
770+
if (!$value) {
771+
throw new \Exception('A value is required.');
772+
}
773+
});
774+
775+
$dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
776+
}
777+
747778
protected function getInputStream($input)
748779
{
749780
$stream = fopen('php://memory', 'r+', false);

src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 8000 @@ public function testAskEscapeLabel()
101101
$this->assertOutputContains('Do you want a \?', $output);
102102
}
103103

104+
/**
105+
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
106+
* @expectedExceptionMessage Aborted
107+
*/
108+
public function testAskThrowsExceptionOnMissingInput()
109+
{
110+
$dialog = new SymfonyQuestionHelper();
111+
112+
$dialog->setInputStream($this->getInputStream(''));
113+
$dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), new Question('What\'s your name?'));
114+
}
115+
104116
protected function getInputStream($input)
105117
{
106118
$stream = fopen('php://memory', 'r+', false);

0 commit comments

Comments
 (0)
0