Skip to content

Commit 85146a0

Browse files
committed
- Add Enlightn dependency to composer.json for improved code analysis.
- Modify BaseCodeAnalyzerPreCommitHook to optimize fixerCommand check. - Introduce Enlightn as a new pre-commit hook, adapting the code to use Enlightn's analyzer. - Update tests to include the new Enlightn pre-commit hook, ensuring they pass for the new implementation. - Register EnlightnServiceProvider in test cases for proper testing.
1 parent 91a0a88 commit 85146a0

File tree

5 files changed

+156
-1
lines changed

5 files changed

+156
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0"
3131
},
3232
"require-dev": {
33+
"enlightn/enlightn": "^2.3",
3334
"laravel/pint": "^1.2",
3435
"mockery/mockery": "^1.5.1",
3536
"nunomaduro/larastan": "^2.0",

src/Console/Commands/Hooks/BaseCodeAnalyzerPreCommitHook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ protected function suggestAutoFixOrExit()
208208
{
209209
$hasFixerCommand = ! empty($this->fixerCommand());
210210

211-
if (Terminal::hasSttyAvailable() && $hasFixerCommand &&
211+
if ($hasFixerCommand && Terminal::hasSttyAvailable() &&
212212
$this->command->confirm('Would you like to attempt to correct files automagically?')
213213
) {
214214
$errorFilesString = implode(' ', $this->filesBadlyFormattedPaths);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Igorsgm\GitHooks\Console\Commands\Hooks;
4+
5+
use Closure;
6+
use Igorsgm\GitHooks\Contracts\CodeAnalyzerPreCommitHook;
7+
use Igorsgm\GitHooks\Facades\GitHooks;
8+
use Igorsgm\GitHooks\Git\ChangedFiles;
9+
use Illuminate\Support\Facades\Artisan;
10+
11+
class EnlightnPreCommitHook extends BaseCodeAnalyzerPreCommitHook implements CodeAnalyzerPreCommitHook
12+
{
13+
/**
14+
* Name of the hook
15+
*
16+
* @var string
17+
*/
18+
protected $name = 'Enlightn';
19+
20+
/**
21+
* Analyzes committed files using Enlightn
22+
*
23+
* @param ChangedFiles $files The list of committed files to analyze.
24+
* @param Closure $next The next hook in the chain to execute.
25+
* @return mixed|null
26+
*/
27+
public function handle(ChangedFiles $files, Closure $next)
28+
{
29+
$commitFiles = $files->getAddedToCommit();
30+
31+
if ($commitFiles->isEmpty() || GitHooks::isMergeInProgress()) {
32+
return $next($files);
33+
}
34+
35+
if (Artisan::call('enlightn') !== 0) {
36+
$this->commitFailMessage()
37+
->suggestAutoFixOrExit();
38+
}
39+
40+
return $next($files);
41+
}
42+
43+
/**
44+
* Returns the command to run Enlightn analyzer
45+
*/
46+
public function analyzerCommand(): string
47+
{
48+
return 'php artisan enlightn';
49+
}
50+
51+
/**
52+
* Empty fixer command because Enlightn doesn't provide any type of auto-fixing.
53+
*/
54+
public function fixerCommand(): string
55+
{
56+
return '';
57+
}
58+
59+
/**
60+
* Returns the message to display when the commit fails.
61+
*
62+
* @return $this
63+
*/
64+
protected function commitFailMessage()
65+
{
66+
$this->command->newLine();
67+
68+
$message = '<bg=red;fg=white> COMMIT FAILED </> ';
69+
$message .= sprintf(
70+
'You can check which %s errors happened by executing: <comment>%s</comment>',
71+
$this->getName(), $this->analyzerCommand()
72+
);
73+
74+
$this->command->getOutput()->writeln($message);
75+
76+
return $this;
77+
}
78+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
use Igorsgm\GitHooks\Console\Commands\Hooks\EnlightnPreCommitHook;
4+
use Igorsgm\GitHooks\Facades\GitHooks;
5+
use Igorsgm\GitHooks\Git\ChangedFiles;
6+
use Igorsgm\GitHooks\Traits\GitHelper;
7+
use Illuminate\Support\Facades\Artisan;
8+
use Symfony\Component\Console\Input\ArrayInput;
9+
use Symfony\Component\Console\Output\BufferedOutput;
10+
11+
uses(GitHelper::class);
12+
beforeEach(function () {
13+
$this->gitInit();
14+
$this->initializeTempDirectory(base_path('temp'));
15+
});
16+
17+
test('Skips Enlightn check if there are no files added to commit', function () {
18+
$changedFiles = mock(ChangedFiles::class)
19+
->shouldReceive('getAddedToCommit')
20+
->andReturn(collect())
21+
->getMock();
22+
23+
$next = function ($files) {
24+
return 'passed';
25+
};
26+
27+
$hook = new EnlightnPreCommitHook();
28+
$result = $hook->handle($changedFiles, $next);
29+
expect($result)->toBe('passed');
30+
});
31+
32+
test('Fails commit when Enlightn is not passing', function ($listOfChangedFiles) {
33+
$this->config->set('git-hooks.pre-commit', [
34+
EnlightnPreCommitHook::class,
35+
]);
36+
37+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
38+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfChangedFiles);
39+
40+
Artisan::command('enlightn', function () {
41+
return 1;
42+
});
43+
44+
// Get all registered commands
45+
$commands = Artisan::all();
46+
47+
// Access the 'git-hooks:pre-commit' command instance
48+
$command = $commands['git-hooks:pre-commit'];
49+
50+
$input = new ArrayInput([]);
51+
$output = new BufferedOutput();
52+
53+
$exitCode = $command->run($input, $output);
54+
$outputText = $output->fetch();
55+
56+
$this->assertStringContainsString('COMMIT FAILED', $outputText);
57+
$this->assertStringContainsString('php artisan enlightn', $outputText);
58+
$this->assertEquals(1, $exitCode);
59+
})->with('listOfChangedFiles');
60+
61+
test('Commit passes when Enlightn check is OK', function ($listOfChangedFiles) {
62+
$this->config->set('git-hooks.pre-commit', [
63+
EnlightnPreCommitHook::class,
64+
]);
65+
66+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
67+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfChangedFiles);
68+
69+
Artisan::command('enlightn', function () {
70+
return 0;
71+
});
72+
73+
$this->artisan('git-hooks:pre-commit')->assertSuccessful();
74+
})->with('listOfChangedFiles');

tests/TestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Igorsgm\GitHooks\Tests;
44

5+
use Enlightn\Enlightn\EnlightnServiceProvider;
56
use Igorsgm\GitHooks\Facades\GitHooks;
67
use Igorsgm\GitHooks\GitHooksServiceProvider;
78
use Igorsgm\GitHooks\Tests\Traits\WithTmpFiles;
@@ -56,6 +57,7 @@ protected function getPackageProviders($app)
5657
{
5758
return [
5859
GitHooksServiceProvider::class,
60+
EnlightnServiceProvider::class,
5961
];
6062
}
6163

0 commit comments

Comments
 (0)