diff --git a/src/Runner.php b/src/Runner.php index 23bc4d90bc..6104a4ab6f 100644 --- a/src/Runner.php +++ b/src/Runner.php @@ -361,6 +361,13 @@ private function run() } }//end if + $numFiles = count($todo); + if ($numFiles === 0) { + $error = 'ERROR: No files were checked.'.PHP_EOL; + $error .= 'All specified files were excluded or did not match filtering rules.'.PHP_EOL.PHP_EOL; + throw new DeepExitException($error, 3); + } + // Turn all sniff errors into exceptions. set_error_handler([$this, 'handleErrors']); @@ -375,8 +382,7 @@ private function run() $this->config->parallel = 1; } - $lastDir = ''; - $numFiles = count($todo); + $lastDir = ''; if ($this->config->parallel === 1) { // Running normally. diff --git a/tests/Core/Runner/Fixtures/AllFilesExcludedSetA/placeholder.inc b/tests/Core/Runner/Fixtures/AllFilesExcludedSetA/placeholder.inc new file mode 100644 index 0000000000..27619be827 --- /dev/null +++ b/tests/Core/Runner/Fixtures/AllFilesExcludedSetA/placeholder.inc @@ -0,0 +1,8 @@ + $extraArgs Any extra arguments to pass on the command line. + * + * @dataProvider data + * + * @return void + */ + public function testPhpcs($sourceDir, $extraArgs) + { + if (PHP_CODESNIFFER_CBF === true) { + $this->markTestSkipped('This test needs CS mode to run'); + } + + $this->setupTest($sourceDir, $extraArgs); + + $runner = new Runner(); + $runner->runPHPCS(); + + }//end testPhpcs() + + + /** + * Verify that the "All files were excluded" error message is shown when all files are excluded (PHPCBF). + * + * @param string $sourceDir The (fixture) directory to scan for files. + * @param array $extraArgs Any extra arguments to pass on the command line. + * + * @dataProvider data + * @group CBF + * + * @return void + */ + public function testPhpcbf($sourceDir, $extraArgs) + { + if (PHP_CODESNIFFER_CBF === false) { + $this->markTestSkipped('This test needs CBF mode to run'); + } + + $this->setupTest($sourceDir, $extraArgs); + + $runner = new Runner(); + $runner->runPHPCBF(); + + }//end testPhpcbf() + + + /** + * Data provider. + * + * @return array>> + */ + public static function data() + { + return [ + 'All files filtered out via extension' => [ + 'sourceDir' => __DIR__.'/Fixtures/AllFilesExcludedSetA/', + 'extraArgs' => ['--extensions=php'], + ], + 'All files filtered out via ignore pattern' => [ + 'sourceDir' => __DIR__.'/Fixtures/AllFilesExcludedSetB/', + 'extraArgs' => ['--ignore=/place*\.php'], + ], + ]; + + }//end data() + + + /** + * Helper method to prepare the test. + * + * @param string $sourceDir The (fixture) directory to scan for files. + * @param array $extraArgs Any extra arguments to pass on the command line. + * + * @return void + */ + private function setupTest($sourceDir, $extraArgs) + { + $_SERVER['argv'] = [ + 'phpcs', + $sourceDir, + '--standard=PSR1', + '--report-width=80', + ]; + + foreach ($extraArgs as $arg) { + $_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); + + }//end setupTest() + + +}//end class