Skip to content

Commit

Permalink
Merge pull request #106 from ggcasuso/feature/execute-hooks-only-if-p…
Browse files Browse the repository at this point in the history
…revious-was-successful

Concat hooks with &&
  • Loading branch information
BrainMaestro authored Mar 11, 2020
2 parents 97888dd + a5d081d commit 676162c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Commands/AddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function addHook($hook, $contents)
// On windows, the shebang needs to point to bash
// See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
$shebang = ($this->windows ? '#!/bin/bash' : '#!/bin/sh') . PHP_EOL . PHP_EOL;
$contents = is_array($contents) ? implode(PHP_EOL, $contents) : $contents;
$contents = get_hook_contents($contents);
$hookContents = $shebang . $contents . PHP_EOL;

if (! $this->force && $exists) {
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/HookCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$contents = is_array($this->contents) ? implode(PHP_EOL, $this->contents) : $this->contents;
$contents = get_hook_contents($this->contents);

$outputMessage = [];
$returnCode = 0;
Expand Down
14 changes: 14 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,17 @@ function git_dir()
return realpath($gitDir);
}
}

if (! function_exists('get_hook_contents')) {
/**
* Return hook contents
*
* @param array|string $contents
*
* @return string
*/
function get_hook_contents($contents)
{
return is_array($contents) ? implode(" && \\" . PHP_EOL, $contents) : $contents;
}
}
2 changes: 1 addition & 1 deletion tests/AddCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public function it_handles_commands_defined_in_an_array()
$this->assertContains("Added {$hook} hook", $this->commandTester->getDisplay());

$content = file_get_contents(".git/hooks/" . $hook);
$this->assertContains(implode(PHP_EOL, $scripts), $content);
$this->assertContains(implode(" && \\" . PHP_EOL, $scripts), $content);
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/HookCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,24 @@ public function it_tests_hooks_that_exist()
$this->assertContains(str_replace('echo ', '', $script), $commandTester->getDisplay());
}
}

/**
* @test
*/
public function it_terminates_if_previous_hook_fails()
{
$hook = [
'pre-commit' => [
'echo execution-error;exit 1',
'echo before-commit'
],
];

$command = new HookCommand('pre-commit', $hook['pre-commit']);
$commandTester = new CommandTester($command);

$commandTester->execute([]);
$this->assertContains('execution-error', $commandTester->getDisplay());
$this->assertNotContains('before-commit', $commandTester->getDisplay());
}
}
2 changes: 1 addition & 1 deletion tests/UpdateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function it_handles_commands_defined_in_an_array()
$this->assertContains("Updated {$hook} hook", $this->commandTester->getDisplay());

$content = file_get_contents(".git/hooks/" . $hook);
$this->assertContains(implode(PHP_EOL, $scripts), $content);
$this->assertContains(implode(" && \\" . PHP_EOL, $scripts), $content);
}
}

Expand Down

0 comments on commit 676162c

Please sign in to comment.