Skip to content

Commit 32b5738

Browse files
author
Matthieu CODRON
committed
introduce Config::isHookEnabled
1 parent 935d8f4 commit 32b5738

File tree

11 files changed

+56
-22
lines changed

11 files changed

+56
-22
lines changed

src/Config.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,30 @@ public function isFailureAllowed(): bool
158158
return (bool) ($this->settings[self::SETTING_ALLOW_FAILURE] ?? false);
159159
}
160160

161+
/**
162+
* @param string $hook
163+
* @param bool $withVirtual if true, also check if hook is enabled through any enabled virtual hook
164+
* @return bool
165+
*/
166+
public function isHookEnabled(string $hook, bool $withVirtual = true): bool
167+
{
168+
//Either this hook is explicitely enabled
169+
$hookConfig = $this->getHookConfig($hook);
170+
if ($hookConfig->isEnabled()) {
171+
return true;
172+
}
173+
174+
//Or any virtual hook that triggers it is enabled
175+
if ($withVirtual && Hooks::triggersVirtualHook($hookConfig->getName())) {
176+
$virtualHookConfig = $this->getHookConfig(Hooks::getVirtualHook($hookConfig->getName()));
177+
if ($virtualHookConfig->isEnabled()) {
178+
return true;
179+
}
180+
}
181+
182+
return false;
183+
}
184+
161185
/**
162186
* Path getter
163187
*

src/Runner/Hook.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,7 @@ public function run(): void
137137

138138
$this->io->write('<comment>' . $this->hook . ':</comment> ');
139139

140-
// if the hook and all triggered virtual hooks
141-
// are NOT enabled in the captainhook configuration skip the execution
142-
if (!$this->isAnyConfigEnabled($hookConfigs)) {
140+
if(!$this->config->isHookEnabled($this->hook)) {
143141
$this->io->write(' - hook is disabled');
144142
return;
145143
}
@@ -186,20 +184,6 @@ public function getHookConfigsToHandle(): array
186184
return $configs;
187185
}
188186

189-
/**
190-
* @param \CaptainHook\App\Config\Hook[] $configs
191-
* @return bool
192-
*/
193-
private function isAnyConfigEnabled(array $configs): bool
194-
{
195-
foreach ($configs as $hookConfig) {
196-
if ($hookConfig->isEnabled()) {
197-
return true;
198-
}
199-
}
200-
return false;
201-
}
202-
203187
/**
204188
* Returns `true` if something has indicated that the hook should skip all
205189
* remaining actions; pass a boolean value to toggle this

tests/unit/Runner/Hook/CommitMsgTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function testRunHookEnabled(): void
4848
$hookConfig->expects($this->atLeast(1))->method('isEnabled')->willReturn(true);
4949
$hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
5050
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
51+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
5152
$io->expects($this->atLeast(1))->method('write');
5253
$io->expects($this->once())->method('getArgument')->willReturn(CH_PATH_FILES . '/git/message/valid.txt');
5354

@@ -79,6 +80,7 @@ public function testRunHookSkippedBecauseOfFixup(): void
7980
$hookConfig->method('isEnabled')->willReturn(true);
8081
$hookConfig->method('getActions')->willReturn([$actionConfig]);
8182
$config->method('getHookConfig')->willReturn($hookConfig);
83+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
8284
$io->expects($this->atLeast(1))->method('write');
8385
$io->expects($this->once())->method('getArgument')->willReturn(CH_PATH_FILES . '/git/message/valid.txt');
8486

@@ -105,6 +107,7 @@ public function testRunWithoutCommitMsgFile(): void
105107
$hookConfig->method('isEnabled')->willReturn(true);
106108
$hookConfig->method('getActions')->willReturn([$actionConfig]);
107109
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
110+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
108111
$io->expects($this->once())->method('getArgument')->willReturn('');
109112

110113
$runner = new CommitMsg($io, $config, $repo);

tests/unit/Runner/Hook/PostCheckoutTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function testRunHookEnabled(): void
3838
$hookConfig->expects($this->atLeast(1))->method('isEnabled')->willReturn(true);
3939
$hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig1, $actionConfig2]);
4040
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
41+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
4142
$io->expects($this->atLeast(1))->method('write');
4243

4344
// Ensure that our actions are processed.
@@ -61,8 +62,8 @@ public function testRunHookDisabled(): void
6162
$config = $this->createConfigMock();
6263
$hookConfig = $this->createHookConfigMock();
6364
$repo = $this->createRepositoryMock();
64-
$hookConfig->expects($this->atLeast(1))->method('isEnabled')->willReturn(false);
6565
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
66+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(false);
6667
$io->expects($this->atLeast(1))->method('write');
6768

6869
$runner = new PostCheckout($io, $config, $repo);

tests/unit/Runner/Hook/PostCommitTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function testRunHookEnabled(): void
4242
$hookConfig->expects($this->atLeast(1))->method('isEnabled')->willReturn(true);
4343
$hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
4444
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
45+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
4546
$io->expects($this->atLeast(1))->method('write');
4647

4748
$runner = new PostCommit($io, $config, $repo);
@@ -59,8 +60,8 @@ public function testRunHookDisabled(): void
5960
$config = $this->createConfigMock();
6061
$hookConfig = $this->createHookConfigMock();
6162
$repo = $this->createRepositoryMock();
62-
$hookConfig->expects($this->atLeast(1))->method('isEnabled')->willReturn(false);
6363
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
64+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(false);
6465
$io->expects($this->atLeast(1))->method('write');
6566

6667
$runner = new PostCommit($io, $config, $repo);

tests/unit/Runner/Hook/PostMergeTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function testRunHookEnabled(): void
4242
$hookConfig->expects($this->atLeast(1))->method('isEnabled')->willReturn(true);
4343
$hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
4444
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
45+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
4546
$io->expects($this->atLeast(1))->method('write');
4647

4748
$runner = new PostMerge($io, $config, $repo);
@@ -65,6 +66,7 @@ public function testRunHookWithConditionsApply(): void
6566
$hookConfig->expects($this->atLeast(1))->method('isEnabled')->willReturn(true);
6667
$hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
6768
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
69+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
6870
$io->expects($this->atLeast(1))->method('write');
6971

7072
$runner = new PostMerge($io, $config, $repo);
@@ -89,6 +91,7 @@ public function testRunHookWithConditionsFail()
8991
$hookConfig->expects($this->atLeast(1))->method('isEnabled')->willReturn(true);
9092
$hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
9193
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
94+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
9295
$io->expects($this->atLeast(1))->method('write');
9396

9497
$runner = new PostMerge($io, $config, $repo);
@@ -106,8 +109,8 @@ public function testRunHookDisabled()
106109
$config = $this->createConfigMock();
107110
$hookConfig = $this->createHookConfigMock();
108111
$repo = $this->createRepositoryMock();
109-
$hookConfig->expects($this->atLeast(1))->method('isEnabled')->willReturn(false);
110112
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
113+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(false);
111114
$io->expects($this->atLeast(1))->method('write');
112115

113116
$runner = new PostMerge($io, $config, $repo);

tests/unit/Runner/Hook/PostRewriteTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public function testRunHookEnabled(): void
5151
->method('getHookConfig')
5252
->willReturnOnConsecutiveCalls($hookConfig, $vHookConfig);
5353

54+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
55+
56+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
57+
5458
$io->expects($this->atLeast(1))->method('write');
5559

5660
$runner = new PostRewrite($io, $config, $repo);
@@ -84,6 +88,10 @@ public function testRunVirtualHookDisabled(): void
8488
->method('getHookConfig')
8589
->willReturnOnConsecutiveCalls($hookConfig, $vHookConfig);
8690

91+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturnCallback(function($hook){
92+
return $hook === 'post-rewrite';
93+
});
94+
8795
$io->expects($this->atLeast(1))->method('write');
8896

8997
$runner = new PostRewrite($io, $config, $repo);

tests/unit/Runner/Hook/PreCommitTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function testRunHookEnabled(): void
4747
$hookConfig->expects($this->atLeast(1))->method('isEnabled')->willReturn(true);
4848
$hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
4949
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
50+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
5051
$io->expects($this->atLeast(1))->method('write');
5152

5253
$runner = new PreCommit($io, $config, $repo);
@@ -91,6 +92,7 @@ public function testRunHookDontFailOnFirstError(): void
9192
->willReturn([$actionConfigFail, $actionConfigSuccess]);
9293

9394
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
95+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
9496
$io->expects($this->atLeast(1))->method('write');
9597

9698
$runner = new PreCommit($io, $config, $repo);
@@ -127,6 +129,7 @@ public function testRunHookDontFailEvenOnExceptions(): void
127129
->willReturn([$actionConfigFail, $actionConfigSuccess]);
128130

129131
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
132+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
130133
$io->expects($this->atLeast(1))->method('write');
131134

132135
$runner = new PreCommit($io, $config, $repo);
@@ -167,6 +170,7 @@ public function testRunHookAllowFailureGlobally(): void
167170
->willReturn([$actionConfigFail, $actionConfigSuccess]);
168171

169172
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
173+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
170174
$io->expects($this->atLeast(1))->method('write');
171175

172176
$runner = new PreCommit($io, $config, $repo);
@@ -184,8 +188,8 @@ public function testRunHookDisabled(): void
184188
$config = $this->createConfigMock();
185189
$hookConfig = $this->createHookConfigMock();
186190
$repo = $this->createRepositoryMock();
187-
$hookConfig->expects($this->once())->method('isEnabled')->willReturn(false);
188191
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
192+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(false);
189193
$io->expects($this->atLeast(1))->method('write');
190194

191195
$runner = new PreCommit($io, $config, $repo);

tests/unit/Runner/Hook/PrePushTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function testRunHookEnabled(): void
3737
$hookConfig->expects($this->atLeast(1))->method('isEnabled')->willReturn(true);
3838
$hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
3939
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
40+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
4041
$io->expects($this->atLeast(1))->method('write');
4142

4243
$runner = new PrePush($io, $config, $repo);
@@ -54,8 +55,8 @@ public function testRunHookDisabled(): void
5455
$config = $this->createConfigMock();
5556
$hookConfig = $this->createHookConfigMock();
5657
$repo = $this->createRepositoryMock();
57-
$hookConfig->expects($this->atLeast(1))->method('isEnabled')->willReturn(false);
5858
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
59+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(false);
5960
$io->expects($this->atLeast(1))->method('write');
6061

6162
$runner = new PrePush($io, $config, $repo);

tests/unit/Runner/Hook/PrepareCommitMsgTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function testRunHookEnabled(): void
4343
$hookConfig->expects($this->atLeast(1))->method('isEnabled')->willReturn(true);
4444
$hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
4545
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
46+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
4647

4748
// setup fake vfs repos directory
4849
$repoDir = new DummyRepo();
@@ -82,6 +83,7 @@ public function testRunHookNoMessageException(): void
8283
$hookConfig->method('isEnabled')->willReturn(true);
8384
$hookConfig->method('getActions')->willReturn([$actionConfig]);
8485
$config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
86+
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
8587
$io->expects($this->exactly(3))->method('getArgument')->willReturn('');
8688

8789
$runner = new PrepareCommitMsg($io, $config, $repo);

0 commit comments

Comments
 (0)