Skip to content

Commit 9e27df2

Browse files
authored
Merge pull request #16 from igorsgm/develop
PrettierPreCommitHook and Pest upgrade to v2.0
2 parents 440920f + 20a5bf2 commit 9e27df2

17 files changed

+266
-55
lines changed

composer.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@
3333
"laravel/pint": "^1.2",
3434
"mockery/mockery": "^1.5.1",
3535
"nunomaduro/larastan": "^2.0",
36-
"orchestra/testbench": "^7.0",
37-
"pestphp/pest": "^1.22",
38-
"pestphp/pest-plugin-mock": "^1.0",
39-
"phpunit/phpunit": "^9.0",
36+
"orchestra/testbench": "^v8.0.0",
37+
"pestphp/pest": "^2.0",
38+
"pestphp/pest-plugin-mock": "^2.0",
4039
"squizlabs/php_codesniffer": "^3.7"
4140
},
4241
"autoload": {
@@ -50,8 +49,8 @@
5049
}
5150
},
5251
"scripts": {
53-
"test": "vendor/bin/phpunit",
54-
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
52+
"test": "vendor/bin/pest",
53+
"test-coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage"
5554
},
5655
"config": {
5756
"sort-packages": true,

config/git-hooks.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@
187187
'path' => env('BLADE_FORMATTER_PATH', 'node_modules/.bin/blade-formatter'),
188188
'config' => env('BLADE_FORMATTER_CONFIG', '.bladeformatterrc.json'),
189189
],
190+
'prettier' => [
191+
'path' => env('PRETTIER_PATH', 'node_modules/.bin/prettier'),
192+
'config' => env('PRETTIER_CONFIG', '.prettierrc.json'),
193+
'additional_params' => env('PRETTIER_ADDITIONAL_PARAMS', ''),
194+
],
190195
],
191196

192197
];

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"devDependencies": {
3-
"blade-formatter": "^1.32.9"
3+
"blade-formatter": "^1.32.9",
4+
"prettier": "2.8.7"
45
}
56
}

phpunit.xml

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
6-
>
7-
<testsuites>
8-
<testsuite name="Test Suite">
9-
<directory suffix="Test.php">./tests</directory>
10-
</testsuite>
11-
</testsuites>
12-
<coverage processUncoveredFiles="true">
13-
<include>
14-
<directory suffix=".php">./app</directory>
15-
<directory suffix=".php">./src</directory>
16-
</include>
17-
</coverage>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
3+
<testsuites>
4+
<testsuite name="Test Suite">
5+
<directory suffix="Test.php">./tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<coverage>
9+
<include>
10+
<directory suffix=".php">./app</directory>
11+
<directory suffix=".php">./src</directory>
12+
</include>
13+
</coverage>
1814
</phpunit>

src/Console/Commands/Hooks/BaseCodeAnalyzerPreCommitHook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ protected function commitFailMessage()
150150

151151
$message = '<bg=red;fg=white> COMMIT FAILED </> ';
152152
$message .= sprintf("Your commit contains files that should pass %s but do not. Please fix the errors in the files above and try again.\n", $this->getName());
153-
$message .= sprintf("You can check which %s errors happened in them by executing: <comment>%s {filePath}</comment>", $this->getName(), $this->analyzerCommand());
153+
$message .= sprintf('You can check which %s errors happened in them by executing: <comment>%s {filePath}</comment>', $this->getName(), $this->analyzerCommand());
154154

