From 242fedc05abf11439add8c8454722f278f98bb1d Mon Sep 17 00:00:00 2001 From: Jiri Hrazdil Date: Tue, 16 May 2023 20:26:37 +0200 Subject: [PATCH] fix symlink handling in Finder --- src/Utils/Finder.php | 5 +++- tests/Utils/Finder.basic.phpt | 23 ++++++++++++++++++- .../fixtures.finder3/another_subdir/subdir | 1 + tests/Utils/fixtures.finder3/file.txt | 1 + tests/Utils/fixtures.finder3/subdir/file.txt | 1 + 5 files changed, 29 insertions(+), 2 deletions(-) create mode 120000 tests/Utils/fixtures.finder3/another_subdir/subdir create mode 100644 tests/Utils/fixtures.finder3/file.txt create mode 120000 tests/Utils/fixtures.finder3/subdir/file.txt diff --git a/src/Utils/Finder.php b/src/Utils/Finder.php index 2c9a3200f..6abd6d1f0 100644 --- a/src/Utils/Finder.php +++ b/src/Utils/Finder.php @@ -382,7 +382,10 @@ private function traverseDir(string $dir, array $searches, array $subdirs = []): $relativePathname = FileSystem::unixSlashes($file->getRelativePathname()); foreach ($searches as $search) { if ( - $file->getType() === $search->mode + ( + $file->getType() === $search->mode + || ($file->getType() === 'link' && in_array($search->mode, ['file', 'dir'], true)) + ) && preg_match($search->pattern, $relativePathname) && $this->proveFilters($this->filters, $file, $cache) ) { diff --git a/tests/Utils/Finder.basic.phpt b/tests/Utils/Finder.basic.phpt index 053f24e6b..5d9a2457e 100644 --- a/tests/Utils/Finder.basic.phpt +++ b/tests/Utils/Finder.basic.phpt @@ -28,7 +28,7 @@ function export($iterator, bool $sort = true) } -test('expty search', function () { +test('empty search', function () { $finder = (new Finder)->in('fixtures.finder'); Assert::same([], export($finder)); @@ -172,3 +172,24 @@ test('absolute path in mask', function () { // will not work if there are charac FileSystem::unixSlashes(__DIR__), ], export($finder)); }); + +test('symlink to file', function() { + $finder = Finder::find('subdir/*.txt')->in('fixtures.finder3'); + Assert::same([ + 'fixtures.finder3/subdir/file.txt', + ], export($finder)); +}); + +test('symlink to directory', function() { + $finder = Finder::findDirectories()->in('fixtures.finder3/another_subdir'); + Assert::same([ + 'fixtures.finder3/another_subdir/subdir', + ], export($finder)); +}); + +test('symlink to file in symlinked directory', function() { + $finder = Finder::find('subdir/*.txt')->in('fixtures.finder3/another_subdir'); + Assert::same([ + 'fixtures.finder3/another_subdir/subdir/file.txt', + ], export($finder)); +}); diff --git a/tests/Utils/fixtures.finder3/another_subdir/subdir b/tests/Utils/fixtures.finder3/another_subdir/subdir new file mode 120000 index 000000000..61f519d04 --- /dev/null +++ b/tests/Utils/fixtures.finder3/another_subdir/subdir @@ -0,0 +1 @@ +../subdir \ No newline at end of file diff --git a/tests/Utils/fixtures.finder3/file.txt b/tests/Utils/fixtures.finder3/file.txt new file mode 100644 index 000000000..379e57270 --- /dev/null +++ b/tests/Utils/fixtures.finder3/file.txt @@ -0,0 +1 @@ +File for testing purposes diff --git a/tests/Utils/fixtures.finder3/subdir/file.txt b/tests/Utils/fixtures.finder3/subdir/file.txt new file mode 120000 index 000000000..1247f6970 --- /dev/null +++ b/tests/Utils/fixtures.finder3/subdir/file.txt @@ -0,0 +1 @@ +../file.txt \ No newline at end of file