Skip to content

Commit

Permalink
Fixing the path sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
mamazu committed Mar 9, 2024
1 parent d0e2902 commit abff473
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
@@ -1,6 +1,9 @@
Changelog
=========

Bug fixes:
- Fixing include and exclude patterns #2593 @mamazu

## 2024-03-09

Features:
Expand Down Expand Up @@ -51,7 +54,7 @@ Bug fixes:

Documentation:

- Added Helix LSP instructions #2581 @lens0021
- Added Helix LSP instructions #2581 @lens0021
- Fix typos in Behat #2534 @vuon9
- Fix broken external links #2500 @einenlum

Expand Down
22 changes: 21 additions & 1 deletion lib/Filesystem/Domain/FileList.php
Expand Up @@ -104,7 +104,27 @@ public function includeAndExclude(array $includePatterns = [], array $excludePat
}

// Sort map by keys so that more specific paths are getting matched first
krsort($inclusionMap);
uksort($inclusionMap, function (string $x, string $y) {
$partsX = explode(DIRECTORY_SEPARATOR, $x);
$partsY = explode(DIRECTORY_SEPARATOR, $y);
// Longer paths should come first
$countDiff = -(count($partsX) <=> count($partsY));
if ($countDiff !== 0) {
return $countDiff;
}

foreach ($partsX as $i => $pathPartX) {
if ($pathPartX === '**' || $pathPartX === '*') {
return 1;
}

$compare = strcmp($pathPartX, $partsY[$i]);
if($compare !== 0) {
return $compare;
}
}
return 0;
});

return $this->filter(function (SplFileInfo $info) use ($inclusionMap): bool {
foreach($inclusionMap as $glob => $isIncluded) {
Expand Down
53 changes: 42 additions & 11 deletions lib/Filesystem/Tests/Unit/Domain/FileListTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Phpactor\Filesystem\Tests\Unit\Domain;

use Generator;
use Phpactor\Filesystem\Domain\FileList;
use Phpactor\Filesystem\Domain\FilePath;
use Phpactor\Filesystem\Tests\IntegrationTestCase;
Expand Down Expand Up @@ -197,22 +198,52 @@ public function testIncludesExcludePatterns(): void
);
}

public function testExcludesWithShortFolderName(): void
{
$list = FileList::fromFilePaths([
FilePath::fromString('/src/package/test.php'),
FilePath::fromString('/src/a/test.php'),
])->includeAndExclude(
includePatterns: [ '/src/**/*'],
excludePatterns: [ '/src/a/*' ],
/**
@param array<FilePath> $fileList
@param array<string> $includePatterns
@param array<string> $excludePatterns
@param array<FilePath> $expected
* @dataProvider provideExcludesWithShortFolderName()
*/
public function testExcludesWithShortFolderName(
array $fileList,
array $includePatterns,
array $excludePatterns,
array $expected,
): void {
$list = FileList::fromFilePaths($fileList)->includeAndExclude(
includePatterns:$includePatterns,
excludePatterns: $excludePatterns
);

self::assertEquals(
self::assertEquals($expected, array_map(fn (FilePath $x) => (string) $x, iterator_to_array($list)));
}

public function provideExcludesWithShortFolderName(): Generator
{
yield 'ascii file name' => [
[
FilePath::fromString('/src/package/test.php'),
FilePath::fromString('/src/a/test.php'),
],
iterator_to_array($list)
);
[ '/src/**/*'],
[ '/src/a/*' ],
[
'/src/package/test.php',
],
];

yield 'unicode file name' => [
[
FilePath::fromString('/src/package/test.php'),
FilePath::fromString('/src/ü/test.php'),
],
[ '/src/**/*'],
[ '/src/ü/*' ],
[
'/src/package/test.php',
],
];
}

public function testContainingString(): void
Expand Down

0 comments on commit abff473

Please sign in to comment.