From 4cfbf177ae90a6d51af295191ac163f4d851505f Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 26 Apr 2025 15:39:51 +0200 Subject: [PATCH] Runner: use StatusWriter for caught errors Errors should go to `STDERR`. This was mostly handled in PR 1010. However, the `Runner` class contains two `catch (DeepExitException $e)` blocks which are used to exit "early", both for legitimate requests (`--version`, `-i` etc), as well as for error output. This commit changes how the output is handled in these catch blocks, to differentiate between legitimate requests (exit code `0`) and error output, sending legitimate output to `STDOUT` and sending error output to `STDERR`. Includes updating a unit test to reflect the change. Related to squizlabs/PHP_CodeSniffer 1612 --- src/Runner.php | 26 ++++++++++++++++--- .../Runner/RunAllFilesExcludedErrorTest.php | 26 ++++++++++++++++--- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/Runner.php b/src/Runner.php index 351a09afaf..8e54783f4b 100644 --- a/src/Runner.php +++ b/src/Runner.php @@ -126,8 +126,17 @@ public function runPHPCS() Timing::printRunTime(); } } catch (DeepExitException $e) { - echo $e->getMessage(); - return $e->getCode(); + $exitCode = $e->getCode(); + $message = $e->getMessage(); + if ($message !== '') { + if ($exitCode === 0) { + echo $e->getMessage(); + } else { + StatusWriter::write($e->getMessage(), 0, 0); + } + } + + return $exitCode; }//end try if ($numErrors === 0) { @@ -212,8 +221,17 @@ public function runPHPCBF() Timing::printRunTime(); } } catch (DeepExitException $e) { - echo $e->getMessage(); - return $e->getCode(); + $exitCode = $e->getCode(); + $message = $e->getMessage(); + if ($message !== '') { + if ($exitCode === 0) { + echo $e->getMessage(); + } else { + StatusWriter::write($e->getMessage(), 0, 0); + } + } + + return $exitCode; }//end try if ($this->reporter->totalFixed === 0) { diff --git a/tests/Core/Runner/RunAllFilesExcludedErrorTest.php b/tests/Core/Runner/RunAllFilesExcludedErrorTest.php index 844ee7dd56..36ff3dedb5 100644 --- a/tests/Core/Runner/RunAllFilesExcludedErrorTest.php +++ b/tests/Core/Runner/RunAllFilesExcludedErrorTest.php @@ -10,6 +10,7 @@ use PHP_CodeSniffer\Runner; use PHP_CodeSniffer\Tests\Core\Runner\AbstractRunnerTestCase; +use PHP_CodeSniffer\Tests\Core\StatusWriterTestHelper; /** * Tests for the "All files were excluded" error message. @@ -18,6 +19,7 @@ */ final class RunAllFilesExcludedErrorTest extends AbstractRunnerTestCase { + use StatusWriterTestHelper; /** @@ -41,6 +43,8 @@ public function testPhpcs($sourceDir, $extraArgs) $runner = new Runner(); $runner->runPHPCS(); + $this->verifyOutput(); + }//end testPhpcs() @@ -66,6 +70,8 @@ public function testPhpcbf($sourceDir, $extraArgs) $runner = new Runner(); $runner->runPHPCBF(); + $this->verifyOutput(); + }//end testPhpcbf() @@ -111,12 +117,24 @@ private function setupTest($sourceDir, $extraArgs) $_SERVER['argv'][] = $arg; } - $message = 'ERROR: No files were checked.'.PHP_EOL; - $message .= 'All specified files were excluded or did not match filtering rules.'.PHP_EOL.PHP_EOL; - - $this->expectOutputString($message); + $this->expectNoStdoutOutput(); }//end setupTest() + /** + * Helper method to verify the output expectation for STDERR. + * + * @return void + */ + private function verifyOutput() + { + $expected = 'ERROR: No files were checked.'.PHP_EOL; + $expected .= 'All specified files were excluded or did not match filtering rules.'.PHP_EOL.PHP_EOL; + + $this->assertStderrOutputSameString($expected); + + }//end verifyOutput() + + }//end class