Skip to content

Commit

Permalink
Updated readme and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cradu committed Apr 15, 2024
1 parent f027910 commit 75e4c97
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ There are also several debug options which can be adjusted using the following p
*/
'debug_output' => false,
```
### Docker support

By default commands are executed locally, however this behavior can be adjusted for each hook using the parameters `run_in_docker` and `docker_container`:

```php
'run_in_docker' => env('LARAVEL_PINT_RUN_IN_DOCKER', true),
'docker_container' => env('LARAVEL_PINT_DOCKER_CONTAINER', 'app'),
```

### Creating Custom Git Hooks
1) If you need to create a custom Git hook for your project, Laravel Git Hooks makes it easy with the `git-hooks:make` Artisan command. To create a new custom hook, simply run the following command:
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
},
"require-dev": {
"enlightn/enlightn": "^2.3",
"larastan/larastan": "^2.9",
"laravel/pint": "^1.2",
"mockery/mockery": "^1.5.1",
"larastan/larastan": "^2.9",
"orchestra/testbench": "^v8.0.0|^v9.0.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-laravel": "^2.3",
"squizlabs/php_codesniffer": "^3.7"
},
"autoload": {
Expand Down
108 changes: 108 additions & 0 deletions tests/Features/Commands/Hooks/PintPreCommitHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,111 @@ function ($pintConfiguration, $listOfFixablePhpFiles) {
->doesntExpectOutputToContain('Pint Failed')
->assertSuccessful();
})->with('pintConfiguration', 'listOfFixablePhpFiles');

test('Commit passes when Pint fixes the files automatically', function ($pintConfiguration, $listOfFixablePhpFiles) {
$this->config->set('git-hooks.code_analyzers.laravel_pint', $pintConfiguration);
$this->config->set('git-hooks.pre-commit', [
PintPreCommitHook::class,
]);
$this->config->set('git-hooks.automatically_fix_errors', true);

$this->makeTempFile('ClassWithFixableIssues.php',
file_get_contents(__DIR__.'/../../../Fixtures/ClassWithFixableIssues.php')
);

GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixablePhpFiles);

$this->artisan('git-hooks:pre-commit')
->expectsOutputToContain('Pint Failed')
->expectsOutputToContain('COMMIT FAILED')
->expectsOutputToContain('AUTOFIX')
->assertSuccessful();

$this->artisan('git-hooks:pre-commit')
->doesntExpectOutputToContain('Pint Failed')
->assertSuccessful();
})->with('pintConfiguration', 'listOfFixablePhpFiles');

test('Commit passes when Pint fixes the files automatically with analyzer rerun', function ($pintConfiguration, $listOfFixablePhpFiles) {
$this->config->set('git-hooks.code_analyzers.laravel_pint', $pintConfiguration);
$this->config->set('git-hooks.pre-commit', [
PintPreCommitHook::class,
]);
$this->config->set('git-hooks.automatically_fix_errors', true);
$this->config->set('git-hooks.rerun_analyzer_after_autofix', true);
$this->config->set('git-hooks.debug_commands', true);

$this->makeTempFile('ClassWithFixableIssues.php',
file_get_contents(__DIR__.'/../../../Fixtures/ClassWithFixableIssues.php')
);

GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixablePhpFiles);

$this->artisan('git-hooks:pre-commit')
->expectsOutputToContain('Pint Failed')
->expectsOutputToContain('COMMIT FAILED')
->expectsOutputToContain('AUTOFIX')
->doesntExpectOutputToContain('Pint Failed')
->assertSuccessful();

$this->artisan('git-hooks:pre-commit')
->doesntExpectOutputToContain('Pint Failed')
->assertSuccessful();
})->with('pintConfiguration', 'listOfFixablePhpFiles');

test('Commit passes when Pint fixes the files automatically with debug commands', function ($pintConfiguration, $listOfFixablePhpFiles) {
$this->config->set('git-hooks.code_analyzers.laravel_pint', $pintConfiguration);
$this->config->set('git-hooks.pre-commit', [
PintPreCommitHook::class,
]);
$this->config->set('git-hooks.automatically_fix_errors', true);
$this->config->set('git-hooks.debug_commands', true);

$this->makeTempFile('ClassWithFixableIssues.php',
file_get_contents(__DIR__.'/../../../Fixtures/ClassWithFixableIssues.php')
);

GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixablePhpFiles);

$this->artisan('git-hooks:pre-commit')
->expectsOutputToContain('Pint Failed')
->expectsOutputToContain('COMMIT FAILED')
->expectsOutputToContain('AUTOFIX')
->doesntExpectOutputToContain('Pint Failed')
->assertSuccessful();

$this->artisan('git-hooks:pre-commit')
->doesntExpectOutputToContain('Pint Failed')
->assertSuccessful();
})->with('pintConfiguration', 'listOfFixablePhpFiles');

test('Commit passes when Pint fixes the files automatically with output errors', function ($pintConfiguration, $listOfFixablePhpFiles) {
$this->config->set('git-hooks.code_analyzers.laravel_pint', $pintConfiguration);
$this->config->set('git-hooks.pre-commit', [
PintPreCommitHook::class,
]);
$this->config->set('git-hooks.automatically_fix_errors', true);
$this->config->set('git-hooks.output_errors', true);
$this->config->set('git-hooks.debug_commands', false);

$this->makeTempFile('ClassWithFixableIssues.php',
file_get_contents(__DIR__.'/../../../Fixtures/ClassWithFixableIssues.php')
);

GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixablePhpFiles);

$this->artisan('git-hooks:pre-commit')
->expectsOutputToContain('Pint Failed')
->expectsOutputToContain('COMMIT FAILED')
->expectsOutputToContain('AUTOFIX')
->doesntExpectOutputToContain('Pint Failed')
->assertSuccessful();

$this->artisan('git-hooks:pre-commit')
->doesntExpectOutputToContain('Pint Failed')
->assertSuccessful();
})->with('pintConfiguration', 'listOfFixablePhpFiles');
110 changes: 109 additions & 1 deletion tests/Features/Commands/Hooks/PrettierPreCommitHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function ($prettierConfiguration, $listOfFixableJSFiles) {
->assertExitCode(1);
})->with('prettierConfiguration', 'listOfFixableJSFiles');

test('Commit passes when Prettier fixes the files', function ($prettierConfiguration, $listOfFixableJSFiles) {
test('Commit passes when Prettier fixes the files with CLI confirmation', function ($prettierConfiguration, $listOfFixableJSFiles) {
$this->config->set('git-hooks.code_analyzers.prettier', $prettierConfiguration);
$this->config->set('git-hooks.pre-commit', [
PrettierPreCommitHook::class,
Expand All @@ -67,3 +67,111 @@ function ($prettierConfiguration, $listOfFixableJSFiles) {
->doesntExpectOutputToContain('Prettier Failed')
->assertSuccessful();
})->with('prettierConfiguration', 'listOfFixableJSFiles');

test('Commit passes when Prettier fixes the files automatically', function ($prettierConfiguration, $listOfFixableJSFiles) {
$this->config->set('git-hooks.code_analyzers.prettier', $prettierConfiguration);
$this->config->set('git-hooks.pre-commit', [
PrettierPreCommitHook::class,
]);
$this->config->set('git-hooks.automatically_fix_errors', true);

$this->makeTempFile('fixable-js-file.js',
file_get_contents(__DIR__.'/../../../Fixtures/fixable-js-file.js')
);

GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableJSFiles);

$this->artisan('git-hooks:pre-commit')
->expectsOutputToContain('Prettier Failed')
->expectsOutputToContain('COMMIT FAILED')
->expectsOutputToContain('AUTOFIX')
->assertSuccessful();

$this->artisan('git-hooks:pre-commit')
->doesntExpectOutputToContain('Prettier Failed')
->assertSuccessful();
})->with('prettierConfiguration', 'listOfFixableJSFiles');

test('Commit passes when Prettier fixes the files automatically with analyzer rerun', function ($prettierConfiguration, $listOfFixableJSFiles) {
$this->config->set('git-hooks.code_analyzers.prettier', $prettierConfiguration);
$this->config->set('git-hooks.pre-commit', [
PrettierPreCommitHook::class,
]);
$this->config->set('git-hooks.automatically_fix_errors', true);
$this->config->set('git-hooks.rerun_analyzer_after_autofix', true);
$this->config->set('git-hooks.debug_commands', true);

$this->makeTempFile('fixable-js-file.js',
file_get_contents(__DIR__.'/../../../Fixtures/fixable-js-file.js')
);

GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableJSFiles);

$this->artisan('git-hooks:pre-commit')
->expectsOutputToContain('Prettier Failed')
->expectsOutputToContain('COMMIT FAILED')
->expectsOutputToContain('AUTOFIX')
->doesntExpectOutputToContain('Prettier Failed')
->assertSuccessful();

$this->artisan('git-hooks:pre-commit')
->doesntExpectOutputToContain('Prettier Failed')
->assertSuccessful();
})->with('prettierConfiguration', 'listOfFixableJSFiles');

test('Commit passes when Prettier fixes the files automatically with debug commands', function ($prettierConfiguration, $listOfFixableJSFiles) {
$this->config->set('git-hooks.code_analyzers.prettier', $prettierConfiguration);
$this->config->set('git-hooks.pre-commit', [
PrettierPreCommitHook::class,
]);
$this->config->set('git-hooks.automatically_fix_errors', true);
$this->config->set('git-hooks.debug_commands', true);

$this->makeTempFile('fixable-js-file.js',
file_get_contents(__DIR__.'/../../../Fixtures/fixable-js-file.js')
);

GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableJSFiles);

$this->artisan('git-hooks:pre-commit')
->expectsOutputToContain('Prettier Failed')
->expectsOutputToContain('COMMIT FAILED')
->expectsOutputToContain('AUTOFIX')
->doesntExpectOutputToContain('Prettier Failed')
->assertSuccessful();

$this->artisan('git-hooks:pre-commit')
->doesntExpectOutputToContain('Prettier Failed')
->assertSuccessful();
})->with('prettierConfiguration', 'listOfFixableJSFiles');

test('Commit passes when Prettier fixes the files automatically with output errors', function ($prettierConfiguration, $listOfFixableJSFiles) {
$this->config->set('git-hooks.code_analyzers.prettier', $prettierConfiguration);
$this->config->set('git-hooks.pre-commit', [
PrettierPreCommitHook::class,
]);
$this->config->set('git-hooks.automatically_fix_errors', true);
$this->config->set('git-hooks.output_errors', true);
$this->config->set('git-hooks.debug_commands', false);

$this->makeTempFile('fixable-js-file.js',
file_get_contents(__DIR__.'/../../../Fixtures/fixable-js-file.js')
);

GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableJSFiles);

$this->artisan('git-hooks:pre-commit')
->expectsOutputToContain('Prettier Failed')
->expectsOutputToContain('COMMIT FAILED')
->expectsOutputToContain('AUTOFIX')
->doesntExpectOutputToContain('Prettier Failed')
->assertSuccessful();

$this->artisan('git-hooks:pre-commit')
->doesntExpectOutputToContain('Prettier Failed')
->assertSuccessful();
})->with('prettierConfiguration', 'listOfFixableJSFiles');

0 comments on commit 75e4c97

Please sign in to comment.