Skip to content

Commit

Permalink
Merge pull request consolidation#145 from burzum/bugfix/CopyDir-recur…
Browse files Browse the repository at this point in the history
…sive-destination-check

Bugfix/copy dir recursive destination check
  • Loading branch information
burzum committed Mar 15, 2015
2 parents c6e7d3f + 3a8b232 commit 5e822d3
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 4 deletions.
30 changes: 29 additions & 1 deletion src/Task/FileSystem/CopyDir.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
class CopyDir extends BaseDir
{
/** @var int $chmod */
protected $chmod = 0755;

public function run()
{
foreach ($this->dirs as $src => $dst) {
Expand All @@ -26,13 +29,38 @@ public function run()
return Result::success($this);
}

/**
* Sets the default folder permissions for the destination if it doesn't exist
*
* @link http://en.wikipedia.org/wiki/Chmod
* @link http://php.net/manual/en/function.mkdir.php
* @link http://php.net/manual/en/function.chmod.php
* @param int $value
* @return $this
*/
public function dirPermissions($value)
{
$this->chmod = (int)$value;
return $this;
}

/**
* Copies a directory to another location.
*
* @param string $src Source directory
* @param string $dst Destination directory
* @throws \Robo\Exception\TaskException
* @return void
*/
protected function copyDir($src, $dst)
{
$dir = @opendir($src);
if (false === $dir) {
throw new TaskException($this, "Cannot open source directory '" . $src . "'");
}
@mkdir($dst);
if (!is_dir($dst)) {
mkdir($dst, $this->chmod, true);
}
while (false !== ($file = readdir($dir))) {
if (($file !== '.') && ($file !== '..')) {
$srcFile = $src . '/' . $file;
Expand Down
1 change: 1 addition & 0 deletions tests/_data/claypit/some/deeply/existing_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some existing file
1 change: 1 addition & 0 deletions tests/_data/claypit/some/deeply/nested/structu.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Just a file
1 change: 1 addition & 0 deletions tests/_data/claypit/some_destination/deeply/existing_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some_destination existing file
6 changes: 3 additions & 3 deletions tests/cli/CliGuy.php
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ public function taskConcat($files) {
* [!] Method is generated. Documentation taken from corresponding module.
*
* @param $file
* @return ReplaceInFile
* @return Replace
* @see \Codeception\Module\CliHelper::taskReplaceInFile()
*/
public function taskReplaceInFile($file) {
Expand All @@ -760,7 +760,7 @@ public function taskReplaceInFile($file) {
* [!] Method is generated. Documentation taken from corresponding module.
*
* @param $file
* @return WriteToFile
* @return Write
* @see \Codeception\Module\CliHelper::taskWriteToFile()
*/
public function taskWriteToFile($file) {
Expand Down Expand Up @@ -807,7 +807,7 @@ public function taskCopyDir($dirs) {
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @return Filesystem
* @return FilesystemStack
* @see \Codeception\Module\CliHelper::taskFilesystemStack()
*/
public function taskFilesystemStack() {
Expand Down
11 changes: 11 additions & 0 deletions tests/cli/CopyDirOverwritesFilesCept.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
$I = new CliGuy($scenario);
$I->wantTo('overwrite a file with CopyDir task');
$I->amInPath(codecept_data_dir() . 'sandbox');
$I->seeDirFound('some');
$I->seeFileFound('existing_file', 'some');
$I->taskCopyDir(['some' => 'some_destination'])
->run();
$I->seeFileFound('existing_file', 'some_destination/deeply');
$I->openFile('some_destination/deeply/existing_file');
$I->seeInThisFile('some existing file');
10 changes: 10 additions & 0 deletions tests/cli/CopyDirRecursiveCept.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
$I = new CliGuy($scenario);
$I->wantTo('copy dir recursively with CopyDir task');
$I->amInPath(codecept_data_dir() . 'sandbox');
$I->seeDirFound('some/deeply/nested');
$I->seeFileFound('structu.re', 'some/deeply/nested');
$I->taskCopyDir(['some/deeply' => 'some_destination/deeply'])
->run();
$I->seeDirFound('some_destination/deeply/nested');
$I->seeFileFound('structu.re', 'some_destination/deeply/nested');

0 comments on commit 5e822d3

Please sign in to comment.