Gergő Tisza has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/399768 )
Change subject: Allow programmatic input in Command
......................................................................
Allow programmatic input in Command
Change-Id: Ib68180c7af12558686f4864c24fd85f01201d6fb
---
M includes/shell/Command.php
M tests/phpunit/includes/shell/CommandTest.php
2 files changed, 33 insertions(+), 1 deletion(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/68/399768/1
diff --git a/includes/shell/Command.php b/includes/shell/Command.php
index 998b3ed..bb5799c 100644
--- a/includes/shell/Command.php
+++ b/includes/shell/Command.php
@@ -56,6 +56,9 @@
/** @var string */
private $method;
+ /** @var string */
+ private $inputString;
+
/** @var bool */
private $doIncludeStderr = false;
@@ -185,6 +188,18 @@
*/
public function profileMethod( $method ) {
$this->method = $method;
+
+ return $this;
+ }
+
+ /**
+ * Sends the provided input to the command.
+ * When set to null (default), the command will use the standard input.
+ * @param string|null $inputString
+ * @return $this
+ */
+ public function input( $inputString ) {
+ $this->inputString = $inputString;
return $this;
}
@@ -349,7 +364,7 @@
}
$desc = [
- 0 => [ 'file', 'php://stdin', 'r' ],
+ 0 => $this->inputString === null ? [ 'file',
'php://stdin', 'r' ] : [ 'pipe', 'r' ],
1 => [ 'pipe', 'w' ],
2 => [ 'pipe', 'w' ],
];
@@ -363,6 +378,13 @@
$this->logger->error( "proc_open() failed: {command}",
[ 'command' => $cmd ] );
throw new ProcOpenError();
}
+
+ if ( $this->inputString ) {
+ fwrite( $pipes[0], $this->inputString );
+ fclose( $pipes[0] );
+ unset( $pipes[0] );
+ }
+
$outBuffer = $logBuffer = '';
$errBuffer = null;
$emptyArray = [];
diff --git a/tests/phpunit/includes/shell/CommandTest.php
b/tests/phpunit/includes/shell/CommandTest.php
index 385dd50..c65f71a 100644
--- a/tests/phpunit/includes/shell/CommandTest.php
+++ b/tests/phpunit/includes/shell/CommandTest.php
@@ -151,4 +151,14 @@
$this->assertSame( 1, count( $logger->getBuffer() ) );
$this->assertSame( trim( $logger->getBuffer()[0][2]['error'] ),
'ThisIsStderr' );
}
+
+ public function testInput() {
+ $this->requirePosix();
+
+ $command = new Command();
+ $command->params( 'cat' );
+ $command->input( 'abc' );
+ $result = $command->execute();
+ $this->assertSame( 'abc', $result->getStdOut() );
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/399768
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib68180c7af12558686f4864c24fd85f01201d6fb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Gergő Tisza <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits