Skip to content

Commit

Permalink
Merge pull request #3 from gbell12/symlink_fix
Browse files Browse the repository at this point in the history
Fix problem in remove when broken symlinks exist
  • Loading branch information
gbell12 committed Aug 11, 2015
2 parents f44f7cf + bf8503f commit 45c0403
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/deit/filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function remove($path) {
} else {

//remove the file if it exists
if (is_file($path)) {
if (is_file($path) || is_link($path)) {
if (!unlink($path)) {
throw new \Exception(sprintf('Unable to delete file "%s".', $path));
}
Expand Down Expand Up @@ -291,4 +291,4 @@ public function setContent($file, $content) {
}

}


37 changes: 37 additions & 0 deletions tests/deit/filesystem/FilesystemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace deit\filesystem;

/**
* Filesystem test
* @author Greg Bell <[email protected]>
*/

class FilesystemTest extends \PHPUnit_Framework_TestCase {

public function rmdir_recursive($dir) {
foreach(scandir($dir) as $file) {
if ('.' === $file || '..' === $file) continue;
if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
else unlink("$dir/$file");
}
rmdir($dir);
}

public function test_symlink_removal() {

$test_dir = 'tests/test-data/symlinks';
$test_link = $test_dir . '/points_nowhere';

if ( is_dir($test_dir) ) {
$this->rmdir_recursive( $test_dir );
}
mkdir( $test_dir );
symlink( "does_not_exist", $test_link );

$fs = new Filesystem();
$fs->remove( $test_dir );
$this->assertFalse ( is_link($test_link) || is_file($test_link) );

}
}
8 changes: 3 additions & 5 deletions tests/deit/filesystem/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function test_files_named() {
global $total_files;

$finder = new Finder('tests/test-data/bigtree');

$this->assertEquals($total_files, count($finder->files()));

foreach ($finder->files()->named('#\.json$#') as $path) {
Expand Down Expand Up @@ -58,10 +58,8 @@ public function test_folders() {

public function test_copy() {

$fs = new Filesystem();

$f = new Finder('tests/test-data/bigtree');
$f
$finder = new Finder('tests/test-data/bigtree');
$finder
->files()
->named('#\.json#')
->copyTo('tests/test-data-copy')
Expand Down

0 comments on commit 45c0403

Please sign in to comment.