diff --git a/app/Actions/ElaborateSummary.php b/app/Actions/ElaborateSummary.php index ae861378..7e91f2f7 100644 --- a/app/Actions/ElaborateSummary.php +++ b/app/Actions/ElaborateSummary.php @@ -51,7 +51,7 @@ public function execute($totalFiles, $changes) $this->summaryOutput->handle($summary, $totalFiles); } - $failure = ($summary->isDryRun() && count($changes) > 0) + $failure = (($summary->isDryRun() || $this->input->getOption('repair')) && count($changes) > 0) || count($this->errors->getInvalidErrors()) > 0 || count($this->errors->getExceptionErrors()) > 0 || count($this->errors->getLintErrors()) > 0; diff --git a/app/Commands/DefaultCommand.php b/app/Commands/DefaultCommand.php index 5c6e1002..4d487f12 100644 --- a/app/Commands/DefaultCommand.php +++ b/app/Commands/DefaultCommand.php @@ -40,6 +40,7 @@ protected function configure() new InputOption('preset', '', InputOption::VALUE_REQUIRED, 'The preset that should be used'), new InputOption('test', '', InputOption::VALUE_NONE, 'Test for code style errors without fixing them'), new InputOption('bail', '', InputOption::VALUE_NONE, 'Test for code style errors without fixing them and stop on first error'), + new InputOption('repair', '', InputOption::VALUE_NONE, 'Fix code style errors but exit with status 1 if there were any changes made'), new InputOption('dirty', '', InputOption::VALUE_NONE, 'Only fix files that have uncommitted changes'), new InputOption('format', '', InputOption::VALUE_REQUIRED, 'The output format that should be used'), new InputOption('cache-file', '', InputArgument::OPTIONAL, 'The path to the cache file'), diff --git a/tests/Feature/RepairTest.php b/tests/Feature/RepairTest.php new file mode 100644 index 00000000..2316e659 --- /dev/null +++ b/tests/Feature/RepairTest.php @@ -0,0 +1,34 @@ +contents = file_get_contents(base_path('tests/Fixtures/with-fixable-issues/file.php')); +}); + +afterEach(function () { + file_put_contents(base_path('tests/Fixtures/with-fixable-issues/file.php'), $this->contents); +}); + +it('exits with status 1 with fixes', function () { + [$statusCode, $output] = run('default', [ + 'path' => base_path('tests/Fixtures/with-fixable-issues'), + '--preset' => 'psr12', + '--repair' => true, + '--test' => false, + ]); + + expect($statusCode)->toBe(1) + ->and($output) + ->toContain('FIXED'); +}); + +it('exits with status 0 without fixes', function () { + [$statusCode, $output] = run('default', [ + 'path' => base_path('tests/Fixtures/without-issues'), + '--repair' => true, + '--test' => false, + ]); + + expect($statusCode)->toBe(0) + ->and($output) + ->toContain('PASS'); +});