Skip to content

Commit b53f97c

Browse files
Make check for hidden directory explicit and portable
1 parent e268db8 commit b53f97c

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/TextUI/Configuration/SourceMapper.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
*/
1010
namespace PHPUnit\TextUI\Configuration;
1111

12+
use const DIRECTORY_SEPARATOR;
1213
use function file_get_contents;
1314
use function file_put_contents;
1415
use function is_array;
16+
use function preg_match;
1517
use function realpath;
1618
use function serialize;
19+
use function str_replace;
1720
use function unserialize;
1821
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;
1922
use SplObjectStorage;
@@ -80,13 +83,19 @@ public function map(Source $source): array
8083
$directories = $this->aggregateDirectories($source->includeDirectories());
8184

8285
foreach ($directories as $path => [$prefixes, $suffixes]) {
86+
$basePath = realpath($path);
87+
8388
foreach ((new FileIteratorFacade)->getFilesAsArray($path, $suffixes, $prefixes) as $file) {
8489
$file = realpath($file);
8590

8691
if (!$file) {
8792
continue;
8893
}
8994

95+
if ($this->isInHiddenDirectory($file, $basePath)) {
96+
continue;
97+
}
98+
9099
$files[$file] = true;
91100
}
92101
}
@@ -178,6 +187,15 @@ public function mapForCodeCoverage(Source $source): array
178187
return $files;
179188
}
180189

190+
private function isInHiddenDirectory(string $path, false|string $basePath): bool
191+
{
192+
$relativePath = str_replace((string) $basePath, '', $path);
193+
194+
$separator = DIRECTORY_SEPARATOR === '\\' ? '\\\\' : '/';
195+
196+
return preg_match('=' . $separator . '\.[^' . $separator . ']*' . $separator . '=', $relativePath) === 1;
197+
}
198+
181199
/**
182200
* @return array<string,array{list<string>,list<string>}>
183201
*/

tests/unit/TextUI/SourceMapperTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ public static function provider(): Generator
7373
),
7474
];
7575

76-
$fileHiddenOnUnix = self::fixturePath('a/c/.hidden/PrefixSuffix.php');
77-
7876
$expectedFiles = [
79-
$fileHiddenOnUnix => true,
8077
self::fixturePath('a/PrefixSuffix.php') => true,
8178
self::fixturePath('a/c/Prefix.php') => true,
8279
self::fixturePath('a/c/PrefixSuffix.php') => true,
@@ -92,10 +89,6 @@ public static function provider(): Generator
9289
self::fixturePath('b/f/h/PrefixSuffix.php') => true,
9390
];
9491

95-
if (PHP_OS_FAMILY !== 'Windows') {
96-
unset($expectedFiles[$fileHiddenOnUnix]);
97-
}
98-
9992
yield 'file included using directory' => [
10093
$expectedFiles,
10194
self::createSource(
@@ -112,7 +105,6 @@ public static function provider(): Generator
112105
];
113106

114107
$expectedFiles = [
115-
$fileHiddenOnUnix => true,
116108
self::fixturePath('a/c/Prefix.php') => true,
117109
self::fixturePath('a/c/PrefixSuffix.php') => true,
118110
self::fixturePath('a/c/Suffix.php') => true,
@@ -127,10 +119,6 @@ public static function provider(): Generator
127119
self::fixturePath('b/f/h/PrefixSuffix.php') => true,
128120
];
129121

130-
if (PHP_OS_FAMILY !== 'Windows') {
131-
unset($expectedFiles[$fileHiddenOnUnix]);
132-
}
133-
134122
yield 'file included using directory, but excluded using file' => [
135123
$expectedFiles,
136124
self::createSource(

0 commit comments

Comments
 (0)