Skip to content

Commit c21208b

Browse files
authored
Merge pull request #22 from igorsgm/develop
Add support for custom Artisan executable path (Laravel Zero Usage)
2 parents f178206 + 488d3e7 commit c21208b

File tree

11 files changed

+108
-27
lines changed

11 files changed

+108
-27
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
matrix:
2222
operating-system: [ ubuntu-latest ]
2323
php: [ '8.1', '8.2' ]
24-
dependency-stability: [ 'prefer-stable','prefer-lowest' ]
24+
dependency-stability: [ 'prefer-stable' ]
2525

2626
laravel: [ '10.*' ]
2727
include:

config/git-hooks.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,21 @@
204204
],
205205
],
206206

207+
/*
208+
|--------------------------------------------------------------------------
209+
| Artisan Executable Path
210+
|--------------------------------------------------------------------------
211+
|
212+
| This configuration option allows you to set the path of the Artisan
213+
| executable within your Laravel or Laravel Zero application.
214+
|
215+
| By default, the path is set to the base_path('artisan') which corresponds
216+
| to the main Artisan executable in a standard Laravel installation.
217+
|
218+
| In case you are using a Laravel Zero CLI tool, you may need to change
219+
| this value to match the path of your main executable file instead
220+
| of artisan.php.
221+
|
222+
*/
223+
'artisan_path' => base_path('artisan'),
207224
];

src/Console/Commands/stubs/hook

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ if sh -c ": >/dev/tty" >/dev/null 2>/dev/null; then
44
exec < /dev/tty
55
fi
66

7-
php {path}/artisan {command} $@ >&2
7+
php {artisanPath} {command} $@ >&2

src/GitHooks.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public function install(string $hookName)
5656

5757
$hookPath = $this->getGitHooksDir().'/'.$hookName;
5858
$hookScript = str_replace(
59-
['{command}', '{path}'],
60-
[$command, base_path()],
59+
['{command}', '{artisanPath}'],
60+
[$command, config('git-hooks.artisan_path')],
6161
$this->getHookStub()
6262
);
6363

tests/Features/Commands/PostCommitTest.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@
66
use Igorsgm\GitHooks\Git\Log;
77

