ProcessExecutorTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Util\ProcessExecutor;
  13. use Composer\Test\TestCase;
  14. use Composer\IO\BufferIO;
  15. use Symfony\Component\Console\Output\StreamOutput;
  16. class ProcessExecutorTest extends TestCase
  17. {
  18. public function testExecuteCapturesOutput()
  19. {
  20. $process = new ProcessExecutor;
  21. $process->execute('echo foo', $output);
  22. $this->assertEquals("foo".PHP_EOL, $output);
  23. }
  24. public function testExecuteOutputsIfNotCaptured()
  25. {
  26. $process = new ProcessExecutor;
  27. ob_start();
  28. $process->execute('echo foo');
  29. $output = ob_get_clean();
  30. $this->assertEquals("foo".PHP_EOL, $output);
  31. }
  32. public function testUseIOIsNotNullAndIfNotCaptured()
  33. {
  34. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  35. $io->expects($this->once())
  36. ->method('write')
  37. ->with($this->equalTo('foo'.PHP_EOL), false);
  38. $process = new ProcessExecutor($io);
  39. $process->execute('echo foo');
  40. }
  41. public function testExecuteCapturesStderr()
  42. {
  43. $process = new ProcessExecutor;
  44. $process->execute('cat foo', $output);
  45. $this->assertNotNull($process->getErrorOutput());
  46. }
  47. public function testTimeout()
  48. {
  49. ProcessExecutor::setTimeout(1);
  50. $process = new ProcessExecutor;
  51. $this->assertEquals(1, $process->getTimeout());
  52. ProcessExecutor::setTimeout(60);
  53. }
  54. /**
  55. * @dataProvider hidePasswordProvider
  56. */
  57. public function testHidePasswords($command, $expectedCommandOutput)
  58. {
  59. $process = new ProcessExecutor($buffer = new BufferIO('', StreamOutput::VERBOSITY_DEBUG));
  60. $process->execute($command, $output);
  61. $this->assertEquals('Executing command (CWD): ' . $expectedCommandOutput, trim($buffer->getOutput()));
  62. }
  63. public function hidePasswordProvider()
  64. {
  65. return array(
  66. array('echo https://foo:bar@example.org/', 'echo https://foo:***@example.org/'),
  67. array('echo http://foo@example.org', 'echo http://foo@example.org'),
  68. array('echo http://abcdef1234567890234578:x-oauth-token@github.com/', 'echo http://***:***@github.com/'),
  69. 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/'"),
  70. 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/'"),
  71. );
  72. }
  73. public function testDoesntHidePorts()
  74. {
  75. $process = new ProcessExecutor($buffer = new BufferIO('', StreamOutput::VERBOSITY_DEBUG));
  76. $process->execute('echo https://localhost:1234/', $output);
  77. $this->assertEquals('Executing command (CWD): echo https://localhost:1234/', trim($buffer->getOutput()));
  78. }
  79. public function testSplitLines()
  80. {
  81. $process = new ProcessExecutor;
  82. $this->assertEquals(array(), $process->splitLines(''));
  83. $this->assertEquals(array(), $process->splitLines(null));
  84. $this->assertEquals(array('foo'), $process->splitLines('foo'));
  85. $this->assertEquals(array('foo', 'bar'), $process->splitLines("foo\nbar"));
  86. $this->assertEquals(array('foo', 'bar'), $process->splitLines("foo\r\nbar"));
  87. $this->assertEquals(array('foo', 'bar'), $process->splitLines("foo\r\nbar\n"));
  88. }
  89. }