Skip to content

Commit 27a21cd

Browse files
committed
Add verbosity support
1 parent 10339ad commit 27a21cd

10 files changed

+48
-37
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
.PHONY: test
1+
.PHONY: build exec
22

3-
test:
3+
build:
44
docker build --rm -t cghooks .
55

66
exec:

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@
4949
"extra": {
5050
"hooks": {
5151
"pre-commit": "composer check-style",
52-
"pre-push": "composer test"
52+
"pre-push": [
53+
"composer test",
54+
"grep -q $(git tag --sort=-v:refname | head -n 1) cghooks"
55+
]
5356
}
5457
}
5558
}

src/Commands/AddCommand.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected function global_dir_fallback()
6767
{
6868
if (!empty($this->dir = trim(getenv('COMPOSER_HOME')))) {
6969
$this->dir = realpath($this->dir);
70-
$this->comment("No global git hook path was provided. Falling back to COMPOSER_HOME [{$this->dir}]");
70+
$this->debug("No global git hook path was provided. Falling back to COMPOSER_HOME [{$this->dir}]");
7171
}
7272
}
7373

@@ -82,11 +82,11 @@ private function addHook($hook, $contents)
8282
$contents = is_array($contents) ? implode(PHP_EOL, $contents) : $contents;
8383

8484
if (! $this->force && $exists) {
85-
$this->comment("{$hook} already exists");
85+
$this->debug("[{$hook}] already exists");
8686
return;
8787
}
8888

89-
file_put_contents($filename, $shebang . $contents);
89+
file_put_contents($filename, $shebang . $contents . PHP_EOL);
9090
chmod($filename, 0755);
9191

9292
$operation = $exists ? 'Updated' : 'Added';
@@ -98,12 +98,12 @@ private function addHook($hook, $contents)
9898
private function addLockFile()
9999
{
100100
if ($this->noLock) {
101-
$this->comment("Skipped creating a {$this->lockFile} file");
101+
$this->debug("Skipped creating a [{$this->lockFile}] file");
102102
return;
103103
}
104104

105105
file_put_contents(Hook::LOCK_FILE, json_encode($this->addedHooks));
106-
$this->comment("Created {$this->lockFile} file");
106+
$this->debug("Created [{$this->lockFile}] file");
107107
}
108108

109109
private function ignoreLockFile()
@@ -113,7 +113,7 @@ private function ignoreLockFile()
113113
}
114114

115115
if (! $this->ignoreLock) {
116-
$this->comment("Skipped adding {$this->lockFile} to .gitignore");
116+
$this->debug("Skipped adding [{$this->lockFile}] to .gitignore");
117117
return;
118118
}
119119

@@ -122,7 +122,7 @@ private function ignoreLockFile()
122122

123123
if ($return === false) {
124124
file_put_contents('.gitignore', $this->lockFile . PHP_EOL, FILE_APPEND);
125-
$this->comment("Added {$this->lockFile} to .gitignore");
125+
$this->debug("Added [{$this->lockFile}] to .gitignore");
126126
}
127127
}
128128

@@ -139,7 +139,7 @@ private function setGlobalGitHooksPath()
139139
return;
140140
}
141141

142-
$this->comment(
142+
$this->info(
143143
'About to modify global git hook path. '
144144
. ($previousGlobalHookDir !== ''
145145
? "Previous value was [{$previousGlobalHookDir}]"
@@ -155,6 +155,6 @@ private function setGlobalGitHooksPath()
155155
return;
156156
}
157157

158-
$this->comment("Global git hook path set to {$globalHookDir}");
158+
$this->info("Global git hook path set to [{$globalHookDir}]");
159159
}
160160
}

src/Commands/Command.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ protected function info($info)
5959
$this->output->writeln($info);
6060
}
6161

62-
protected function comment($comment)
62+
protected function debug($debug)
6363
{
64-
$comment = str_replace('[', '<comment>', $comment);
65-
$comment = str_replace(']', '</comment>', $comment);
64+
$debug = str_replace('[', '<comment>', $debug);
65+
$debug = str_replace(']', '</comment>', $debug);
6666

67-
$this->output->writeln($comment);
67+
$this->output->writeln($debug, OutputInterface::VERBOSITY_VERBOSE);
6868
}
6969

7070
protected function error($error)
7171
{
72-
$this->output->writeln("<bg=red>{$error}</>");
72+
$this->output->writeln("<fg=red>{$error}</>");
7373
}
7474
}

src/Commands/RemoveCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected function command()
5353
$filename = "{$this->dir}/hooks/{$hook}";
5454

5555
if (! array_key_exists($hook, $this->lockFileHooks) && ! $this->force) {
56-
$this->comment("Skipped {$hook} hook - not present in lock file");
56+
$this->info("Skipped [{$hook}] hook - not present in lock file");
5757
continue;
5858
}
5959