88
test('Git Log is sent through HookPipes', function (string $logText) {
9-
$postCommitHook1 = mock(PostCommitHook::class)->expect(
10-
handle: fn (Log $log, Closure $closure) => expect($log->getHash())->toBe(mockCommitHash())
11-
);
9+
// This approach is broken in the current version of Mockery
10+
// @TODO: Update this test once Pest or Mockery versions are updated
11+
// $postCommitHook1 = mock(PostCommitHook::class)->expect(
12+
// handle: fn (Log $log, Closure $closure) => expect($log->getHash())->toBe(mockCommitHash())
13+
// );
14+
15+
$postCommitHook1 = mock(PostCommitHook::class);
16+
$postCommitHook1->expects('handle')
17+
->withArgs(fn($log, $closure) => $log->getHash() === mockCommitHash());
18+
1219
$postCommitHook2 = clone $postCommitHook1;
1320

1421
$this->config->set('git-hooks.post-commit', [
@@ -22,11 +29,19 @@
2229
})->with('lastCommitLogText');
2330

2431
it('Returns 1 on HookFailException', function ($logText) {
25-
$postCommitHook1 = mock(PostCommitHook::class)->expect(
26-
handle: function (Log $log, Closure $closure) {
32+
// This approach is broken in the current version of Mockery
33+
// @TODO: Update this test once Pest or Mockery versions are updated
34+
// $postCommitHook1 = mock(PostCommitHook::class)->expect(
35+
// handle: function (Log $log, Closure $closure) {
36+
// throw new HookFailException();
37+
// }
38+
// );
39+
40+
$postCommitHook1 = mock(PostCommitHook::class);
41+
$postCommitHook1->expects('handle')
42+
->andReturnUsing(function (Log $log, Closure $closure) {
2743
throw new HookFailException();
28-
}
29-
);
44+
});
3045

3146
$this->config->set('git-hooks.post-commit', [
3247
$postCommitHook1,

tests/Features/Commands/PreCommitTest.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@
66
use Igorsgm\GitHooks\Git\ChangedFiles;
77

88
test('Sends ChangedFiles through HookPipes', function (string $listOfChangedFiles) {
9-
$preCommitHook1 = mock(PreCommitHook::class)->expect(
10-
handle: function (ChangedFiles $files, Closure $closure) use ($listOfChangedFiles) {
9+
// This approach is broken in the current version of Mockery
10+
// @TODO: Update this test once Pest or Mockery versions are updated
11+
// $preCommitHook1 = mock(PreCommitHook::class)->expect(
12+
// handle: function (ChangedFiles $files, Closure $closure) use ($listOfChangedFiles) {
13+
// $firstChangedFile = (string) $files->getFiles()->first();
14+
// expect($firstChangedFile)->toBe($listOfChangedFiles);
15+
// }
16+
// );
17+
$preCommitHook1 = mock(PreCommitHook::class);
18+
$preCommitHook1->expects('handle')
19+
->andReturnUsing(function (ChangedFiles $files, Closure $closure) use ($listOfChangedFiles) {
1120
$firstChangedFile = (string) $files->getFiles()->first();
1221
expect($firstChangedFile)->toBe($listOfChangedFiles);
13-
}
14-
);
22+
});
23+
1524
$preCommitHook2 = clone $preCommitHook1;
1625

1726
$this->config->set('git-hooks.pre-commit', [
@@ -25,11 +34,19 @@
2534
})->with('listOfChangedFiles');
2635

2736
it('Returns 1 on HookFailException', function ($listOfChangedFiles) {
28-
$preCommitHook1 = mock(PreCommitHook::class)->expect(
29-
handle: function (ChangedFiles $files, Closure $closure) {
37+
// This approach is broken in the current version of Mockery
38+
// @TODO: Update this test once Pest or Mockery versions are updated
39+
// $preCommitHook1 = mock(PreCommitHook::class)->expect(
40+
// handle: function (ChangedFiles $files, Closure $closure) {
41+
// throw new HookFailException();
42+
// }
43+
// );
44+
$preCommitHook1 = mock(PreCommitHook::class);
45+
$preCommitHook1->expects('handle')
46+
->andReturnUsing(function (ChangedFiles $files, Closure $closure) {
3047
throw new HookFailException();
31-
}
32-
);
48+
});
49+
3350

3451
$this->config->set('git-hooks.pre-commit', [
3552
$preCommitHook1,

tests/Features/Commands/PrePushTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55
use Igorsgm\GitHooks\Git\Log;
66

77
test('Git Log is sent through HookPipes', function (string $logText) {
8-
$prePushHook1 = mock(PostCommitHook::class)->expect(
9-
handle: fn (Log $log, Closure $closure) => expect($log->getHash())->toBe(mockCommitHash())
10-
);
8+
// This approach is broken in the current version of Mockery
9+
// @TODO: Update this test once Pest or Mockery versions are updated
10+
// $prePushHook1 = mock(PostCommitHook::class)->expect(
11+
// handle: fn (Log $log, Closure $closure) => expect($log->getHash())->toBe(mockCommitHash())
12+
// );
13+
$prePushHook1 = mock(PostCommitHook::class);
14+
$prePushHook1->expects('handle')
15+
->withArgs(fn(Log $log, Closure $closure) => $log->getHash() === mockCommitHash())
16+
->once();
17+
1118
$prePushHook2 = clone $prePushHook1;
1219

1320
$this->config->set('git-hooks.pre-push', [

tests/Features/Commands/PrepareCommitMessageTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,19 @@
3535
})->with('listOfChangedFiles');
3636

3737
it('Returns 1 on HookFailException', function ($listOfChangedFiles) {
38-
$postCommitHook1 = mock(MessageHook::class)->expect(
39-
handle: function (CommitMessage $commitMessage, Closure $closure) {
38+
// This approach is broken in the current version of Mockery
39+
// @TODO: Update this test once Pest or Mockery versions are updated
40+
// $postCommitHook1 = mock(MessageHook::class)->expect(
41+
// handle: function (CommitMessage $commitMessage, Closure $closure) {
42+
// throw new HookFailException();
43+
// }
44+
// );
45+
$postCommitHook1 = mock(MessageHook::class);
46+
$postCommitHook1->expects('handle')
47+
->andReturnUsing(function (CommitMessage $commitMessage, Closure $closure) {
4048
throw new HookFailException();
41-
}
42-
);
49+
});
50+
4351

4452
$this->config->set('git-hooks.prepare-commit-msg', [
4553
$postCommitHook1,

tests/Features/Commands/RegisterHooksTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,19 @@
3737
->toBeFile()
3838
->toContainHookArtisanCommand($hookName);
3939
})->with('registrableHookTypes');
40+
41+
test('Installs hook file in .git/hooks folder with custom artisan path', function ($hookClass, $hookName) {
42+
$this->config->set('git-hooks.'.$hookName, [
43+
$hookClass,
44+
]);
45+
46+
$this->config->set('git-hooks.artisan_path', base_path('custom-artisan-path'));
47+
48+
$this->artisan('git-hooks:register')->assertSuccessful();
49+
50+
$hookFile = base_path('.git/hooks/'.$hookName);
51+
52+
expect($hookFile)
53+
->toBeFile()
54+
->toContainHookArtisanCommand($hookName);
55+
})->with('registrableHookTypes');

tests/Pest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
expect()->extend('toContainHookArtisanCommand', function ($hookName) {
3636
$this->value = file_get_contents($this->value);
37-
$artisanCommand = sprintf('php %s git-hooks:%s $@ >&2', base_path('artisan'), $hookName);
37+
$artisanCommand = sprintf('php %s git-hooks:%s $@ >&2', config('git-hooks.artisan_path'), $hookName);
3838

3939
return $this->toContain($artisanCommand);
4040
});

0 commit comments

Comments
 (0)