Skip to content

Commit 1dc3e64

Browse files
committed
chore: Handle blacklist and blacklist_files options.
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.
1 parent 3e150d9 commit 1dc3e64

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

spec/Listener/CodeCoverageListenerSpec.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,23 @@ public function let(ConsoleIO $io, Driver $driver)
3535
public function it_can_process_all_directory_filtering_options(SuiteEvent $event)
3636
{
3737
$this->setOptions([
38+
'whitelist' => [
39+
'src',
40+
['directory' => 'src', 'suffix' => 'Spec.php', 'prefix' => 'Get'],
41+
['directory' => 'src', 'suffix' => 'Test.php'],
42+
['directory' => 'src'],
43+
],
44+
'whitelist_files' => 'path/to/file.php',
3845
'blacklist' => [
3946
'src',
4047
['directory' => 'src', 'suffix' => 'Spec.php', 'prefix' => 'Get'],
4148
['directory' => 'src', 'suffix' => 'Test.php'],
4249
['directory' => 'src'],
4350
],
51+
'blacklist_files' => [
52+
'path/to/file.php',
53+
'path/to/file2.php'
54+
]
4455
]);
4556

4657
$this

src/Listener/CodeCoverageListener.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
use PhpSpec\Event\SuiteEvent;
2121
use SebastianBergmann\CodeCoverage\CodeCoverage;
2222
use SebastianBergmann\CodeCoverage\Report;
23-
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2423
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;
24+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2525

2626
use function gettype;
2727
use function is_array;
@@ -139,10 +139,6 @@ public function beforeExample(ExampleEvent $event): void
139139
$this->coverage->start($name);
140140
}
141141

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

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

154-
foreach ($this->options['whitelist'] as $option) {
150+
// We compute the list of file / folder to be excluded
151+
// If the blacklist contains suffixes and/or prefixes, we extract an
152+
// exhaustive list of files that match to be added in the excluded list.
153+
$excludes = $this->options['blacklist_files'];
154+
foreach ($this->options['blacklist'] as $option) {
155155
$settings = $this->filterDirectoryParams($option);
156-
157-
foreach ((new FileIteratorFacade())->getFilesAsArray($settings['directory'], $settings['suffix'], $settings['prefix']) as $file) {
158-
$filter->includeFile($file);
156+
if (!empty($settings['suffix']) || !empty($settings['prefix'])) {
157+
$excludes = $excludes + (new FileIteratorFacade())->getFilesAsArray(
158+
$settings['directory'],
159+
$settings['suffix'],
160+
$settings['prefix']
161+
);
162+
} else {
163+
$excludes[] = $settings['directory'];
159164
}
160165
}
161166

162-
foreach ($this->options['blacklist'] as $option) {
167+
foreach ($this->options['whitelist'] as $option) {
163168
$settings = $this->filterDirectoryParams($option);
164-
foreach ((new FileIteratorFacade)->getFilesAsArray($directory, $suffix, $prefix) as $file) {
165-
$filter->excludeFile($file);
169+
$fileIterator = (new FileIteratorFacade())->getFilesAsArray(
170+
[$settings['directory']] + $this->options['whitelist_files'],
171+
$settings['suffix'],
172+
$settings['prefix'],
173+
// We exclude the files from the previously built list.
174+
$excludes
175+
);
176+
177+
foreach ($fileIterator as $file) {
178+
$filter->includeFile($file);
166179
}
167-
$filter->excludeDirectory($settings['directory'], $settings['suffix'], $settings['prefix']);
168-
}
169-
170-
$filter->includeFiles($this->options['whitelist_files']);
171-
172-
foreach ($this->options['blacklist_files'] as $option) {
173-
$filter->excludeFile($option);
174180
}
175181
}
176182

@@ -198,7 +204,7 @@ public function setOptions(array $options): void
198204
/**
199205
* @param array<string, string>|string $option
200206
*
201-
* @return array{directory:string, prefix:string, suffix:string}
207+
* @return array{directory:non-empty-string, prefix:string, suffix:string}
202208
*/
203209
protected function filterDirectoryParams($option): array
204210
{
@@ -213,7 +219,7 @@ protected function filterDirectoryParams($option): array
213219
));
214220
}
215221

216-
if (!isset($option['directory'])) {
222+
if (empty($option['directory'])) {
217223
throw new ConfigurationException('Missing required directory path.');
218224
}
219225

0 commit comments

Comments
 (0)