From 06a6bf29ee8983cff51647b6719461cde08c98b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Tue, 16 Oct 2018 12:00:20 +0200 Subject: [PATCH] Fix blacklisting of the main script (#307) The blacklisting was misconfigured causing the blacklisting of any file or directory containing the name of the main script. Closes #303 --- src/Configuration.php | 26 ++++++++++++++----------- tests/ConfigurationFileNoConfigTest.php | 2 ++ tests/ConfigurationFileTest.php | 26 +++++++++++++++++++++++++ tests/ConfigurationSigningTest.php | 2 ++ tests/ConfigurationTest.php | 2 ++ 5 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/Configuration.php b/src/Configuration.php index 1e27a7934..8041584be 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -40,6 +40,7 @@ use function array_column; use function array_diff; use function array_filter; +use function array_flip; use function array_key_exists; use function array_keys; use function array_map; @@ -49,7 +50,6 @@ use function defined; use function dirname; use function file_exists; -use function in_array; use function intval; use function is_array; use function is_bool; @@ -478,7 +478,7 @@ public function getDecodedComposerLockContents(): ?array } /** - * @return string[] + * @return SplFileInfo[] */ public function getFiles(): array { @@ -486,7 +486,7 @@ public function getFiles(): array } /** - * @return string[] + * @return SplFileInfo[] */ public function getBinaryFiles(): array { @@ -739,7 +739,9 @@ private static function retrieveBlacklistFilter( ConfigurationLogger $logger, ?string ...$excludedPaths ): array { - $blacklist = self::retrieveBlacklist($raw, $basePath, $logger, ...$excludedPaths); + $blacklist = array_flip( + self::retrieveBlacklist($raw, $basePath, $logger, ...$excludedPaths) + ); $blacklistFilter = function (SplFileInfo $file) use ($blacklist): ?bool { if ($file->isLink()) { @@ -750,14 +752,14 @@ private static function retrieveBlacklistFilter( return false; } - if (in_array($file->getRealPath(), $blacklist, true)) { + if (array_key_exists($file->getRealPath(), $blacklist)) { return false; } return null; }; - return [$blacklist, $blacklistFilter]; + return [array_keys($blacklist), $blacklistFilter]; } /** @@ -775,13 +777,15 @@ private static function retrieveBlacklist( ): array { self::checkIfDefaultValue($logger, $raw, self::BLACKLIST_KEY, []); - /** @var string[] $blacklist */ - $blacklist = array_merge( - array_filter($excludedPaths), - $raw->{self::BLACKLIST_KEY} ?? [] + $normalizedBlacklist = array_map( + function (string $excludedPath) use ($basePath): string { + return self::normalizePath($excludedPath, $basePath); + }, + array_filter($excludedPaths) ); - $normalizedBlacklist = []; + /** @var string[] $blacklist */ + $blacklist = $raw->{self::BLACKLIST_KEY} ?? []; foreach ($blacklist as $file) { $normalizedBlacklist[] = self::normalizePath($file, $basePath); diff --git a/tests/ConfigurationFileNoConfigTest.php b/tests/ConfigurationFileNoConfigTest.php index bd4ea1cf7..19f464b57 100644 --- a/tests/ConfigurationFileNoConfigTest.php +++ b/tests/ConfigurationFileNoConfigTest.php @@ -23,6 +23,8 @@ /** * @covers \KevinGH\Box\Configuration + * + * @group config */ class ConfigurationFileNoConfigTest extends ConfigurationTestCase { diff --git a/tests/ConfigurationFileTest.php b/tests/ConfigurationFileTest.php index 253bb4e6e..77a4344c5 100644 --- a/tests/ConfigurationFileTest.php +++ b/tests/ConfigurationFileTest.php @@ -26,6 +26,8 @@ /** * @covers \KevinGH\Box\Configuration + * + * @group config */ class ConfigurationFileTest extends ConfigurationTestCase { @@ -1877,6 +1879,30 @@ function (): void { ['file0'], ['file1'], ]; + + yield [ + // https://github.com/humbug/box/issues/303 + // The main script is blacklisted but ensures this does not affect the other files collected, like here + // the files found in a directory which has the same name as the main script + function (): void { + dump_file('acme'); + dump_file('src/file00'); + dump_file('src/file10'); + dump_file('src/acme/file00'); + dump_file('src/acme/file10'); + }, + [ + 'main' => 'acme', + 'directories' => ['src'], + ], + [ + 'src/acme/file00', + 'src/acme/file10', + 'src/file00', + 'src/file10', + ], + [], + ]; } public function provideJsonValidNonStringArray(): Generator diff --git a/tests/ConfigurationSigningTest.php b/tests/ConfigurationSigningTest.php index c16e268b7..63896928f 100644 --- a/tests/ConfigurationSigningTest.php +++ b/tests/ConfigurationSigningTest.php @@ -24,6 +24,8 @@ /** * @covers \KevinGH\Box\Configuration * @covers \KevinGH\Box\MapFile + * + * @group config */ class ConfigurationSigningTest extends ConfigurationTestCase { diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php index e3628841d..252175c97 100644 --- a/tests/ConfigurationTest.php +++ b/tests/ConfigurationTest.php @@ -44,6 +44,8 @@ /** * @covers \KevinGH\Box\Configuration * @covers \KevinGH\Box\MapFile + * + * @group config */ class ConfigurationTest extends ConfigurationTestCase {