Skip to content

Commit 44b3325

Browse files
Merge pull request #4 from NoumanAhmad448/dev
add more test caes
2 parents eff8109 + 909a0a1 commit 44b3325

File tree

4 files changed

+148
-6
lines changed

4 files changed

+148
-6
lines changed

.github/workflows/tests.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ jobs:
4444
working-directory: laravel-test-project
4545
run: composer show laravel/framework
4646

47+
- name: Laravel version
48+
working-directory: laravel-test-project
49+
run: yes | sudo chmod -R 777 storage/ bootstrap/cache
50+
51+
- name: Laravel version
52+
working-directory: laravel-test-project
53+
run: sudo ls -l storage/ bootstrap/cache
54+
4755
- name: Copy Local Package
4856
run: rsync -av --exclude='laravel-test-project' ./ laravel-test-project/packages/eren/laravel-commands/
4957

src/Commands/DeleteAllFiles.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@
77

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

1315
public function handle()
1416
{
15-
$path = $this->argument('path') ?? base_path();
17+
$path = $this->argument('path') ?? storage_path("logs");
1618

1719
$this->info("Deleting files and directories from: $path");
18-
19-
$rootPath = base_path($path); // Change this if you want to delete from another path
20+
$this->extension = $this->option('ext'); // Get the file extension filter
21+
if ($path != storage_path("logs")) {
22+
$rootPath = base_path($path); // Change this if you want to delete from another path
23+
} else {
24+
$rootPath = $path;
25+
}
2026
$undeletedFiles = [];
2127

2228
// OS Detection
@@ -41,21 +47,44 @@ public function handle()
4147
private function deleteFilesRecursively($path, &$undeletedFiles)
4248
{
4349
if (!File::exists($path)) {
50+
$this->info("{$path} does not exist");
4451
return;
4552
}
4653

47-
$items = File::allFiles($path);
54+
if ($this->extension) {
55+
$extension = $this->extension;
56+
// Get all files (filtered if ext is provided)
57+
$items = collect(File::files($path))
58+
->filter(function ($file) use ($extension) {
59+
return !$extension || $file->getExtension() === $extension;
60+
});
61+
62+
if ($items->isEmpty()) {
63+
$this->info('No files found to delete.');
64+
return 0;
65+
}
66+
} else {
67+
$items = File::allFiles($path);
68+
}
69+
70+
$total_files = count($items);
71+
$this->info("Total number of files to be deleted by the operations: {$total_files}");
4872
foreach ($items as $item) {
4973
try {
74+
$this->info("deleting file: {$item}");
5075
File::delete($item);
5176
} catch (\Exception $e) {
5277
$undeletedFiles[] = $item->getPathname();
5378
}
5479
}
5580

5681
$directories = File::directories($path);
82+
$total_directores = count($directories);
83+
$this->info("deleting directories: {$total_directores}");
5784
foreach ($directories as $directory) {
5885
try {
86+
$this->warning("Stop here if you don\'t plan to delete directories.");
87+
$this->info("deleting directory: {$directory}");
5988
File::deleteDirectory($directory);
6089
} catch (\Exception $e) {
6190
$undeletedFiles[] = $directory;

src/Tests/CreateContractAndResponseTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Facades\Artisan;
66
use Illuminate\Support\Facades\File;
7+
use Illuminate\Support\Facades\Storage;
78
use Tests\TestCase;
89

910

@@ -21,4 +22,47 @@ public function test_command_creates_contract_and_response()
2122
Artisan::call('storage:link-custom');
2223
$this->assertTrue(true);
2324
}
25+
26+
public function test_deletes_logs_by_default()
27+
{
28+
Storage::fake();
29+
30+
// Create log files
31+
Storage::put('logs/laravel.log', 'Dummy content');
32+
Storage::put('logs/error.log', 'Error log');
33+
34+
// Run command
35+
$this->artisan('files:delete-all')
36+
->assertExitCode(0);
37+
38+
// Ensure files are deleted
39+
$this->assertFalse(File::exists(storage_path('logs/laravel.log')));
40+
$this->assertFalse(File::exists(storage_path('logs/error.log')));
41+
}
42+
public function test_deletes_files_from_custom_path()
43+
{
44+
Storage::fake();
45+
46+
// Create dummy files in custom directory
47+
Storage::put('custom/logs/app.log', 'Log file');
48+
Storage::put('custom/logs/debug.log', 'Debugging');
49+
50+
// Run command with a custom path
51+
$this->artisan('files:delete-all storage/app/custom/logs')
52+
->assertExitCode(0);
53+
54+
// Assert files are deleted
55+
$this->assertFalse(File::exists('storage/app/custom/logs/app.log'));
56+
$this->assertFalse(File::exists('storage/app/custom/logs/debug.log'));
57+
}
58+
public function test_handles_missing_files_gracefully()
59+
{
60+
Storage::fake();
61+
$directory = storage_path('logs');
62+
// Run command when no logs exist
63+
$this->artisan('files:delete-all')
64+
->assertExitCode(0);
65+
$this->assertFalse(File::exists($directory) && count(File::files($directory)) > 0);
66+
}
67+
2468
}

src/Tests/DeleteFilesTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use Tests\TestCase;
6+
use Illuminate\Support\Facades\Artisan;
7+
use Illuminate\Support\Facades\File;
8+
use Illuminate\Support\Facades\Storage;
9+
10+
class DeleteFilesTest extends TestCase
11+
{
12+
/**
13+
* A basic test example.
14+
*
15+
* @return void
16+
*/
17+
public function test_the_application_returns_a_successful_response()
18+
{
19+
$response = $this->get('/');
20+
21+
$response->assertStatus(200);
22+
}
23+
public function test_command_help_message()
24+
{
25+
$this->artisan('files:delete-all --help')
26+
->expectsOutputToContain('Delete all files and folders recursively, skipping undeletable ones and logging them');
27+
}
28+
29+
public function test_deletes_only_specific_file_extensions()
30+
{
31+
Storage::fake();
32+
33+
// Create multiple file types
34+
Storage::put('logs/app.log', 'Log content');
35+
Storage::put('logs/debug.txt', 'Debug content');
36+
37+
// Run command with `--ext=log`
38+
$this->artisan('files:delete-all storage/app/logs --ext=log');
39+
40+
// Ensure only `.log` is deleted
41+
$this->assertFalse(File::exists(storage_path('app.logs/app.log')));
42+
$this->assertTrue(Storage::exists('logs/debug.txt'));
43+
Storage::delete('logs/debug.txt');
44+
}
45+
public function test_deletes_directories()
46+
{
47+
Storage::fake();
48+
49+
// Create multiple file types
50+
Storage::put('custom/logs/app.log', 'Log content');
51+
Storage::put('custom/app.log', 'Log content');
52+
53+
// Run command with `--ext=log`
54+
$this->artisan('files:delete-all storage/app/custom/app.log --ext=log');
55+
56+
// Ensure only `.log` is deleted
57+
$this->assertFalse(File::exists(storage_path('app/custom/logs/app.log')));
58+
$this->assertFalse(File::exists(storage_path('app/custom/app.log')));
59+
$this->assertFalse(File::exists(storage_path('app/custom')) && count(File::files(storage_path('app/custom'))) > 0);
60+
}
61+
}

0 commit comments

Comments
 (0)