Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/vendor/
/.idea/
/test-git-dir/
14 changes: 11 additions & 3 deletions src/Commands/AddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (file_exists($filename)) {
$output->writeln("<comment>{$hook} already exists</comment>");
} else {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// On windows we need to add a SHEBANG
// See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
$script = '#!/bin/bash' . PHP_EOL . $script;
}

file_put_contents($filename, $script);
chmod($filename, 0755);
$output->writeln("Added <info>{$hook}</info> hook");
Expand Down Expand Up @@ -82,9 +88,11 @@ private function addLockFile($hooks, $output)

private function ignoreLockFile($output)
{
passthru('grep -q ' . Hook::LOCK_FILE . ' .gitignore', $return);
if ($return !== 0) {
passthru('echo ' . Hook::LOCK_FILE . ' >> .gitignore');
$contents = file_get_contents('.gitignore');
$return = strpos($contents, Hook::LOCK_FILE);

if ($return === false) {
file_put_contents('.gitignore', Hook::LOCK_FILE . PHP_EOL, FILE_APPEND);
$output->writeln('<comment>Added ' . Hook::LOCK_FILE . ' to .gitignore</comment>');
}
}
Expand Down
19 changes: 19 additions & 0 deletions tests/AddCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ public function it_adds_hooks_that_do_not_already_exist()
}
}

/**
* @test
*/
public function it_adds_shebang_to_hooks_on_windows()
{
if(strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')
$this->markTestSkipped('This test is only relevant on windows. You\'re running Linux.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, I am not. I'm on macOS ;)


$this->commandTester->execute([]);

foreach (array_keys(self::$hooks) as $hook) {
$this->assertContains("Added {$hook} hook", $this->commandTester->getDisplay());

$content = file_get_contents(".git/hooks/" . $hook);
$this->assertNotFalse(strpos($content, "#!/bin/bash"));
$this->assertEquals(strpos($content, "#!/bin/bash"), 0);
}
}

/**
* @test
*/
Expand Down
6 changes: 4 additions & 2 deletions tests/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public function it_lists_hooks_that_exist()
public function it_uses_a_different_git_path_if_specified()
{
$gitDir = 'test-git-dir';
passthru("mkdir -p {$gitDir}/hooks");

mkdir("{$gitDir}/hooks", 0777, true);

self::createHooks($gitDir);

$this->commandTester->execute(['--git-dir' => $gitDir]);
Expand All @@ -46,6 +48,6 @@ public function it_uses_a_different_git_path_if_specified()
$this->assertContains($hook, $this->commandTester->getDisplay());
}

passthru("rm -rf {$gitDir}");
$this->recursive_rmdir($gitDir);
}
}
30 changes: 30 additions & 0 deletions tests/PrepareHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public static function tearDownAfterClass()

public static function createHooks($gitDir = '.git')
{
if (!is_dir("{$gitDir}/hooks")) {
mkdir("{$gitDir}/hooks", 0777, true);
}

foreach (self::$hooks as $hook => $script) {
file_put_contents("{$gitDir}/hooks/{$hook}", $script);
}
Expand All @@ -43,4 +47,30 @@ private static function prepare()
$ignoreContents = file_get_contents('.gitignore');
file_put_contents('.gitignore', str_replace(Hook::LOCK_FILE . PHP_EOL, '', $ignoreContents));
}

/**
* Since PHP does not support the recursive deletion of
* a directory and its entire contents we need a helper here.
*
* @see https://stackoverflow.com/a/3338133
*
* @param $dir string
*/
protected function recursive_rmdir($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);

foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir."/".$object))
$this->recursive_rmdir($dir."/".$object);
else
unlink($dir."/".$object);
}
}

rmdir($dir);
}
}
}
20 changes: 14 additions & 6 deletions tests/RemoveCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ public function it_removes_hooks_that_were_added()
public function it_removes_removed_hooks_from_the_lock_file()
{
foreach (array_keys(self::$hooks) as $hook) {
passthru("grep -q {$hook} " . Hook::LOCK_FILE, $return);
$contents = file_get_contents('.gitignore');
$return = strpos($contents, Hook::LOCK_FILE);

$this->assertEquals(0, $return);

$this->commandTester->execute(['hooks' => [$hook]]);
$this->assertContains("Removed {$hook} hook", $this->commandTester->getDisplay());

passthru("grep -q {$hook} " . Hook::LOCK_FILE, $return);
$this->assertEquals(1, $return);
$contents = file_get_contents('.gitignore');
$return = strpos($contents, Hook::LOCK_FILE);
$this->assertFalse($return);
}
}

Expand All @@ -66,7 +69,9 @@ public function it_does_not_remove_hooks_not_present_in_the_lock_file()
{
$hook = 'test-hook';
$this->commandTester->execute(['hooks' => [$hook]]);
$this->assertContains("Skipped {$hook} hook - not present in lock file", $this->commandTester->getDisplay());
$this->assertContains(
"Skipped {$hook} hook - not present in lock file", $this->commandTester->getDisplay()
);
}

/**
Expand All @@ -89,7 +94,9 @@ public function it_removes_hooks_not_present_in_the_lock_file_if_forced_to()
public function it_uses_a_different_git_path_if_specified()
{
$gitDir = 'test-git-dir';
passthru("mkdir -p {$gitDir}/hooks");

mkdir("{$gitDir}/hooks", 0777, true);

self::createHooks($gitDir);
$this->assertFalse(self::isDirEmpty("{$gitDir}/hooks"));

Expand All @@ -100,7 +107,8 @@ public function it_uses_a_different_git_path_if_specified()
}

$this->assertTrue(self::isDirEmpty("{$gitDir}/hooks"));
passthru("rm -rf {$gitDir}");

$this->recursive_rmdir($gitDir);
}

public function tearDown()
Expand Down
6 changes: 4 additions & 2 deletions tests/UpdateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ public function it_updates_hooks_that_already_exist()
public function it_uses_a_different_git_path_if_specified()
{
$gitDir = 'test-git-dir';
passthru("mkdir -p {$gitDir}/hooks");

mkdir("{$gitDir}/hooks", 0777, true);

$this->commandTester->execute(['--git-dir' => $gitDir]);

foreach (array_keys(self::$hooks) as $hook) {
$this->assertTrue(file_exists("{$gitDir}/hooks/{$hook}"));
}

passthru("rm -rf {$gitDir}");
$this->recursive_rmdir($gitDir);
}

/**
Expand Down