1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RoaveTest\BackwardCompatibility\Git; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Roave\BackwardCompatibility\Git\CheckedOutRepository; |
9
|
|
|
use Roave\BackwardCompatibility\Git\GitCheckoutRevisionToTemporaryPath; |
10
|
|
|
use Roave\BackwardCompatibility\Git\Revision; |
11
|
|
|
use RuntimeException; |
12
|
|
|
use Symfony\Component\Process\Process; |
13
|
|
|
use function Safe\file_put_contents; |
14
|
|
|
use function Safe\mkdir; |
15
|
|
|
use function Safe\realpath; |
16
|
|
|
use function Safe\tempnam; |
17
|
|
|
use function Safe\unlink; |
18
|
|
|
use function sys_get_temp_dir; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @covers \Roave\BackwardCompatibility\Git\GitCheckoutRevisionToTemporaryPath |
22
|
|
|
*/ |
23
|
|
|
final class GitCheckoutRevisionToTemporaryPathTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
private const TEST_REVISION_TO_CHECKOUT = '428327492a803b6e0c612b157a67a50a47275461'; |
26
|
|
|
|
27
|
|
|
public function testCheckoutAndRemove() : void |
28
|
|
|
{ |
29
|
|
|
$git = new GitCheckoutRevisionToTemporaryPath(); |
30
|
|
|
$revision = Revision::fromSha1(self::TEST_REVISION_TO_CHECKOUT); |
31
|
|
|
|
32
|
|
|
$temporaryClone = $git->checkout($this->sourceRepository(), $revision); |
33
|
|
|
|
34
|
|
|
self::assertDirectoryExists((string) $temporaryClone); |
|
|
|
|
35
|
|
|
|
36
|
|
|
$git->remove($temporaryClone); |
37
|
|
|
|
38
|
|
|
self::assertDirectoryNotExists((string) $temporaryClone); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testCanCheckOutSameRevisionTwice() : void |
42
|
|
|
{ |
43
|
|
|
$git = new GitCheckoutRevisionToTemporaryPath(); |
44
|
|
|
$sourceRepository = $this->sourceRepository(); |
45
|
|
|
$revision = Revision::fromSha1(self::TEST_REVISION_TO_CHECKOUT); |
46
|
|
|
|
47
|
|
|
$first = $git->checkout($sourceRepository, $revision); |
48
|
|
|
$second = $git->checkout($sourceRepository, $revision); |
49
|
|
|
|
50
|
|
|
self::assertDirectoryExists((string) $first); |
51
|
|
|
self::assertDirectoryExists((string) $second); |
52
|
|
|
|
53
|
|
|
$git->remove($first); |
54
|
|
|
$git->remove($second); |
55
|
|
|
|
56
|
|
|
self::assertDirectoryNotExists((string) $first); |
57
|
|
|
self::assertDirectoryNotExists((string) $second); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testCheckedOutRevisionIsAtExpectedRevisionState() : void |
61
|
|
|
{ |
62
|
|
|
$repoPath = tempnam(sys_get_temp_dir(), 'test-git-repo-'); |
63
|
|
|
|
64
|
|
|
unlink($repoPath); |
65
|
|
|
mkdir($repoPath); |
66
|
|
|
|
67
|
|
|
(new Process(['git', 'init'], $repoPath)) |
68
|
|
|
->mustRun(); |
69
|
|
|
|
70
|
|
|
(new Process(['git', 'config', 'user.email', '[email protected]'], $repoPath)) |
71
|
|
|
->mustRun(); |
72
|
|
|
|
73
|
|
|
(new Process(['git', 'config', 'user.name', 'Mr Magoo'], $repoPath)) |
74
|
|
|
->mustRun(); |
75
|
|
|
|
76
|
|
|
(new Process(['git', 'commit', '-m', 'initial commit', '--allow-empty'], $repoPath)) |
77
|
|
|
->mustRun(); |
78
|
|
|
|
79
|
|
|
$firstCommit = Revision::fromSha1( |
80
|
|
|
(new Process(['git', 'rev-parse', 'HEAD'], $repoPath)) |
81
|
|
|
->mustRun() |
82
|
|
|
->getOutput() |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
file_put_contents($repoPath . '/a-file.txt', 'file contents'); |
86
|
|
|
|
87
|
|
|
(new Process(['git', 'add', 'a-file.txt'], $repoPath)) |
88
|
|
|
->mustRun(); |
89
|
|
|
|
90
|
|
|
(new Process(['git', 'commit', '-m', 'second commit', '--allow-empty'], $repoPath)) |
91
|
|
|
->mustRun(); |
92
|
|
|
|
93
|
|
|
$secondCommit = Revision::fromSha1( |
94
|
|
|
(new Process(['git', 'rev-parse', 'HEAD'], $repoPath)) |
95
|
|
|
->mustRun() |
96
|
|
|
->getOutput() |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$git = new GitCheckoutRevisionToTemporaryPath(); |
100
|
|
|
|
101
|
|
|
$sourceRepository = CheckedOutRepository::fromPath($repoPath); |
102
|
|
|
$first = $git->checkout($sourceRepository, $firstCommit); |
103
|
|
|
$second = $git->checkout($sourceRepository, $secondCommit); |
104
|
|
|
|
105
|
|
|
self::assertFileNotExists($first->__toString() . '/a-file.txt'); |
|
|
|
|
106
|
|
|
self::assertFileExists($second->__toString() . '/a-file.txt'); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$git->remove($first); |
109
|
|
|
$git->remove($second); |
110
|
|
|
|
111
|
|
|
(new Process(['rm', '-rf', $repoPath]))->mustRun(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testExceptionIsThrownWhenTwoPathsCollide() : void |
115
|
|
|
{ |
116
|
|
|
$git = new GitCheckoutRevisionToTemporaryPath(static function () : string { |
117
|
|
|
return 'foo'; |
118
|
|
|
}); |
119
|
|
|
$sourceRepository = $this->sourceRepository(); |
120
|
|
|
$revision = Revision::fromSha1(self::TEST_REVISION_TO_CHECKOUT); |
121
|
|
|
|
122
|
|
|
$first = $git->checkout($sourceRepository, $revision); |
123
|
|
|
|
124
|
|
|
$successfullyCheckedOutSecondClone = false; |
125
|
|
|
|
126
|
|
|
try { |
127
|
|
|
$second = $git->checkout($sourceRepository, $revision); |
128
|
|
|
$successfullyCheckedOutSecondClone = true; |
129
|
|
|
$git->remove($second); |
130
|
|
|
} catch (RuntimeException $runtimeException) { |
131
|
|
|
self::assertStringMatchesFormat( |
|
|
|
|
132
|
|
|
'Tried to check out revision "%s" to directory "%s" which already exists', |
133
|
|
|
$runtimeException->getMessage() |
134
|
|
|
); |
135
|
|
|
} finally { |
136
|
|
|
$git->remove($first); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
self::assertFalse($successfullyCheckedOutSecondClone); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function sourceRepository() : CheckedOutRepository |
143
|
|
|
{ |
144
|
|
|
return CheckedOutRepository::fromPath(realpath(__DIR__ . '/../../..')); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.