bug #13185 Fixes Issue #13184 - incremental output getters now return empty strings (Bailey Parker)

This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #13185).

Discussion
----------

Fixes Issue #13184 - incremental output getters now return empty strings

fixed Issue #13184

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | Issue #13184
| License       | MIT
| Doc PR        | [`Symfony\Component\Process\Process::getIncrementalOutput()`](http://api.symfony.com/2.6/Symfony/Component/Process/Process.html#method_getIncrementalOutput), [`Symfony\Component\Process\Process::getIncrementalErrorOutput()`](http://api.symfony.com/2.6/Symfony/Component/Process/Process.html#method_getIncrementalErrorOutput)

Commits
-------

3c608eb Fixes Issue #13184 - incremental output getters now return empty strings
This commit is contained in:
Fabien Potencier 2015-01-02 09:09:58 +01:00
commit 14ca1c537d
2 changed files with 72 additions and 0 deletions

View File

@ -401,6 +401,11 @@ class Process
$data = $this->getOutput();
$latest = substr($data, $this->incrementalOutputOffset);
if (false === $latest) {
return '';
}
$this->incrementalOutputOffset = strlen($data);
return $latest;
@ -442,6 +447,11 @@ class Process
$data = $this->getErrorOutput();
$latest = substr($data, $this->incrementalErrorOutputOffset);
if (false === $latest) {
return '';
}
$this->incrementalErrorOutputOffset = strlen($data);
return $latest;

View File

@ -276,6 +276,37 @@ abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
unlink($lock);
}
public function testGetEmptyIncrementalErrorOutput()
{
// use a lock file to toggle between writing ("W") and reading ("R") the
// output stream
$lock = tempnam(sys_get_temp_dir(), get_class($this).'Lock');
file_put_contents($lock, 'W');
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { if (\'W\' === file_get_contents('.var_export($lock, true).')) { file_put_contents(\'php://stderr\', \'ERROR\'); $n++; file_put_contents('.var_export($lock, true).', \'R\'); } usleep(100); }')));
$p->start();
$shouldWrite = false;
while ($p->isRunning()) {
if ('R' === file_get_contents($lock)) {
if (!$shouldWrite) {
$this->assertLessThanOrEqual(1, preg_match_all('/ERROR/', $p->getIncrementalOutput(), $matches));
$shouldWrite = true;
} else {
$this->assertSame('', $p->getIncrementalOutput());
file_put_contents($lock, 'W');
$shouldWrite = false;
}
}
usleep(100);
}
unlink($lock);
}
public function testGetOutput()
{
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { echo \' foo \'; $n++; }')));
@ -305,6 +336,37 @@ abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
unlink($lock);
}
public function testGetEmptyIncrementalOutput()
{
// use a lock file to toggle between writing ("W") and reading ("R") the
// output stream
$lock = tempnam(sys_get_temp_dir(), get_class($this).'Lock');
file_put_contents($lock, 'W');
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { if (\'W\' === file_get_contents('.var_export($lock, true).')) { echo \' foo \'; $n++; file_put_contents('.var_export($lock, true).', \'R\'); } usleep(100); }')));
$p->start();
$shouldWrite = false;
while ($p->isRunning()) {
if ('R' === file_get_contents($lock)) {
if (!$shouldWrite) {
$this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getIncrementalOutput(), $matches));
$shouldWrite = true;
} else {
$this->assertSame('', $p->getIncrementalOutput());
file_put_contents($lock, 'W');
$shouldWrite = false;
}
}
usleep(100);
}
unlink($lock);
}
public function testZeroAsOutput()
{
if ('\\' === DIRECTORY_SEPARATOR) {