Skip to content

Commit 7433859

Browse files
FleuvAnthony MARTIN
authored andcommitted
Skipping iterations in the mirror() method where the iterated file's path name is equal to the target path
Added a new test what is called testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory Adjustments to comply with the Symfony Code Standards
1 parent fc2eed9 commit 7433859

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Filesystem.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,10 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
569569
}
570570

571571
foreach ($iterator as $file) {
572+
if ($file->getPathName() === $targetDir) {
573+
continue;
574+
}
575+
572576
if (false === strpos($file->getPath(), $originDir)) {
573577
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);
574578
}

Tests/FilesystemTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,31 @@ public function testMirrorWithCustomIteratorWithRelativePath()
13721372
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
13731373
}
13741374

1375+
public function testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory()
1376+
{
1377+
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
1378+
$directory = $sourcePath.'directory'.\DIRECTORY_SEPARATOR;
1379+
$file1 = $directory.'file1';
1380+
$file2 = $sourcePath.'file2';
1381+
1382+
mkdir($sourcePath);
1383+
mkdir($directory);
1384+
file_put_contents($file1, 'FILE1');
1385+
file_put_contents($file2, 'FILE2');
1386+
1387+
$targetPath = $sourcePath.'target'.\DIRECTORY_SEPARATOR;
1388+
1389+
$this->filesystem->mirror($sourcePath, $targetPath);
1390+
1391+
$this->assertTrue(is_dir($targetPath));
1392+
$this->assertTrue(is_dir($targetPath.'directory'));
1393+
1394+
$this->assertFileEquals($file1, $targetPath.'directory'.\DIRECTORY_SEPARATOR.'file1');
1395+
$this->assertFileEquals($file2, $targetPath.'file2');
1396+
1397+
$this->assertFileNotExists($targetPath.'target');
1398+
}
1399+
13751400
/**
13761401
* @dataProvider providePathsForIsAbsolutePath
13771402
*/

0 commit comments

Comments
 (0)