From 231313534dded84c7ecaa79d14bc5da4ccb69b7d Mon Sep 17 00:00:00 2001 From: Julien Falque Date: Thu, 20 Jan 2022 18:44:49 +0100 Subject: [PATCH] [Finder] Fix finding VCS re-included files in excluded directory --- Iterator/VcsIgnoredFilterIterator.php | 2 - .../Iterator/VcsIgnoredFilterIteratorTest.php | 103 +++++++++++++----- 2 files changed, 78 insertions(+), 27 deletions(-) diff --git a/Iterator/VcsIgnoredFilterIterator.php b/Iterator/VcsIgnoredFilterIterator.php index 2a84f4a1..e27158cb 100644 --- a/Iterator/VcsIgnoredFilterIterator.php +++ b/Iterator/VcsIgnoredFilterIterator.php @@ -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; } diff --git a/Tests/Iterator/VcsIgnoredFilterIteratorTest.php b/Tests/Iterator/VcsIgnoredFilterIteratorTest.php index 9a85c49e..14cb3c44 100644 --- a/Tests/Iterator/VcsIgnoredFilterIteratorTest.php +++ b/Tests/Iterator/VcsIgnoredFilterIteratorTest.php @@ -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); @@ -64,10 +68,12 @@ public function getAcceptData(): iterable [ 'a.txt', 'b.txt', + 'dir/', 'dir/a.txt', ], [ 'b.txt', + 'dir', ], ]; @@ -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', ], @@ -100,7 +109,7 @@ public function getAcceptData(): iterable ], ]; - yield 'directy matching a file' => [ + yield 'directory matching a file' => [ [ '.gitignore' => 'dir.txt/', ], @@ -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', ], ]; @@ -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', ], ]; @@ -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', ], ]; @@ -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', ], ]; @@ -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', ], @@ -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() @@ -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) {