Skip to content

Commit

Permalink
Don't delete non-empty subdirectories that contain hidden files (#26)…
Browse files Browse the repository at this point in the history
… and delete hidden files (#27) (#28)
  • Loading branch information
bluec authored and freekmurze committed Jan 18, 2019
1 parent cb27ea0 commit 40c3b4a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/DirectoryCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function deleteFilesOlderThanMinutes(int $minutes) : Collection
{
$timeInPast = Carbon::now()->subMinutes($minutes);

return collect($this->filesystem->allFiles($this->directory))
return collect($this->filesystem->allFiles($this->directory, true))
->filter(function ($file) use ($timeInPast) {
return Carbon::createFromTimestamp(filemtime($file))
->lt($timeInPast);
Expand All @@ -48,7 +48,7 @@ public function deleteEmptySubdirectories() : Collection
{
return collect($this->filesystem->directories($this->directory))
->filter(function ($directory) {
return ! $this->filesystem->allFiles($directory);
return ! $this->filesystem->allFiles($directory, true);
})
->each(function ($directory) {
$this->filesystem->deleteDirectory($directory);
Expand Down
12 changes: 12 additions & 0 deletions tests/DirectoryCleanupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function it_can_cleanup_the_directories_specified_in_the_config_file()
foreach ($directories as $directory => $config) {
foreach (range(1, $numberOfDirectories) as $ageInMinutes) {
$this->createFile("{$directory}/{$ageInMinutes}MinutesOld.txt", $ageInMinutes);
$this->createFile("{$directory}/.{$ageInMinutes}MinutesOld.txt", $ageInMinutes);
}
}

Expand All @@ -36,8 +37,10 @@ public function it_can_cleanup_the_directories_specified_in_the_config_file()
foreach (range(1, $numberOfDirectories) as $ageInMinutes) {
if ($ageInMinutes < $config['deleteAllOlderThanMinutes']) {
$this->assertFileExists("{$directory}/{$ageInMinutes}MinutesOld.txt");
$this->assertFileExists("{$directory}/.{$ageInMinutes}MinutesOld.txt");
} else {
$this->assertFileNotExists("{$directory}/{$ageInMinutes}MinutesOld.txt");
$this->assertFileNotExists("{$directory}/.{$ageInMinutes}MinutesOld.txt");
}
}
}
Expand All @@ -59,6 +62,7 @@ public function it_can_cleanup_the_directories_specified_in_the_config_file_recu
foreach (range(1, $numberSubOfDirectories + 1) as $level) {
foreach (range(1, $numberSubOfDirectories) as $ageInMinutes) {
$this->createFile("{$path}/{$ageInMinutes}MinutesOld.txt", $ageInMinutes);
$this->createFile("{$path}/.{$ageInMinutes}MinutesOld.txt", $ageInMinutes);
}
$path .= "{$level}/";
}
Expand All @@ -72,8 +76,10 @@ public function it_can_cleanup_the_directories_specified_in_the_config_file_recu
foreach (range(1, $numberSubOfDirectories) as $ageInMinutes) {
if ($ageInMinutes < $config['deleteAllOlderThanMinutes']) {
$this->assertFileExists("{$path}/{$ageInMinutes}MinutesOld.txt");
$this->assertFileExists("{$path}/.{$ageInMinutes}MinutesOld.txt");
} else {
$this->assertFileNotExists("{$path}/{$ageInMinutes}MinutesOld.txt");
$this->assertFileNotExists("{$path}/.{$ageInMinutes}MinutesOld.txt");
}
}
$path .= "{$level}/";
Expand Down Expand Up @@ -141,13 +147,19 @@ public function it_can_delete_empty_subdirectories()
$this->createFile("{$directory}/emptyDir/5MinutesOld.txt", 5);
$this->createDirectory("{$directory}/notEmptyDir");
$this->createFile("{$directory}/notEmptyDir/1MinutesOld.txt", 1);
$this->createDirectory("{$directory}/emptyDirWithHiddenFile");
$this->createFile("{$directory}/emptyDirWithHiddenFile/.5MinutesOld.txt", 5);
$this->createDirectory("{$directory}/notEmptyDirWithHiddenFile");
$this->createFile("{$directory}/notEmptyDirWithHiddenFile/.1MinutesOld.txt", 1);
}

$this->artisan('clean:directories');

foreach ($directories as $directory => $config) {
$this->assertDirectoryExists("{$directory}/notEmptyDir");
$this->assertDirectoryNotExists("{$directory}/emptyDir");
$this->assertDirectoryExists("{$directory}/notEmptyDirWithHiddenFile");
$this->assertDirectoryNotExists("{$directory}/emptyDirWithHiddenFile");
}
}

Expand Down

0 comments on commit 40c3b4a

Please sign in to comment.