Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the blacklist usage #110

Merged
merged 1 commit into from
Apr 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 38 additions & 51 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@
use stdClass;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;
use function array_filter;
use function array_map;
use function array_unique;
use function Humbug\PhpScoper\create_scoper;
use function iter\chain;
use function iter\fn\method;
use function iter\map;
use function iter\toArray;
use function iterator_to_array;
use function KevinGH\Box\FileSystem\canonicalize;
use function KevinGH\Box\FileSystem\file_contents;
use function KevinGH\Box\FileSystem\longest_common_base_path;
use function KevinGH\Box\FileSystem\make_path_absolute;
use function KevinGH\Box\FileSystem\make_path_relative;
use function preg_quote;
use function substr;
use function uniqid;

final class Configuration
Expand Down Expand Up @@ -198,13 +205,13 @@ public static function create(?string $file, stdClass $raw): self
} else {
$files = self::retrieveFiles($raw, 'files', $basePath);
$directories = self::retrieveDirectories($raw, 'directories', $basePath, $blacklistFilter, $excludedPaths);
$filesFromFinders = self::retrieveFilesFromFinders($raw, 'finder', $basePath, $blacklistFilter, $excludedPaths, $devPackages);
$filesFromFinders = self::retrieveFilesFromFinders($raw, 'finder', $basePath, $blacklistFilter, $devPackages);

$filesAggregate = array_unique(iterator_to_array(chain($files, $directories, ...$filesFromFinders)));

$binaryFiles = self::retrieveFiles($raw, 'files-bin', $basePath);
$binaryDirectories = self::retrieveDirectories($raw, 'directories-bin', $basePath, $blacklistFilter, $excludedPaths);
$binaryFilesFromFinders = self::retrieveFilesFromFinders($raw, 'finder-bin', $basePath, $blacklistFilter, $excludedPaths, $devPackages);
$binaryFilesFromFinders = self::retrieveFilesFromFinders($raw, 'finder-bin', $basePath, $blacklistFilter, $devPackages);

$binaryFilesAggregate = array_unique(iterator_to_array(chain($binaryFiles, $binaryDirectories, ...$binaryFilesFromFinders)));
}
Expand Down Expand Up @@ -480,6 +487,10 @@ private static function retrieveBlacklistFilter(stdClass $raw, string $basePath)
return false;
}

if (false === $file->getRealPath()) {
return false;
}

if (in_array($file->getRealPath(), $blacklist, true)) {
return false;
}
Expand All @@ -505,10 +516,6 @@ private static function retrieveBlacklist(stdClass $raw, string $basePath): arra
/** @var string[] $blacklist */
$blacklist = $raw->blacklist;

$normalizePath = function (string $file) use ($basePath): string {
return self::normalizePath($file, $basePath);
};

$normalizedBlacklist = [];

foreach ($blacklist as $file) {
Expand All @@ -519,24 +526,6 @@ private static function retrieveBlacklist(stdClass $raw, string $basePath): arra
return array_unique($normalizedBlacklist);
}

/**
* @return string[]
*/
private static function retrieveExcludes(stdClass $raw, string $basePath): array
{
if (false === isset($raw->exclude)) {
return [];
}

$exclude = $raw->exclude;

$normalizePath = function (string $file) use ($basePath): string {
return canonicalize(make_path_relative(trim($file), $basePath));
};

return array_unique(array_map($normalizePath, $exclude));
}

