Skip to content

Commit 9d1289c

Browse files
test
1 parent 13f0676 commit 9d1289c

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

src/Commands/DeleteAllFiles.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
class DeleteAllFiles extends Command
99
{
1010
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; only for linux and mac';
11+
protected $description = 'Delete all files and folders recursively, skipping undeletable ones and logging them; For Every OS😍😍.
12+
Perhaps Sallary saver😘😘😘';
1213
private $extension;
1314

1415
public function handle()
@@ -17,8 +18,11 @@ public function handle()
1718

1819
$this->info("Deleting files and directories from: $path");
1920
$this->extension = $this->option('ext'); // Get the file extension filter
20-
21-
$rootPath = base_path($path); // Change this if you want to delete from another path
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+
}
2226
$undeletedFiles = [];
2327

2428
// OS Detection
@@ -43,13 +47,14 @@ public function handle()
4347
private function deleteFilesRecursively($path, &$undeletedFiles)
4448
{
4549
if (!File::exists($path)) {
50+
$this->info("{$path} does not exist");
4651
return;
4752
}
4853

4954
if ($this->extension) {
5055
$extension = $this->extension;
5156
// Get all files (filtered if ext is provided)
52-
$items = collect(File::files(base_path($path)))
57+
$items = collect(File::files($path))
5358
->filter(function ($file) use ($extension) {
5459
return !$extension || $file->getExtension() === $extension;
5560
});
@@ -58,20 +63,28 @@ private function deleteFilesRecursively($path, &$undeletedFiles)
5863
$this->info('No files found to delete.');
5964
return 0;
6065
}
61-
}else{
66+
} else {
6267
$items = File::allFiles($path);
6368
}
69+
70+
$total_files = count($items);
71+
$this->info("Total number of files to be deleted by the operations: {$total_files}");
6472
foreach ($items as $item) {
6573
try {
74+
$this->info("deleting file: {$item}");
6675
File::delete($item);
6776
} catch (\Exception $e) {
6877
$undeletedFiles[] = $item->getPathname();
6978
}
7079
}
7180

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

src/Tests/CreateContractAndResponseTest.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public function test_deletes_logs_by_default()
3636
->assertExitCode(0);
3737

3838
// Ensure files are deleted
39-
$this->assertFalse(Storage::exists('logs/laravel.log'));
40-
$this->assertFalse(Storage::exists('logs/error.log'));
39+
$this->assertFalse(File::exists(storage_path('logs/laravel.log')));
40+
$this->assertFalse(File::exists(storage_path('logs/error.log')));
4141
}
4242
public function test_deletes_files_from_custom_path()
4343
{
@@ -48,22 +48,22 @@ public function test_deletes_files_from_custom_path()
4848
Storage::put('custom/logs/debug.log', 'Debugging');
4949

5050
// Run command with a custom path
51-
$this->artisan('files:delete-all custom/logs')
52-
->expectsOutput('All files and folders deleted successfully!')
51+
$this->artisan('files:delete-all storage/app/custom/logs')
5352
->assertExitCode(0);
5453

5554
// Assert files are deleted
56-
$this->assertFalse(Storage::exists('custom/logs/app.log'));
57-
$this->assertFalse(Storage::exists('custom/logs/debug.log'));
55+
$this->assertFalse(File::exists('storage/app/custom/logs/app.log'));
56+
$this->assertFalse(File::exists('storage/app/custom/logs/debug.log'));
5857
}
5958
public function test_handles_missing_files_gracefully()
6059
{
6160
Storage::fake();
62-
61+
$directory = storage_path('logs');
6362
// Run command when no logs exist
6463
$this->artisan('files:delete-all')
65-
->expectsOutput('No files found to delete.')
6664
->assertExitCode(0);
65+
$this->assertFalse(File::exists($directory) && count(File::files($directory)) > 0);
66+
6767
}
6868

6969
public function test_handles_permission_errors_gracefully()
@@ -76,14 +76,14 @@ public function test_handles_permission_errors_gracefully()
7676
chmod($filePath, 0444); // Read-only (no delete permission)
7777

7878
// Run command
79-
$this->artisan('files:delete-all')
80-
->expectsOutput('Files that couldn\'t be deleted:');
79+
$this->artisan('files:delete-all');
8180

8281
// Ensure file still exists
8382
$this->assertFileExists($filePath);
8483

8584
// Reset permissions
86-
chmod($filePath, 0644);
85+
chmod($filePath, 0775);
86+
File::delete($filePath);
8787
}
8888

8989

@@ -105,7 +105,23 @@ public function test_deletes_only_specific_file_extensions()
105105
$this->artisan('files:delete-all --ext=log');
106106

107107
// Ensure only `.log` is deleted
108-
$this->assertFalse(Storage::exists('logs/app.log'));
109-
$this->assertTrue(Storage::exists('logs/debug.txt'));
108+
$this->assertFalse(File::exists(storage_path('app/logs/app.log')));
109+
$this->assertTrue(File::exists(storage_path('app/logs/debug.txt')));
110+
}
111+
public function test_deletes_directories()
112+
{
113+
Storage::fake();
114+
115+
// Create multiple file types
116+
Storage::put('custom/logs/app.log', 'Log content');
117+
Storage::put('custom/app.log', 'Log content');
118+
119+
// Run command with `--ext=log`
120+
$this->artisan('files:delete-all storage/app/custom/app.log --ext=log');
121+
122+
// Ensure only `.log` is deleted
123+
$this->assertFalse(File::exists(storage_path('app/custom/logs/app.log')));
124+
$this->assertFalse(File::exists(storage_path('app/custom/app.log')));
125+
$this->assertFalse(File::exists(storage_path('app/custom')) && count(File::files(storage_path('app/custom'))) > 0);
110126
}
111127
}

0 commit comments

Comments
 (0)