Skip to content

Commit 0db9279

Browse files
committed
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
1 parent fce7295 commit 0db9279

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/Runner.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,17 @@ public function runPHPCS()
126126
Timing::printRunTime();
127127
}
128128
} catch (DeepExitException $e) {
129-
echo $e->getMessage();
130-
return $e->getCode();
129+
$exitCode = $e->getCode();
130+
$message = $e->getMessage();
131+
if ($message !== '') {
132+
if ($exitCode === 0) {
133+
echo $e->getMessage();
134+
} else {
135+
StatusWriter::write($e->getMessage(), 0, 0);
136+
}
137+
}
138+
139+
return $exitCode;
131140
}//end try
132141

133142
if ($numErrors === 0) {
@@ -212,8 +221,17 @@ public function runPHPCBF()
212221
Timing::printRunTime();
213222
}
214223
} catch (DeepExitException $e) {
215-
echo $e->getMessage();
216-
return $e->getCode();
224+
$exitCode = $e->getCode();
225+
$message = $e->getMessage();
226+
if ($message !== '') {
227+
if ($exitCode === 0) {
228+
echo $e->getMessage();
229+
} else {
230+
StatusWriter::write($e->getMessage(), 0, 0);
231+
}
232+
}
233+
234+
return $exitCode;
217235
}//end try
218236

219237
if ($this->reporter->totalFixed === 0) {

tests/Core/Runner/RunAllFilesExcludedErrorTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use PHP_CodeSniffer\Runner;
1212
use PHP_CodeSniffer\Tests\Core\Runner\AbstractRunnerTestCase;
13+
use PHP_CodeSniffer\Tests\Core\StatusWriterTestHelper;
1314

1415
/**
1516
* Tests for the "All files were excluded" error message.
@@ -18,6 +19,7 @@
1819
*/
1920
final class RunAllFilesExcludedErrorTest extends AbstractRunnerTestCase
2021
{
22+
use StatusWriterTestHelper;
2123

2224

2325
/**
@@ -41,6 +43,8 @@ public function testPhpcs($sourceDir, $extraArgs)
4143
$runner = new Runner();
4244
$runner->runPHPCS();
4345

46+
$this->verifyOutput();
47+
4448
}//end testPhpcs()
4549

4650

@@ -66,6 +70,8 @@ public function testPhpcbf($sourceDir, $extraArgs)
6670
$runner = new Runner();
6771
$runner->runPHPCBF();
6872

73+
$this->verifyOutput();
74+
6975
}//end testPhpcbf()
7076

7177

@@ -111,12 +117,24 @@ private function setupTest($sourceDir, $extraArgs)
111117
$_SERVER['argv'][] = $arg;
112118
}
113119

114-
$message = 'ERROR: No files were checked.'.PHP_EOL;
115-
$message .= 'All specified files were excluded or did not match filtering rules.'.PHP_EOL.PHP_EOL;
116-
117-
$this->expectOutputString($message);
120+
$this->expectNoStdoutOutput();
118121

119122
}//end setupTest()
120123

121124

125+
/**
126+
* Helper method to verify the output expectation for STDERR.
127+
*
128+
* @return void
129+
*/
130+
private function verifyOutput()
131+
{
132+
$expected = 'ERROR: No files were checked.'.PHP_EOL;
133+
$expected .= 'All specified files were excluded or did not match filtering rules.'.PHP_EOL.PHP_EOL;
134+
135+
$this->assertStderrOutputSameString($expected);
136+
137+
}//end verifyOutput()
138+
139+
122140
}//end class

0 commit comments

Comments
 (0)