Skip to content

Commit

Permalink
Fix exclude-from-classpath containing wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraM committed Mar 7, 2018
1 parent 7666fea commit da222e3
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ private function filterFilesByExtension(Traversable $files, string $fileExtensio
{
$extensionMatcher = '/.*' . preg_quote($fileExtension) . '$/';

$blacklist = $this->prepareBlacklistPatterns($blacklist);

/* @var $file \SplFileInfo */
foreach ($files as $file) {
if ($blacklist && preg_match('{('.implode('|', $blacklist).')}', $file->getPathname())) {
Expand All @@ -37,4 +39,19 @@ private function filterFilesByExtension(Traversable $files, string $fileExtensio
yield $file->getPathname();
}
}

private function prepareBlacklistPatterns(?array $blacklistPaths)
{
if ($blacklistPaths === null) {
return $blacklistPaths;
}

foreach ($blacklistPaths as &$path) {
$path = preg_replace('{/+}', '/', preg_quote(trim(strtr($path, '\\', '/'), '/')));
$path = str_replace('\\*\\*', '.+?', $path);
$path = str_replace('\\*', '[^/]+?', $path);
}

return $blacklistPaths;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,82 @@ public function testFromPsr4WithNestedDirectoryAlternativeDirectorySeparator()
$this->assertContains($this->root->getChild('src/MyNamespace/MyClassA.php')->url(), $files);
}

public function testFromPsr4WithExcludeFromClassmap()
/**
* @dataProvider provideExcludePattern
*/
public function testFromPsr4WithExcludeFromClassmap(array $excludedPattern, array $expectedFiles)
{
$excludedPatternJson = json_encode($excludedPattern);

vfsStream::create([
'composer.json' => '{"autoload": {"psr-4": {"MyNamespace\\\\": "./"}, "exclude-from-classmap": ["/tests/"]}}',
'MyClass.php' => '<?php namespace MyNamespace; class MyClass {}',
'composer.json' => '{"autoload": {"psr-4": {"MyNamespace\\\\": ""}, "exclude-from-classmap": ' . $excludedPatternJson . '}}',
'ClassA.php' => '<?php namespace MyNamespace; class ClassA {}',
'tests' => [
'MyClassTest.php' => '<?php namespace MyNamespace; class MyClassTest {}',
]
'ATest.php' => '<?php namespace MyNamespace; class ATest {}',
],
'foo' => [
'Tests' => [
'BTest.php' => '<?php namespace MyNamespace; class BTest {}',
],
'src' => [
'ClassB.php' => '<?php namespace MyNamespace; class ClassB {}',
'Tests' => [
'CTest.php' => '<?php namespace MyNamespace; class CTest {}',
],
],
],
]);

$files = $this->files($this->root->getChild('composer.json')->url());

$this->assertCount(1, $files);
$this->assertCount(count($expectedFiles), $files);
foreach ($expectedFiles as $expectedFile) {
$this->assertContains($this->root->getChild($expectedFile)->url(), $files);
}
}

/**
* @return array[]
*/
public function provideExcludePattern(): array
{
return [
'No exclude pattern' => [
[],
[
'ClassA.php',
'tests/ATest.php',
'foo/Tests/BTest.php',
'foo/src/ClassB.php',
'foo/src/Tests/CTest.php',

],
],
'Exclude single directory by pattern' => [
['/tests/'],
[
'ClassA.php',
'foo/Tests/BTest.php',
'foo/src/ClassB.php',
'foo/src/Tests/CTest.php',
],
],
'Exclude all subdirectories by pattern' => [
['/**Tests/'],
[
'ClassA.php',
'tests/ATest.php',
'foo/src/ClassB.php',
],
],
'Combine multiple patterns' => [
['/tests/', '/**Tests/'],
[
'ClassA.php',
'foo/src/ClassB.php',
],
],
];
}

/**
Expand Down

0 comments on commit da222e3

Please sign in to comment.