1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of CaptainHook |
5
|
|
|
* |
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CaptainHook\App\Hook\Template; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Config\Run; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use CaptainHook\App\Config\Mockery as ConfigMockery; |
17
|
|
|
|
18
|
|
|
class DockerTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
use ConfigMockery; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Tests Docker::getCode |
24
|
|
|
*/ |
25
|
|
|
public function testTemplateCaptainHookDevelopment(): void |
26
|
|
|
{ |
27
|
|
|
$repo = realpath(CH_PATH_FILES . '/template-ch'); |
28
|
|
|
$config = $repo . '/captainhook.json'; |
29
|
|
|
$executable = $repo . '/does/not/matter'; |
30
|
|
|
$pathInfo = new PathInfo($repo, $config, $executable, false); |
31
|
|
|
|
32
|
|
|
$configMock = $this->createConfigMock(false, $repo . '/captainhook.json'); |
33
|
|
|
$runConfig = new Run(['mode' => 'docker', 'exec' => 'docker exec cap-container', 'path' => '']); |
34
|
|
|
$configMock->method('getBootstrap')->willReturn('vendor/autoload.php'); |
35
|
|
|
$configMock->method('getRunConfig')->willReturn($runConfig); |
36
|
|
|
|
37
|
|
|
$template = new Docker($pathInfo, $configMock); |
38
|
|
|
$code = $template->getCode('commit-msg'); |
39
|
|
|
|
40
|
|
|
$this->assertStringContainsString('#!/bin/sh', $code); |
41
|
|
|
$this->assertStringContainsString('docker exec -i cap-container', $code); |
42
|
|
|
$this->assertStringContainsString('./bin/captainhook', $code); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Tests Docker::getCode |
47
|
|
|
*/ |
48
|
|
|
public function testTemplateCaptainHookAsLibrary(): void |
49
|
|
|
{ |
50
|
|
|
$pathInfo = $this->createMock(PathInfo::class); |
51
|
|
|
$pathInfo->method('getExecutablePath')->willReturn('./vendor/bin/captainhook'); |
52
|
|
|
$pathInfo->method('getConfigPath')->willReturn('captainhook.json'); |
53
|
|
|
|
54
|
|
|
$configMock = $this->createConfigMock(false, 'captainhook.json'); |
55
|
|
|
$runConfig = new Run(['mode' => 'docker', 'exec' => 'docker exec cap-container', 'path' => '']); |
56
|
|
|
$configMock->method('getBootstrap')->willReturn(''); |
57
|
|
|
$configMock->method('getRunConfig')->willReturn($runConfig); |
58
|
|
|
|
59
|
|
|
$template = new Docker($pathInfo, $configMock); |
60
|
|
|
$code = $template->getCode('commit-msg'); |
61
|
|
|
|
62
|
|
|
$this->assertStringContainsString('#!/bin/sh', $code); |
63
|
|
|
$this->assertStringContainsString('docker exec -i cap-container', $code); |
64
|
|
|
$this->assertStringContainsString('./vendor/bin/captainhook', $code); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Tests Docker::getCode |
69
|
|
|
*/ |
70
|
|
|
public function testTemplateCustomPath(): void |
71
|
|
|
{ |
72
|
|
|
$repo = realpath(CH_PATH_FILES . '/template-ch'); |
73
|
|
|
$executable = $repo . '/does/not/matter'; |
74
|
|
|
$config = $repo . '/captainhook.json'; |
75
|
|
|
$pathInfo = new PathInfo($repo, $config, $executable, false); |
76
|
|
|
|
77
|
|
|
$configMock = $this->createConfigMock(false, $repo . '/captainhook.json'); |
78
|
|
|
$runConfig = new Run( |
79
|
|
|
['mode' => 'docker', 'exec' => 'docker exec cap-container', 'path' => './foo/captainhook'] |
80
|
|
|
); |
81
|
|
|
$configMock->method('getBootstrap')->willReturn('vendor/autoload.php'); |
82
|
|
|
$configMock->method('getRunConfig')->willReturn($runConfig); |
83
|
|
|
|
84
|
|
|
$template = new Docker($pathInfo, $configMock); |
85
|
|
|
$code = $template->getCode('commit-msg'); |
86
|
|
|
|
87
|
|
|
$this->assertStringContainsString('#!/bin/sh', $code); |
88
|
|
|
$this->assertStringContainsString('docker exec -i cap-container', $code); |
89
|
|
|
$this->assertStringContainsString('./foo/captainhook', $code); |
90
|
|
|
$this->assertStringContainsString('bootstrap=vendor/autoload.php', $code); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Tests Docker::getCode |
95
|
|
|
* |
96
|
|
|
* @dataProvider replacementPossibilitiesWithoutGitMapping |
97
|
|
|
*/ |
98
|
|
|
public function testDockerCommandOptimizationWithoutGitMapping(string $exec, string $expected, string $msg): void |
99
|
|
|
{ |
100
|
|
|
$pathInfo = $this->createMock(PathInfo::class); |
101
|
|
|
$pathInfo->method('getExecutablePath')->willReturn('./vendor/bin/captainhook'); |
102
|
|
|
$pathInfo->method('getConfigPath')->willReturn('captainhook.json'); |
103
|
|
|
|
104
|
|
|
$configMock = $this->createConfigMock(false, 'captainhook.json'); |
105
|
|
|
$runConfig = new Run([ |
106
|
|
|
'mode' => 'docker', |
107
|
|
|
'exec' => 'docker exec ' . $exec, |
108
|
|
|
'path' => '/usr/local/bin/captainhook' |
109
|
|
|
]); |
110
|
|
|
$configMock->method('getBootstrap')->willReturn(''); |
111
|
|
|
$configMock->method('getRunConfig')->willReturn($runConfig); |
112
|
|
|
|
113
|
|
|
$template = new Docker($pathInfo, $configMock); |
114
|
|
|
$code = $template->getCode('prepare-commit-msg'); |
115
|
|
|
|
116
|
|
|
$this->assertStringContainsString('docker exec ' . $expected, $code, $msg); |
117
|
|
|
$this->assertStringContainsString('/usr/local/bin/captainhook', $code); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* The testDockerCommandOptimization data provider |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function replacementPossibilitiesWithoutGitMapping(): array |
126
|
|
|
{ |
127
|
|
|
return [ |
128
|
|
|
['cap-container', '-i cap-container', 'none'], |
129
|
|
|
['-it cap-container', '-it cap-container', '-it'], |
130
|
|
|
['-ti cap-container', '-ti cap-container', '-ti'], |
131
|
|
|
['--interactive --tty cap-container', '--interactive --tty cap-container', 'long it'], |
132
|
|
|
['--tty --interactive cap-container', '--tty --interactive cap-container', 'long ti'], |
133
|
|
|
['--tty cap-container', '-i --tty cap-container', 'no i'], |
134
|
|
|
['-xit cap-container', '-xit cap-container', 'prefixed i'], |
135
|
|
|
['-xit cap-container', '-xit cap-container', 'prefixed e'], |
136
|
|
|
['cap-container', '-i cap-container', 'long e'], |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Tests Docker::getCode |
142
|
|
|
* |
143
|
|
|
* @dataProvider replacementPossibilitiesWithGitMapping |
144
|
|
|
*/ |
145
|
|
|
public function testDockerCommandOptimizationWithGitMapping(string $exec, string $expected, string $msg): void |
146
|
|
|
{ |
147
|
|
|
$pathInfo = $this->createMock(PathInfo::class); |
148
|
|
|
$pathInfo->method('getExecutablePath')->willReturn('./vendor/bin/captainhook'); |
149
|
|
|
$pathInfo->method('getConfigPath')->willReturn('captainhook.json'); |
150
|
|
|
|
151
|
|
|
$configMock = $this->createConfigMock(false, 'captainhook.json'); |
152
|
|
|
$runConfig = new Run([ |
153
|
|
|
'mode' => 'docker', |
154
|
|
|
'exec' => 'docker exec ' . $exec, |
155
|
|
|
'path' => '/usr/local/bin/captainhook', |
156
|
|
|
'git' => '/docker/.git' |
157
|
|
|
]); |
158
|
|
|
$configMock->method('getBootstrap')->willReturn(''); |
159
|
|
|
$configMock->method('getRunConfig')->willReturn($runConfig); |
160
|
|
|
|
161
|
|
|
$template = new Docker($pathInfo, $configMock); |
162
|
|
|
$code = $template->getCode('prepare-commit-msg'); |
163
|
|
|
$codePush = $template->getCode('pre-push'); |
164
|
|
|
|
165
|
|
|
$this->assertStringContainsString('docker exec' . $expected, $code, $msg); |
166
|
|
|
$this->assertStringContainsString('/usr/local/bin/captainhook', $code); |
167
|
|
|
$this->assertStringNotContainsString('docker exec' . $expected, $codePush, $msg); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* The testDockerCommandOptimization data provider |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public function replacementPossibilitiesWithGitMapping(): array |
176
|
|
|
{ |
177
|
|
|
$env = ' -e GIT_INDEX_FILE="/docker/.git/$(basename $GIT_INDEX_FILE)"'; |
178
|
|
|
return [ |
179
|
|
|
['cap-container', ' -i' . $env . ' cap-container', 'none'], |
180
|
|
|
['-it cap-container', $env . ' -it cap-container', '-it'], |
181
|
|
|
['-ti cap-container', $env . ' -ti cap-container', '-ti'], |
182
|
|
|
['--interactive --tty cap-container', $env . ' --interactive --tty cap-container', 'long it'], |
183
|
|
|
['--tty --interactive cap-container', $env . ' --tty --interactive cap-container', 'long ti'], |
184
|
|
|
['--tty cap-container', ' -i' . $env . ' --tty cap-container', 'no i'], |
185
|
|
|
['-xit cap-container', $env . ' -xit cap-container', 'prefixed i'], |
186
|
|
|
['-xit cap-container', $env . ' -xit cap-container', 'prefixed e'], |
187
|
|
|
['cap-container', ' -i' . $env . ' cap-container', 'long e'], |
188
|
|
|
]; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|