From 7e5fbdf04939cf6a8cb1de8b5edba37e82c23bfa Mon Sep 17 00:00:00 2001 From: Juan Traverso Date: Sun, 16 Jun 2013 11:12:20 +0200 Subject: [PATCH 1/4] [Process] Added support for stdout and stderr flush (Issue #7884) --- src/Symfony/Component/Process/Process.php | 79 +++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/Symfony/Component/Process/Process.php b/src/Symfony/Component/Process/Process.php index 4822e8863ee84..d5d7bb775164a 100644 --- a/src/Symfony/Component/Process/Process.php +++ b/src/Symfony/Component/Process/Process.php @@ -532,6 +532,46 @@ public function getIncrementalOutput() return $latest; } + /** + * Clears the process output. + * + * @return Process + */ + public function flushOutput() + { + $this->stdout = ''; + $this->incrementalOutputOffset = 0; + + return $this; + } + + /** + * Returns the current output of the process and clears it + * + * @return string The process output + */ + public function getAndFlushOutput() + { + $data = $this->getOutput(); + $this->flushOutput(); + + return $data; + } + + /** + * Returns the current output of the process and clears it + * + * @return string The process output since the last call. + */ + public function getAndFlushIncrementalOutput() + { + $data = $this->getIncrementalOutput(); + $this->flushOutput(); + + return $data; + } + + /** * Returns the current error output of the process (STDERR). * @@ -565,6 +605,45 @@ public function getIncrementalErrorOutput() return $latest; } + /** + * Clears the process output. + * + * @return Process + */ + public function flushErrorOutput() + { + $this->stderr = ''; + $this->incrementalErrorOutputOffset = 0; + + return $this; + } + + /** + * Returns the current output of the process and clears it + * + * @return string The process output + */ + public function getAndFlushErrorOutput() + { + $data = $this->getErrorOutput(); + $this->flushErrorOutput(); + + return $data; + } + + /** + * Returns the current output of the process and clears it + * + * @return string The process output since the last call. + */ + public function getAndFlushIncrementalErrorOutput() + { + $data = $this->getIncrementalErrorOutput(); + $this->flushErrorOutput(); + + return $data; + } + /** * Returns the exit code returned by the process. * From 9f5aef941c347aeac9f32ee567b428863a340d2d Mon Sep 17 00:00:00 2001 From: Juan Traverso Date: Sun, 16 Jun 2013 11:54:56 +0200 Subject: [PATCH 2/4] [Process] New tests for flush methods --- .../Process/Tests/AbstractProcessTest.php | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/Symfony/Component/Process/Tests/AbstractProcessTest.php b/src/Symfony/Component/Process/Tests/AbstractProcessTest.php index 5cd52efbd9bbb..db016663f2d80 100644 --- a/src/Symfony/Component/Process/Tests/AbstractProcessTest.php +++ b/src/Symfony/Component/Process/Tests/AbstractProcessTest.php @@ -156,6 +156,36 @@ public function testGetIncrementalErrorOutput() } } + public function testFlushErrorOutput() + { + $p = new Process(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { file_put_contents(\'php://stderr\', \'ERROR\'); $n++; }'))); + + $p->run(); + $p->flushErrorOutput(); + $this->assertEquals('', $p->getErrorOutput()); + } + + public function testGetAndFlushErrorOutput() + { + $p = new Process(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { file_put_contents(\'php://stderr\', \'ERROR\'); $n++; }'))); + + $p->run(); + $this->assertEquals(3, preg_match_all('/ERROR/', $p->getAndFlushErrorOutput(), $matches)); + $this->assertEquals('', $p->getErrorOutput()); + } + + public function testGetAndFlushIncrementalErrorOutput() + { + $p = new Process(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { usleep(50000); file_put_contents(\'php://stderr\', \'ERROR\'); $n++; }'))); + + $p->start(); + while ($p->isRunning()) { + $this->assertLessThanOrEqual(1, preg_match_all('/ERROR/', $p->getandFlushIncrementalErrorOutput(), $matches)); + $this->assertEquals('', $p->getErrorOutput()); + usleep(20000); + } + } + public function testGetOutput() { $p = new Process(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) {echo \' foo \';$n++;}'))); @@ -175,6 +205,36 @@ public function testGetIncrementalOutput() } } + public function testFlushOutput() + { + $p = new Process(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) {echo \' foo \';$n++;}'))); + + $p->run(); + $p->flushOutput(); + $this->assertEquals('', $p->getOutput()); + } + + public function testGetAndFlushOutput() + { + $p = new Process(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) {echo \' foo \';$n++;}'))); + + $p->run(); + $this->assertEquals(3, preg_match_all('/foo/', $p->getAndFlushOutput(), $matches)); + $this->assertEquals('', $p->getOutput()); + } + + public function testGetAndFlushIncrementalOutput() + { + $p = new Process(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) { echo \' foo \'; usleep(50000); $n++; }'))); + + $p->start(); + while ($p->isRunning()) { + $this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getAndFlushIncrementalOutput(), $matches)); + $this->assertEquals('', $p->getOutput()); + usleep(20000); + } + } + public function testExitCodeCommandFailed() { if (defined('PHP_WINDOWS_VERSION_BUILD')) { From 321cbc16023ce7a1d38d1b88b6fef258ab19fdc2 Mon Sep 17 00:00:00 2001 From: Juan Traverso Date: Sun, 16 Jun 2013 13:23:04 +0200 Subject: [PATCH 3/4] [Process] [Tests] Fixing faulty tests for stdout and stderr flush methods --- src/Symfony/Component/Process/Tests/AbstractProcessTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Process/Tests/AbstractProcessTest.php b/src/Symfony/Component/Process/Tests/AbstractProcessTest.php index db016663f2d80..bcf00d6752f44 100644 --- a/src/Symfony/Component/Process/Tests/AbstractProcessTest.php +++ b/src/Symfony/Component/Process/Tests/AbstractProcessTest.php @@ -181,9 +181,10 @@ public function testGetAndFlushIncrementalErrorOutput() $p->start(); while ($p->isRunning()) { $this->assertLessThanOrEqual(1, preg_match_all('/ERROR/', $p->getandFlushIncrementalErrorOutput(), $matches)); - $this->assertEquals('', $p->getErrorOutput()); usleep(20000); } + $this->assertLessThanOrEqual(1, preg_match_all('/ERROR/', $p->getandFlushIncrementalErrorOutput(), $matches)); + $this->assertEquals('', $p->getErrorOutput()); } public function testGetOutput() @@ -230,9 +231,10 @@ public function testGetAndFlushIncrementalOutput() $p->start(); while ($p->isRunning()) { $this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getAndFlushIncrementalOutput(), $matches)); - $this->assertEquals('', $p->getOutput()); usleep(20000); } + $this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getAndFlushIncrementalOutput(), $matches)); + $this->assertEquals('', $p->getOutput()); } public function testExitCodeCommandFailed() From 20e67f3eacaede371b2d9ec35a40b4416c2abe30 Mon Sep 17 00:00:00 2001 From: Juan Traverso Date: Fri, 9 Aug 2013 16:52:59 +0200 Subject: [PATCH 4/4] [Process] Removed unnecesary flush + get methods keeping flush only --- src/Symfony/Component/Process/Process.php | 53 ------------------- .../Process/Tests/AbstractProcessTest.php | 48 +---------------- 2 files changed, 2 insertions(+), 99 deletions(-) diff --git a/src/Symfony/Component/Process/Process.php b/src/Symfony/Component/Process/Process.php index d5d7bb775164a..6b042db5923a2 100644 --- a/src/Symfony/Component/Process/Process.php +++ b/src/Symfony/Component/Process/Process.php @@ -545,33 +545,6 @@ public function flushOutput() return $this; } - /** - * Returns the current output of the process and clears it - * - * @return string The process output - */ - public function getAndFlushOutput() - { - $data = $this->getOutput(); - $this->flushOutput(); - - return $data; - } - - /** - * Returns the current output of the process and clears it - * - * @return string The process output since the last call. - */ - public function getAndFlushIncrementalOutput() - { - $data = $this->getIncrementalOutput(); - $this->flushOutput(); - - return $data; - } - - /** * Returns the current error output of the process (STDERR). * @@ -618,32 +591,6 @@ public function flushErrorOutput() return $this; } - /** - * Returns the current output of the process and clears it - * - * @return string The process output - */ - public function getAndFlushErrorOutput() - { - $data = $this->getErrorOutput(); - $this->flushErrorOutput(); - - return $data; - } - - /** - * Returns the current output of the process and clears it - * - * @return string The process output since the last call. - */ - public function getAndFlushIncrementalErrorOutput() - { - $data = $this->getIncrementalErrorOutput(); - $this->flushErrorOutput(); - - return $data; - } - /** * Returns the exit code returned by the process. * diff --git a/src/Symfony/Component/Process/Tests/AbstractProcessTest.php b/src/Symfony/Component/Process/Tests/AbstractProcessTest.php index bcf00d6752f44..aaaf064470728 100644 --- a/src/Symfony/Component/Process/Tests/AbstractProcessTest.php +++ b/src/Symfony/Component/Process/Tests/AbstractProcessTest.php @@ -162,29 +162,7 @@ public function testFlushErrorOutput() $p->run(); $p->flushErrorOutput(); - $this->assertEquals('', $p->getErrorOutput()); - } - - public function testGetAndFlushErrorOutput() - { - $p = new Process(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { file_put_contents(\'php://stderr\', \'ERROR\'); $n++; }'))); - - $p->run(); - $this->assertEquals(3, preg_match_all('/ERROR/', $p->getAndFlushErrorOutput(), $matches)); - $this->assertEquals('', $p->getErrorOutput()); - } - - public function testGetAndFlushIncrementalErrorOutput() - { - $p = new Process(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { usleep(50000); file_put_contents(\'php://stderr\', \'ERROR\'); $n++; }'))); - - $p->start(); - while ($p->isRunning()) { - $this->assertLessThanOrEqual(1, preg_match_all('/ERROR/', $p->getandFlushIncrementalErrorOutput(), $matches)); - usleep(20000); - } - $this->assertLessThanOrEqual(1, preg_match_all('/ERROR/', $p->getandFlushIncrementalErrorOutput(), $matches)); - $this->assertEquals('', $p->getErrorOutput()); + $this->assertEmpty($p->getErrorOutput()); } public function testGetOutput() @@ -212,29 +190,7 @@ public function testFlushOutput() $p->run(); $p->flushOutput(); - $this->assertEquals('', $p->getOutput()); - } - - public function testGetAndFlushOutput() - { - $p = new Process(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) {echo \' foo \';$n++;}'))); - - $p->run(); - $this->assertEquals(3, preg_match_all('/foo/', $p->getAndFlushOutput(), $matches)); - $this->assertEquals('', $p->getOutput()); - } - - public function testGetAndFlushIncrementalOutput() - { - $p = new Process(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) { echo \' foo \'; usleep(50000); $n++; }'))); - - $p->start(); - while ($p->isRunning()) { - $this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getAndFlushIncrementalOutput(), $matches)); - usleep(20000); - } - $this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getAndFlushIncrementalOutput(), $matches)); - $this->assertEquals('', $p->getOutput()); + $this->assertEmpty($p->getOutput()); } public function testExitCodeCommandFailed()