Skip to content

Commit 84746b8

Browse files
committed
test: clean generated command files reliably
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 647bbdb commit 84746b8

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

tests/system/Commands/Generators/CommandGeneratorTest.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@ final class CommandGeneratorTest extends CIUnitTestCase
2727

2828
protected function tearDown(): void
2929
{
30-
$result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', $this->getStreamFilterBuffer());
31-
$file = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, trim(substr($result, 14)));
32-
$dir = dirname($file);
30+
preg_match_all('/File (?:created|overwritten): (APPPATH[^\r\n\x1b]+)/', $this->getStreamFilterBuffer(), $matches);
3331

34-
if (is_file($file)) {
35-
unlink($file);
36-
}
37-
if (is_dir($dir) && str_contains($dir, 'Commands')) {
38-
rmdir($dir);
32+
foreach ($matches[1] as $file) {
33+
$path = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, $file);
34+
35+
if (is_file($path)) {
36+
unlink($path);
37+
}
38+
39+
$dir = dirname($path);
40+
$dirFiles = is_dir($dir) ? scandir($dir) : false;
41+
42+
if (str_starts_with($dir, APPPATH . 'Commands') && $dirFiles !== false && count($dirFiles) === 2) {
43+
rmdir($dir);
44+
}
3945
}
4046
}
4147

0 commit comments

Comments
 (0)