Skip to content

Commit db8758e

Browse files
add more test caes
1 parent 907f74d commit db8758e

File tree

2 files changed

+107
-4
lines changed

2 files changed

+107
-4
lines changed

src/Commands/DeleteAllFiles.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
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; only for linux and mac';
12+
private $extension;
1213

1314
public function handle()
1415
{
15-
$path = $this->argument('path') ?? base_path();
16+
$path = $this->argument('path') ?? storage_path("logs");
1617

1718
$this->info("Deleting files and directories from: $path");
19+
$this->extension = $this->option('ext'); // Get the file extension filter
1820

1921
$rootPath = base_path($path); // Change this if you want to delete from another path
2022
$undeletedFiles = [];
@@ -44,7 +46,21 @@ private function deleteFilesRecursively($path, &$undeletedFiles)
4446
return;
4547
}
4648

47-
$items = File::allFiles($path);
49+
if ($this->extension) {
50+
$extension = $this->extension;
51+
// Get all files (filtered if ext is provided)
52+
$items = collect(File::files(base_path($path)))
53+
->filter(function ($file) use ($extension) {
54+
return !$extension || $file->getExtension() === $extension;
55+
});
56+
57+
if ($items->isEmpty()) {
58+
$this->info('No files found to delete.');
59+
return 0;
60+
}
61+
}else{
62+
$items = File::allFiles($path);
63+
}
4864
foreach ($items as $item) {
4965
try {
5066
File::delete($item);

src/Tests/CreateContractAndResponseTest.php

Lines changed: 87 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,90 @@ 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(Storage::exists('logs/laravel.log'));
40+
$this->assertFalse(Storage::exists('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 custom/logs')
52+
->expectsOutput('All files and folders deleted successfully!')
53+
->assertExitCode(0);
54+
55+
// Assert files are deleted
56+
$this->assertFalse(Storage::exists('custom/logs/app.log'));
57+
$this->assertFalse(Storage::exists('custom/logs/debug.log'));
58+
}
59+
public function test_handles_missing_files_gracefully()
60+
{
61+
Storage::fake();
62+
63+
// Run command when no logs exist
64+
$this->artisan('files:delete-all')
65+
->expectsOutput('No files found to delete.')
66+
->assertExitCode(0);
67+
}
68+
69+
public function test_handles_permission_errors_gracefully()
70+
{
71+
Storage::fake();
72+
73+
// Create a log file and make it read-only
74+
$filePath = storage_path('logs/protected.log');
75+
file_put_contents($filePath, 'Protected log');
76+
chmod($filePath, 0444); // Read-only (no delete permission)
77+
78+
// Run command
79+
$this->artisan('files:delete-all')
80+
->expectsOutput('Files that couldn\'t be deleted:');
81+
82+
// Ensure file still exists
83+
$this->assertFileExists($filePath);
84+
85+
// Reset permissions
86+
chmod($filePath, 0644);
87+
}
88+
89+
90+
public function test_command_help_message()
91+
{
92+
$this->artisan('files:delete-all --help')
93+
->expectsOutputToContain('Delete all files and folders recursively, skipping undeletable ones and logging them');
94+
}
95+
96+
public function test_deletes_only_specific_file_extensions()
97+
{
98+
Storage::fake();
99+
100+
// Create multiple file types
101+
Storage::put('logs/app.log', 'Log content');
102+
Storage::put('logs/debug.txt', 'Debug content');
103+
104+
// Run command with `--ext=log`
105+
$this->artisan('files:delete-all --ext=log');
106+
107+
// Ensure only `.log` is deleted
108+
$this->assertFalse(Storage::exists('logs/app.log'));
109+
$this->assertTrue(Storage::exists('logs/debug.txt'));
110+
}
24111
}

0 commit comments

Comments
 (0)