From cd617bf50c0bbd45106b546f1eb856b43faa5122 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 20 Nov 2024 03:11:10 +0100 Subject: [PATCH] Ruleset::expandSniffDirectory(): add test --- .../Core/Ruleset/ExpandSniffDirectoryTest.php | 78 +++++++++++++++++++ .../Core/Ruleset/ExpandSniffDirectoryTest.xml | 8 ++ .../.hidden/HiddenDirShouldBeIgnoredSniff.php | 15 ++++ .../src/MyStandard/AbstractSniff.php | 12 +++ .../src/MyStandard/DummySniff.php | 25 ++++++ .../.hidden/HiddenDirShouldBeIgnoredSniff.php | 15 ++++ .../src/MyStandard/Sniffs/AbstractSniff.php | 12 +++ .../Sniffs/CategoryA/.HiddenFileSniff.php | 12 +++ .../CategoryA/.hidden/DoNotFindMeSniff.txt | 0 .../.hidden/HiddenDirShouldBeIgnoredSniff.php | 15 ++++ .../Sniffs/CategoryA/DoNotFindMeSniff.txt | 12 +++ .../Sniffs/CategoryA/FindMeSniff.php | 12 +++ .../CategoryA/IncorrectFileExtensionSniff.inc | 12 +++ .../Sniffs/CategoryA/MissingSniffSuffix.php | 12 +++ .../IncorrectLevelShouldStillBeFoundSniff.php | 12 +++ .../Sniffs/CategoryB/AnotherAbstractSniff.php | 12 +++ .../Sniffs/CategoryB/FindMeSniff.php | 12 +++ .../IncorrectFileExtensionSniff.php3 | 12 +++ .../IncorrectLevelShouldStillBeFoundSniff.php | 12 +++ .../MyStandard/Utils/NotInSniffsDirSniff.php | 12 +++ .../Utils/SubDir/NotInSniffsDirSniff.php | 12 +++ .../.hiddenAbove/src/MyStandard/ruleset.xml | 4 + 22 files changed, 328 insertions(+) create mode 100644 tests/Core/Ruleset/ExpandSniffDirectoryTest.php create mode 100644 tests/Core/Ruleset/ExpandSniffDirectoryTest.xml create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/.hidden/HiddenDirShouldBeIgnoredSniff.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/AbstractSniff.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/DummySniff.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/.hidden/HiddenDirShouldBeIgnoredSniff.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/AbstractSniff.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/.HiddenFileSniff.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/.hidden/DoNotFindMeSniff.txt create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/.hidden/HiddenDirShouldBeIgnoredSniff.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/DoNotFindMeSniff.txt create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/FindMeSniff.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/IncorrectFileExtensionSniff.inc create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/MissingSniffSuffix.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/Subdir/IncorrectLevelShouldStillBeFoundSniff.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryB/AnotherAbstractSniff.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryB/FindMeSniff.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryB/IncorrectFileExtensionSniff.php3 create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/IncorrectLevelShouldStillBeFoundSniff.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Utils/NotInSniffsDirSniff.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Utils/SubDir/NotInSniffsDirSniff.php create mode 100644 tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/ruleset.xml diff --git a/tests/Core/Ruleset/ExpandSniffDirectoryTest.php b/tests/Core/Ruleset/ExpandSniffDirectoryTest.php new file mode 100644 index 0000000000..8f7f4f93f6 --- /dev/null +++ b/tests/Core/Ruleset/ExpandSniffDirectoryTest.php @@ -0,0 +1,78 @@ + + * @copyright 2024 PHPCSStandards and contributors + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Tests\Core\Ruleset; + +use PHP_CodeSniffer\Ruleset; +use PHP_CodeSniffer\Tests\ConfigDouble; +use PHPUnit\Framework\TestCase; + +/** + * Test the Ruleset::expandSniffDirectory() method. + * + * @covers \PHP_CodeSniffer\Ruleset::expandSniffDirectory + */ +final class ExpandSniffDirectoryTest extends TestCase +{ + + + /** + * Test finding sniff files based on a given directory. + * + * This test verifies that: + * - Hidden (sub)directories are ignored, but the given directory is allowed to be within a hidden directory. + * - Hidden files are ignored. + * - Files without a "php" extension are ignored. + * - Files without a "Sniff" suffix in the file name are ignored. + * + * Note: the "[Another]AbstractSniff" files will be found and included in the return value + * from `Ruleset::expandSniffDirectory()`. + * Those are filtered out later in the `Ruleset::registerSniffs()` method. + * + * @return void + */ + public function testExpandSniffDirectory() + { + // Set up the ruleset. + $standard = __DIR__.'/ExpandSniffDirectoryTest.xml'; + $config = new ConfigDouble(["--standard=$standard"]); + $ruleset = new Ruleset($config); + + $expectedPathToRuleset = __DIR__.'/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/ruleset.xml'; + $expectedPathToRuleset = realpath($expectedPathToRuleset); + $this->assertNotFalse($expectedPathToRuleset, 'Ruleset file could not be found'); + $this->assertContains($expectedPathToRuleset, $ruleset->paths, 'Ruleset file not included in the "seen ruleset paths"'); + + /* + * Take note: the expectation includes some "undesirables" related to the convoluted directory structure + * in the "standard" used as a test fixture. + * + * That is okay as (for now) non-standard directory layouts are supported. + * + * This test is not about the standard directory layout. + */ + + $expectedSniffCodes = [ + '.Sniffs.IncorrectLevelShouldStillBeFound' => 'MyStandard\\Sniffs\\IncorrectLevelShouldStillBeFoundSniff', + 'MyStandard.CategoryA.FindMe' => 'MyStandard\\Sniffs\\CategoryA\\FindMeSniff', + 'MyStandard.CategoryB.FindMe' => 'MyStandard\\Sniffs\\CategoryB\\FindMeSniff', + 'Sniffs.SubDir.IncorrectLevelShouldStillBeFound' => 'MyStandard\\Sniffs\\CategoryA\\SubDir\\IncorrectLevelShouldStillBeFoundSniff', + ]; + + // Sort the value to make the tests stable as different OSes will read directories + // in a different order and the order is not relevant for these tests. Just the values. + $actual = $ruleset->sniffCodes; + ksort($actual); + + $this->assertSame($expectedSniffCodes, $actual, 'Registered sniffs do not match expectation'); + + }//end testExpandSniffDirectory() + + +}//end class diff --git a/tests/Core/Ruleset/ExpandSniffDirectoryTest.xml b/tests/Core/Ruleset/ExpandSniffDirectoryTest.xml new file mode 100644 index 0000000000..f2646da02a --- /dev/null +++ b/tests/Core/Ruleset/ExpandSniffDirectoryTest.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/.hidden/HiddenDirShouldBeIgnoredSniff.php b/tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/.hidden/HiddenDirShouldBeIgnoredSniff.php new file mode 100644 index 0000000000..3ee64dbba6 --- /dev/null +++ b/tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/.hidden/HiddenDirShouldBeIgnoredSniff.php @@ -0,0 +1,15 @@ + + + +