2012-01-22 19:08:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test\Util;
|
|
|
|
|
2020-01-11 08:57:50 +00:00
|
|
|
use Composer\IO\ConsoleIO;
|
2012-01-22 19:08:29 +00:00
|
|
|
use Composer\Util\ProcessExecutor;
|
2018-11-12 14:23:32 +00:00
|
|
|
use Composer\Test\TestCase;
|
2016-06-21 10:01:09 +00:00
|
|
|
use Composer\IO\BufferIO;
|
2020-09-08 09:33:41 +00:00
|
|
|
use React\Promise\Promise;
|
2020-01-11 08:57:50 +00:00
|
|
|
use Symfony\Component\Console\Helper\HelperSet;
|
|
|
|
use Symfony\Component\Console\Input\ArrayInput;
|
|
|
|
use Symfony\Component\Console\Output\BufferedOutput;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2016-06-21 10:01:09 +00:00
|
|
|
use Symfony\Component\Console\Output\StreamOutput;
|
2012-01-22 19:08:29 +00:00
|
|
|
|
|
|
|
class ProcessExecutorTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testExecuteCapturesOutput()
|
|
|
|
{
|
|
|
|
$process = new ProcessExecutor;
|
|
|
|
$process->execute('echo foo', $output);
|
|
|
|
$this->assertEquals("foo".PHP_EOL, $output);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExecuteOutputsIfNotCaptured()
|
|
|
|
{
|
|
|
|
$process = new ProcessExecutor;
|
|
|
|
ob_start();
|
|
|
|
$process->execute('echo foo');
|
|
|
|
$output = ob_get_clean();
|
|
|
|
$this->assertEquals("foo".PHP_EOL, $output);
|
|
|
|
}
|
|
|
|
|
2016-11-06 04:18:18 +00:00
|
|
|
public function testUseIOIsNotNullAndIfNotCaptured()
|
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
2016-11-06 04:18:18 +00:00
|
|
|
$io->expects($this->once())
|
2020-01-14 15:27:16 +00:00
|
|
|
->method('writeRaw')
|
2016-12-06 15:03:03 +00:00
|
|
|
->with($this->equalTo('foo'.PHP_EOL), false);
|
2016-11-06 04:18:18 +00:00
|
|
|
|
|
|
|
$process = new ProcessExecutor($io);
|
|
|
|
$process->execute('echo foo');
|
|
|
|
}
|
|
|
|
|
2012-02-29 11:05:25 +00:00
|
|
|
public function testExecuteCapturesStderr()
|
|
|
|
{
|
|
|
|
$process = new ProcessExecutor;
|
|
|
|
$process->execute('cat foo', $output);
|
|
|
|
$this->assertNotNull($process->getErrorOutput());
|
|
|
|
}
|
|
|
|
|
2012-02-21 07:59:52 +00:00
|
|
|
public function testTimeout()
|
|
|
|
{
|
|
|
|
ProcessExecutor::setTimeout(1);
|
|
|
|
$process = new ProcessExecutor;
|
|
|
|
$this->assertEquals(1, $process->getTimeout());
|
|
|
|
ProcessExecutor::setTimeout(60);
|
|
|
|
}
|
|
|
|
|
2019-06-23 17:59:36 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider hidePasswordProvider
|
|
|
|
*/
|
|
|
|
public function testHidePasswords($command, $expectedCommandOutput)
|
2016-06-21 10:01:09 +00:00
|
|
|
{
|
|
|
|
$process = new ProcessExecutor($buffer = new BufferIO('', StreamOutput::VERBOSITY_DEBUG));
|
2019-06-23 17:59:36 +00:00
|
|
|
$process->execute($command, $output);
|
|
|
|
$this->assertEquals('Executing command (CWD): ' . $expectedCommandOutput, trim($buffer->getOutput()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hidePasswordProvider()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('echo https://foo:bar@example.org/', 'echo https://foo:***@example.org/'),
|
|
|
|
array('echo http://foo@example.org', 'echo http://foo@example.org'),
|
|
|
|
array('echo http://abcdef1234567890234578:x-oauth-token@github.com/', 'echo http://***:***@github.com/'),
|
|
|
|
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/'"),
|
|
|
|
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/'"),
|
|
|
|
);
|
2016-06-21 10:01:09 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 12:45:19 +00:00
|
|
|
public function testDoesntHidePorts()
|
|
|
|
{
|
|
|
|
$process = new ProcessExecutor($buffer = new BufferIO('', StreamOutput::VERBOSITY_DEBUG));
|
|
|
|
$process->execute('echo https://localhost:1234/', $output);
|
|
|
|
$this->assertEquals('Executing command (CWD): echo https://localhost:1234/', trim($buffer->getOutput()));
|
|
|
|
}
|
|
|
|
|
2012-01-22 19:08:29 +00:00
|
|
|
public function testSplitLines()
|
|
|
|
{
|
|
|
|
$process = new ProcessExecutor;
|
|
|
|
$this->assertEquals(array(), $process->splitLines(''));
|
|
|
|
$this->assertEquals(array(), $process->splitLines(null));
|
|
|
|
$this->assertEquals(array('foo'), $process->splitLines('foo'));
|
|
|
|
$this->assertEquals(array('foo', 'bar'), $process->splitLines("foo\nbar"));
|
|
|
|
$this->assertEquals(array('foo', 'bar'), $process->splitLines("foo\r\nbar"));
|
2013-06-19 07:43:02 +00:00
|
|
|
$this->assertEquals(array('foo', 'bar'), $process->splitLines("foo\r\nbar\n"));
|
2012-01-22 19:08:29 +00:00
|
|
|
}
|
2020-01-11 08:57:50 +00:00
|
|
|
|
|
|
|
public function testConsoleIODoesNotFormatSymfonyConsoleStyle()
|
|
|
|
{
|
|
|
|
$output = new BufferedOutput(OutputInterface::VERBOSITY_NORMAL, true);
|
2020-01-13 12:45:04 +00:00
|
|
|
$process = new ProcessExecutor(new ConsoleIO(new ArrayInput(array()), $output, new HelperSet(array())));
|
2020-01-11 08:57:50 +00:00
|
|
|
|
2020-02-07 06:52:31 +00:00
|
|
|
$process->execute('php -r "echo \'<error>foo</error>\'.PHP_EOL;"');
|
2020-01-13 12:30:46 +00:00
|
|
|
$this->assertSame('<error>foo</error>'.PHP_EOL, $output->fetch());
|
2020-01-11 08:57:50 +00:00
|
|
|
}
|
2020-09-08 09:33:41 +00:00
|
|
|
|
|
|
|
public function testExecuteAsyncCancel()
|
|
|
|
{
|
|
|
|
$process = new ProcessExecutor($buffer = new BufferIO('', StreamOutput::VERBOSITY_DEBUG));
|
|
|
|
$process->enableAsync();
|
2020-10-16 09:23:03 +00:00
|
|
|
$start = microtime(true);
|
2020-09-08 09:33:41 +00:00
|
|
|
/** @var Promise $promise */
|
2020-10-16 09:23:03 +00:00
|
|
|
$promise = $process->executeAsync('sleep 2');
|
2020-09-08 09:33:41 +00:00
|
|
|
$this->assertEquals(1, $process->countActiveJobs());
|
|
|
|
$promise->cancel();
|
|
|
|
$this->assertEquals(0, $process->countActiveJobs());
|
2020-10-16 09:23:03 +00:00
|
|
|
$end = microtime(true);
|
|
|
|
$this->assertTrue($end - $start < 0.5, 'Canceling took longer than it should, lasted '.($end - $start));
|
2020-09-08 09:33:41 +00:00
|
|
|
}
|
2012-01-22 19:08:29 +00:00
|
|
|
}
|