155155
$this->command->getOutput()->writeln($message);
156156

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Igorsgm\GitHooks\Console\Commands\Hooks;
4+
5+
use Closure;
6+
use Igorsgm\GitHooks\Contracts\CodeAnalyzerPreCommitHook;
7+
use Igorsgm\GitHooks\Git\ChangedFiles;
8+
9+
class PrettierPreCommitHook extends BaseCodeAnalyzerPreCommitHook implements CodeAnalyzerPreCommitHook
10+
{
11+
/**
12+
* @var string
13+
*/
14+
protected $configParam;
15+
16+
/**
17+
* Name of the hook
18+
*
19+
* @var string
20+
*/
21+
protected $name = 'Prettier';
22+
23+
/**
24+
* Analyze and fix committed JS files using Prettier.
25+
*
26+
* @param ChangedFiles $files The files that have been changed in the current commit.
27+
* @param Closure $next A closure that represents the next middleware in the pipeline.
28+
* @return mixed|null
29+
*/
30+
public function handle(ChangedFiles $files, Closure $next)
31+
{
32+
$this->configParam = $this->configParam();
33+
34+
return $this->setFileExtensions('/\.(jsx?|tsx?|vue)$/')
35+
->setAnalyzerExecutable(config('git-hooks.code_analyzers.prettier.path'), true)
36+
->handleCommittedFiles($files, $next);
37+
}
38+
39+
/**
40+
* Returns the command to run Prettier tester
41+
*/
42+
public function analyzerCommand(): string
43+
{
44+
return trim(
45+
sprintf('%s --check %s %s', $this->getAnalyzerExecutable(), $this->configParam, $this->additionalParams())
46+
);
47+
}
48+
49+
/**
50+
* Returns the command to run Prettier fixer
51+
*/
52+
public function fixerCommand(): string
53+
{
54+
return trim(
55+
sprintf('%s --write %s %s', $this->getFixerExecutable(), $this->configParam, $this->additionalParams())
56+
);
57+
}
58+
59+
/**
60+
* Gets the command-line parameter for specifying the configuration file for Prettier.
61+
*
62+
* @return string The command-line parameter for the configuration file, or an empty string if not set.
63+
*/
64+
protected function configParam(): string
65+
{
66+
$prettierConfig = rtrim(config('git-hooks.code_analyzers.prettier.config'), '/');
67+
68+
return empty($prettierConfig) ? '' : '--config='.$prettierConfig;
69+
}
70+
71+
/**
72+
* Retrieves additional parameters for the Prettier code analyzer from the configuration file,
73+
* filters out pre-defined parameters to avoid conflicts, and returns them as a string.
74+
*/
75+
protected function additionalParams(): string
76+
{
77+
$additionalParams = config('git-hooks.code_analyzers.prettier.additional_params');
78+
79+
if (! empty($additionalParams)) {
80+
$additionalParams = preg_replace('/\s+\.(?:(\s)|$)/', '$1', $additionalParams);
81+
$additionalParams = preg_replace('/\s*--(config|find-config-path|write|check)\b(=\S*)?\s*/', '', $additionalParams);
82+
}
83+
84+
return $additionalParams;
85+
}
86+
}

tests/Datasets/GitLogsDataset.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@
2424
'List Of Changed Files' => 'AM src/ChangedFiles.php',
2525
]);
2626

27-
dataset('listOfFixableFiles', [
27+
dataset('listOfFixablePhpFiles', [
2828
'List of Fixable Files' => implode(PHP_EOL, [
2929
'AM temp/ClassWithFixableIssues.php',
3030
'AM temp/fixable-blade-file.blade.php',
3131
]),
3232
]);
33+
34+
dataset('listOfFixableJSFiles', [
35+
'List of Fixable Files' => implode(PHP_EOL, [
36+
'AM temp/fixable-js-file.js',
37+
]),
38+
]);

tests/Datasets/PreCommitHooksDataset.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Igorsgm\GitHooks\Console\Commands\Hooks\LarastanPreCommitHook;
55
use Igorsgm\GitHooks\Console\Commands\Hooks\PHPCodeSnifferPreCommitHook;
66
use Igorsgm\GitHooks\Console\Commands\Hooks\PintPreCommitHook;
7+
use Igorsgm\GitHooks\Console\Commands\Hooks\PrettierPreCommitHook;
78

89
dataset('pintConfigurations', [
910
'Config File' => [
@@ -49,6 +50,16 @@
4950
],
5051
]);
5152

53+
dataset('prettierConfiguration', [
54+
'.prettierrc.json file & additional params' => [
55+
[
56+
'path' => '../../../../node_modules/.bin/prettier',
57+
'config' => __DIR__.'/../Fixtures/.prettierrcFixture.json',
58+
'additional_params' => '--config --find-config-path',
59+
],
60+
],
61+
]);
62+
5263
$nonExistentPath = [
5364
'path' => 'nonexistent/path',
5465
'phpcs_path' => 'nonexistent/path',
@@ -75,4 +86,9 @@
7586
$nonExistentPath,
7687
LarastanPreCommitHook::class,
7788
],
89+
'Prettier' => [
90+
'prettier',
91+
$nonExistentPath,
92+
PrettierPreCommitHook::class,
93+
],
7894
]);

tests/Features/Commands/Hooks/BaseCodeAnalyzerPreCommitHookTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@
4040
})->with('modifiedFilesList');
4141

4242
test('Throws HookFailException and notifies when Code Analyzer is not installed',
43-
function ($configName, $nonExistentPathConfig, $preCommitHookClass, $listOfFixableFiles) {
43+
function ($configName, $nonExistentPathConfig, $preCommitHookClass, $listOfFixablePhpFiles) {
4444
$this->config->set('git-hooks.code_analyzers.'.$configName, $nonExistentPathConfig);
4545

4646
$this->config->set('git-hooks.pre-commit', [
4747
$preCommitHookClass,
4848
]);
4949

5050
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
51-
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableFiles);
51+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixablePhpFiles);
5252

5353
$preCommitHook = new $preCommitHookClass();
5454
$this->artisan('git-hooks:pre-commit')
5555
->expectsOutputToContain($preCommitHook->getName().' is not installed.')
5656
->assertExitCode(1);
57-
})->with('codeAnalyzersList', 'listOfFixableFiles');
57+
})->with('codeAnalyzersList', 'listOfFixablePhpFiles');

0 commit comments

Comments
 (0)