Skip to content

Commit

Permalink
Clean up all temporary files
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk committed Feb 13, 2023
1 parent 09cfc72 commit 86ea5b2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
9 changes: 9 additions & 0 deletions src/WrapperRunner/WrapperRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ final class WrapperRunner implements RunnerInterface
/** @var array<int,int> */
private array $batches = [];

/** @var list<SplFileInfo> */
private array $statusFiles = [];
/** @var list<SplFileInfo> */
private array $progressFiles = [];
/** @var list<SplFileInfo> */
private array $testresultFiles = [];
/** @var list<SplFileInfo> */
Expand Down Expand Up @@ -204,7 +208,10 @@ private function startWorker(int $token): WrapperWorker
$worker->start();
$this->batches[$token] = 0;

$this->statusFiles[] = $worker->statusFile;
$this->progressFiles[] = $worker->progressFile;
$this->testresultFiles[] = $worker->testresultFile;

if (isset($worker->junitFile)) {
$this->junitFiles[] = $worker->junitFile;
}
Expand Down Expand Up @@ -289,6 +296,8 @@ private function complete(TestResult $testResultSum): int
$testResultSum,
);

$this->clearFiles($this->statusFiles);
$this->clearFiles($this->progressFiles);
$this->clearFiles($this->testresultFiles);
$this->clearFiles($this->coverageFiles);
$this->clearFiles($this->junitFiles);
Expand Down
19 changes: 10 additions & 9 deletions src/WrapperRunner/WrapperWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ final class WrapperWorker
{
public const COMMAND_EXIT = "EXIT\n";

public readonly SplFileInfo $statusFile;
public readonly SplFileInfo $progressFile;
public readonly SplFileInfo $testresultFile;
public readonly SplFileInfo $junitFile;
public readonly SplFileInfo $coverageFile;
public readonly SplFileInfo $teamcityFile;
public readonly SplFileInfo $testdoxFile;

public readonly SplFileInfo $testdoxFile;
private ?string $currentlyExecuting = null;
private Process $process;
private int $inExecution = 0;
private InputStream $input;
private int $exitCode = -1;
private string $statusFilepath;

/** @param non-empty-string[] $parameters */
public function __construct(
Expand All @@ -51,15 +51,15 @@ public function __construct(
array $parameters,
private readonly int $token
) {
$commonTmpFilePath = sprintf(
$commonTmpFilePath = sprintf(
'%s%sworker_%02s_stdout_%s_',
$options->tmpDir,
DIRECTORY_SEPARATOR,
$token,
uniqid(),
);
$this->statusFilepath = $commonTmpFilePath . 'status';
touch($this->statusFilepath);
$this->statusFile = new SplFileInfo($commonTmpFilePath . 'status');
touch($this->statusFile->getPathname());
$this->progressFile = new SplFileInfo($commonTmpFilePath . 'progress');
touch($this->progressFile->getPathname());
$this->testresultFile = new SplFileInfo($commonTmpFilePath . 'testresult');
Expand All @@ -80,7 +80,7 @@ public function __construct(
}

$parameters[] = '--status-file';
$parameters[] = $this->statusFilepath;
$parameters[] = $this->statusFile->getPathname();
$parameters[] = '--progress-file';
$parameters[] = $this->progressFile->getPathname();
$parameters[] = '--testresult-file';
Expand Down Expand Up @@ -181,12 +181,13 @@ public function stop(): void

public function isFree(): bool
{
clearstatcache(true, $this->statusFilepath);
$statusFilepath = $this->statusFile->getPathname();
clearstatcache(true, $statusFilepath);

$isFree = $this->inExecution === filesize($this->statusFilepath);
$isFree = $this->inExecution === filesize($statusFilepath);

if ($isFree && $this->inExecution > 0) {
$exitCodes = file_get_contents($this->statusFilepath);
$exitCodes = file_get_contents($statusFilepath);
assert(is_string($exitCodes) && $exitCodes !== '');
$this->exitCode = (int) $exitCodes[-1];
}
Expand Down
3 changes: 1 addition & 2 deletions test/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ abstract class TestBase extends TestCase
protected string $runnerClass = WrapperRunner::class;
/** @var array<string, string|bool|int|null> */
protected array $bareOptions = [];
/** @var string */
protected $tmpDir;
protected string $tmpDir;

final protected function setUp(): void
{
Expand Down
4 changes: 4 additions & 0 deletions test/Unit/WrapperRunner/WrapperRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function defined;
use function file_get_contents;
use function file_put_contents;
use function glob;
use function min;
use function posix_mkfifo;
use function preg_match;
Expand Down Expand Up @@ -468,6 +469,9 @@ public function testRunningFewerTestsThanTheWorkersIsPossible(): void

$runnerResult = $this->runRunner();
self::assertEquals(0, $runnerResult->exitCode);
$glob = glob($this->tmpDir . '/*');
self::assertNotFalse($glob);
self::assertCount(0, $glob);
}

public function testResultsAreCorrect(): void
Expand Down

0 comments on commit 86ea5b2

Please sign in to comment.