Skip to content

Runner: use StatusWriter for caught errors #1062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
26 changes: 22 additions & 4 deletions tests/Core/Runner/RunAllFilesExcludedErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -18,6 +19,7 @@
*/
final class RunAllFilesExcludedErrorTest extends AbstractRunnerTestCase
{
use StatusWriterTestHelper;


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

$this->verifyOutput();

}//end testPhpcs()


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

$this->verifyOutput();

}//end testPhpcbf()


Expand Down Expand Up @@ -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