Skip to content

Commit 14ca1c5

Browse files
committed
bug symfony#13185 Fixes Issue symfony#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 symfony#13185). Discussion ---------- Fixes Issue symfony#13184 - incremental output getters now return empty strings fixed Issue symfony#13184 | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | Issue symfony#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 symfony#13184 - incremental output getters now return empty strings
2 parents a609ca0 + 3c608eb commit 14ca1c5

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ public function getIncrementalOutput()
401401
$data = $this->getOutput();
402402

403403
$latest = substr($data, $this->incrementalOutputOffset);
404+
405+
if (false === $latest) {
406+
return '';
407+
}
408+
404409
$this->incrementalOutputOffset = strlen($data);
405410

406411
return $latest;
@@ -442,6 +447,11 @@ public function getIncrementalErrorOutput()
442447
$data = $this->getErrorOutput();
443448

444449
$latest = substr($data, $this->incrementalErrorOutputOffset);
450+
451+
if (false === $latest) {
452+
return '';
453+
}
454+
445455
$this->incrementalErrorOutputOffset = strlen($data);
446456

447457
return $latest;

src/Symfony/Component/Process/Tests/AbstractProcessTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,37 @@ public function testGetIncrementalErrorOutput()
276276
unlink($lock);
277277
}
278278

279+
public function testGetEmptyIncrementalErrorOutput()
280+
{
281+
// use a lock file to toggle between writing ("W") and reading ("R") the
282+
// output stream
283+
$lock = tempnam(sys_get_temp_dir(), get_class($this).'Lock');
284+
file_put_contents($lock, 'W');
285+
286+
$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); }')));
287+
288+
$p->start();
289+
290+
$shouldWrite = false;
291+
292+
while ($p->isRunning()) {
293+
if ('R' === file_get_contents($lock)) {
294+
if (!$shouldWrite) {
295+
$this->assertLessThanOrEqual(1, preg_match_all('/ERROR/', $p->getIncrementalOutput(), $matches));
296+
$shouldWrite = true;
297+
} else {
298+
$this->assertSame('', $p->getIncrementalOutput());
299+
300+
file_put_contents($lock, 'W');
301+
$shouldWrite = false;
302+
}
303+
}
304+
usleep(100);
305+
}
306+
307+
unlink($lock);
308+
}
309+
279310
public function testGetOutput()
280311
{
281312
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { echo \' foo \'; $n++; }')));
@@ -305,6 +336,37 @@ public function testGetIncrementalOutput()
305336
unlink($lock);
306337
}
307338

339+
public function testGetEmptyIncrementalOutput()
340+
{
341+
// use a lock file to toggle between writing ("W") and reading ("R") the
342+
// output stream
343+
$lock = tempnam(sys_get_temp_dir(), get_class($this).'Lock');
344+
file_put_contents($lock, 'W');
345+
346+
$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); }')));
347+
348+
$p->start();
349+
350+
$shouldWrite = false;
351+
352+
while ($p->isRunning()) {
353+
if ('R' === file_get_contents($lock)) {
354+
if (!$shouldWrite) {
355+
$this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getIncrementalOutput(), $matches));
356+
$shouldWrite = true;
357+
} else {
358+
$this->assertSame('', $p->getIncrementalOutput());
359+
360+
file_put_contents($lock, 'W');
361+
$shouldWrite = false;
362+
}
363+
}
364+
usleep(100);
365+
}
366+
367+
unlink($lock);
368+
}
369+
308370
public function testZeroAsOutput()
309371
{
310372
if ('\\' === DIRECTORY_SEPARATOR) {

0 commit comments

Comments
 (0)