Skip to content

Commit 7fa1101

Browse files
authored
refactor: add full testing for debugbar:clear command (#10093)
1 parent 2aaf6b6 commit 7fa1101

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

system/Commands/Housekeeping/ClearDebugbar.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,13 @@ public function run(array $params)
5858
helper('filesystem');
5959

6060
if (! delete_files(WRITEPATH . 'debugbar', false, true)) {
61-
// @codeCoverageIgnoreStart
6261
CLI::error('Error deleting the debugbar JSON files.');
63-
CLI::newLine();
6462

65-
return;
66-
// @codeCoverageIgnoreEnd
63+
return EXIT_ERROR;
6764
}
6865

6966
CLI::write('Debugbar cleared.', 'green');
70-
CLI::newLine();
67+
68+
return EXIT_SUCCESS;
7169
}
7270
}

tests/system/Commands/ClearDebugbarTest.php

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\Test\CIUnitTestCase;
1717
use CodeIgniter\Test\StreamFilterTrait;
1818
use PHPUnit\Framework\Attributes\Group;
19+
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;
1920

2021
/**
2122
* @internal
@@ -31,10 +32,22 @@ protected function setUp(): void
3132
{
3233
parent::setUp();
3334

35+
command('debugbar:clear');
36+
$this->resetStreamFilterBuffer();
37+
3438
$this->time = time();
39+
$this->createDummyDebugbarJson();
40+
}
41+
42+
protected function tearDown(): void
43+
{
44+
command('debugbar:clear');
45+
$this->resetStreamFilterBuffer();
46+
47+
parent::tearDown();
3548
}
3649

37-
protected function createDummyDebugbarJson(): void
50+
private function createDummyDebugbarJson(): void
3851
{
3952
$time = $this->time;
4053
$path = WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$time}.json";
@@ -50,18 +63,56 @@ protected function createDummyDebugbarJson(): void
5063

5164
public function testClearDebugbarWorks(): void
5265
{
53-
// test clean debugbar dir
54-
$this->assertFileDoesNotExist(WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$this->time}.json");
55-
56-
// test dir is now populated with json
57-
$this->createDummyDebugbarJson();
5866
$this->assertFileExists(WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$this->time}.json");
5967

6068
command('debugbar:clear');
61-
$result = $this->getStreamFilterBuffer();
6269

6370
$this->assertFileDoesNotExist(WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$this->time}.json");
6471
$this->assertFileExists(WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . 'index.html');
65-
$this->assertStringContainsString('Debugbar cleared.', $result);
72+
$this->assertSame(
73+
"Debugbar cleared.\n",
74+
preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()),
75+
);
76+
}
77+
78+
#[RequiresOperatingSystem('Darwin|Linux')]
79+
public function testClearDebugbarWithError(): void
80+
{
81+
$path = WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$this->time}.json";
82+
83+
// Attempt to make the file itself undeletable by setting the
84+
// immutable/uchg flag on supported platforms.
85+
$immutableSet = false;
86+
if (str_starts_with(PHP_OS, 'Darwin')) {
87+
@exec(sprintf('chflags uchg %s', escapeshellarg($path)), $output, $rc);
88+
$immutableSet = $rc === 0;
89+
} else {
90+
// Try chattr on Linux with sudo (for containerized environments)
91+
@exec('which chattr', $whichOut, $whichRc);
92+
93+
if ($whichRc === 0) {
94+
@exec(sprintf('sudo chattr +i %s', escapeshellarg($path)), $output, $rc);
95+
$immutableSet = $rc === 0;
96+
}
97+
}
98+
99+
if (! $immutableSet) {
100+
$this->markTestSkipped('Cannot set file immutability in this environment');
101+
}
102+
103+
command('debugbar:clear');
104+
105+
// Restore attributes so other tests are not affected.
106+
if (str_starts_with(PHP_OS, 'Darwin')) {
107+
@exec(sprintf('chflags nouchg %s', escapeshellarg($path)));
108+
} else {
109+
@exec(sprintf('sudo chattr -i %s', escapeshellarg($path)));
110+
}
111+
112+
$this->assertFileExists($path);
113+
$this->assertSame(
114+
"Error deleting the debugbar JSON files.\n",
115+
preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()),
116+
);
66117
}
67118
}

0 commit comments

Comments
 (0)