Skip to content

Commit

Permalink
Fixes #2697: Remove disabled git hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
malikkotob committed Apr 6, 2018
1 parent 1afafad commit 9d0273e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 11 deletions.
22 changes: 18 additions & 4 deletions src/Robo/Commands/Setup/SettingsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ public function gitHooks() {
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function installGitHook($hook) {
$fs = new Filesystem();
$project_hook_directory = $this->getConfigValue('repo.root') . "/.git/hooks";
$project_hook = $project_hook_directory . "/$hook";
if ($this->getConfigValue('git.hooks.' . $hook)) {
$this->say("Installing $hook git hook...");
$hook_source = $this->getConfigValue('git.hooks.' . $hook) . "/$hook";
$project_hook_directory = $this->getConfigValue('repo.root') . "/.git/hooks";
$fs = new Filesystem();
$path_to_hook_source = rtrim($fs->makePathRelative($hook_source, $project_hook_directory), '/');
$project_hook = $project_hook_directory . "/$hook";

$result = $this->taskFilesystemStack()
->mkdir($this->getConfigValue('repo.root') . '/.git/hooks')
Expand All @@ -236,7 +236,21 @@ protected function installGitHook($hook) {
}
}
else {
$this->say("Skipping installation of $hook git hook");
if (file_exists($project_hook)) {
$this->say("Removing disabled $hook git hook...");
$result = $this->taskFilesystemStack()
->remove($project_hook)
->stopOnFail()
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();

if (!$result->wasSuccessful()) {
throw new BltException("Unable to remove disabled $hook git hook");
}
}
else {
$this->say("Skipping installation of $hook git hook...");
}
}
}

Expand Down
71 changes: 64 additions & 7 deletions tests/phpunit/BltProject/SetupGitHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,52 @@
class GitTasksTest extends BltProjectTestBase {

/**
* Tests blt:init:git-hooks command.
* Tests setup of git hooks via blt:init:git-hooks command.
*
* @dataProvider disabledHooksProvider
*/
public function testGitConfig() {
public function testGitHookSetup($disabled_hooks) {
$this->assertFileExists($this->sandboxInstance . '/.git');

foreach ($disabled_hooks as $disabled_hook) {
$this->config->set("git.hooks.${$disabled_hook}", FALSE);
}

$this->blt("blt:init:git-hooks");

$hooks = $this->config->get('git.hooks');
$this->assertGitHookSetupValidity($hooks, $disabled_hooks);
}

/**
* Tests removal of disabled git hooks via blt:init:git-hooks command.
*
* @dataProvider disabledHooksProvider
*/
public function testDisabledGitHookRemoval($disabled_hooks) {
$this->assertFileExists($this->sandboxInstance . '/.git');
foreach ($this->config->get('git.hooks') as $hook => $path) {
$project_hook = $this->sandboxInstance . '/.git/hooks' . "/$hook";
$this->assertFileExists($project_hook);
$source_hook = readlink($project_hook);
$this->assertFileExists($source_hook);

$this->blt("blt:init:git-hooks");
$hooks = $this->config->get('git.hooks');

foreach ($disabled_hooks as $disabled_hook) {
$this->config->set("git.hooks.${$disabled_hook}", FALSE);
}

$this->blt("blt:init:git-hooks");
$this->assertGitHookSetupValidity($hooks, $disabled_hooks);
}

/**
* Data provider.
*/
public function disabledHooksProvider() {
return [
[[]],
[['pre-commit']],
[['commit-msg']],
[['pre-commit', 'commit-msg']],
];
}

/**
Expand Down Expand Up @@ -78,6 +113,28 @@ public function testGitPreCommitHook() {
$this->assertContains('tests:twig:lint:files', $output);
}

/**
* Asserts that the given hooks were setup in a valid manner.
*
* @param array $hooks
* The possible git hooks provided by BLT.
* @param array $disabled_hooks
* The disabled git hooks.
*/
protected function assertGitHookSetupValidity(array $hooks, array $disabled_hooks) {
foreach ($hooks as $hook => $path) {
$project_hook = $this->sandboxInstance . '/.git/hooks' . "/$hook";
if (array_key_exists($hook, $disabled_hooks)) {
$this->assertFileNotExists($project_hook, "Failed asserting that the disabled {$hook} hook was not setup.");
}
else {
$this->assertFileExists($project_hook, "Failed asserting that the enabled {$hook} hook was setup.");
$source_hook = readlink($project_hook);
$this->assertFileExists($source_hook, "Failed asserting that the enabled {$hook} hook was setup properly.");
}
}
}

/**
* Asserts that a given commit message is valid or not.
*
Expand Down

0 comments on commit 9d0273e

Please sign in to comment.