|
| 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'); |
0 commit comments