8000 Skipping iterations in the mirror() method where the iterated file's … · symfony/symfony@a908ef2 · GitHub
[go: up one dir, main page]

Skip to content

Commit a908ef2

Browse files
FleuvAnthony MARTIN
authored and
Anthony MARTIN
committed
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 bb54e40 commit a908ef2

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

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

569569
foreach ($iterator as $file) {
570+
if ($file->getPathName() === $targetDir) {
571+
continue;
572+
}
573+
570574
$target = $targetDir.substr($file->getPathname(), $originDirLen);
571575

572576
if ($copyOnWindows) {

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,71 @@ public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption()
13481348
$this->assertFileNotExists($targetPath.'target');
13491349
}
13501350

1351+
public function testMirrorWithCustomIterator()
1352+
{
1353+
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
1354+
mkdir($sourcePath);
1355+
1356+
$file = $sourcePath.\DIRECTORY_SEPARATOR.'file';
1357+
file_put_contents($file, 'FILE');
1358+
1359+
$targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
1360+
1361+
$splFile = new \SplFileInfo($file);
1362+
$iterator = new \ArrayObject([$splFile]);
1363+
1364+
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
1365+
1366+
$this->assertTrue(is_dir($targetPath));
1367+
$this->assertFileEquals($file, $targetPath.\DIRECTORY_SEPARATOR.'file');
1368+
}
1369+
1370+
/**
1371+
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
1372+
* @expectedExceptionMessageRegExp /Unable to mirror "(.*)" directory/
1373+
*/
1374+
public function testMirrorWithCustomIteratorWithRelativePath()
1375+
{
1376+
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
1377+
$realSourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
1378+
mkdir($realSourcePath);
1379+
1380+
$file = $realSourcePath.'file';
1381+
file_put_contents($file, 'FILE');
1382+
1383+
$targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
1384+
1385+
$splFile = new \SplFileInfo($file);
1386+
$iterator = new \ArrayObject([$splFile]);
1387+
1388+
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
1389+
}
1390+
1391+
public function testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory()
1392+
{
1393+
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
1394+
$directory = $sourcePath.'directory'.\DIRECTORY_SEPARATOR;
1395+
$file1 = $directory.'file1';
1396+
$file2 = $sourcePath.'file2';
1397+
1398+
mkdir($sourcePath);
1399+
mkdir($directory);
1400+
file_put_contents($file1, 'FILE1');
1401+
file_put_contents($file2, 'FILE2');
1402+
1403+
$targetPath = $sourcePath.'target'.\DIRECTORY_SEPARATOR;
1404+
1405+
$this->filesystem->mirror($sourcePath, $targetPath);
1406+
1407+
$this->assertTrue(is_dir($targetPath));
1408+
$this->assertTrue(is_dir($targetPath.'directory'));
1409+
1410+
$this->assertFileEquals($file1, $targetPath.'directory'.\DIRECTORY_SEPARATOR.'file1');
1411+
$this->assertFileEquals($file2, $targetPath.'file2');
1412+
1413+
$this->assertFileNotExists($targetPath.'target');
1414+
}
1415+
13511416
/**
13521417
* @dataProvider providePathsForIsAbsolutePath
13531418
*/

0 commit comments

Comments
 (0)
0