-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Process] Turn getIterator() args to flags & add ITER_SKIP_OUT/ERR modes #18513
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1154,7 +1154,7 @@ public function pipesCodeProvider() | |
* @dataProvider provideVariousIncrementals | ||
*/ | ||
public function testIncrementalOutputDoesNotRequireAnotherCall($stream, $method) { | ||
$process = new Process(self::$phpBin.' -r '.escapeshellarg('$n = 0; while ($n < 3) { file_put_contents(\''.$stream.'\', $n, 1); $n++; usleep(1000); }'), null, null, null, null); | ||
$process = $this->getProcess(self::$phpBin.' -r '.escapeshellarg('$n = 0; while ($n < 3) { file_put_contents(\''.$stream.'\', $n, 1); $n++; usleep(1000); }'), null, null, null, null); | ||
$process->start(); | ||
$result = ''; | ||
$limit = microtime(true) + 3; | ||
|
@@ -1182,7 +1182,7 @@ public function testIteratorInput() | |
yield 'pong'; | ||
}; | ||
|
||
$process = new Process(self::$phpBin.' -r '.escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);'), null, null, $input()); | ||
$process = $this->getProcess(self::$phpBin.' -r '.escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);'), null, null, $input()); | ||
$process->run(); | ||
$this->assertSame('pingpong', $process->getOutput()); | ||
} | ||
|
@@ -1191,7 +1191,7 @@ public function testSimpleInputStream() | |
{ | ||
$input = new InputStream(); | ||
|
||
$process = new Process(self::$phpBin.' -r '.escapeshellarg('echo \'ping\'; stream_copy_to_stream(STDIN, STDOUT);')); | ||
$process = $this->getProcess(self::$phpBin.' -r '.escapeshellarg('echo \'ping\'; stream_copy_to_stream(STDIN, STDOUT);')); | ||
$process->setInput($input); | ||
|
||
$process->start(function ($type, $data) use ($input) { | ||
|
@@ -1225,7 +1225,7 @@ public function testInputStreamWithCallable() | |
$input->onEmpty($stream); | ||
$input->write($stream()); | ||
|
||
$process = new Process(self::$phpBin.' -r '.escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);')); | ||
$process = $this->getProcess(self::$phpBin.' -r '.escapeshellarg('echo fread(STDIN, 3);')); | ||
$process->setInput($input); | ||
$process->start(function ($type, $data) use ($input) { | ||
$input->close(); | ||
|
@@ -1243,7 +1243,7 @@ public function testInputStreamWithGenerator() | |
$input->close(); | ||
}); | ||
|
||
$process = new Process(self::$phpBin.' -r '.escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);')); | ||
$process = $this->getProcess(self::$phpBin.' -r '.escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);')); | ||
$process->setInput($input); | ||
$process->start(); | ||
$input->write('ping'); | ||
|
@@ -1257,7 +1257,7 @@ public function testInputStreamOnEmpty() | |
$input = new InputStream(); | ||
$input->onEmpty(function () use (&$i) {++$i;}); | ||
|
||
$process = new Process(self::$phpBin.' -r '.escapeshellarg('echo 123; echo fread(STDIN, 1); echo 456;')); | ||
$process = $this->getProcess(self::$phpBin.' -r '.escapeshellarg('echo 123; echo fread(STDIN, 1); echo 456;')); | ||
$process->setInput($input); | ||
$process->start(function ($type, $data) use ($input) { | ||
if ('123' === $data) { | ||
|
@@ -1274,7 +1274,7 @@ public function testIteratorOutput() | |
{ | ||
$input = new InputStream(); | ||
|
||
$process = new Process(self::$phpBin.' -r '.escapeshellarg('fwrite(STDOUT, 123); fwrite(STDERR, 234); fwrite(STDOUT, fread(STDIN, 3)); fwrite(STDERR, 456);')); | ||
$process = $this->getProcess(self::$phpBin.' -r '.escapeshellarg('fwrite(STDOUT, 123); fwrite(STDERR, 234); flush(); usleep(10000); fwrite(STDOUT, fread(STDIN, 3)); fwrite(STDERR, 456);')); | ||
$process->setInput($input); | ||
$process->start(); | ||
$output = array(); | ||
|
@@ -1310,12 +1310,12 @@ public function testNonBlockingNorClearingIteratorOutput() | |
{ | ||
$input = new InputStream(); | ||
|
||
$process = new Process(self::$phpBin.' -r '.escapeshellarg('fwrite(STDOUT, fread(STDIN, 3));')); | ||
$process = $this->getProcess(self::$phpBin.' -r '.escapeshellarg('fwrite(STDOUT, fread(STDIN, 3));')); | ||
$process->setInput($input); | ||
$process->start(); | ||
$output = array(); | ||
|
||
foreach ($process->getIterator(false, false) as $type => $data) { | ||
foreach ($process->getIterator($process::ITER_NON_BLOCKING | $process::ITER_KEEP_OUTPUT) as $type => $data) { | ||
$output[] = array($type, $data); | ||
break; | ||
} | ||
|
@@ -1326,7 +1326,7 @@ public function testNonBlockingNorClearingIteratorOutput() | |
|
||
$input->write(123); | ||
|
||
foreach ($process->getIterator(false, false) as $type => $data) { | ||
foreach ($process->getIterator($process::ITER_NON_BLOCKING | $process::ITER_KEEP_OUTPUT) as $type => $data) { | ||
if ('' !== $data) { | ||
$output[] = array($type, $data); | ||
} | ||
|
@@ -1342,6 +1342,21 @@ public function testNonBlockingNorClearingIteratorOutput() | |
$this->assertSame($expectedOutput, $output); | ||
} | ||
|
||
public function testChainedProcesses() | ||
{ | ||
$p1 = new Process(self::$phpBin.' -r '.escapeshellarg('fwrite(STDERR, 123); fwrite(STDOUT, 456);')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can have only one getProces per test case. And there is not need to configure differently the process for sigchild. So no need. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was not aware of |
||
$p2 = $this->getProcess(sprintf('%s -r %s', self::$phpBin, escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);'))); | ||
$p2->setInput($p1); | ||
|
||
$p1->start(); | ||
$p2->run(); | ||
|
||
$this->assertSame('123', $p1->getErrorOutput()); | ||
$this->assertSame('', $p1->getOutput()); | ||
$this->assertSame('', $p2->getErrorOutput()); | ||
$this->assertSame('456', $p2->getOutput()); | ||
} | ||
|
||
/** | ||
* @param string $commandline | ||
* @param null|string $cwd | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of the best advantage of this is to be able to pipe a process in another process.
We could use the same behavior as unix
|
and forward only stdout (useself:: ITER_SKIP_ERR)
by default).WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 good catch, I added a test case chaining two processes, works like a charm!