8000 [Filesystem] Fix mirroring a directory with a relative path and a cus… · symfony/symfony@711a108 · GitHub
[go: up one dir, main page]

Skip to content

Commit 711a108

Browse files
Frederic GodfrinFrederic Godfrin
Frederic Godfrin
authored and
Frederic Godfrin
committed
[Filesystem] Fix mirroring a directory with a relative path and a custom iterator
1 parent 518ec86 commit 711a108

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,12 @@ public function makePathRelative($endPath, $startPath)
445445
public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array())
446446
{
447447
$targetDir = rtrim($targetDir, '/\\');
448-
$originDir = rtrim($originDir, '/\\');
448+
$originDir = realpath(rtrim($originDir, '/\\'));
449449
$originDirLen = strlen($originDir);
450450

451451
// Iterate in destination folder to remove obsolete entries
452452
if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) {
453+
$targetDir = realpath($targetDir);
453454
$deleteIterator = $iterator;
454455
if (null === $deleteIterator) {
455456
$flags = \FilesystemIterator::SKIP_DOTS;

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,48 @@ public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption()
10651065
$this->assertFileNotExists($targetPath.'target');
10661066
}
10671067

1068+
public function testMirrorWithCustomIterator()
1069+
{
1070+
$sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
1071+
mkdir($sourcePath);
1072+
1073+
$file = $sourcePath.DIRECTORY_SEPARATOR.'file';
1074+
file_put_contents($file, 'FILE');
1075+
1076+
$targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
1077+
1078+
$splFile = new \SplFileInfo($file);
1079+
$iterator = new \ArrayObject(array($splFile));
1080+
1081+
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
1082+
1083+
$this->assertTrue(is_dir($targetPath));
1084+
$this->assertFileEquals($file, $targetPath.DIRECTORY_SEPARATOR.'file');
1085+
}
1086+
1087+
public function testMirrorWithCustomIteratorWithRelativePath()
1088+
{
1089+
$sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
1090+
$realSourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
1091+
mkdir($realSourcePath);
1092+
1093+
$file = $realSourcePath.'file';
1094+
file_put_contents($file, 'FILE');
1095+
1096+
$targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
1097+
$realTargetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
1098+
1099+
$splFile = new \SplFileInfo($file);
1100+
$iterator = new \ArrayObject(array($splFile));
1101+
1102+
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
1103+
1104+
$this->assertTrue(is_dir($targetPath));
1105+
$this->assertTrue(is_dir($realTargetPath));
1106+
$this->assertFileEquals($file, $targetPath.DIRECTORY_SEPARATOR.'file');
1107+
$this->assertFileEquals($file, $realTargetPath.DIRECTORY_SEPARATOR.'file');
1108+
}
1109+
10681110
/**
10691111
* @dataProvider providePathsForIsAbsolutePath
10701112
*/

0 commit comments

Comments
 (0)
0