2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2012-01-22 19:08:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
{
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testExecuteCapturesOutput(): void
|
2012-01-22 19:08:29 +00:00
|
|
|
{
|
|
|
|
$process = new ProcessExecutor;
|
|
|
|
$process->execute('echo foo', $output);
|
|
|
|
$this->assertEquals("foo".PHP_EOL, $output);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testExecuteOutputsIfNotCaptured(): void
|
2012-01-22 19:08:29 +00:00
|
|
|
{
|
|
|
|
$process = new ProcessExecutor;
|
|
|
|
ob_start();
|
|
|
|
$process->execute('echo foo');
|
|
|
|
$output = ob_get_clean();
|
|
|
|
$this->assertEquals("foo".PHP_EOL, $output);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testUseIOIsNotNullAndIfNotCaptured(): void
|
2016-11-06 04:18:18 +00:00
|
|
|
{
|
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');
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testExecuteCapturesStderr(): void
|
2012-02-29 11:05:25 +00:00
|
|
|
{
|
|
|
|
$process = new ProcessExecutor;
|
|
|
|
$process->execute('cat foo', $output);
|
|
|
|
$this->assertNotNull($process->getErrorOutput());
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testTimeout(): void
|
2012-02-21 07:59:52 +00:00
|
|
|
{
|
|
|
|
ProcessExecutor::setTimeout(1);
|
|
|
|
$process = new ProcessExecutor;
|
|
|
|
$this->assertEquals(1, $process->getTimeout());
|
|
|
|
ProcessExecutor::setTimeout(60);
|
|
|
|
}
|
|
|
|
|
2019-06-23 17:59:36 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider hidePasswordProvider
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testHidePasswords(string $command, string $expectedCommandOutput): void
|
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()));
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function hidePasswordProvider(): array
|
2019-06-23 17:59:36 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
['echo https://foo:bar@example.org/', 'echo https://foo:***@example.org/'],
|
|
|
|
['echo http://foo@example.org', 'echo http://foo@example.org'],
|
|
|
|
['echo http://abcdef1234567890234578:x-oauth-token@github.com/', 'echo http://***:***@github.com/'],
|
|
|
|
["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/'"],
|
|
|
|
["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
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testDoesntHidePorts(): void
|
2016-09-27 12:45:19 +00:00
|
|
|
{
|
|
|
|
$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()));
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testSplitLines(): void
|
2012-01-22 19:08:29 +00:00
|
|
|
{
|
|
|
|
$process = new ProcessExecutor;
|
2022-08-17 12:20:07 +00:00
|
|
|
$this->assertEquals([], $process->splitLines(''));
|
|
|
|
$this->assertEquals([], $process->splitLines(null));
|
|
|
|
$this->assertEquals(['foo'], $process->splitLines('foo'));
|
|
|
|
$this->assertEquals(['foo', 'bar'], $process->splitLines("foo\nbar"));
|
|
|
|
$this->assertEquals(['foo', 'bar'], $process->splitLines("foo\r\nbar"));
|
|
|
|
$this->assertEquals(['foo', 'bar'], $process->splitLines("foo\r\nbar\n"));
|
2012-01-22 19:08:29 +00:00
|
|
|
}
|
2020-01-11 08:57:50 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testConsoleIODoesNotFormatSymfonyConsoleStyle(): void
|
2020-01-11 08:57:50 +00:00
|
|
|
{
|
|
|
|
$output = new BufferedOutput(OutputInterface::VERBOSITY_NORMAL, true);
|
2022-08-17 12:20:07 +00:00
|
|
|
$process = new ProcessExecutor(new ConsoleIO(new ArrayInput([]), $output, new HelperSet([])));
|
2020-01-11 08:57:50 +00:00
|
|
|
|
2021-07-21 12:02:30 +00:00
|
|
|
$process->execute('php -ddisplay_errors=0 -derror_reporting=0 -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
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testExecuteAsyncCancel(): void
|
2020-09-08 09:33:41 +00:00
|
|
|
{
|
|
|
|
$process = new ProcessExecutor($buffer = new BufferIO('', StreamOutput::VERBOSITY_DEBUG));
|
|
|
|
$process->enableAsync();
|
2020-10-16 09:23:03 +00:00
|
|
|
$start = microtime(true);
|
|
|
|
$promise = $process->executeAsync('sleep 2');
|
2020-11-22 13:48:56 +00:00
|
|
|
$this->assertEquals(1, $process->countActiveJobs());
|
2020-09-08 09:33:41 +00:00
|
|
|
$promise->cancel();
|
2020-11-22 13:48:56 +00:00
|
|
|
$this->assertEquals(0, $process->countActiveJobs());
|
2022-04-01 09:27:13 +00:00
|
|
|
$process->wait();
|
2020-10-16 09:23:03 +00:00
|
|
|
$end = microtime(true);
|
2022-04-01 09:27:13 +00:00
|
|
|
$this->assertTrue($end - $start < 2, 'Canceling took longer than it should, lasted '.($end - $start));
|
2020-09-08 09:33:41 +00:00
|
|
|
}
|
2021-10-13 20:12:54 +00:00
|
|
|
|
2021-10-08 18:55:35 +00:00
|
|
|
/**
|
|
|
|
* Test various arguments are escaped as expected
|
|
|
|
*
|
|
|
|
* @dataProvider dataEscapeArguments
|
2021-10-27 14:18:46 +00:00
|
|
|
*
|
|
|
|
* @param string|false|null $argument
|
2021-10-08 18:55:35 +00:00
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testEscapeArgument($argument, string $win, string $unix): void
|
2021-10-08 18:55:35 +00:00
|
|
|
{
|
|
|
|
$expected = defined('PHP_WINDOWS_VERSION_BUILD') ? $win : $unix;
|
|
|
|
$this->assertSame($expected, ProcessExecutor::escape($argument));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Each named test is an array of:
|
|
|
|
* argument, win-expected, unix-expected
|
|
|
|
*/
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function dataEscapeArguments(): array
|
2021-10-08 18:55:35 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
2021-10-08 18:55:35 +00:00
|
|
|
// empty argument - must be quoted
|
2022-08-17 12:20:07 +00:00
|
|
|
'empty' => ['', '""', "''"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// null argument - must be quoted
|
2022-08-17 12:20:07 +00:00
|
|
|
'empty null' => [null, '""', "''"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// false argument - must be quoted
|
2022-08-17 12:20:07 +00:00
|
|
|
'empty false' => [false, '""', "''"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// unix single-quote must be escaped
|
2022-08-17 12:20:07 +00:00
|
|
|
'unix-sq' => ["a'bc", "a'bc", "'a'\\''bc'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
2021-10-13 13:49:18 +00:00
|
|
|
// new lines must be replaced
|
2022-08-17 12:20:07 +00:00
|
|
|
'new lines' => ["a\nb\nc", '"a b c"', "'a\nb\nc'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// whitespace <space> must be quoted
|
2022-08-17 12:20:07 +00:00
|
|
|
'ws space' => ['a b c', '"a b c"', "'a b c'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// whitespace <tab> must be quoted
|
2022-08-17 12:20:07 +00:00
|
|
|
'ws tab' => ["a\tb\tc", "\"a\tb\tc\"", "'a\tb\tc'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// no whitespace must not be quoted
|
2022-08-17 12:20:07 +00:00
|
|
|
'no-ws' => ['abc', 'abc', "'abc'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
2022-05-12 19:13:55 +00:00
|
|
|
// commas must be quoted
|
2022-08-17 12:20:07 +00:00
|
|
|
'comma' => ['a,bc', '"a,bc"', "'a,bc'"],
|
2022-05-12 19:13:55 +00:00
|
|
|
|
2021-10-08 18:55:35 +00:00
|
|
|
// double-quotes must be backslash-escaped
|
2022-08-17 12:20:07 +00:00
|
|
|
'dq' => ['a"bc', 'a\^"bc', "'a\"bc'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
2022-10-12 14:45:10 +00:00
|
|
|
// double-quotes must be backslash-escaped with preceding backslashes doubled
|
2022-08-17 12:20:07 +00:00
|
|
|
'dq-bslash' => ['a\\"bc', 'a\\\\\^"bc', "'a\\\"bc'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
2022-10-12 14:45:10 +00:00
|
|
|
// backslashes not preceding a double-quote are treated as literal
|
2022-08-17 12:20:07 +00:00
|
|
|
'bslash' => ['ab\\\\c\\', 'ab\\\\c\\', "'ab\\\\c\\'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// trailing backslashes must be doubled up when the argument is quoted
|
2022-08-17 12:20:07 +00:00
|
|
|
'bslash dq' => ['a b c\\\\', '"a b c\\\\\\\\"', "'a b c\\\\'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// meta: outer double-quotes must be caret-escaped as well
|
2022-08-17 12:20:07 +00:00
|
|
|
'meta dq' => ['a "b" c', '^"a \^"b\^" c^"', "'a \"b\" c'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// meta: percent expansion must be caret-escaped
|
2022-08-17 12:20:07 +00:00
|
|
|
'meta-pc1' => ['%path%', '^%path^%', "'%path%'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// meta: expansion must have two percent characters
|
2022-08-17 12:20:07 +00:00
|
|
|
'meta-pc2' => ['%path', '%path', "'%path'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// meta: expansion must have have two surrounding percent characters
|
2022-08-17 12:20:07 +00:00
|
|
|
'meta-pc3' => ['%%path', '%%path', "'%%path'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// meta: bang expansion must be double caret-escaped
|
2022-08-17 12:20:07 +00:00
|
|
|
'meta-bang1' => ['!path!', '^^!path^^!', "'!path!'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// meta: bang expansion must have two bang characters
|
2022-08-17 12:20:07 +00:00
|
|
|
'meta-bang2' => ['!path', '!path', "'!path'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// meta: bang expansion must have two surrounding ang characters
|
2022-08-17 12:20:07 +00:00
|
|
|
'meta-bang3' => ['!!path', '!!path', "'!!path'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// meta: caret-escaping must escape all other meta chars (triggered by double-quote)
|
2022-08-17 12:20:07 +00:00
|
|
|
'meta-all-dq' => ['<>"&|()^', '^<^>\^"^&^|^(^)^^', "'<>\"&|()^'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// other meta: no caret-escaping when whitespace in argument
|
2022-08-17 12:20:07 +00:00
|
|
|
'other meta' => ['<> &| ()^', '"<> &| ()^"', "'<> &| ()^'"],
|
2021-10-08 18:55:35 +00:00
|
|
|
|
|
|
|
// other meta: quote escape chars when no whitespace in argument
|
2022-08-17 12:20:07 +00:00
|
|
|
'other-meta' => ['<>&|()^', '"<>&|()^"', "'<>&|()^'"],
|
|
|
|
];
|
2021-10-08 18:55:35 +00:00
|
|
|
}
|
2012-01-22 19:08:29 +00:00
|
|
|
}
|