tests/AddCommandTest.php

+25-14
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use BrainMaestro\GitHooks\Commands\AddCommand;
66
use BrainMaestro\GitHooks\Hook;
7-
use Symfony\Component\Console\Application;
87
use Symfony\Component\Console\Tester\CommandTester;
8+
use Symfony\Component\Console\Output\OutputInterface;
99

1010
class AddCommandTest extends TestCase
1111
{
@@ -54,11 +54,13 @@ public function it_adds_shebang_to_hooks_on_windows()
5454
public function it_does_not_add_hooks_that_already_exist()
5555
{
5656
self::createHooks();
57-
$this->commandTester->execute([]);
57+
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
5858

5959
foreach (array_keys(self::$hooks) as $hook) {
6060
$this->assertContains("{$hook} already exists", $this->commandTester->getDisplay());
6161
}
62+
63+
$this->assertContains('No hooks were added. Try updating', $this->commandTester->getDisplay());
6264
}
6365

6466
/**
@@ -79,7 +81,7 @@ public function it_overrides_hooks_that_already_exist()
7981
*/
8082
public function it_correctly_creates_the_hook_lock_file()
8183
{
82-
$this->commandTester->execute([]);
84+
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
8385

8486
$this->assertContains('Created '. Hook::LOCK_FILE . ' file', $this->commandTester->getDisplay());
8587
$this->assertFileExists(Hook::LOCK_FILE);
@@ -91,7 +93,7 @@ public function it_correctly_creates_the_hook_lock_file()
9193
*/
9294
public function it_does_not_create_the_hook_lock_file_if_the_no_lock_option_is_passed()
9395
{
94-
$this->commandTester->execute(['--no-lock' => true]);
96+
$this->commandTester->execute(['--no-lock' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
9597

9698
$this->assertContains('Skipped creating a '. Hook::LOCK_FILE . ' file', $this->commandTester->getDisplay());
9799
$this->assertFileNotExists(Hook::LOCK_FILE);
@@ -102,7 +104,7 @@ public function it_does_not_create_the_hook_lock_file_if_the_no_lock_option_is_p
102104
*/
103105
public function it_does_not_ignore_the_hook_lock_file()
104106
{
105-
$this->commandTester->execute([]);
107+
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
106108

107109
$this->assertContains('Skipped adding '. Hook::LOCK_FILE . ' to .gitignore', $this->commandTester->getDisplay());
108110
$this->assertFalse(strpos(file_get_contents('.gitignore'), Hook::LOCK_FILE));
@@ -113,7 +115,7 @@ public function it_does_not_ignore_the_hook_lock_file()
113115
*/
114116
public function it_ignores_the_hook_lock_file_if_the_ignore_lock_option_is_passed()
115117
{
116-
$this->commandTester->execute(['--ignore-lock' => true]);
118+
$this->commandTester->execute(['--ignore-lock' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
117119

118120
$this->assertContains('Added ' . Hook::LOCK_FILE . ' to .gitignore', $this->commandTester->getDisplay());
119121
$this->assertTrue(strpos(file_get_contents('.gitignore'), Hook::LOCK_FILE) !== false);
@@ -216,7 +218,10 @@ public function it_adds_global_git_hooks()
216218

217219
self::createTestComposerFile($gitDir);
218220

219-
$this->commandTester->execute(['--global' => true, '--git-dir' => $gitDir]);
221+
$this->commandTester->execute(
222+
['--global' => true, '--git-dir' => $gitDir],
223+
['verbosity' => OutputInterface::VERBOSITY_VERBOSE]
224+
);
220225

221226
foreach (array_keys(self::$hooks) as $hook) {
222227
$this->assertContains("Added {$hook} hook", $this->commandTester->getDisplay());
@@ -244,7 +249,10 @@ public function it_adds_global_git_hooks_and_shows_previous_global_dir()
244249

245250
self::createTestComposerFile($gitDir);
246251

247-
$this->commandTester->execute(['--global' => true, '--git-dir' => $gitDir]);
252+
$this->commandTester->execute(
253+
['--global' => true, '--git-dir' => $gitDir],
254+
['verbosity' => OutputInterface::VERBOSITY_VERBOSE]
255+
);
248256

249257
foreach (array_keys(self::$hooks) as $hook) {
250258
$this->assertContains("Added {$hook} hook", $this->commandTester->getDisplay());
@@ -253,7 +261,7 @@ public function it_adds_global_git_hooks_and_shows_previous_global_dir()
253261

254262
$hookDir = realpath("{$gitDir}/hooks");
255263
$this->assertContains(
256-
"About to modify global git hook path. Previous value was [{$previousHookDir}]",
264+
"About to modify global git hook path. Previous value was {$previousHookDir}",
257265
$this->commandTester->getDisplay()
258266
);
259267
$this->assertContains("Global git hook path set to {$hookDir}", $this->commandTester->getDisplay());
@@ -272,15 +280,18 @@ public function it_adds_global_git_hooks_and_does_not_change_global_dir_if_it_ma
272280

273281
self::createTestComposerFile($gitDir);
274282

275-
$this->commandTester->execute(['--global' => true, '--git-dir' => $gitDir]);
283+
$this->commandTester->execute(
284+
['--global' => true, '--git-dir' => $gitDir],
285+
['verbosity' => OutputInterface::VERBOSITY_VERBOSE]
286+
);
276287

277288
foreach (array_keys(self::$hooks) as $hook) {
278289
$this->assertContains("Added {$hook} hook", $this->commandTester->getDisplay());
279290
$this->assertFileExists("{$hookDir}/{$hook}");
280291
}
281292

282293
$this->assertNotContains(
283-
"About to modify global git hook path. Previous value was [{$hookDir}]",
294+
"About to modify global git hook path. Previous value was {$hookDir}",
284295
$this->commandTester->getDisplay()
285296
);
286297
$this->assertNotContains("Global git hook path set to {$hookDir}", $this->commandTester->getDisplay());
@@ -300,7 +311,7 @@ public function it_falls_back_to_composer_home_if_no_global_hook_dir_is_provided
300311

301312
self::createTestComposerFile($gitDir);
302313

303-
$this->commandTester->execute(['--global' => true]);
314+
$this->commandTester->execute(['--global' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
304315

305316
foreach (array_keys(self::$hooks) as $hook) {
306317
$this->assertContains("Added {$hook} hook", $this->commandTester->getDisplay());
@@ -310,7 +321,7 @@ public function it_falls_back_to_composer_home_if_no_global_hook_dir_is_provided
310321
$gitDir = realpath('test-global-composer-home-dir');
311322
$hookDir = "{$gitDir}/hooks";
312323
$this->assertContains(
313-
"No global git hook path was provided. Falling back to COMPOSER_HOME [{$gitDir}]",
324+
"No global git hook path was provided. Falling back to COMPOSER_HOME {$gitDir}",
314325
$this->commandTester->getDisplay()
315326
);
316327
$this->assertContains(
@@ -332,7 +343,7 @@ public function it_fails_if_global_hook_dir_is_missing()
332343

333344
shell_exec('git config --global --unset core.hooksPath');
334345

335-
$this->commandTester->execute(['--global' => true]);
346+
$this->commandTester->execute(['--global' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
336347

337348
foreach (array_keys(self::$hooks) as $hook) {
338349
$this->assertNotContains("Updated {$hook} hook", $this->commandTester->getDisplay());

tests/HookCommandTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace BrainMaestro\GitHooks\Tests;
44

55
use BrainMaestro\GitHooks\Commands\HookCommand;
6-
use Symfony\Component\Console\Application;
76
use Symfony\Component\Console\Tester\CommandTester;
87

98
class HookCommandTest extends TestCase

tests/ListCommandTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace BrainMaestro\GitHooks\Tests;
44

55
use BrainMaestro\GitHooks\Commands\ListCommand;
6-
use Symfony\Component\Console\Application;
76
use Symfony\Component\Console\Tester\CommandTester;
87

98
class ListCommandTest extends TestCase

tests/RemoveCommandTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use BrainMaestro\GitHooks\Commands\RemoveCommand;
66
use BrainMaestro\GitHooks\Hook;
7-
use Symfony\Component\Console\Application;
87
use Symfony\Component\Console\Tester\CommandTester;
8+
use Symfony\Component\Console\Output\OutputInterface;
99

1010
class RemoveCommandTest extends TestCase
1111
{
@@ -68,7 +68,7 @@ public function it_does_not_remove_hooks_not_present_in_the_lock_file()
6868
$hook = 'pre-commit';
6969
unlink(Hook::LOCK_FILE);
7070

71-
$this->commandTester->execute(['hooks' => [$hook]]);
71+
$this->commandTester->execute(['hooks' => [$hook]], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
7272
$this->assertContains(
7373
"Skipped {$hook} hook - not present in lock file",
7474
$this->commandTester->getDisplay()

tests/UpdateCommandTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use BrainMaestro\GitHooks\Commands\UpdateCommand;
66
use BrainMaestro\GitHooks\Hook;
7-
use Symfony\Component\Console\Application;
87
use Symfony\Component\Console\Tester\CommandTester;
98

109
class UpdateCommandTest extends TestCase

0 commit comments

Comments
 (0)