diff --git a/Filesystem.php b/Filesystem.php index 2d703843dc..1c01373996 100644 --- a/Filesystem.php +++ b/Filesystem.php @@ -569,6 +569,10 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o } foreach ($iterator as $file) { + if ($file->getPathName() === $targetDir) { + continue; + } + if (false === strpos($file->getPath(), $originDir)) { throw new IOException(sprintf('Unable to mirror "%s" directory. If the origin directory is relative, try using "realpath" before calling the mirror method.', $originDir), 0, null, $originDir); } diff --git a/Tests/FilesystemTest.php b/Tests/FilesystemTest.php index 521c16b710..f81d5f0e06 100644 --- a/Tests/FilesystemTest.php +++ b/Tests/FilesystemTest.php @@ -1188,7 +1188,6 @@ public function testMirrorCopiesFilesAndDirectoriesRecursively() $this->assertFileEquals($file2, $targetPath.'file2'); $this->filesystem->remove($file1); - $this->filesystem->mirror($sourcePath, $targetPath, null, array('delete' => false)); $this->assertTrue($this->filesystem->exists($targetPath.'directory'.\DIRECTORY_SEPARATOR.'file1')); @@ -1372,6 +1371,31 @@ public function testMirrorWithCustomIteratorWithRelativePath() $this->filesystem->mirror($sourcePath, $targetPath, $iterator); } + public function testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory() + { + $sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR; + $directory = $sourcePath.'directory'.\DIRECTORY_SEPARATOR; + $file1 = $directory.'file1'; + $file2 = $sourcePath.'file2'; + + mkdir($sourcePath); + mkdir($directory); + file_put_contents($file1, 'FILE1'); + file_put_contents($file2, 'FILE2'); + + $targetPath = $sourcePath . 'target' . \DIRECTORY_SEPARATOR; + + $this->filesystem->mirror($sourcePath, $targetPath); + + $this->assertTrue(is_dir($targetPath)); + $this->assertTrue(is_dir($targetPath.'directory')); + + $this->assertFileEquals($file1, $targetPath.'directory'.\DIRECTORY_SEPARATOR.'file1'); + $this->assertFileEquals($file2, $targetPath.'file2'); + + $this->assertFalse(file_exists($targetPath.'target')); + } + /** * @dataProvider providePathsForIsAbsolutePath */