Skip to content

Commit c0621bd

Browse files
committed
ESLintPreCommitHook tests
1 parent eab2dc3 commit c0621bd

File tree

9 files changed

+2415
-51
lines changed

9 files changed

+2415
-51
lines changed

package-lock.json

Lines changed: 2298 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
{
22
"devDependencies": {
33
"blade-formatter": "^1.32.9",
4-
"prettier": "2.8.7"
4+
"prettier": "2.8.7",
5+
"eslint": "^8.37.0",
6+
"eslint-config-airbnb-base": "^15.0.0",
7+
"eslint-config-standard": "^17.0.0",
8+
"eslint-plugin-import": "^2.27.5",
9+
"eslint-plugin-n": "^15.7.0",
10+
"eslint-plugin-promise": "^6.1.1",
11+
"eslint-plugin-vue": "^9.10.0"
512
}
613
}

src/Console/Commands/Hooks/ESLintPreCommitHook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected function configParam(): string
7474
* Retrieves additional parameters for the ESLint code analyzer from the configuration file,
7575
* filters out pre-defined parameters to avoid conflicts, and returns them as a string.
7676
*/
77-
protected function additionalParams(): string
77+
protected function additionalParams(): ?string
7878
{
7979
$additionalParams = config('git-hooks.code_analyzers.eslint.additional_params');
8080

src/Console/Commands/Hooks/PrettierPreCommitHook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function configParam(): string
7272
* Retrieves additional parameters for the Prettier code analyzer from the configuration file,
7373
* filters out pre-defined parameters to avoid conflicts, and returns them as a string.
7474
*/
75-
protected function additionalParams(): string
75+
protected function additionalParams(): ?string
7676
{
7777
$additionalParams = config('git-hooks.code_analyzers.prettier.additional_params');
7878

tests/Datasets/PreCommitHooksDataset.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@
6060
],
6161
]);
6262

63+
dataset('eslintConfiguration', [
64+
'.eslintrc.js file & additional params' => [
65+
[
66+
'path' => '../../../../node_modules/.bin/eslint',
67+
'config' => __DIR__.'/../Fixtures/.eslintrcFixture.js',
68+
'additional_params' => '--config',
69+
],
70+
],
71+
]);
72+
6373
$nonExistentPath = [
6474
'path' => 'nonexistent/path',
6575
'phpcs_path' => 'nonexistent/path',
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
use Igorsgm\GitHooks\Console\Commands\Hooks\ESLintPreCommitHook;
4+
use Igorsgm\GitHooks\Facades\GitHooks;
5+
use Igorsgm\GitHooks\Traits\GitHelper;
6+
7+
uses(GitHelper::class);
8+
beforeEach(function () {
9+
$this->gitInit();
10+
$this->initializeTempDirectory(base_path('temp'));
11+
});
12+
13+
test('Skips ESLint check when there is none JS files added to commit', function ($eslintConfiguration, $listOfFixablePHPFiles) {
14+
$this->config->set('git-hooks.code_analyzers.eslint', $eslintConfiguration);
15+
$this->config->set('git-hooks.pre-commit', [
16+
ESLintPreCommitHook::class,
17+
]);
18+
19+
$this->makeTempFile('ClassWithFixableIssues.php',
20+
file_get_contents(__DIR__.'/../../../Fixtures/ClassWithFixableIssues.php')
21+
);
22+
23+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
24+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixablePHPFiles);
25+
26+
$this->artisan('git-hooks:pre-commit')->assertSuccessful();
27+
})->with('eslintConfiguration', 'listOfFixablePhpFiles');
28+
29+
test('Fails commit when ESLint is not passing and user does not autofix the files',
30+
function ($eslintConfiguration, $listOfFixableJSFiles) {
31+
$this->config->set('git-hooks.code_analyzers.eslint', $eslintConfiguration);
32+
$this->config->set('git-hooks.pre-commit', [
33+
ESLintPreCommitHook::class,
34+
]);
35+
36+
$this->makeTempFile('fixable-js-file.js',
37+
file_get_contents(__DIR__.'/../../../Fixtures/fixable-js-file.js')
38+
);
39+
40+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
41+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableJSFiles);
42+
43+
$this->artisan('git-hooks:pre-commit')
44+
->expectsOutputToContain('ESLint Failed')
45+
->expectsOutputToContain('COMMIT FAILED')
46+
->expectsConfirmation('Would you like to attempt to correct files automagically?', 'no')
47+
->assertExitCode(1);
48+
})->with('eslintConfiguration', 'listOfFixableJSFiles');
49+
50+
test('Commit passes when ESLint fixes the files', function ($eslintConfiguration, $listOfFixableJSFiles) {
51+
$this->config->set('git-hooks.code_analyzers.eslint', $eslintConfiguration);
52+
$this->config->set('git-hooks.pre-commit', [
53+
ESLintPreCommitHook::class,
54+
]);
55+
56+
$this->makeTempFile('fixable-js-file.js',
57+
file_get_contents(__DIR__.'/../../../Fixtures/fixable-js-file.js')
58+
);
59+
60+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
61+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableJSFiles);
62+
63+
$this->artisan('git-hooks:pre-commit')
64+
->expectsOutputToContain('ESLint Failed')
65+
->expectsOutputToContain('COMMIT FAILED')
66+
->expectsConfirmation('Would you like to attempt to correct files automagically?', 'yes');
67+
68+
$this->artisan('git-hooks:pre-commit')
69+
->doesntExpectOutputToContain('ESLint Failed')
70+
->assertSuccessful();
71+
})->with('eslintConfiguration', 'listOfFixableJSFiles');

tests/Features/Commands/Hooks/PrettierPreCommitHookTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
$this->initializeTempDirectory(base_path('temp'));
1111
});
1212

13-
test('Skips Prettier check when there is none JS files added to commit', function ($prettierConfiguration, $listOfFixableJSFiles) {
13+
test('Skips Prettier check when there is none JS files added to commit', function ($prettierConfiguration, $listOfFixablePHPFiles) {
1414
$this->config->set('git-hooks.code_analyzers.prettier', $prettierConfiguration);
1515
$this->config->set('git-hooks.pre-commit', [
1616
PrettierPreCommitHook::class,
@@ -21,10 +21,10 @@
2121
);
2222

2323
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
24-
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableJSFiles);
24+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixablePHPFiles);
2525

2626
$this->artisan('git-hooks:pre-commit')->assertSuccessful();
27-
})->with('prettierConfiguration', 'listOfFixableJSFiles');
27+
})->with('prettierConfiguration', 'listOfFixablePhpFiles');
2828

2929
test('Fails commit when Prettier is not passing and user does not autofix the files',
3030
function ($prettierConfiguration, $listOfFixableJSFiles) {

tests/Fixtures/.eslintrcFixture.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
es2021: true,
5+
},
6+
extends: [
7+
'plugin:vue/vue3-essential',
8+
'airbnb-base',
9+
],
10+
overrides: [
11+
],
12+
parserOptions: {
13+
ecmaVersion: 'latest',
14+
sourceType: 'module',
15+
},
16+
plugins: [
17+
'vue',
18+
],
19+
rules: {
20+
},
21+
};

tests/Fixtures/fixable-js-file.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
$(document).ready(function ()
1+
(function test()
22

33
{
44
return false;
55

6-
});
6+
})();

0 commit comments

Comments
 (0)