$desc = [
0 => [ 'file', 'php://stdin', 'r' ],
1 => [ 'pipe', 'w' ],
- 2 => [ 'file', 'php://stderr', 'w' ],
+ 2 => [ 'pipe', 'w' ],
];
if ( $useLogPipe ) {
$desc[3] = [ 'pipe', 'w' ];
throw new ProcOpenError();
}
$outBuffer = $logBuffer = '';
+ $errBuffer = null;
$emptyArray = [];
$status = false;
$logMsg = false;
} elseif ( $fd == 1 ) {
// From stdout
$outBuffer .= $block;
+ } elseif ( $fd == 2 ) {
+ // From stderr
+ $errBuffer .= $block;
} elseif ( $fd == 3 ) {
// From log FD
$logBuffer .= $block;
$this->logger->warning( "$logMsg: {command}", [ 'command' => $cmd ] );
}
- return new Result( $retval, $outBuffer );
+ return new Result( $retval, $outBuffer, $errBuffer );
}
}
/** @var string */
private $stdout;
+ /** @var string|null */
+ private $stderr;
+
/**
* @param int $exitCode
* @param string $stdout
*/
- public function __construct( $exitCode, $stdout ) {
+ public function __construct( $exitCode, $stdout, $stderr = null ) {
$this->exitCode = $exitCode;
$this->stdout = $stdout;
+ $this->stderr = $stderr;
}
/**
public function getStdout() {
return $this->stdout;
}
+
+ /**
+ * Returns stderr of the process or null if the Command was configured to add stderr to stdout
+ * with includeStderr( true )
+ *
+ * @return string|null
+ */
+ public function getStderr() {
+ return $this->stderr;
+ }
}
$this->assertSame( "bar\n", $result->getStdout() );
}
+ public function testStdout() {
+ $this->requirePosix();
+
+ $command = new Command();
+
+ $result = $command
+ ->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' )
+ ->execute();
+
+ $this->assertNotContains( 'ThisIsStderr', $result->getStdout() );
+ $this->assertEquals( "ThisIsStderr\n", $result->getStderr() );
+ }
+
+ public function testStdoutRedirection() {
+ $this->requirePosix();
+
+ $command = new Command();
+
+ $result = $command
+ ->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' )
+ ->includeStderr( true )
+ ->execute();
+
+ $this->assertEquals( "ThisIsStderr\n", $result->getStdout() );
+ $this->assertNull( $result->getStderr() );
+ }
+
public function testOutput() {
global $IP;
$this->requirePosix();
+ chdir( $IP );
$command = new Command();
$result = $command
- ->params( [ 'ls', "$IP/index.php" ] )
+ ->params( [ 'ls', 'index.php' ] )
->execute();
- $this->assertSame( "$IP/index.php", trim( $result->getStdout() ) );
+ $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
+ $this->assertSame( null, $result->getStderr() );
$command = new Command();
$result = $command
->params( [ 'ls', 'index.php', 'no-such-file' ] )
->includeStderr()
->execute();
+ $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
$this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStdout() );
+ $this->assertSame( null, $result->getStderr() );
+
+ $command = new Command();
+ $result = $command
+ ->params( [ 'ls', 'index.php', 'no-such-file' ] )
+ ->execute();
+ $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
+ $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStderr() );
}
public function testT69870() {