X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/tests/phpunit/includes/shell/CommandTest.php diff --git a/tests/phpunit/includes/shell/CommandTest.php b/tests/phpunit/includes/shell/CommandTest.php new file mode 100644 index 00000000..d57b1b1c --- /dev/null +++ b/tests/phpunit/includes/shell/CommandTest.php @@ -0,0 +1,94 @@ +markTestSkipped( 'This test requires a POSIX environment.' ); + } + } + + /** + * @dataProvider provideExecute + */ + public function testExecute( $commandInput, $expectedExitCode, $expectedOutput ) { + $this->requirePosix(); + + $command = new Command(); + $result = $command + ->params( $commandInput ) + ->execute(); + + $this->assertSame( $expectedExitCode, $result->getExitCode() ); + $this->assertSame( $expectedOutput, $result->getStdout() ); + } + + public function provideExecute() { + return [ + 'success status' => [ 'true', 0, '' ], + 'failure status' => [ 'false', 1, '' ], + 'output' => [ [ 'echo', '-n', 'x', '>', 'y' ], 0, 'x > y' ], + ]; + } + + public function testEnvironment() { + $this->requirePosix(); + + $command = new Command(); + $result = $command + ->params( [ 'printenv', 'FOO' ] ) + ->environment( [ 'FOO' => 'bar' ] ) + ->execute(); + $this->assertSame( "bar\n", $result->getStdout() ); + } + + public function testOutput() { + global $IP; + + $this->requirePosix(); + + $command = new Command(); + $result = $command + ->params( [ 'ls', "$IP/index.php" ] ) + ->execute(); + $this->assertSame( "$IP/index.php", trim( $result->getStdout() ) ); + + $command = new Command(); + $result = $command + ->params( [ 'ls', 'index.php', 'no-such-file' ] ) + ->includeStderr() + ->execute(); + $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStdout() ); + } + + /** + * Test that null values are skipped by params() and unsafeParams() + */ + public function testNullsAreSkipped() { + $command = TestingAccessWrapper::newFromObject( new Command ); + $command->params( 'echo', 'a', null, 'b' ); + $command->unsafeParams( 'c', null, 'd' ); + $this->assertEquals( "'echo' 'a' 'b' c d", $command->command ); + } + + public function testT69870() { + $commandLine = wfIsWindows() + // 333 = 331 + CRLF + ? ( 'for /l %i in (1, 1, 1001) do @echo ' . str_repeat( '*', 331 ) ) + : 'printf "%-333333s" "*"'; + + // Test several times because it involves a race condition that may randomly succeed or fail + for ( $i = 0; $i < 10; $i++ ) { + $command = new Command(); + $output = $command->unsafeParams( $commandLine ) + ->execute() + ->getStdout(); + $this->assertEquals( 333333, strlen( $output ) ); + } + } +}