3 use MediaWiki\Shell\Command
;
4 use Wikimedia\TestingAccessWrapper
;
9 class CommandTest
extends PHPUnit_Framework_TestCase
{
10 private function requirePosix() {
11 if ( wfIsWindows() ) {
12 $this->markTestSkipped( 'This test requires a POSIX environment.' );
17 * @dataProvider provideExecute
19 public function testExecute( $commandInput, $expectedExitCode, $expectedOutput ) {
20 $this->requirePosix();
22 $command = new Command();
24 ->params( $commandInput )
27 $this->assertSame( $expectedExitCode, $result->getExitCode() );
28 $this->assertSame( $expectedOutput, $result->getStdout() );
31 public function provideExecute() {
33 'success status' => [ 'true', 0, '' ],
34 'failure status' => [ 'false', 1, '' ],
35 'output' => [ [ 'echo', '-n', 'x', '>', 'y' ], 0, 'x > y' ],
39 public function testEnvironment() {
40 $this->requirePosix();
42 $command = new Command();
44 ->params( [ 'printenv', 'FOO' ] )
45 ->environment( [ 'FOO' => 'bar' ] )
47 $this->assertSame( "bar\n", $result->getStdout() );
50 public function testStdout() {
51 $this->requirePosix();
53 $command = new Command();
56 ->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' )
59 $this->assertNotContains( 'ThisIsStderr', $result->getStdout() );
60 $this->assertEquals( "ThisIsStderr\n", $result->getStderr() );
63 public function testStdoutRedirection() {
64 $this->requirePosix();
66 $command = new Command();
69 ->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' )
70 ->includeStderr( true )
73 $this->assertEquals( "ThisIsStderr\n", $result->getStdout() );
74 $this->assertNull( $result->getStderr() );
77 public function testOutput() {
80 $this->requirePosix();
83 $command = new Command();
85 ->params( [ 'ls', 'index.php' ] )
87 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
88 $this->assertSame( null, $result->getStderr() );
90 $command = new Command();
92 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
95 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
96 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStdout() );
97 $this->assertSame( null, $result->getStderr() );
99 $command = new Command();
101 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
103 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
104 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStderr() );
108 * Test that null values are skipped by params() and unsafeParams()
110 public function testNullsAreSkipped() {
111 $command = TestingAccessWrapper
::newFromObject( new Command
);
112 $command->params( 'echo', 'a', null, 'b' );
113 $command->unsafeParams( 'c', null, 'd' );
114 $this->assertEquals( "'echo' 'a' 'b' c d", $command->command
);
117 public function testT69870() {
118 $commandLine = wfIsWindows()
120 ?
( 'for /l %i in (1, 1, 1001) do @echo ' . str_repeat( '*', 331 ) )
121 : 'printf "%-333333s" "*"';
123 // Test several times because it involves a race condition that may randomly succeed or fail
124 for ( $i = 0; $i < 10; $i++
) {
125 $command = new Command();
126 $output = $command->unsafeParams( $commandLine )
129 $this->assertEquals( 333333, strlen( $output ) );
133 public function testLogStderr() {
134 $this->requirePosix();
136 $logger = new TestLogger( true, function ( $message, $level, $context ) {
137 return $level === Psr\Log\LogLevel
::ERROR ?
'1' : null;
139 $command = new Command();
140 $command->setLogger( $logger );
141 $command->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' );
143 $this->assertEmpty( $logger->getBuffer() );
145 $command = new Command();
146 $command->setLogger( $logger );
147 $command->logStderr();
148 $command->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' );
150 $this->assertSame( 1, count( $logger->getBuffer() ) );
151 $this->assertSame( trim( $logger->getBuffer()[0][2]['error'] ), 'ThisIsStderr' );