Skip to content

Commit

Permalink
Create destination directory if not already exists (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden authored Jun 15, 2023
1 parent f5f9a60 commit 3ace321
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 1.2.2
### Fixed
- Parent directory at destination is now created if it did not exist already.

## 1.2.1
### Changed
- Package is now also installable in PHP 8.
Expand Down
10 changes: 9 additions & 1 deletion src/FileInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Youwe\Composer;

use Composer\IO\IOInterface;
use RuntimeException;
use SplFileObject;
use Youwe\FileMapping\FileMappingInterface;
use Youwe\FileMapping\FileMappingReaderInterface;
Expand Down Expand Up @@ -67,8 +68,15 @@ public function install(IOInterface $io)
*/
public function installFile(FileMappingInterface $mapping)
{
$destination = $mapping->getDestination();
$parent = dirname($destination);

if (!is_dir($parent) && !mkdir($parent, 0755, true) && !is_dir($parent)) {
throw new RuntimeException("Directory \"$parent\" could not be created");
}

$inputFile = new SplFileObject($mapping->getSource(), 'r');
$targetFile = new SplFileObject($mapping->getDestination(), 'w+');
$targetFile = new SplFileObject($destination, 'w+');

foreach ($inputFile as $input) {
$targetFile->fwrite($input);
Expand Down
45 changes: 45 additions & 0 deletions tests/FileInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,51 @@ public function testInstallFile()
);
}

/**
* @return void
* @covers ::installFile
*/
public function testInstallFileWhenDestinationPathNotExistsYet()
{
/** @var FileMappingReaderInterface $reader */
$reader = $this->createMock(FileMappingReaderInterface::class);
$installer = new FileInstaller($reader);

$fs = vfsStream::setup(
sha1(__METHOD__),
null,
[
'source' => [
'foo.php' => 'Foo'
],
'destination' => []
]
);

/** @var FileMappingInterface|PHPUnit_Framework_MockObject_MockObject $mapping */
$mapping = $this->createMock(FileMappingInterface::class);
$mapping
->expects($this->once())
->method('getSource')
->willReturn(
$fs->getChild('source/foo.php')->url()
);

$mapping
->expects($this->once())
->method('getDestination')
->willReturn(
$fs->getChild('destination')->url() . '/path/to/foo.php'
);

$installer->installFile($mapping);

$this->assertStringEqualsFile(
$fs->getChild('destination/path/to/foo.php')->url(),
'Foo'
);
}

/**
* @return void
* @covers ::install
Expand Down

0 comments on commit 3ace321

Please sign in to comment.