Skip to content
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
8 changes: 8 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ jobs:
working-directory: laravel-test-project
run: composer show laravel/framework

- name: Laravel version
working-directory: laravel-test-project
run: yes | sudo chmod -R 777 storage/ bootstrap/cache

- name: Laravel version
working-directory: laravel-test-project
run: sudo ls -l storage/ bootstrap/cache

- name: Copy Local Package
run: rsync -av --exclude='laravel-test-project' ./ laravel-test-project/packages/eren/laravel-commands/

Expand Down
41 changes: 35 additions & 6 deletions src/Commands/DeleteAllFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@

class DeleteAllFiles extends Command
{
protected $signature = 'files:delete-all {path? : The path to delete files from}';
protected $description = 'Delete all files and folders recursively, skipping undeletable ones and logging them';
protected $signature = 'files:delete-all {path? : The path to delete files from} {--ext= : optional file extension}';
protected $description = 'Delete all files and folders recursively, skipping undeletable ones and logging them; For Every OS😍😍.
Perhaps Sallary saver😘😘😘';
private $extension;

public function handle()
{
$path = $this->argument('path') ?? base_path();
$path = $this->argument('path') ?? storage_path("logs");

$this->info("Deleting files and directories from: $path");

$rootPath = base_path($path); // Change this if you want to delete from another path
$this->extension = $this->option('ext'); // Get the file extension filter
if ($path != storage_path("logs")) {
$rootPath = base_path($path); // Change this if you want to delete from another path
} else {
$rootPath = $path;
}
$undeletedFiles = [];

// OS Detection
Expand All @@ -41,21 +47,44 @@ public function handle()
private function deleteFilesRecursively($path, &$undeletedFiles)
{
if (!File::exists($path)) {
$this->info("{$path} does not exist");
return;
}

$items = File::allFiles($path);
if ($this->extension) {
$extension = $this->extension;
// Get all files (filtered if ext is provided)
$items = collect(File::files($path))
->filter(function ($file) use ($extension) {
return !$extension || $file->getExtension() === $extension;
});

if ($items->isEmpty()) {
$this->info('No files found to delete.');
return 0;
}
} else {
$items = File::allFiles($path);
}

$total_files = count($items);
$this->info("Total number of files to be deleted by the operations: {$total_files}");
foreach ($items as $item) {
try {
$this->info("deleting file: {$item}");
File::delete($item);
} catch (\Exception $e) {
$undeletedFiles[] = $item->getPathname();
}
}

$directories = File::directories($path);
$total_directores = count($directories);
$this->info("deleting directories: {$total_directores}");
foreach ($directories as $directory) {
try {
$this->warning("Stop here if you don\'t plan to delete directories.");
$this->info("deleting directory: {$directory}");
File::deleteDirectory($directory);
} catch (\Exception $e) {
$undeletedFiles[] = $directory;
Expand Down
44 changes: 44 additions & 0 deletions src/Tests/CreateContractAndResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;


Expand All @@ -21,4 +22,47 @@ public function test_command_creates_contract_and_response()
Artisan::call('storage:link-custom');
$this->assertTrue(true);
}

public function test_deletes_logs_by_default()
{
Storage::fake();

// Create log files
Storage::put('logs/laravel.log', 'Dummy content');
Storage::put('logs/error.log', 'Error log');

// Run command
$this->artisan('files:delete-all')
->assertExitCode(0);

// Ensure files are deleted
$this->assertFalse(File::exists(storage_path('logs/laravel.log')));
$this->assertFalse(File::exists(storage_path('logs/error.log')));
}
public function test_deletes_files_from_custom_path()
{
Storage::fake();

// Create dummy files in custom directory
Storage::put('custom/logs/app.log', 'Log file');
Storage::put('custom/logs/debug.log', 'Debugging');

// Run command with a custom path
$this->artisan('files:delete-all storage/app/custom/logs')
->assertExitCode(0);

// Assert files are deleted
$this->assertFalse(File::exists('storage/app/custom/logs/app.log'));
$this->assertFalse(File::exists('storage/app/custom/logs/debug.log'));
}
public function test_handles_missing_files_gracefully()
{
Storage::fake();
$directory = storage_path('logs');
// Run command when no logs exist
$this->artisan('files:delete-all')
->assertExitCode(0);
$this->assertFalse(File::exists($directory) && count(File::files($directory)) > 0);
}

}
61 changes: 61 additions & 0 deletions src/Tests/DeleteFilesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

class DeleteFilesTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function test_the_application_returns_a_successful_response()
{
$response = $this->get('/');

$response->assertStatus(200);
}
public function test_command_help_message()
{
$this->artisan('files:delete-all --help')
->expectsOutputToContain('Delete all files and folders recursively, skipping undeletable ones and logging them');
}

public function test_deletes_only_specific_file_extensions()
{
Storage::fake();

// Create multiple file types
Storage::put('logs/app.log', 'Log content');
Storage::put('logs/debug.txt', 'Debug content');

// Run command with `--ext=log`
$this->artisan('files:delete-all storage/app/logs --ext=log');

// Ensure only `.log` is deleted
$this->assertFalse(File::exists(storage_path('app.logs/app.log')));
$this->assertTrue(Storage::exists('logs/debug.txt'));
Storage::delete('logs/debug.txt');
}
public function test_deletes_directories()
{
Storage::fake();

// Create multiple file types
Storage::put('custom/logs/app.log', 'Log content');
Storage::put('custom/app.log', 'Log content');

// Run command with `--ext=log`
$this->artisan('files:delete-all storage/app/custom/app.log --ext=log');

// Ensure only `.log` is deleted
$this->assertFalse(File::exists(storage_path('app/custom/logs/app.log')));
$this->assertFalse(File::exists(storage_path('app/custom/app.log')));
$this->assertFalse(File::exists(storage_path('app/custom')) && count(File::files(storage_path('app/custom'))) > 0);
}
}