Skip to content

Commit

Permalink
chore: Handle blacklist and blacklist_files options.
Browse files Browse the repository at this point in the history
Since the phpunit/php-code-coverage isn't able to exclude any folder from version 11, we must handle that logic differently.

We now compute a list of files and folder to be excluded and we give it to the filter.
  • Loading branch information
shulard committed Jul 16, 2024
1 parent 3e150d9 commit 1dc3e64
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
11 changes: 11 additions & 0 deletions spec/Listener/CodeCoverageListenerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,23 @@ public function let(ConsoleIO $io, Driver $driver)
public function it_can_process_all_directory_filtering_options(SuiteEvent $event)
{
$this->setOptions([
'whitelist' => [
'src',
['directory' => 'src', 'suffix' => 'Spec.php', 'prefix' => 'Get'],
['directory' => 'src', 'suffix' => 'Test.php'],
['directory' => 'src'],
],
'whitelist_files' => 'path/to/file.php',
'blacklist' => [
'src',
['directory' => 'src', 'suffix' => 'Spec.php', 'prefix' => 'Get'],
['directory' => 'src', 'suffix' => 'Test.php'],
['directory' => 'src'],
],
'blacklist_files' => [
'path/to/file.php',
'path/to/file2.php'
]
]);

$this
Expand Down
48 changes: 27 additions & 21 deletions src/Listener/CodeCoverageListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use PhpSpec\Event\SuiteEvent;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Report;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

use function gettype;
use function is_array;
Expand Down Expand Up @@ -139,10 +139,6 @@ public function beforeExample(ExampleEvent $event): void
$this->coverage->start($name);
}

/**
* Note: We use array_map() instead of array_walk() because the latter expects
* the callback to take the value as the first and the index as the seconds parameter.
*/
public function beforeSuite(SuiteEvent $event): void
{
if ($this->skipCoverage) {
Expand All @@ -151,26 +147,36 @@ public function beforeSuite(SuiteEvent $event): void

$filter = $this->coverage->filter();

foreach ($this->options['whitelist'] as $option) {
// We compute the list of file / folder to be excluded
// If the blacklist contains suffixes and/or prefixes, we extract an
// exhaustive list of files that match to be added in the excluded list.
$excludes = $this->options['blacklist_files'];
foreach ($this->options['blacklist'] as $option) {
$settings = $this->filterDirectoryParams($option);

foreach ((new FileIteratorFacade())->getFilesAsArray($settings['directory'], $settings['suffix'], $settings['prefix']) as $file) {
$filter->includeFile($file);
if (!empty($settings['suffix']) || !empty($settings['prefix'])) {
$excludes = $excludes + (new FileIteratorFacade())->getFilesAsArray(
$settings['directory'],
$settings['suffix'],
$settings['prefix']
);
} else {
$excludes[] = $settings['directory'];
}
}

foreach ($this->options['blacklist'] as $option) {
foreach ($this->options['whitelist'] as $option) {
$settings = $this->filterDirectoryParams($option);
foreach ((new FileIteratorFacade)->getFilesAsArray($directory, $suffix, $prefix) as $file) {
$filter->excludeFile($file);
$fileIterator = (new FileIteratorFacade())->getFilesAsArray(
[$settings['directory']] + $this->options['whitelist_files'],
$settings['suffix'],
$settings['prefix'],
// We exclude the files from the previously built list.
$excludes
);

foreach ($fileIterator as $file) {
$filter->includeFile($file);
}
$filter->excludeDirectory($settings['directory'], $settings['suffix'], $settings['prefix']);
}

$filter->includeFiles($this->options['whitelist_files']);

foreach ($this->options['blacklist_files'] as $option) {
$filter->excludeFile($option);
}
}

Expand Down Expand Up @@ -198,7 +204,7 @@ public function setOptions(array $options): void
/**
* @param array<string, string>|string $option
*
* @return array{directory:string, prefix:string, suffix:string}
* @return array{directory:non-empty-string, prefix:string, suffix:string}
*/
protected function filterDirectoryParams($option): array
{
Expand All @@ -213,7 +219,7 @@ protected function filterDirectoryParams($option): array
));
}

if (!isset($option['directory'])) {
if (empty($option['directory'])) {
throw new ConfigurationException('Missing required directory path.');
}

Expand Down

0 comments on commit 1dc3e64

Please sign in to comment.