Skip to content

Commit

Permalink
Merge pull request #24 from ElvenSpellmaker/fix/zero-read-bug
Browse files Browse the repository at this point in the history
Fixes a bug whereby `0` is interpreted as falsy
  • Loading branch information
ElvenSpellmaker authored Dec 7, 2019
2 parents 02feb75 + 361a516 commit dfc2cab
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/bin/sca/phpcs.txt
/bin/sca/phpmd.html
composer.lock
/.vscode
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ language: php
php:
- '5.6'
- '7.0'
- '7.1'
- '7.2'
- '7.3'
- '7.4'
- hhvm
- nightly

before_install:
- git config --global github.accesstoken $GITHUB_OAUTH_TOKEN
- composer config github-oauth.github.com $GITHUB_OAUTH_TOKEN
Expand Down
2 changes: 1 addition & 1 deletion bin/runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

BASEDIR=$(dirname $0)

php BASEDIR/../tests/TestRig.php
php $BASEDIR/../tests/TestRig.php
42 changes: 42 additions & 0 deletions examples/LoopExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env php
<?php
/**
* @title Shows handling of falsy data such as `0` in a loop.
* @command yes 0 | grep 0 | head
* @expected 0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n
*/

error_reporting(E_ALL);

include __DIR__ . '/../vendor/autoload.php';

use ElvenSpellmaker\PipeSys as PS;
use ElvenSpellmaker\PipeSys\Command as Command;
use ElvenSpellmaker\PipeSys\IO as IO;

class LoopSystem extends Command\AbstractCommand
{
private $i = 5;

public function getCommand()
{
while($this->i--)
{
yield new IO\OutputIntent('0');
echo (yield new IO\ReadIntent), "\n";
}
}
}

$loopBuffer = new IO\QueueBuffer;

$start = new LoopSystem;
$end = new LoopSystem;

$start->setStdIn($loopBuffer);
$end->setStdOut($loopBuffer);

$c = new PS\Scheduler(new Command\StandardConnector);
$c->addCommand($start);
$c->addCommand($end);
$c->run();
3 changes: 2 additions & 1 deletion src/PipeSys/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ElvenSpellmaker\PipeSys\Command\CommandInterface;
use ElvenSpellmaker\PipeSys\IO\BufferInterface;
use ElvenSpellmaker\PipeSys\IO\EOF;
use ElvenSpellmaker\PipeSys\IO\InvalidBufferException;
use ElvenSpellmaker\PipeSys\IO\IOableTrait;
use ElvenSpellmaker\PipeSys\IO\OutputIntent;
Expand Down Expand Up @@ -89,7 +90,7 @@ private function attemptRun($firstRun)
$this->generator->next();
}

$genResponse = $data
$genResponse = $data !== null
? $this->generator->send($data)
: $this->generator->current();

Expand Down
2 changes: 1 addition & 1 deletion src/PipeSys/IO/IOableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function attemptRead()
{
$data = $this->read($this->readIntent->getChannel());

if ($data)
if ($data !== null)
{
$this->readIntent = null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PipeSys/IO/QueueBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function read()
}
catch (RuntimeException $e)
{
return false;
return null;
}

if (isset($this->block))
Expand Down

0 comments on commit dfc2cab

Please sign in to comment.