Skip to content

Commit 8ac3cad

Browse files
authored
Merge pull request #1023 from PHPCSStandards/phpcs-4.0/feature/sq-1595-display-error-on-no-files-checked
Runner: error message if no files were checked during a run
2 parents 156740b + 14cc840 commit 8ac3cad

File tree

9 files changed

+178
-2
lines changed

9 files changed

+178
-2
lines changed

src/Runner.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@ private function run()
361361
}
362362
}//end if
363363

364+
$numFiles = count($todo);
365+
if ($numFiles === 0) {
366+
$error = 'ERROR: No files were checked.'.PHP_EOL;
367+
$error .= 'All specified files were excluded or did not match filtering rules.'.PHP_EOL.PHP_EOL;
368+
throw new DeepExitException($error, 3);
369+
}
370+
364371
// Turn all sniff errors into exceptions.
365372
set_error_handler([$this, 'handleErrors']);
366373

@@ -375,8 +382,7 @@ private function run()
375382
$this->config->parallel = 1;
376383
}
377384

378-
$lastDir = '';
379-
$numFiles = count($todo);
385+
$lastDir = '';
380386

381387
if ($this->config->parallel === 1) {
382388
// Running normally.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Runner\RunAllFilesExcludedErrorTest
6+
*/
7+
8+
echo 'hello';

tests/Core/Runner/Fixtures/AllFilesExcludedSetA/placeholder.js

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Runner\RunAllFilesExcludedErrorTest
6+
*/
7+
8+
echo 'hello';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Runner\RunAllFilesExcludedErrorTest
6+
*/
7+
8+
echo 'hello';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Runner\RunAllFilesExcludedErrorTest
6+
*/
7+
8+
echo 'hello';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Runner\RunAllFilesExcludedErrorTest
6+
*/
7+
8+
echo 'hello';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Runner\RunAllFilesExcludedErrorTest
6+
*/
7+
8+
echo 'hello';
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
/**
3+
* Tests for the "All files were excluded" error message.
4+
*
5+
* @copyright 2025 PHPCSStandards and contributors
6+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
7+
*/
8+
9+
namespace PHP_CodeSniffer\Tests\Core\Runner;
10+
11+
use PHP_CodeSniffer\Runner;
12+
use PHP_CodeSniffer\Tests\Core\Runner\AbstractRunnerTestCase;
13+
14+
/**
15+
* Tests for the "All files were excluded" error message.
16+
*
17+
* @covers \PHP_CodeSniffer\Runner::run
18+
*/
19+
final class RunAllFilesExcludedErrorTest extends AbstractRunnerTestCase
20+
{
21+
22+
23+
/**
24+
* Verify that the "All files were excluded" error message is shown when all files are excluded (PHPCS).
25+
*
26+
* @param string $sourceDir The (fixture) directory to scan for files.
27+
* @param array<string> $extraArgs Any extra arguments to pass on the command line.
28+
*
29+
* @dataProvider data
30+
*
31+
* @return void
32+
*/
33+
public function testPhpcs($sourceDir, $extraArgs)
34+
{
35+
if (PHP_CODESNIFFER_CBF === true) {
36+
$this->markTestSkipped('This test needs CS mode to run');
37+
}
38+
39+
$this->setupTest($sourceDir, $extraArgs);
40+
41+
$runner = new Runner();
42+
$runner->runPHPCS();
43+
44+
}//end testPhpcs()
45+
46+
47+
/**
48+
* Verify that the "All files were excluded" error message is shown when all files are excluded (PHPCBF).
49+
*
50+
* @param string $sourceDir The (fixture) directory to scan for files.
51+
* @param array<string> $extraArgs Any extra arguments to pass on the command line.
52+
*
53+
* @dataProvider data
54+
* @group CBF
55+
*
56+
* @return void
57+
*/
58+
public function testPhpcbf($sourceDir, $extraArgs)
59+
{
60+
if (PHP_CODESNIFFER_CBF === false) {
61+
$this->markTestSkipped('This test needs CBF mode to run');
62+
}
63+
64+
$this->setupTest($sourceDir, $extraArgs);
65+
66+
$runner = new Runner();
67+
$runner->runPHPCBF();
68+
69+
}//end testPhpcbf()
70+
71+
72+
/**
73+
* Data provider.
74+
*
75+
* @return array<string, array<string, string|array<string>>>
76+
*/
77+
public static function data()
78+
{
79+
return [
80+
'All files filtered out via extension' => [
81+
'sourceDir' => __DIR__.'/Fixtures/AllFilesExcludedSetA/',
82+
'extraArgs' => ['--extensions=php'],
83+
],
84+
'All files filtered out via ignore pattern' => [
85+
'sourceDir' => __DIR__.'/Fixtures/AllFilesExcludedSetB/',
86+
'extraArgs' => ['--ignore=/place*\.php'],
87+
],
88+
];
89+
90+
}//end data()
91+
92+
93+
/**
94+
* Helper method to prepare the test.
95+
*
96+
* @param string $sourceDir The (fixture) directory to scan for files.
97+
* @param array<string> $extraArgs Any extra arguments to pass on the command line.
98+
*
99+
* @return void
100+
*/
101+
private function setupTest($sourceDir, $extraArgs)
102+
{
103+
$_SERVER['argv'] = [
104+
'phpcs',
105+
$sourceDir,
106+
'--standard=PSR1',
107+
'--report-width=80',
108+
];
109+
110+
foreach ($extraArgs as $arg) {
111+
$_SERVER['argv'][] = $arg;
112+
}
113+
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);
118+
119+
}//end setupTest()
120+
121+
122+
}//end class

0 commit comments

Comments
 (0)