/**
* @param stdClass $raw
* @param string $key Config property name
Expand Down Expand Up @@ -608,7 +597,7 @@ private static function retrieveDirectories(
;

foreach ($excludedPaths as $excludedPath) {
$finder->notPath(preg_quote($excludedPath, '/'));
$finder->notPath($excludedPath);
}

return $finder;
Expand All @@ -622,7 +611,6 @@ private static function retrieveDirectories(
* @param string $key
* @param string $basePath
* @param Closure $blacklistFilter
* @param string[] $excludedPaths
* @param string[] $devPackages
*
* @return iterable[]|SplFileInfo[][]
Expand All @@ -632,11 +620,10 @@ private static function retrieveFilesFromFinders(
string $key,
string $basePath,
Closure $blacklistFilter,
array $excludedPaths,
array $devPackages
): array {
if (isset($raw->{$key})) {
return self::processFinders($raw->{$key}, $basePath, $blacklistFilter, $excludedPaths, $devPackages);
return self::processFinders($raw->{$key}, $basePath, $blacklistFilter, $devPackages);
}

return [];
Expand All @@ -646,7 +633,6 @@ private static function retrieveFilesFromFinders(
* @param array $findersConfig
* @param string $basePath
* @param Closure $blacklistFilter
* @param string[] $excludedPaths
* @param string[] $devPackages
*
* @return Finder[]|SplFileInfo[][]
Expand All @@ -655,11 +641,10 @@ private static function processFinders(
array $findersConfig,
string $basePath,
Closure $blacklistFilter,
array $excludedPaths,
array $devPackages
): array {
$processFinderConfig = function (stdClass $config) use ($basePath, $blacklistFilter, $excludedPaths, $devPackages) {
return self::processFinder($config, $basePath, $blacklistFilter, $excludedPaths, $devPackages);
$processFinderConfig = function (stdClass $config) use ($basePath, $blacklistFilter, $devPackages) {
return self::processFinder($config, $basePath, $blacklistFilter, $devPackages);
};

return array_map($processFinderConfig, $findersConfig);
Expand All @@ -669,7 +654,6 @@ private static function processFinders(
* @param stdClass $config
* @param string $basePath
* @param Closure $blacklistFilter
* @param string[] $excludedPaths
* @param string[] $devPackages
*
* @return Finder|SplFileInfo[]
Expand All @@ -678,7 +662,6 @@ private static function processFinder(
stdClass $config,
string $basePath,
Closure $blacklistFilter,
array $excludedPaths,
array $devPackages
): Finder {
$finder = Finder::create()
Expand All @@ -699,10 +682,6 @@ function (SplFileInfo $fileInfo) use ($devPackages): bool {
->ignoreVCS(true)
;

foreach ($excludedPaths as $excludedPath) {
$finder->notPath(preg_quote($excludedPath, '/'));
}

$normalizedConfig = (function (array $config, Finder $finder): array {
$normalizedConfig = [];

Expand Down Expand Up @@ -828,21 +807,29 @@ function (string $packagePath) use ($basePath): string {
->ignoreVCS(true)
;

$excludedPaths = array_unique(
array_filter(
array_map(
function (string $path) use ($basePath): string {
return make_path_relative($path, $basePath);
},
$excludedPaths
),
function (string $path): bool {
return '..' !== substr($path, 0, 2);
}
)
);

foreach ($excludedPaths as $excludedPath) {
$finder->notPath(preg_quote($excludedPath, '/'));
$finder->notPath($excludedPath);
}

return array_filter(
array_unique(
array_map(
function (SplFileInfo $fileInfo): ?string {
if (is_link((string) $fileInfo)) {
return null;
}

return false !== $fileInfo->getRealPath() ? $fileInfo->getRealPath() : null;
},
iterator_to_array($finder)
return array_unique(
toArray(
map(
method('getRealPath'),
$finder
)
)
);
Expand Down
2 changes: 1 addition & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

use Assert\Assertion;
use Phar;
use Symfony\Component\Console\Output\OutputInterface;
use function constant;
use function define;
use function defined;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @internal
Expand Down
38 changes: 38 additions & 0 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ public function test_the_files_can_be_configured(): void
touch('B/file1');
touch('B/fileB0');
touch('B/fileB1');
touch('B/glob_finder_excluded_file');
touch('B/glob-finder_excluded_file');

mkdir('C');
touch('C/fileC0');
Expand All @@ -261,6 +263,13 @@ public function test_the_files_can_be_configured(): void
touch('E/fileE1');
touch('E/finder_excluded_file');

mkdir('vendor');
touch('vendor/glob_finder_excluded_file');
touch('vendor/glob-finder_excluded_file');

mkdir('vendor-bin');
touch('vendor-bin/file1');

$this->setConfig([
'files' => [
'file0',
Expand Down Expand Up @@ -290,6 +299,9 @@ public function test_the_files_can_be_configured(): void
'C/fileC1',
'D/fileD1',
'E/fileE1',
'glob_finder_excluded_file',
'glob-finder_excluded_file',
'vendor-bin',
],
]);

Expand Down Expand Up @@ -323,6 +335,8 @@ public function test_configured_files_are_relative_to_base_path(): void
mkdir('B');
touch('B/fileB0');
touch('B/fileB1');
touch('B/glob_finder_excluded_file');
touch('B/glob-finder_excluded_file');

mkdir('C');
touch('C/fileC0');
Expand All @@ -338,6 +352,13 @@ public function test_configured_files_are_relative_to_base_path(): void
touch('E/fileE1');
touch('E/finder_excluded_file');

mkdir('vendor');
touch('vendor/glob_finder_excluded_file');
touch('vendor/glob-finder_excluded_file');

mkdir('vendor-bin');
touch('vendor-bin/file1');

chdir($this->tmp);

$this->setConfig([
Expand Down Expand Up @@ -370,6 +391,9 @@ public function test_configured_files_are_relative_to_base_path(): void
'C/fileC1',
'D/fileD1',
'E/fileE1',
'glob_finder_excluded_file',
'glob-finder_excluded_file',
'vendor-bin',
],
]);

Expand Down Expand Up @@ -1707,6 +1731,8 @@ public function test_the_blacklist_setting_is_applied_to_all_the_files_found_in_
mkdir('B');
touch('B/fileB0');
touch('B/fileB1');
touch('B/glob_finder_excluded_file');
touch('B/glob-finder_excluded_file');

mkdir('C');
touch('C/fileC0');
Expand All @@ -1722,6 +1748,14 @@ public function test_the_blacklist_setting_is_applied_to_all_the_files_found_in_
touch('E/fileE1');
touch('E/finder_excluded_file');

mkdir('vendor');
touch('vendor/glob_finder_excluded_file');
touch('vendor/glob-finder_excluded_file');

mkdir('vendor-bin');
touch('vendor-bin/file0');
touch('vendor-bin/file1');

// Relative to the current working directory for readability
$expected = [
'file0',
Expand All @@ -1740,6 +1774,10 @@ public function test_the_blacklist_setting_is_applied_to_all_the_files_found_in_
'D',
'D/finder_excluded_file',
'E/finder_excluded_file',
'vendor-bin',
'vendor-bin-file1',
'glob_finder_excluded_file',
'glob-finder_excluded_file',
],
]);

Expand Down