Skip to content

Commit

Permalink
Fix blacklisting of the main script (#307)
Browse files Browse the repository at this point in the history
The blacklisting was misconfigured causing the blacklisting of any file or directory containing the
name of the main script.

Closes #303
  • Loading branch information
theofidry authored Oct 16, 2018
1 parent cdfb118 commit 06a6bf2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -478,15 +478,15 @@ public function getDecodedComposerLockContents(): ?array
}

/**
* @return string[]
* @return SplFileInfo[]
*/
public function getFiles(): array
{
return $this->files;
}

/**
* @return string[]
* @return SplFileInfo[]
*/
public function getBinaryFiles(): array
{
Expand Down Expand Up @@ -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()) {
Expand All @@ -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];
}

/**
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions tests/ConfigurationFileNoConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

/**
* @covers \KevinGH\Box\Configuration
*
* @group config
*/
class ConfigurationFileNoConfigTest extends ConfigurationTestCase
{
Expand Down
26 changes: 26 additions & 0 deletions tests/ConfigurationFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

/**
* @covers \KevinGH\Box\Configuration
*
* @group config
*/
class ConfigurationFileTest extends ConfigurationTestCase
{
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/ConfigurationSigningTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
/**
* @covers \KevinGH\Box\Configuration
* @covers \KevinGH\Box\MapFile
*
* @group config
*/
class ConfigurationSigningTest extends ConfigurationTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
/**
* @covers \KevinGH\Box\Configuration
* @covers \KevinGH\Box\MapFile
*
* @group config
*/
class ConfigurationTest extends ConfigurationTestCase
{
Expand Down

0 comments on commit 06a6bf2

Please sign in to comment.