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\Console\Command; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Console\IO\NullIO; |
15
|
|
|
use CaptainHook\App\Console\Runtime\Resolver; |
16
|
|
|
use CaptainHook\App\Git\DummyRepo; |
17
|
|
|
use CaptainHook\App\Hook\Template; |
18
|
|
|
use Exception; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
21
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
22
|
|
|
|
23
|
|
|
class InstallTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
public function testFailMissingConfig(): void |
26
|
|
|
{ |
27
|
|
|
$output = new NullOutput(); |
28
|
|
|
$input = new ArrayInput( |
29
|
|
|
[ |
30
|
|
|
'hook' => 'pre-commit', |
31
|
|
|
'--configuration' => 'foo', |
32
|
|
|
'--git-directory' => 'bar' |
33
|
|
|
] |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
$install = new Install(new Resolver(CH_PATH_FILES . '/bin/captainhook')); |
37
|
|
|
$code = $install->run($input, $output); |
38
|
|
|
|
39
|
|
|
$this->assertEquals(1, $code); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testFailInvalidRepository(): void |
43
|
|
|
{ |
44
|
|
|
$output = new NullOutput(); |
45
|
|
|
$input = new ArrayInput( |
46
|
|
|
[ |
47
|
|
|
'hook' => 'pre-commit', |
48
|
|
|
'--configuration' => CH_PATH_FILES . '/config/valid.json', |
49
|
|
|
'--git-directory' => 'bar/.git' |
50
|
|
|
] |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$install = new Install(new Resolver(CH_PATH_FILES . '/bin/captainhook')); |
54
|
|
|
$install->setIO(new NullIO()); |
55
|
|
|
$code = $install->run($input, $output); |
56
|
|
|
|
57
|
|
|
$this->assertEquals(1, $code); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testFailMissingRunExecOption(): void |
61
|
|
|
{ |
62
|
|
|
$repo = new DummyRepo(); |
63
|
|
|
$output = new NullOutput(); |
64
|
|
|
$input = new ArrayInput( |
65
|
|
|
[ |
66
|
|
|
'hook' => 'pre-commit', |
67
|
|
|
'--configuration' => CH_PATH_FILES . '/template/captainhook.json', |
68
|
|
|
'--git-directory' => $repo->getGitDir(), |
69
|
|
|
'--run-mode' => Template::DOCKER |
70
|
|
|
] |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$install = new Install(new Resolver(CH_PATH_FILES . '/bin/captainhook')); |
74
|
|
|
$code = $install->run($input, $output); |
75
|
|
|
|
76
|
|
|
$this->assertEquals(1, $code); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testInstallPreCommitHook(): void |
80
|
|
|
{ |
81
|
|
|
$repo = new DummyRepo(); |
82
|
|
|
$output = new NullOutput(); |
83
|
|
|
$input = new ArrayInput( |
84
|
|
|
[ |
85
|
|
|
'hook' => 'pre-commit', |
86
|
|
|
'--configuration' => CH_PATH_FILES . '/template/captainhook.json', |
87
|
|
|
'--git-directory' => $repo->getGitDir() |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$install = new Install(new Resolver(CH_PATH_FILES . '/bin/captainhook')); |
92
|
|
|
$install->run($input, $output); |
93
|
|
|
|
94
|
|
|
$this->assertTrue($repo->hookExists('pre-commit')); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testInstallMultipleHooks(): void |
98
|
|
|
{ |
99
|
|
|
$repo = new DummyRepo(); |
100
|
|
|
$output = new NullOutput(); |
101
|
|
|
$input = new ArrayInput( |
102
|
|
|
[ |
103
|
|
|
'hook' => 'pre-commit,pre-push,post-checkout', |
104
|
|
|
'--configuration' => CH_PATH_FILES . '/template/captainhook.json', |
105
|
|
|
'--git-directory' => $repo->getGitDir(), |
106
|
|
|
] |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$install = new Install(new Resolver(CH_PATH_FILES . '/bin/captainhook')); |
110
|
|
|
$install->run($input, $output); |
111
|
|
|
|
112
|
|
|
$this->assertTrue($repo->hookExists('pre-commit')); |
113
|
|
|
$this->assertTrue($repo->hookExists('pre-push')); |
114
|
|
|
$this->assertTrue($repo->hookExists('post-checkout')); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testInstallMultipleHooksWithSpacesAfterAndBetweenSeparator(): void |
118
|
|
|
{ |
119
|
|
|
$repo = new DummyRepo(); |
120
|
|
|
$output = new NullOutput(); |
121
|
|
|
$input = new ArrayInput( |
122
|
|
|
[ |
123
|
|
|
'hook' => ' pre-commit , pre-push , post-checkout, post-commit', |
124
|
|
|
'--configuration' => CH_PATH_FILES . '/template/captainhook.json', |
125
|
|
|
'--git-directory' => $repo->getGitDir(), |
126
|
|
|
] |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$install = new Install(new Resolver(CH_PATH_FILES . '/bin/captainhook')); |
130
|
|
|
$install->run($input, $output); |
131
|
|
|
|
132
|
|
|
$this->assertTrue($repo->hookExists('pre-commit')); |
133
|
|
|
$this->assertTrue($repo->hookExists('pre-push')); |
134
|
|
|
$this->assertTrue($repo->hookExists('post-checkout')); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testInstallMultipleHooksWithOneWrong(): void |
138
|
|
|
{ |
139
|
|
|
$repo = new DummyRepo(); |
140
|
|
|
$output = new NullOutput(); |
141
|
|
|
$input = new ArrayInput( |
142
|
|
|
[ |
143
|
|
|
'hook' => 'pre-commit,pre-push,post-checkout,something-wrong', |
144
|
|
|
'--configuration' => CH_PATH_FILES . '/template/captainhook.json', |
145
|
|
|
'--git-directory' => $repo->getGitDir(), |
146
|
|
|
] |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
$install = new Install(new Resolver(CH_PATH_FILES . '/bin/captainhook')); |
150
|
|
|
$code = $install->run($input, $output); |
151
|
|
|
|
152
|
|
|
$this->assertEquals(1, $code); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testInstallMultipleHooksWithMultipleWrong(): void |
156
|
|
|
{ |
157
|
|
|
$repo = new DummyRepo(); |
158
|
|
|
$output = new NullOutput(); |
159
|
|
|
$input = new ArrayInput( |
160
|
|
|
[ |
161
|
|
|
'hook' => 'pre-commit,pre-push,post-checkout,something-wrong1,something-wrong2', |
162
|
|
|
'--configuration' => CH_PATH_FILES . '/template/captainhook.json', |
163
|
|
|
'--git-directory' => $repo->getGitDir(), |
164
|
|
|
] |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
$install = new Install(new Resolver(CH_PATH_FILES . '/bin/captainhook')); |
168
|
|
|
$code = $install->run($input, $output); |
169
|
|
|
|
170
|
|
|
$this->assertEquals(1, $code); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testInstallOnlyEnabled(): void |
174
|
|
|
{ |
175
|
|
|
$repo = new DummyRepo(); |
176
|
|
|
$output = new NullOutput(); |
177
|
|
|
$input = new ArrayInput( |
178
|
|
|
[ |
179
|
|
|
'--only-enabled' => true, |
180
|
|
|
'--force' => true, |
181
|
|
|
'--configuration' => CH_PATH_FILES . '/template/captainhook.json', |
182
|
|
|
'--git-directory' => $repo->getGitDir() |
183
|
|
|
] |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
$install = new Install(new Resolver(CH_PATH_FILES . '/bin/captainhook')); |
187
|
|
|
$install->run($input, $output); |
188
|
|
|
|
189
|
|
|
$this->assertTrue($repo->hookExists('prepare-commit-msg')); |
190
|
|
|
$this->assertTrue($repo->hookExists('commit-msg')); |
191
|
|
|
$this->assertTrue($repo->hookExists('pre-commit')); |
192
|
|
|
$this->assertFalse($repo->hookExists('pre-push')); |
193
|
|
|
$this->assertFalse($repo->hookExists('post-commit')); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testInstallOnlyEnabledOnlyVirtual(): void |
197
|
|
|
{ |
198
|
|
|
$repo = new DummyRepo(); |
199
|
|
|
$output = new NullOutput(); |
200
|
|
|
$input = new ArrayInput( |
201
|
|
|
[ |
202
|
|
|
'--only-enabled' => true, |
203
|
|
|
'--force' => true, |
204
|
|
|
'--configuration' => CH_PATH_FILES . '/template/captainhook-post-change.json', |
205
|
|
|
'--git-directory' => $repo->getGitDir() |
206
|
|
|
] |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
$install = new Install(new Resolver(CH_PATH_FILES . '/bin/captainhook')); |
210
|
|
|
$install->run($input, $output); |
211
|
|
|
|
212
|
|
|
$this->assertTrue($repo->hookExists('post-checkout')); |
213
|
|
|
$this->assertTrue($repo->hookExists('post-merge')); |
214
|
|
|
$this->assertTrue($repo->hookExists('post-rewrite')); |
215
|
|
|
$this->assertFalse($repo->hookExists('pre-commit')); |
216
|
|
|
$this->assertFalse($repo->hookExists('pre-push')); |
217
|
|
|
$this->assertFalse($repo->hookExists('post-commit')); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function testInstallOnlyEnabledNotOnlyVirtual(): void |
221
|
|
|
{ |
222
|
|
|
$repo = new DummyRepo(); |
223
|
|
|
$output = new NullOutput(); |
224
|
|
|
$input = new ArrayInput( |
225
|
|
|
[ |
226
|
|
|
'--only-enabled' => true, |
227
|
|
|
'--force' => true, |
228
|
|
|
'--configuration' => CH_PATH_FILES . '/template/captainhook-post-change-pre-commit.json', |
229
|
|
|
'--git-directory' => $repo->getGitDir() |
230
|
|
|
] |
231
|
|
|
); |
232
|
|
|
|
233
|
|
|
$install = new Install(new Resolver(CH_PATH_FILES . '/bin/captainhook')); |
234
|
|
|
$install->run($input, $output); |
235
|
|
|
|
236
|
|
|
$this->assertTrue($repo->hookExists('post-checkout')); |
237
|
|
|
$this->assertTrue($repo->hookExists('post-merge')); |
238
|
|
|
$this->assertTrue($repo->hookExists('post-rewrite')); |
239
|
|
|
$this->assertTrue($repo->hookExists('pre-commit')); |
240
|
|
|
$this->assertFalse($repo->hookExists('pre-push')); |
241
|
|
|
$this->assertFalse($repo->hookExists('post-commit')); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function testInstallOnlyEnabledNotOnlyVirtualOverlaps(): void |
245
|
|
|
{ |
246
|
|
|
$repo = new DummyRepo(); |
247
|
|
|
$output = new NullOutput(); |
248
|
|
|
$input = new ArrayInput( |
249
|
|
|
[ |
250
|
|
|
'--only-enabled' => true, |
251
|
|
|
'--force' => true, |
252
|
|
|
'--configuration' => CH_PATH_FILES . '/template/captainhook-post-change-post-merge.json', |
253
|
|
|
'--git-directory' => $repo->getGitDir() |
254
|
|
|
] |
255
|
|
|
); |
256
|
|
|
|
257
|
|
|
$install = new Install(new Resolver(CH_PATH_FILES . '/bin/captainhook')); |
258
|
|
|
$install->run($input, $output); |
259
|
|
|
|
260
|
|
|
$this->assertTrue($repo->hookExists('post-checkout')); |
261
|
|
|
$this->assertTrue($repo->hookExists('post-merge')); |
262
|
|
|
$this->assertTrue($repo->hookExists('post-rewrite')); |
263
|
|
|
$this->assertFalse($repo->hookExists('pre-commit')); |
264
|
|
|
$this->assertFalse($repo->hookExists('pre-push')); |
265
|
|
|
$this->assertFalse($repo->hookExists('post-commit')); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function testInstallOnlyEnabledNotOnlyVirtualOverlapsDisabled(): void |
269
|
|
|
{ |
270
|
|
|
$repo = new DummyRepo(); |
271
|
|
|
$output = new NullOutput(); |
272
|
|
|
$input = new ArrayInput( |
273
|
|
|
[ |
274
|
|
|
'--only-enabled' => true, |
275
|
|
|
'--force' => true, |
276
|
|
|
'--configuration' => CH_PATH_FILES . '/template/captainhook-post-change-post-merge-disabled.json', |
277
|
|
|
'--git-directory' => $repo->getGitDir() |
278
|
|
|
] |
279
|
|
|
); |
280
|
|
|
|
281
|
|
|
$install = new Install(new Resolver(CH_PATH_FILES . '/bin/captainhook')); |
282
|
|
|
$install->run($input, $output); |
283
|
|
|
|
284
|
|
|
$this->assertTrue($repo->hookExists('post-checkout')); |
285
|
|
|
$this->assertTrue($repo->hookExists('post-merge')); |
286
|
|
|
$this->assertTrue($repo->hookExists('post-rewrite')); |
287
|
|
|
$this->assertFalse($repo->hookExists('pre-commit')); |
288
|
|
|
$this->assertFalse($repo->hookExists('pre-push')); |
289
|
|
|
$this->assertFalse($repo->hookExists('post-commit')); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function testInstallOnlyEnabledAndHook(): void |
293
|
|
|
{ |
294
|
|
|
$repo = new DummyRepo(); |
295
|
|
|
$output = new NullOutput(); |
296
|
|
|
$input = new ArrayInput( |
297
|
|
|
[ |
298
|
|
|
'hook' => 'pre-commit', |
299
|
|
|
'--only-enabled' => true, |
300
|
|
|
'--configuration' => CH_PATH_FILES . '/template/captainhook.json', |
301
|
|
|
'--git-directory' => $repo->getGitDir(), |
302
|
|
|
] |
303
|
|
|
); |
304
|
|
|
|
305
|
|
|
$install = new Install(new Resolver(CH_PATH_FILES . '/bin/captainhook')); |
306
|
|
|
$code = $install->run($input, $output); |
307
|
|
|
|
308
|
|
|
$this->assertEquals(1, $code); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|