Browse Source

Convert warnings into exceptions when stream_select fails

Mark Story 6 years ago
parent
commit
3bb3195ef3
2 changed files with 19 additions and 1 deletions
  1. 11 0
      src/Console/ConsoleInput.php
  2. 8 1
      tests/TestCase/Console/ConsoleInputTest.php

+ 11 - 0
src/Console/ConsoleInput.php

@@ -16,6 +16,8 @@ declare(strict_types=1);
  */
 namespace Cake\Console;
 
+use Cake\Console\Exception\ConsoleException;
+
 /**
  * Object wrapper for interacting with stdin
  */
@@ -80,7 +82,16 @@ class ConsoleInput
         $readFds = [$this->_input];
         $writeFds = null;
         $errorFds = null;
+
+        $error = null;
+        set_error_handler(function (int $code, string $message) use (&$error) {
+            $error = "stream_select failed with code={$code} message={$message}.";
+        });
         $readyFds = stream_select($readFds, $writeFds, $errorFds, $timeout);
+        restore_error_handler();
+        if ($error !== null) {
+            throw new ConsoleException($error);
+        }
 
         return $readyFds > 0;
     }

+ 8 - 1
tests/TestCase/Console/ConsoleInputTest.php

@@ -48,6 +48,13 @@ class ConsoleInputTest extends TestCase
             'Skip ConsoleInput tests on Windows as they fail on AppVeyor.'
         );
 
-        $this->assertFalse($this->in->dataAvailable());
+        try {
+            $this->assertFalse($this->in->dataAvailable());
+        } catch (ConsoleException $e) {
+            $this->markTestSkipped(
+                'stream_select raised an exception. ' .
+                'This can happen when FD_SETSIZE is too small.'
+            );
+        }
     }
 }