Skip to content

Commit 310b422

Browse files
committed
Cleanup temp FS writes
- prefix temp dir with php-school - fire application.tear-down on all app exits - listener on application.tear-down rm's temp/php-school
1 parent c496fec commit 310b422

File tree

10 files changed

+80
-8
lines changed

10 files changed

+80
-8
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/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/Check/DatabaseCheckTest.php

Lines changed: 1 addition & 1 deletion
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

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+
}

test/Solution/InTempSolutionMapperTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function testFileMapping(): void
2727

2828
self::assertFileExists($mappedFile);
2929
self::assertNotSame($filePath, $mappedFile);
30-
self::assertStringContainsString(System::tempDir('php-school'), $mappedFile);
30+
self::assertStringContainsString(System::tempDir(), $mappedFile);
3131
}
3232

3333
public function testDirectoryMapping(): void
@@ -42,7 +42,7 @@ public function testDirectoryMapping(): void
4242
self::assertFileExists(Path::join($mappedDir, 'test.file'));
4343
self::assertFileExists(Path::join($mappedDir, 'innerDir', 'test.file'));
4444
self::assertNotSame($this->getTemporaryDirectory(), $mappedDir);
45-
self::assertStringContainsString(System::tempDir('php-school'), $mappedDir);
45+
self::assertStringContainsString(System::tempDir(), $mappedDir);
4646
}
4747

4848
public function testMappingIsDeterministicTempDir(): void
@@ -58,7 +58,7 @@ public function testMappingIsDeterministicTempDir(): void
5858

5959
self::assertSame(
6060
InTempSolutionMapper::mapFile($filePath),
61-
Path::join(System::tempDir(), 'php-school', $fileHash, 'test.file')
61+
Path::join(System::tempDir(), $fileHash, 'test.file')
6262
);
6363

6464
self::assertNotSame(

test/Utils/SystemTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public function testRealpathReturnsFullPath(): void
2525

2626
public function testTempDir(): void
2727
{
28-
self::assertSame(realpath(sys_get_temp_dir()), System::tempDir());
28+
self::assertSame(realpath(sys_get_temp_dir()) . '/php-school', System::tempDir());
2929
}
3030

3131
public function testTempDirWithPath(): void
3232
{
33-
$expect = sprintf('%s/%s', realpath(sys_get_temp_dir()), 'test');
33+
$expect = sprintf('%s/php-school/%s', realpath(sys_get_temp_dir()), 'test');
3434
self::assertSame($expect, System::tempDir('test'));
3535
}
3636
}

0 commit comments

Comments
 (0)