From fd17538d8e669095d537e4c765cae1b82a64c698 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 6 Jun 2025 09:29:28 -0300 Subject: [PATCH] Filters/Filter: expand test coverage for the Filter::accept() method This commit expands the Filter::accept() tests to cover cases that were not covered before: - Run the tests on Windows as there is dedicated code when the directory separator is "\". - Real directories trigger code that relies on is_dir() returning `true`. - Sets Config::$local to `true` and ensure in this case directories are not accepted. - Files without an extension, with an invalid one or starting with a dot are ignored. - Duplicated files are ignored. - Relative exclude patterns are handled correctly. --- tests/Core/Filters/Filter/AcceptTest.php | 64 +++++++++++++++++++++++- tests/Core/Filters/Filter/AcceptTest.xml | 6 ++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/tests/Core/Filters/Filter/AcceptTest.php b/tests/Core/Filters/Filter/AcceptTest.php index d4da0a248c..1d2dbcdb4b 100644 --- a/tests/Core/Filters/Filter/AcceptTest.php +++ b/tests/Core/Filters/Filter/AcceptTest.php @@ -20,6 +20,7 @@ * Tests for the \PHP_CodeSniffer\Filters\Filter::accept method. * * @covers \PHP_CodeSniffer\Filters\Filter + * @group Windows */ final class AcceptTest extends AbstractFilterTestCase { @@ -46,16 +47,23 @@ public static function initializeConfigAndRuleset() * * @param array $inputPaths List of file paths to be filtered. * @param array $expectedOutput Expected filtering result. + * @param bool $localFiles Value of the Config class "local" setting (default: false). * * @dataProvider dataExcludePatterns * * @return void */ - public function testExcludePatterns($inputPaths, $expectedOutput) + public function testExcludePatterns($inputPaths, $expectedOutput, $localFiles=false) { $fakeDI = new RecursiveArrayIterator($inputPaths); $filter = new Filter($fakeDI, '/', self::$config, self::$ruleset); + self::$config->local = false; + + if ($localFiles === true) { + self::$config->local = true; + } + $this->assertSame($expectedOutput, $this->getFilteredResultsAsArray($filter)); }//end testExcludePatterns() @@ -99,6 +107,60 @@ public static function dataExcludePatterns() '/path/to/src/anything-generic/Main.php', ], ], + 'Filter should exclude files without an extension, using unsupported extension and starting with a dot' => [ + 'inputPaths' => [ + '/path/to/src/Main.php', + '/path/to/src/.hiddenfile', + '/path/to/src/NoExtension', + '/path/to/src/UnsupportedExtension.txt', + '/path/to/src/UnsupportedExtension.php.bak', + ], + 'expectedOutput' => [ + '/path/to/src/Main.php', + ], + ], + 'Filter should ignore duplicate files' => [ + 'inputPaths' => [ + __FILE__, + __FILE__, + '/path/to/src/Main.php', + ], + 'expectedOutput' => [ + __FILE__, + '/path/to/src/Main.php', + ], + ], + 'Filter should work for relative exclude patterns' => [ + 'inputPaths' => [ + 'src/Main.php', + 'src/AnotherDir/File.php', + ], + 'expectedOutput' => [ + 'src/Main.php', + ], + ], + + // Uses real directories to test code that calls is_dir(). + 'Filter should handle directories' => [ + 'inputPaths' => [ + self::getBaseDir().'/src/Generators', + self::getBaseDir().'/src/Standards', + ], + 'expectedOutput' => [ + self::getBaseDir().'/src/Standards', + ], + ], + 'Filter should ignore directories when --local is used' => [ + 'inputPaths' => [ + 'src/Main.php', + self::getBaseDir().'/src/Generators', + self::getBaseDir().'/src/Standards', + ], + 'expectedOutput' => [ + 'src/Main.php', + ], + 'localFiles' => true, + ], ]; // Allow these tests to work on Windows as well. diff --git a/tests/Core/Filters/Filter/AcceptTest.xml b/tests/Core/Filters/Filter/AcceptTest.xml index 2800298ebf..705daf081b 100644 --- a/tests/Core/Filters/Filter/AcceptTest.xml +++ b/tests/Core/Filters/Filter/AcceptTest.xml @@ -2,11 +2,15 @@ Ruleset to test the filtering based on exclude patterns. - + */something/* + */src/Generators/* */Other/Main\.php$ + + /AnotherDir/* +