Skip to content

Commit

Permalink
[Finder] Fix finding VCS re-included files in excluded directory
Browse files Browse the repository at this point in the history
  • Loading branch information
julienfalque authored and nicolas-grekas committed Jan 26, 2022
1 parent 6c7f13f commit 2313135
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 27 deletions.
2 changes: 0 additions & 2 deletions Iterator/VcsIgnoredFilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ private function isIgnored(string $fileRealPath): bool

foreach ($this->parentsDirectoryDownward($fileRealPath) as $parentDirectory) {
if ($this->isIgnored($parentDirectory)) {
$ignored = true;

// rules in ignored directories are ignored, no need to check further.
break;
}
Expand Down
103 changes: 78 additions & 25 deletions Tests/Iterator/VcsIgnoredFilterIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ protected function tearDown(): void
*/
public function testAccept(array $gitIgnoreFiles, array $otherFileNames, array $expectedResult)
{
foreach ($gitIgnoreFiles as $path => $content) {
$this->createFile("{$this->tmpDir}/{$path}", $content);
}

$otherFileNames = $this->toAbsolute($otherFileNames);
foreach ($otherFileNames as $path) {
$this->createFile($path);
if (str_ends_with($path, '/')) {
mkdir($path);
} else {
touch($path);
}
}

foreach ($gitIgnoreFiles as $path => $content) {
file_put_contents("{$this->tmpDir}/{$path}", $content);
}

$inner = new InnerNameIterator($otherFileNames);
Expand All @@ -64,10 +68,12 @@ public function getAcceptData(): iterable
[
'a.txt',
'b.txt',
'dir/',
'dir/a.txt',
],
[
'b.txt',
'dir',
],
];

Expand All @@ -78,20 +84,23 @@ public function getAcceptData(): iterable
[
'a.txt',
'b.txt',
'dir/',
'dir/a.txt',
],
[
'b.txt',
'dir',
'dir/a.txt',
],
];

yield 'directy' => [
yield 'directory' => [
[
'.gitignore' => 'dir/',
],
[
'a.txt',
'dir/',
'dir/a.txt',
'dir/b.txt',
],
Expand All @@ -100,7 +109,7 @@ public function getAcceptData(): iterable
],
];

yield 'directy matching a file' => [
yield 'directory matching a file' => [
[
'.gitignore' => 'dir.txt/',
],
Expand All @@ -112,15 +121,20 @@ public function getAcceptData(): iterable
],
];

yield 'directy at root' => [
yield 'directory at root' => [
[
'.gitignore' => '/dir/',
],
[
'dir/',
'dir/a.txt',
'other/',
'other/dir/',
'other/dir/b.txt',
],
[
'other',
'other/dir',
'other/dir/b.txt',
],
];
Expand All @@ -131,11 +145,15 @@ public function getAcceptData(): iterable
],
[
'a.txt',
'nested/',
'nested/a.txt',
'nested/nested/',
'nested/nested/a.txt',
],
[
'a.txt',
'nested',
'nested/nested',
],
];

Expand All @@ -145,58 +163,81 @@ public function getAcceptData(): iterable
],
[
'a.txt',
'nested/',
'nested/a.txt',
'nested/nested/',
'nested/nested/a.txt',
],
[
'a.txt',
'nested',
'nested/nested',
'nested/nested/a.txt',
],
];

yield 'directy in nested .gitignore' => [
yield 'directory in nested .gitignore' => [
[
'nested/.gitignore' => 'dir/',
],
[
'a.txt',
'dir/',
'dir/a.txt',
'nested/',
'nested/dir/',
'nested/dir/a.txt',
'nested/nested/',
'nested/nested/dir/',
'nested/nested/dir/a.txt',
],
[
'a.txt',
'dir',
'dir/a.txt',
'nested',
'nested/nested',
],
];

yield 'directy matching a file in nested .gitignore' => [
yield 'directory matching a file in nested .gitignore' => [
[
'nested/.gitignore' => 'dir.txt/',
],
[
'dir.txt',
'nested/',
'nested/dir.txt',
],
[
'dir.txt',
'nested',
'nested/dir.txt',
],
];

yield 'directy at root of nested .gitignore' => [
yield 'directory at root of nested .gitignore' => [
[
'nested/.gitignore' => '/dir/',
],
[
'a.txt',
'dir/',
'dir/a.txt',
'nested/',
'nested/dir/',
'nested/dir/a.txt',
'nested/nested/',
'nested/nested/dir/',
'nested/nested/dir/a.txt',
],
[
'a.txt',
'dir',
'dir/a.txt',
'nested',
'nested/nested',
'nested/nested/dir',
'nested/nested/dir/a.txt',
],
];
Expand All @@ -209,12 +250,15 @@ public function getAcceptData(): iterable
[
'a.txt',
'b.txt',
'nested/',
'nested/a.txt',
'nested/b.txt',
'nested/dir/',
'nested/dir/a.txt',
'nested/dir/b.txt',
],
[
'nested',
'nested/a.txt',
],
];
Expand All @@ -227,8 +271,10 @@ public function getAcceptData(): iterable
[
'a.txt',
'b.txt',
'nested/',
'nested/a.txt',
'nested/b.txt',
'nested/dir/',
'nested/dir/a.txt',
'nested/dir/b.txt',
],
Expand All @@ -241,12 +287,33 @@ public function getAcceptData(): iterable
'a/.gitignore' => '!c/',
],
[
'a/',
'a/b/',
'a/b/c/',
'a/b/c/d.txt',
],
[
'a',
'a/b',
'a/b/c',
'a/b/c/d.txt',
],
];

yield 'file included from subdirectory with everything excluded' => [
[
'.gitignore' => "/a/**\n!/a/b.txt",
],
[
'a/',
'a/a.txt',
'a/b.txt',
'a/c.txt',
],
[
'a/b.txt',
],
];
}

public function testAcceptAtRootDirectory()
Expand All @@ -267,20 +334,6 @@ private function toAbsolute(array $files): array
return $files;
}

private function createFile(string $path, string $content = null): void
{
$dir = \dirname($path);
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}

if (null !== $content) {
file_put_contents($path, $content);
} else {
touch($path);
}
}

private function removeDirectory(string $dir): void
{
foreach ((new Finder())->in($dir)->ignoreDotFiles(false)->depth('< 1') as $file) {
Expand Down

0 comments on commit 2313135

Please sign in to comment.