ProcessExecutorTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Util;
  12. use Composer\IO\ConsoleIO;
  13. use Composer\Util\ProcessExecutor;
  14. use Composer\Test\TestCase;
  15. use Composer\IO\BufferIO;
  16. use Symfony\Component\Console\Helper\HelperSet;
  17. use Symfony\Component\Console\Input\ArrayInput;
  18. use Symfony\Component\Console\Output\BufferedOutput;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. use Symfony\Component\Console\Output\StreamOutput;
  21. class ProcessExecutorTest extends TestCase
  22. {
  23. public function testExecuteCapturesOutput()
  24. {
  25. $process = new ProcessExecutor;
  26. $process->execute('echo foo', $output);
  27. $this->assertEquals("foo".PHP_EOL, $output);
  28. }
  29. public function testExecuteOutputsIfNotCaptured()
  30. {
  31. $process = new ProcessExecutor;
  32. ob_start();
  33. $process->execute('echo foo');
  34. $output = ob_get_clean();
  35. $this->assertEquals("foo".PHP_EOL, $output);
  36. }
  37. public function testUseIOIsNotNullAndIfNotCaptured()
  38. {
  39. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  40. $io->expects($this->once())
  41. ->method('writeRaw')
  42. ->with($this->equalTo('foo'.PHP_EOL), false);
  43. $process = new ProcessExecutor($io);
  44. $process->execute('echo foo');
  45. }
  46. public function testExecuteCapturesStderr()
  47. {
  48. $process = new ProcessExecutor;
  49. $process->execute('cat foo', $output);
  50. $this->assertNotNull($process->getErrorOutput());
  51. }
  52. public function testTimeout()
  53. {
  54. ProcessExecutor::setTimeout(1);
  55. $process = new ProcessExecutor;
  56. $this->assertEquals(1, $process->getTimeout());
  57. ProcessExecutor::setTimeout(60);
  58. }
  59. /**
  60. * @dataProvider hidePasswordProvider
  61. */
  62. public function testHidePasswords($command, $expectedCommandOutput)
  63. {
  64. $process = new ProcessExecutor($buffer = new BufferIO('', StreamOutput::VERBOSITY_DEBUG));
  65. $process->execute($command, $output);
  66. $this->assertEquals('Executing command (CWD): ' . $expectedCommandOutput, trim($buffer->getOutput()));
  67. }
  68. public function hidePasswordProvider()
  69. {
  70. return array(
  71. array('echo https://foo:bar@example.org/', 'echo https://foo:***@example.org/'),
  72. array('echo http://foo@example.org', 'echo http://foo@example.org'),
  73. array('echo http://abcdef1234567890234578:x-oauth-token@github.com/', 'echo http://***:***@github.com/'),
  74. array("svn ls --verbose --non-interactive --username 'foo' --password 'bar' 'https://foo.example.org/svn/'", "svn ls --verbose --non-interactive --username 'foo' --password '***' 'https://foo.example.org/svn/'"),
  75. array("svn ls --verbose --non-interactive --username 'foo' --password 'bar \'bar' 'https://foo.example.org/svn/'", "svn ls --verbose --non-interactive --username 'foo' --password '***' 'https://foo.example.org/svn/'"),
  76. );
  77. }
  78. public function testDoesntHidePorts()
  79. {
  80. $process = new ProcessExecutor($buffer = new BufferIO('', StreamOutput::VERBOSITY_DEBUG));
  81. $process->execute('echo https://localhost:1234/', $output);
  82. $this->assertEquals('Executing command (CWD): echo https://localhost:1234/', trim($buffer->getOutput()));
  83. }
  84. public function testSplitLines()
  85. {
  86. $process = new ProcessExecutor;
  87. $this->assertEquals(array(), $process->splitLines(''));
  88. $this->assertEquals(array(), $process->splitLines(null));
  89. $this->assertEquals(array('foo'), $process->splitLines('foo'));
  90. $this->assertEquals(array('foo', 'bar'), $process->splitLines("foo\nbar"));
  91. $this->assertEquals(array('foo', 'bar'), $process->splitLines("foo\r\nbar"));
  92. $this->assertEquals(array('foo', 'bar'), $process->splitLines("foo\r\nbar\n"));
  93. }
  94. public function testConsoleIODoesNotFormatSymfonyConsoleStyle()
  95. {
  96. $output = new BufferedOutput(OutputInterface::VERBOSITY_NORMAL, true);
  97. $process = new ProcessExecutor(new ConsoleIO(new ArrayInput(array()), $output, new HelperSet(array())));
  98. $process->execute('php -r "echo \'<error>foo</error>\'.PHP_EOL;"');
  99. $this->assertSame('<error>foo</error>'.PHP_EOL, $output->fetch());
  100. }
  101. }