Skip to content

Fix for handling hidden files #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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