Skip to content

Cleanup temp FS writes #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Colors\Color;
use PhpSchool\PhpWorkshop\Listener\InitialCodeListener;
use PhpSchool\PhpWorkshop\Listener\TearDownListener;
use PhpSchool\PhpWorkshop\Logger\ConsoleLogger;
use PhpSchool\PhpWorkshop\Logger\Logger;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -242,6 +243,9 @@
);
},
RealPathListener::class => create(),
TearDownListener::class => function (ContainerInterface $c) {
return new TearDownListener($c->get(Filesystem::class));
},

//checks
FileExistsCheck::class => create(),
Expand Down Expand Up @@ -414,6 +418,11 @@ function (CgiResult $result) use ($c) {
'exercise.selected' => [
containerListener(InitialCodeListener::class)
]
],
'cleanup-filesystem' => [
'application.tear-down' => [
containerListener(TearDownListener::class, 'cleanupTempDir')
]
]
],
];
3 changes: 3 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ public function run(): int
);
return 1;
}

$this->tearDown($container);

return $exitCode;
}

Expand Down
11 changes: 9 additions & 2 deletions src/Check/DatabaseCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,17 @@ function (CliExecuteEvent $e) {
],
function () use ($db) {
unset($db);
@unlink($this->userDatabasePath);
@unlink($this->solutionDatabasePath);
$this->unlink($this->userDatabasePath);
$this->unlink($this->solutionDatabasePath);
rmdir($this->databaseDirectory);
}
);
}

private function unlink(string $file): void
{
if (file_exists($file)) {
unlink($file);
}
}
}
3 changes: 3 additions & 0 deletions src/Exercise/ExerciseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* $typeCustom = ExerciseType::CUSTOM();
* ```
* @extends Enum<string>
* @method static self CLI()
* @method static self CGI()
* @method static self CUSTOM()
*/
class ExerciseType extends Enum
{
Expand Down
27 changes: 27 additions & 0 deletions src/Listener/TearDownListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshop\Listener;

use PhpSchool\PhpWorkshop\Event\EventInterface;
use PhpSchool\PhpWorkshop\Utils\System;
use Symfony\Component\Filesystem\Filesystem;

class TearDownListener
{
/**
* @var Filesystem
*/
private $filesystem;

public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}

public function cleanupTempDir(): void
{
$this->filesystem->remove(System::tempDir());
}
}
2 changes: 1 addition & 1 deletion src/Solution/InTempSolutionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ public static function mapFile(string $file): string

private static function getDeterministicTempDir(string $path): string
{
return Path::join(System::tempDir(), 'php-school', md5($path));
return Path::join(System::tempDir(), md5($path));
}
}
2 changes: 1 addition & 1 deletion src/Utils/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public static function realpath(string $path): string

public static function tempDir(string $path = ''): string
{
return Path::join(self::realpath(sys_get_temp_dir()), $path);
return Path::join(self::realpath(sys_get_temp_dir()), 'php-school', $path);
}
}
4 changes: 3 additions & 1 deletion test/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public function getTemporaryFile(string $filename, string $content = null): stri
return $file;
}

@mkdir(dirname($file), 0777, true);
if (!file_exists(dirname($file))) {
mkdir(dirname($file), 0777, true);
}

$content !== null
? file_put_contents($file, $content)
Expand Down
4 changes: 2 additions & 2 deletions test/Check/DatabaseCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function setUp(): void
$this->exercise = $this->createMock(DatabaseExerciseInterface::class);
$this->exercise->method('getType')->willReturn(ExerciseType::CLI());
$this->dbDir = sprintf(
'%s/PhpSchool_PhpWorkshop_Check_DatabaseCheck',
'%s/php-school/PhpSchool_PhpWorkshop_Check_DatabaseCheck',
str_replace('\\', '/', realpath(sys_get_temp_dir()))
);

Expand Down Expand Up @@ -89,7 +89,7 @@ private function getRunnerManager(ExerciseInterface $exercise, EventDispatcher $
public function testIfDatabaseFolderExistsExceptionIsThrown(): void
{
$eventDispatcher = new EventDispatcher(new ResultAggregator());
@mkdir($this->dbDir);
mkdir($this->dbDir, 0777, true);
try {
$this->check->attach($eventDispatcher);
$this->fail('Exception was not thrown');
Expand Down
30 changes: 30 additions & 0 deletions test/Listener/TearDownListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshopTest\Listener;

use PhpSchool\PhpWorkshop\Listener\TearDownListener;
use PhpSchool\PhpWorkshop\Utils\System;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;

class TearDownListenerTest extends TestCase
{
public function testCleansUpTempDir(): void
{
$tempDir = System::tempDir();

mkdir($tempDir . '/some/path', 0777, true);
touch($tempDir . '/some.file');
touch($tempDir . '/some/path/another.file');

self::assertFileExists($tempDir . '/some.file');
self::assertFileExists($tempDir . '/some/path/another.file');

(new TearDownListener(new Filesystem()))->cleanupTempDir();

self::assertFileDoesNotExist($tempDir . '/some.file');
self::assertFileDoesNotExist($tempDir . '/some/path/another.file');
}
}
6 changes: 3 additions & 3 deletions test/Solution/InTempSolutionMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testFileMapping(): void

self::assertFileExists($mappedFile);
self::assertNotSame($filePath, $mappedFile);
self::assertStringContainsString(System::tempDir('php-school'), $mappedFile);
self::assertStringContainsString(System::tempDir(), $mappedFile);
}

public function testDirectoryMapping(): void
Expand All @@ -42,7 +42,7 @@ public function testDirectoryMapping(): void
self::assertFileExists(Path::join($mappedDir, 'test.file'));
self::assertFileExists(Path::join($mappedDir, 'innerDir', 'test.file'));
self::assertNotSame($this->getTemporaryDirectory(), $mappedDir);
self::assertStringContainsString(System::tempDir('php-school'), $mappedDir);
self::assertStringContainsString(System::tempDir(), $mappedDir);
}

public function testMappingIsDeterministicTempDir(): void
Expand All @@ -58,7 +58,7 @@ public function testMappingIsDeterministicTempDir(): void

self::assertSame(
InTempSolutionMapper::mapFile($filePath),
Path::join(System::tempDir(), 'php-school', $fileHash, 'test.file')
Path::join(System::tempDir(), $fileHash, 'test.file')
);

self::assertNotSame(
Expand Down
4 changes: 2 additions & 2 deletions test/Utils/SystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function testRealpathReturnsFullPath(): void

public function testTempDir(): void
{
self::assertSame(realpath(sys_get_temp_dir()), System::tempDir());
self::assertSame(realpath(sys_get_temp_dir()) . '/php-school', System::tempDir());
}

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