From 3bebfa19d9307ccf78c09840b140a82ef7a68324 Mon Sep 17 00:00:00 2001 From: Fleuv Date: Fri, 11 Jan 2019 20:33:40 +0100 Subject: [PATCH 1/2] Skipping iterations in FileSystem::mirror() where the iterated file's path name is equal to the target path --- Filesystem.php | 4 ++++ Tests/FilesystemTest.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) 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..9110abaf7e 100644 --- a/Tests/FilesystemTest.php +++ b/Tests/FilesystemTest.php @@ -1178,7 +1178,7 @@ public function testMirrorCopiesFilesAndDirectoriesRecursively() file_put_contents($file1, 'FILE1'); file_put_contents($file2, 'FILE2'); - $targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR; + $targetPath = $sourcePath . 'target' . \DIRECTORY_SEPARATOR; $this->filesystem->mirror($sourcePath, $targetPath); From 2505e8832262b56535c92ad68c28c343fae04401 Mon Sep 17 00:00:00 2001 From: Fleuv Date: Fri, 11 Jan 2019 21:05:34 +0100 Subject: [PATCH 2/2] Created a seperate test for the new feature added to FileSystem::mirror(), called testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory --- Tests/FilesystemTest.php | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Tests/FilesystemTest.php b/Tests/FilesystemTest.php index 9110abaf7e..f81d5f0e06 100644 --- a/Tests/FilesystemTest.php +++ b/Tests/FilesystemTest.php @@ -1178,7 +1178,7 @@ public function testMirrorCopiesFilesAndDirectoriesRecursively() file_put_contents($file1, 'FILE1'); file_put_contents($file2, 'FILE2'); - $targetPath = $sourcePath . 'target' . \DIRECTORY_SEPARATOR; + $targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR; $this->filesystem->mirror($sourcePath, $targetPath); @@ -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 */