From d235827ef1928c5fb68950a3646aeb6d3b7b8d28 Mon Sep 17 00:00:00 2001 From: Patrick Conrad Date: Wed, 26 Jul 2017 10:26:45 +0200 Subject: [PATCH] Fix missing shebang after update This will also add and test the shebang on update. --- src/Commands/UpdateCommand.php | 7 +++++++ tests/UpdateCommandTest.php | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Commands/UpdateCommand.php b/src/Commands/UpdateCommand.php index 3e87fe4..13f8ccd 100644 --- a/src/Commands/UpdateCommand.php +++ b/src/Commands/UpdateCommand.php @@ -41,6 +41,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $filename = "{$gitDir}/hooks/{$hook}"; $operation = file_exists($filename) ? 'Updated' : 'Added'; + + 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("{$operation} {$hook} hook"); diff --git a/tests/UpdateCommandTest.php b/tests/UpdateCommandTest.php index fe14557..ce704af 100644 --- a/tests/UpdateCommandTest.php +++ b/tests/UpdateCommandTest.php @@ -45,6 +45,23 @@ public function it_updates_hooks_that_already_exist() } } + 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.'); + + self::createHooks(); + $this->commandTester->execute([]); + + foreach (array_keys(self::$hooks) as $hook) { + $this->assertContains("Updated {$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 */