Skip to content

Commit

Permalink
Merge pull request #5493 from kenjis/fix-directory_mirror
Browse files Browse the repository at this point in the history
fix: directory_mirror() throws an error if destination directory exists
  • Loading branch information
kenjis authored Dec 23, 2021
2 parents 160adc2 + 39ce053 commit 60f1367
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
4 changes: 3 additions & 1 deletion system/Helpers/filesystem_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ function directory_mirror(string $originDir, string $targetDir, bool $overwrite
$target = $targetDir . substr($origin, $dirLen);

if ($file->isDir()) {
mkdir($target, 0755);
if (! is_dir($target)) {
mkdir($target, 0755);
}
} elseif (! is_file($target) || ($overwrite && is_file($target))) {
copy($origin, $target);
}
Expand Down
29 changes: 29 additions & 0 deletions tests/system/Helpers/FilesystemHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Test\CIUnitTestCase;
use InvalidArgumentException;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;

/**
* @internal
Expand Down Expand Up @@ -161,6 +162,34 @@ public function testDirectoryMirrorNotOverwrites()
$this->assertSame($this->structure['boo']['faz'], $result);
}

public function testDirectoryMirrorSkipExistingFolder()
{
$this->assertTrue(function_exists('directory_mirror'));

$this->structure = [
'src' => [
'AnEmptyFolder' => [],
],
'dest' => [
'AnEmptyFolder' => [],
],
];
vfsStream::setup('root', null, $this->structure);
$root = rtrim(vfsStream::url('root') . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;

// skips the existing folder
directory_mirror($root . 'src', $root . 'dest');

$structure = vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure();
$this->assertSame([], $structure['root']['dest']['AnEmptyFolder']);

// skips the existing folder (the same as overwrite = true)
directory_mirror($root . 'src', $root . 'dest', false);

$structure = vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure();
$this->assertSame([], $structure['root']['dest']['AnEmptyFolder']);
}

public function testWriteFileSuccess()
{
$vfs = vfsStream::setup('root');
Expand Down

0 comments on commit 60f1367

Please sign in to comment.