Skip to content

Commit 636ca47

Browse files
Add an option to disable all plugins
All hook commands now support the --no-plugins option.
1 parent 03c4ea6 commit 636ca47

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

src/Console/Command/Hook.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ protected function configure(): void
6262
InputOption::VALUE_OPTIONAL,
6363
'Original hook stdIn'
6464
);
65+
$this->addOption(
66+
'no-plugins',
67+
null,
68+
InputOption::VALUE_NONE,
69+
'Disable all hook plugins'
70+
);
6571
}
6672

6773
/**
@@ -70,7 +76,7 @@ protected function configure(): void
7076
* @param \Symfony\Component\Console\Input\InputInterface $input
7177
* @param \Symfony\Component\Console\Output\OutputInterface $output
7278
* @return int
73-
* @throws \Exception
79+
* @throws \Throwable
7480
*/
7581
protected function execute(InputInterface $input, OutputInterface $output): int
7682
{
@@ -94,6 +100,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
94100
$class = '\\CaptainHook\\App\\Runner\\Hook\\' . Util::getHookCommand($this->hookName);
95101
/** @var \CaptainHook\App\Runner\Hook $hook */
96102
$hook = new $class($io, $config, $repository);
103+
$hook->setPluginsDisabled($input->getOption('no-plugins'));
97104
$hook->run();
98105

99106
return 0;
@@ -124,8 +131,8 @@ private function handleBootstrap(Config $config): void
124131
require $bootstrapFile;
125132
} catch (Throwable $t) {
126133
throw new RuntimeException(
127-
'Bootstrapping failed:' . PHP_EOL .
128-
' Please fix your bootstrap file `' . $bootstrapFile . '`' . PHP_EOL
134+
'Loading bootstrap file failed: ' . $bootstrapFile . PHP_EOL .
135+
$t->getMessage() . PHP_EOL
129136
);
130137
}
131138
}

src/Runner/Hook.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ abstract class Hook extends RepositoryAware
8282
*/
8383
private HookLog $hookLog;
8484

85+
/**
86+
* Should the plugins be disabled?
87+
*
88+
* @var boolean
89+
*/
90+
private bool $pluginsDisabled = false;
91+
92+
/**
93+
* Set up the hook runner
94+
*
95+
* @param \CaptainHook\App\Console\IO $io
96+
* @param \CaptainHook\App\Config $config
97+
* @param \SebastianFeldmann\Git\Repository $repository
98+
*/
8599
public function __construct(IO $io, Config $config, Repository $repository)
86100
{
87101
$this->dispatcher = new Dispatcher($io, $config, $repository);
@@ -91,6 +105,17 @@ public function __construct(IO $io, Config $config, Repository $repository)
91105
parent::__construct($io, $config, $repository);
92106
}
93107

108+
/**
109+
* Allow plugins to be disabled
110+
*
111+
* @param bool $disabled
112+
* @return void
113+
*/
114+
public function setPluginsDisabled(bool $disabled): void
115+
{
116+
$this->pluginsDisabled = $disabled;
117+
}
118+
94119
/**
95120
* Return this hook's name
96121
*
@@ -158,6 +183,10 @@ public function run(): void
158183
return;
159184
}
160185

186+
if ($this->pluginsDisabled) {
187+
$this->io->write('<fg=magenta>Running with plugins disabled</>');
188+
}
189+
161190
$this->checkHookScript();
162191

163192
$this->beforeHook();
@@ -412,6 +441,10 @@ private function setupHookPlugins(): array
412441
*/
413442
private function executeHookPluginsFor(string $method, ?Config\Action $action = null): void
414443
{
444+
if ($this->pluginsDisabled) {
445+
return;
446+
}
447+
415448
$plugins = $this->getHookPlugins();
416449

417450
if (count($plugins) === 0) {

tests/unit/Runner/HookTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,35 @@ public function testRunHookSkipsActionsFromPluginBeforeAction(): void
252252
$this->assertSame(3, DummyHookPlugin::$afterActionCalled);
253253
$this->assertSame(1, DummyHookPlugin::$afterHookCalled);
254254
}
255+
256+
public function testRunHookWhenPluginsAreDisabled(): void
257+
{
258+
$pluginConfig1 = new Config\Plugin(DummyHookPlugin::class);
259+
$pluginConfig2 = new Config\Plugin(DummyHookPlugin::class);
260+
261+
$config = $this->createConfigMock();
262+
$config->method('failOnFirstError')->willReturn(false);
263+
$config->method('getPlugins')->willReturn([$pluginConfig1, $pluginConfig2]);
264+
265+
$io = $this->createIOMock();
266+
$repo = $this->createRepositoryMock();
267+
$hookConfig = $this->createHookConfigMock();
268+
$actionConfig = $this->createActionConfigMock();
269+
$actionConfig->expects($this->atLeastOnce())->method('getAction')->willReturn(CH_PATH_FILES . '/bin/success');
270+
$hookConfig->method('isEnabled')->willReturn(true);
271+
$hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig, clone $actionConfig]);
272+
$config->method('isHookEnabled')->willReturn(true);
273+
$config->expects($this->once())->method('getHookConfigToExecute')->willReturn($hookConfig);
274+
275+
$runner = new class ($io, $config, $repo) extends Hook {
276+
protected string $hook = Hooks::PRE_COMMIT;
277+
};
278+
$runner->setPluginsDisabled(true);
279+
$runner->run();
280+
281+
$this->assertSame(0, DummyHookPlugin::$beforeHookCalled);
282+
$this->assertSame(0, DummyHookPlugin::$beforeActionCalled);
283+
$this->assertSame(0, DummyHookPlugin::$afterActionCalled);
284+
$this->assertSame(0, DummyHookPlugin::$afterHookCalled);
285+
}
255286
}

0 commit comments

Comments
 (0)