forked from composer/composer
/
GitDriverTest.php
138 lines (115 loc) · 4.07 KB
/
GitDriverTest.php
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
<?php declare(strict_types=1);
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);
}
}
public function testGetRootIdentifierFromRemoteLocalRepository(): void
{
$process = $this->getProcessExecutorMock();
$io = $this->getMockBuilder(IOInterface::class)->getMock();
$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 remote show origin',
'stdout' => $stdoutFailure,
], [
'cmd' => 'git branch --no-color',
'stdout' => $stdout,
]]);
$this->assertSame('main', $driver->getRootIdentifier());
}
public function testGetRootIdentifierFromRemote(): void
{
$process = $this->getProcessExecutorMock();
$io = $this->getMockBuilder(IOInterface::class)->getMock();
$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' && git remote show origin && git remote set-url origin -- 'https://example.org/acme.git'",
'stdout' => $stdout,
]]);
$this->assertSame('main', $driver->getRootIdentifier());
}
public function testGetRootIdentifierFromLocalWithNetworkDisabled(): void
{
Platform::putEnv('COMPOSER_DISABLE_NETWORK', '1');
$process = $this->getProcessExecutorMock();
$io = $this->getMockBuilder(IOInterface::class)->getMock();
$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,
]]);
$this->assertSame('main', $driver->getRootIdentifier());
}
private function setRepoDir(GitDriver $driver, string $path): void
{
$reflectionClass = new \ReflectionClass($driver);
$reflectionProperty = $reflectionClass->getProperty('repoDir');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($driver, $path);
}
}