Skip to content

Commit 31eb126

Browse files
authored
Merge pull request #214 from php-school/feature/fs-cleanup
Cleanup temp FS writes
2 parents 0f53664 + 9095770 commit 31eb126

File tree

12 files changed

+93
-12
lines changed

12 files changed

+93
-12
lines changed

app/config.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Colors\Color;
66
use PhpSchool\PhpWorkshop\Listener\InitialCodeListener;
7+
use PhpSchool\PhpWorkshop\Listener\TearDownListener;
78
use PhpSchool\PhpWorkshop\Logger\ConsoleLogger;
89
use PhpSchool\PhpWorkshop\Logger\Logger;
910
use Psr\Log\LoggerInterface;
@@ -242,6 +243,9 @@
242243
);
243244
},
244245
RealPathListener::class => create(),
246+
TearDownListener::class => function (ContainerInterface $c) {
247+
return new TearDownListener($c->get(Filesystem::class));
248+
},
245249

246250
//checks
247251
FileExistsCheck::class => create(),
@@ -414,6 +418,11 @@ function (CgiResult $result) use ($c) {
414418
'exercise.selected' => [
415419
containerListener(InitialCodeListener::class)
416420
]
421+
],
422+
'cleanup-filesystem' => [
423+
'application.tear-down' => [
424+
containerListener(TearDownListener::class, 'cleanupTempDir')
425+
]
417426
]
418427
],
419428
];

src/Application.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ public function run(): int
267267
);
268268
return 1;
269269
}
270+
271+
$this->tearDown($container);
272+
270273
return $exitCode;
271274
}
272275

src/Check/DatabaseCheck.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,17 @@ function (CliExecuteEvent $e) {
138138
],
139139
function () use ($db) {
140140
unset($db);
141-
@unlink($this->userDatabasePath);
142-
@unlink($this->solutionDatabasePath);
141+
$this->unlink($this->userDatabasePath);
142+
$this->unlink($this->solutionDatabasePath);
143143
rmdir($this->databaseDirectory);
144144
}
145145
);
146146
}
147+
148+
private function unlink(string $file): void
149+
{
150+
if (file_exists($file)) {
151+
unlink($file);
152+
}
153+
}
147154
}

src/Exercise/ExerciseType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
* $typeCustom = ExerciseType::CUSTOM();
1616
* ```
1717
* @extends Enum<string>
18+
* @method static self CLI()
19+
* @method static self CGI()
20+
* @method static self CUSTOM()
1821
*/
1922
class ExerciseType extends Enum
2023
{

src/Listener/TearDownListener.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpSchool\PhpWorkshop\Listener;
6+
7+
use PhpSchool\PhpWorkshop\Event\EventInterface;
8+
use PhpSchool\PhpWorkshop\Utils\System;
9+
use Symfony\Component\Filesystem\Filesystem;
10+
11+
class TearDownListener
12+
{
13+
/**
14+
* @var Filesystem
15+
*/
16+
private $filesystem;
17+
18+
public function __construct(Filesystem $filesystem)
19+
{
20+
$this->filesystem = $filesystem;
21+
}
22+
23+
public function cleanupTempDir(): void
24+
{
25+
$this->filesystem->remove(System::tempDir());
26+
}
27+
}

src/Solution/InTempSolutionMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ public static function mapFile(string $file): string
5252

5353
private static function getDeterministicTempDir(string $path): string
5454
{
55-
return Path::join(System::tempDir(), 'php-school', md5($path));
55+
return Path::join(System::tempDir(), md5($path));
5656
}
5757
}

src/Utils/System.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public static function realpath(string $path): string
2121

2222
public static function tempDir(string $path = ''): string
2323
{
24-
return Path::join(self::realpath(sys_get_temp_dir()), $path);
24+
return Path::join(self::realpath(sys_get_temp_dir()), 'php-school', $path);
2525
}
2626
}

test/BaseTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public function getTemporaryFile(string $filename, string $content = null): stri
3131
return $file;
3232
}
3333

34-
@mkdir(dirname($file), 0777, true);
34+
if (!file_exists(dirname($file))) {
35+
mkdir(dirname($file), 0777, true);
36+
}
3537

3638
$content !== null
3739
? file_put_contents($file, $content)

test/Check/DatabaseCheckTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function setUp(): void
5858
$this->exercise = $this->createMock(DatabaseExerciseInterface::class);
5959
$this->exercise->method('getType')->willReturn(ExerciseType::CLI());
6060
$this->dbDir = sprintf(
61-
'%s/PhpSchool_PhpWorkshop_Check_DatabaseCheck',
61+
'%s/php-school/PhpSchool_PhpWorkshop_Check_DatabaseCheck',
6262
str_replace('\\', '/', realpath(sys_get_temp_dir()))
6363
);
6464

@@ -89,7 +89,7 @@ private function getRunnerManager(ExerciseInterface $exercise, EventDispatcher $
8989
public function testIfDatabaseFolderExistsExceptionIsThrown(): void
9090
{
9191
$eventDispatcher = new EventDispatcher(new ResultAggregator());
92-
@mkdir($this->dbDir);
92+
mkdir($this->dbDir, 0777, true);
9393
try {
9494
$this->check->attach($eventDispatcher);
9595
$this->fail('Exception was not thrown');
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpSchool\PhpWorkshopTest\Listener;
6+
7+
use PhpSchool\PhpWorkshop\Listener\TearDownListener;
8+
use PhpSchool\PhpWorkshop\Utils\System;
9+
use PHPUnit\Framework\TestCase;
10+
use Symfony\Component\Filesystem\Filesystem;
11+
12+
class TearDownListenerTest extends TestCase
13+
{
14+
public function testCleansUpTempDir(): void
15+
{
16+
$tempDir = System::tempDir();
17+
18+
mkdir($tempDir . '/some/path', 0777, true);
19+
touch($tempDir . '/some.file');
20+
touch($tempDir . '/some/path/another.file');
21+
22+
self::assertFileExists($tempDir . '/some.file');
23+
self::assertFileExists($tempDir . '/some/path/another.file');
24+
25+
(new TearDownListener(new Filesystem()))->cleanupTempDir();
26+
27+
self::assertFileDoesNotExist($tempDir . '/some.file');
28+
self::assertFileDoesNotExist($tempDir . '/some/path/another.file');
29+
}
30+
}

0 commit comments

Comments
 (0)