2022-04-01 15:13:22 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
2022-08-17 12:20:07 +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.
|
|
|
|
*/
|
|
|
|
|
2022-04-01 15:13:22 +00:00
|
|
|
namespace Composer\Test\Repository\Vcs;
|
|
|
|
|
|
|
|
use Composer\Config;
|
|
|
|
use Composer\IO\IOInterface;
|
|
|
|
use Composer\Repository\Vcs\GitDriver;
|
|
|
|
use Composer\Test\TestCase;
|
|
|
|
use Composer\Util\Filesystem;
|
|
|
|
use Composer\Util\Platform;
|
|
|
|
|
|
|
|
class GitDriverTest extends TestCase
|
|
|
|
{
|
|
|
|
/** @var Config */
|
|
|
|
private $config;
|
|
|
|
/** @var string */
|
|
|
|
private $home;
|
|
|
|
/** @var false|string */
|
|
|
|
private $networkEnv;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->home = self::getUniqueTmpDirectory();
|
|
|
|
$this->config = new Config();
|
|
|
|
$this->config->merge([
|
|
|
|
'config' => [
|
|
|
|
'home' => $this->home,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
$this->networkEnv = Platform::getEnv('COMPOSER_DISABLE_NETWORK');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
$fs = new Filesystem;
|
|
|
|
$fs->removeDirectory($this->home);
|
|
|
|
if ($this->networkEnv === false) {
|
|
|
|
Platform::clearEnv('COMPOSER_DISABLE_NETWORK');
|
|
|
|
} else {
|
|
|
|
Platform::putEnv('COMPOSER_DISABLE_NETWORK', $this->networkEnv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 10:11:25 +00:00
|
|
|
public function testGetRootIdentifierFromRemoteLocalRepository(): void
|
|
|
|
{
|
|
|
|
$process = $this->getProcessExecutorMock();
|
2023-06-07 12:35:16 +00:00
|
|
|
$io = $this->getIOMock();
|
2022-04-13 10:11:25 +00:00
|
|
|
|
|
|
|
$driver = new GitDriver(['url' => $this->home], $io, $this->config, $this->getHttpDownloaderMock(), $process);
|
|
|
|
$this->setRepoDir($driver, $this->home);
|
|
|
|
|
|
|
|
$stdoutFailure = <<<GITFAILURE
|
|
|
|
fatal: could not read Username for 'https://example.org/acme.git': terminal prompts disabled
|
|
|
|
GITFAILURE;
|
|
|
|
|
|
|
|
$stdout = <<<GIT
|
|
|
|
* main
|
|
|
|
2.2
|
|
|
|
1.10
|
|
|
|
GIT;
|
|
|
|
|
|
|
|
$process
|
|
|
|
->expects([[
|
|
|
|
'cmd' => 'git branch --no-color',
|
|
|
|
'stdout' => $stdout,
|
2022-08-16 13:53:57 +00:00
|
|
|
]], true);
|
2022-04-13 10:11:25 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertSame('main', $driver->getRootIdentifier());
|
2022-04-13 10:11:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 15:13:22 +00:00
|
|
|
public function testGetRootIdentifierFromRemote(): void
|
|
|
|
{
|
|
|
|
$process = $this->getProcessExecutorMock();
|
2023-06-07 12:35:16 +00:00
|
|
|
$io = $this->getIOMock();
|
2022-04-01 15:13:22 +00:00
|
|
|
|
2023-06-07 12:35:16 +00:00
|
|
|
$io->expects([], true);
|
2022-04-13 12:08:24 +00:00
|
|
|
|
2022-04-01 15:13:22 +00:00
|
|
|
$driver = new GitDriver(['url' => 'https://example.org/acme.git'], $io, $this->config, $this->getHttpDownloaderMock(), $process);
|
2022-04-13 10:11:25 +00:00
|
|
|
$this->setRepoDir($driver, $this->home);
|
2022-04-01 15:13:22 +00:00
|
|
|
|
|
|
|
$stdout = <<<GIT
|
|
|
|
* remote origin
|
|
|
|
Fetch URL: https://example.org/acme.git
|
|
|
|
Push URL: https://example.org/acme.git
|
|
|
|
HEAD branch: main
|
|
|
|
Remote branches:
|
|
|
|
1.10 tracked
|
|
|
|
2.2 tracked
|
|
|
|
main tracked
|
|
|
|
GIT;
|
|
|
|
|
|
|
|
$process
|
|
|
|
->expects([[
|
2022-04-13 10:11:25 +00:00
|
|
|
'cmd' => 'git remote -v',
|
|
|
|
'stdout' => '',
|
2022-04-13 12:08:24 +00:00
|
|
|
], [
|
|
|
|
'cmd' => Platform::isWindows() ? "git remote set-url origin -- https://example.org/acme.git && git remote show origin && git remote set-url origin -- https://example.org/acme.git" : "git remote set-url origin -- 'https://example.org/acme.git' && git remote show origin && git remote set-url origin -- 'https://example.org/acme.git'",
|
2022-04-01 15:13:22 +00:00
|
|
|
'stdout' => $stdout,
|
|
|
|
]]);
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertSame('main', $driver->getRootIdentifier());
|
2022-04-01 15:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetRootIdentifierFromLocalWithNetworkDisabled(): void
|
|
|
|
{
|
|
|
|
Platform::putEnv('COMPOSER_DISABLE_NETWORK', '1');
|
|
|
|
|
|
|
|
$process = $this->getProcessExecutorMock();
|
2023-06-07 12:35:16 +00:00
|
|
|
$io = $this->getIOMock();
|
2022-04-01 15:13:22 +00:00
|
|
|
|
|
|
|
$driver = new GitDriver(['url' => 'https://example.org/acme.git'], $io, $this->config, $this->getHttpDownloaderMock(), $process);
|
2022-04-13 10:11:25 +00:00
|
|
|
$this->setRepoDir($driver, $this->home);
|
2022-04-01 15:13:22 +00:00
|
|
|
|
|
|
|
$stdout = <<<GIT
|
|
|
|
* main
|
|
|
|
2.2
|
|
|
|
1.10
|
|
|
|
GIT;
|
|
|
|
|
|
|
|
$process
|
|
|
|
->expects([[
|
|
|
|
'cmd' => 'git branch --no-color',
|
|
|
|
'stdout' => $stdout,
|
|
|
|
]]);
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertSame('main', $driver->getRootIdentifier());
|
2022-04-01 15:13:22 +00:00
|
|
|
}
|
2022-04-13 10:11:25 +00:00
|
|
|
|
2022-04-13 14:18:25 +00:00
|
|
|
public function testGetBranchesFilterInvalidBranchNames(): void
|
2022-04-13 13:54:58 +00:00
|
|
|
{
|
2022-04-13 14:18:25 +00:00
|
|
|
$process = $this->getProcessExecutorMock();
|
2022-04-13 13:54:58 +00:00
|
|
|
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$driver = new GitDriver(['url' => 'https://example.org/acme.git'], $io, $this->config, $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(), $process);
|
2022-04-13 13:54:58 +00:00
|
|
|
$this->setRepoDir($driver, $this->home);
|
|
|
|
|
|
|
|
// Branches starting with a - character are not valid git branches names
|
|
|
|
// Still assert that they get filtered to prevent issues later on
|
|
|
|
$stdout = <<<GIT
|
|
|
|
* main 089681446ba44d6d9004350192486f2ceb4eaa06 commit
|
|
|
|
2.2 12681446ba44d6d9004350192486f2ceb4eaa06 commit
|
|
|
|
-h 089681446ba44d6d9004350192486f2ceb4eaa06 commit
|
|
|
|
GIT;
|
|
|
|
|
|
|
|
$process
|
2022-08-17 12:20:07 +00:00
|
|
|
->expects([[
|
2022-04-13 13:54:58 +00:00
|
|
|
'cmd' => 'git branch --no-color --no-abbrev -v',
|
|
|
|
'stdout' => $stdout,
|
2022-08-17 12:20:07 +00:00
|
|
|
]]);
|
2022-04-13 13:54:58 +00:00
|
|
|
|
|
|
|
$branches = $driver->getBranches();
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertSame([
|
2022-04-13 13:54:58 +00:00
|
|
|
'main' => '089681446ba44d6d9004350192486f2ceb4eaa06',
|
|
|
|
'2.2' => '12681446ba44d6d9004350192486f2ceb4eaa06',
|
2022-08-17 12:20:07 +00:00
|
|
|
], $branches);
|
2022-04-13 13:54:58 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 14:18:25 +00:00
|
|
|
public function testFileGetContentInvalidIdentifier(): void
|
2022-04-13 13:54:58 +00:00
|
|
|
{
|
2022-04-13 15:10:07 +00:00
|
|
|
self::expectException('\RuntimeException');
|
2022-04-13 13:54:58 +00:00
|
|
|
|
2022-04-13 14:18:25 +00:00
|
|
|
$process = $this->getProcessExecutorMock();
|
2022-04-13 13:54:58 +00:00
|
|
|
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
2022-08-17 12:20:07 +00:00
|
|
|
$driver = new GitDriver(['url' => 'https://example.org/acme.git'], $io, $this->config, $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(), $process);
|
2022-04-13 13:54:58 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertNull($driver->getFileContent('file.txt', 'h'));
|
2022-04-13 13:54:58 +00:00
|
|
|
|
|
|
|
$driver->getFileContent('file.txt', '-h');
|
|
|
|
}
|
|
|
|
|
2022-04-13 10:11:25 +00:00
|
|
|
private function setRepoDir(GitDriver $driver, string $path): void
|
|
|
|
{
|
|
|
|
$reflectionClass = new \ReflectionClass($driver);
|
|
|
|
$reflectionProperty = $reflectionClass->getProperty('repoDir');
|
|
|
|
$reflectionProperty->setAccessible(true);
|
|
|
|
$reflectionProperty->setValue($driver, $path);
|
|
|
|
}
|
2022-04-01 15:13:22 +00:00
|
|
|
}
|