1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
<?php declare(strict_types=1);
/*
* 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\Repository\Vcs;
use Composer\Config;
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);
}
}
public function testGetRootIdentifierFromRemoteLocalRepository(): void
{
$process = $this->getProcessExecutorMock();
$io = $this->getIOMock();
$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,
]], true);
self::assertSame('main', $driver->getRootIdentifier());
}
public function testGetRootIdentifierFromRemote(): void
{
$process = $this->getProcessExecutorMock();
$io = $this->getIOMock();
$io->expects([], true);
$driver = new GitDriver(['url' => 'https://example.org/acme.git'], $io, $this->config, $this->getHttpDownloaderMock(), $process);
$this->setRepoDir($driver, $this->home);
$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([[
'cmd' => ['git', 'remote', '-v'],
'stdout' => '',
], [
'cmd' => ['git', 'remote', 'set-url', 'origin', '--', 'https://example.org/acme.git'],
'stdout' => '',
], [
'cmd' => ['git', 'remote', 'show', 'origin'],
'stdout' => $stdout,
], [
'cmd' => ['git', 'remote', 'set-url', 'origin', '--', 'https://example.org/acme.git'],
'stdout' => '',
]]);
self::assertSame('main', $driver->getRootIdentifier());
}
public function testGetRootIdentifierFromLocalWithNetworkDisabled(): void
{
Platform::putEnv('COMPOSER_DISABLE_NETWORK', '1');
$process = $this->getProcessExecutorMock();
$io = $this->getIOMock();
$driver = new GitDriver(['url' => 'https://example.org/acme.git'], $io, $this->config, $this->getHttpDownloaderMock(), $process);
$this->setRepoDir($driver, $this->home);
$stdout = <<<GIT
* main
2.2
1.10
GIT;
$process
->expects([[
'cmd' => ['git', 'branch', '--no-color'],
'stdout' => $stdout,
]]);
self::assertSame('main', $driver->getRootIdentifier());
}
public function testGetBranchesFilterInvalidBranchNames(): void
{
$process = $this->getProcessExecutorMock();
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$driver = new GitDriver(['url' => 'https://example.org/acme.git'], $io, $this->config, $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(), $process);
$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
->expects([[
'cmd' => ['git', 'branch', '--no-color', '--no-abbrev', '-v'],
'stdout' => $stdout,
]]);
$branches = $driver->getBranches();
self::assertSame([
'main' => '089681446ba44d6d9004350192486f2ceb4eaa06',
'2.2' => '12681446ba44d6d9004350192486f2ceb4eaa06',
], $branches);
}
public function testFileGetContentInvalidIdentifier(): void
{
self::expectException('\RuntimeException');
$process = $this->getProcessExecutorMock();
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$driver = new GitDriver(['url' => 'https://example.org/acme.git'], $io, $this->config, $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(), $process);
self::assertNull($driver->getFileContent('file.txt', 'h'));
$driver->getFileContent('file.txt', '-h');
}
private function setRepoDir(GitDriver $driver, string $path): void
{
$reflectionClass = new \ReflectionClass($driver);
$reflectionProperty = $reflectionClass->getProperty('repoDir');
(\PHP_VERSION_ID < 80100) and $reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($driver, $path);
}
}
|