diff --git a/README.md b/README.md index 287ab17..9465283 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/composer.json b/composer.json index 7853733..5180894 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/tests/Features/Commands/Hooks/PintPreCommitHookTest.php b/tests/Features/Commands/Hooks/PintPreCommitHookTest.php index 068817e..41dcf12 100644 --- a/tests/Features/Commands/Hooks/PintPreCommitHookTest.php +++ b/tests/Features/Commands/Hooks/PintPreCommitHookTest.php @@ -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'); diff --git a/tests/Features/Commands/Hooks/PrettierPreCommitHookTest.php b/tests/Features/Commands/Hooks/PrettierPreCommitHookTest.php index 6110504..18623c9 100644 --- a/tests/Features/Commands/Hooks/PrettierPreCommitHookTest.php +++ b/tests/Features/Commands/Hooks/PrettierPreCommitHookTest.php @@ -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, @@ -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');