Skip to content

Commit d2e9c8a

Browse files
committed
HookFailException tests
1 parent f45e7ab commit d2e9c8a

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/Traits/ProcessHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
use RuntimeException;
66
use Symfony\Component\Process\Process;
77

8+
/**
9+
* @codeCoverageIgnore
10+
*/
811
trait ProcessHelper
912
{
1013
/**
1114
* Run the given commands.
1215
*
16+
*
1317
* @param array|string $commands
1418
* @param array $params
1519
* @return \Symfony\Component\Process\Process

tests/Features/Commands/PostCommitTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Igorsgm\GitHooks\Contracts\PostCommitHook;
4+
use Igorsgm\GitHooks\Exceptions\HookFailException;
45
use Igorsgm\GitHooks\Facades\GitHooks;
56
use Igorsgm\GitHooks\Git\Log;
67

@@ -19,3 +20,18 @@
1920

2021
$this->artisan('git-hooks:post-commit')->assertSuccessful();
2122
})->with('lastCommitLogText');
23+
24+
it('Returns 1 on HookFailException', function ($logText) {
25+
$postCommitHook1 = mock(PostCommitHook::class)->expect(
26+
handle: function (Log $log, Closure $closure) {
27+
throw new HookFailException();
28+
}
29+
);
30+
31+
$this->config->set('git-hooks.post-commit', [
32+
$postCommitHook1,
33+
]);
34+
35+
GitHooks::shouldReceive('getLastCommitFromLog')->andReturn($logText);
36+
$this->artisan('git-hooks:post-commit')->assertExitCode(1);
37+
})->with('lastCommitLogText');

tests/Features/Commands/PrepareCommitMessageTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

3+
use Igorsgm\GitHooks\Contracts\MessageHook;
4+
use Igorsgm\GitHooks\Exceptions\HookFailException;
35
use Igorsgm\GitHooks\Facades\GitHooks;
6+
use Igorsgm\GitHooks\Git\CommitMessage;
47
use Igorsgm\GitHooks\Tests\Fixtures\PrepareCommitMessageFixtureHook1;
58
use Igorsgm\GitHooks\Tests\Fixtures\PrepareCommitMessageFixtureHook2;
69

@@ -30,3 +33,28 @@
3033
$command->expectsOutputToContain(sprintf('Hook: %s...', resolve($hook)->getName()));
3134
}
3235
})->with('listOfChangedFiles');
36+
37+
it('Returns 1 on HookFailException', function ($listOfChangedFiles) {
38+
$postCommitHook1 = mock(MessageHook::class)->expect(
39+
handle: function (CommitMessage $commitMessage, Closure $closure) {
40+
throw new HookFailException();
41+
}
42+
);
43+
44+
$this->config->set('git-hooks.prepare-commit-msg', [
45+
$postCommitHook1,
46+
]);
47+
48+
$file = 'tmp/COMMIT_MESSAGE';
49+
50+
GitHooks::shouldReceive('getCommitMessageContentFromFile')
51+
->andReturn('Test commit');
52+
53+
GitHooks::shouldReceive('getListOfChangedFiles')
54+
->andReturn($listOfChangedFiles);
55+
56+
GitHooks::shouldReceive('updateCommitMessageContentInFile')
57+
->with(base_path($file), 'Test commit hook1 hook2');
58+
59+
$this->artisan('git-hooks:prepare-commit-msg', ['file' => $file])->assertExitCode(1);
60+
})->with('listOfChangedFiles');

0 commit comments

Comments
 (0)