From 1da70698b7268a346e8d9c96116f286b0d98057a Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Thu, 9 Apr 2026 18:47:10 +0800 Subject: [PATCH] refactor: add full testing for `debugbar:clear` command --- .../Commands/Housekeeping/ClearDebugbar.php | 8 +-- tests/system/Commands/ClearDebugbarTest.php | 67 ++++++++++++++++--- 2 files changed, 62 insertions(+), 13 deletions(-) diff --git a/system/Commands/Housekeeping/ClearDebugbar.php b/system/Commands/Housekeeping/ClearDebugbar.php index dd49b24a7656..281a2c865d6b 100644 --- a/system/Commands/Housekeeping/ClearDebugbar.php +++ b/system/Commands/Housekeeping/ClearDebugbar.php @@ -58,15 +58,13 @@ public function run(array $params) helper('filesystem'); if (! delete_files(WRITEPATH . 'debugbar', false, true)) { - // @codeCoverageIgnoreStart CLI::error('Error deleting the debugbar JSON files.'); - CLI::newLine(); - return; - // @codeCoverageIgnoreEnd + return EXIT_ERROR; } CLI::write('Debugbar cleared.', 'green'); - CLI::newLine(); + + return EXIT_SUCCESS; } } diff --git a/tests/system/Commands/ClearDebugbarTest.php b/tests/system/Commands/ClearDebugbarTest.php index b3c2de18c394..dc1de4dc6f3b 100644 --- a/tests/system/Commands/ClearDebugbarTest.php +++ b/tests/system/Commands/ClearDebugbarTest.php @@ -16,6 +16,7 @@ use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\StreamFilterTrait; use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\RequiresOperatingSystem; /** * @internal @@ -31,10 +32,22 @@ protected function setUp(): void { parent::setUp(); + command('debugbar:clear'); + $this->resetStreamFilterBuffer(); + $this->time = time(); + $this->createDummyDebugbarJson(); + } + + protected function tearDown(): void + { + command('debugbar:clear'); + $this->resetStreamFilterBuffer(); + + parent::tearDown(); } - protected function createDummyDebugbarJson(): void + private function createDummyDebugbarJson(): void { $time = $this->time; $path = WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$time}.json"; @@ -50,18 +63,56 @@ protected function createDummyDebugbarJson(): void public function testClearDebugbarWorks(): void { - // test clean debugbar dir - $this->assertFileDoesNotExist(WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$this->time}.json"); - - // test dir is now populated with json - $this->createDummyDebugbarJson(); $this->assertFileExists(WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$this->time}.json"); command('debugbar:clear'); - $result = $this->getStreamFilterBuffer(); $this->assertFileDoesNotExist(WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$this->time}.json"); $this->assertFileExists(WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . 'index.html'); - $this->assertStringContainsString('Debugbar cleared.', $result); + $this->assertSame( + "Debugbar cleared.\n", + preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()), + ); + } + + #[RequiresOperatingSystem('Darwin|Linux')] + public function testClearDebugbarWithError(): void + { + $path = WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$this->time}.json"; + + // Attempt to make the file itself undeletable by setting the + // immutable/uchg flag on supported platforms. + $immutableSet = false; + if (str_starts_with(PHP_OS, 'Darwin')) { + @exec(sprintf('chflags uchg %s', escapeshellarg($path)), $output, $rc); + $immutableSet = $rc === 0; + } else { + // Try chattr on Linux with sudo (for containerized environments) + @exec('which chattr', $whichOut, $whichRc); + + if ($whichRc === 0) { + @exec(sprintf('sudo chattr +i %s', escapeshellarg($path)), $output, $rc); + $immutableSet = $rc === 0; + } + } + + if (! $immutableSet) { + $this->markTestSkipped('Cannot set file immutability in this environment'); + } + + command('debugbar:clear'); + + // Restore attributes so other tests are not affected. + if (str_starts_with(PHP_OS, 'Darwin')) { + @exec(sprintf('chflags nouchg %s', escapeshellarg($path))); + } else { + @exec(sprintf('sudo chattr -i %s', escapeshellarg($path))); + } + + $this->assertFileExists($path); + $this->assertSame( + "Error deleting the debugbar JSON files.\n", + preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()), + ); } }