Skip to content
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
18 changes: 15 additions & 3 deletions src/Analyser/Parallel/ParallelAnalysisNodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,10 +553,22 @@ private function workerFailure(string $error, string $stderrFile): RuntimeExcept
*/
private function createWorkerFiles(string $dir): array
{
$created = [];

try {
$created[] = $inputFile = $this->temporaryFile($dir);
$created[] = $outputFile = $this->temporaryFile($dir);
$stderrFile = $this->temporaryFile($dir);
} catch (RuntimeException $runtimeException) {
$this->cleanup($created);

throw $runtimeException;
}

return [
'inputFile' => $this->temporaryFile($dir),
'outputFile' => $this->temporaryFile($dir),
'stderrFile' => $this->temporaryFile($dir),
'inputFile' => $inputFile,
'outputFile' => $outputFile,
'stderrFile' => $stderrFile,
];
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Analyser/Parallel/MockFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ function tempnam(string $directory, string $prefix): string|false
return false;
}

// an int allows that many successful calls before failing
if (is_int($GLOBALS['mock_tempnam']) && $GLOBALS['mock_tempnam']-- <= 0) {
return false;
}

$result = \tempnam($directory, $prefix);

if ($result !== false) {
Expand Down
32 changes: 32 additions & 0 deletions tests/Analyser/Parallel/ParallelAnalysisNodeExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,38 @@ public function testExtractThrowsWhenTempnamFails(): void
}
}

public function testExtractRemovesCreatedWorkerFilesWhenLaterTempnamFails(): void
{
$GLOBALS['mock_missing_functions'] = ['pcntl_fork'];
$GLOBALS['mock_tempnam'] = 1;
$GLOBALS['mock_tracked_tempnam_files'] = [];

$dir = $this->makeTemporaryDirectory('structarmed-parallel-test');
$file = $dir . '/Foo.php';
file_put_contents($file, '<?php class Foo {}');

$parallelAnalysisNodeExtractor = new ParallelAnalysisNodeExtractor($dir, [], [], 2);

try {
$parallelAnalysisNodeExtractor->extract([$file]);
$this->fail('Expected RuntimeException was not thrown.');
} catch (RuntimeException $runtimeException) {
$this->assertSame(
'Unable to create temporary file for parallel analysis.',
$runtimeException->getMessage()
);

/** @var list<string> $created */
$created = $GLOBALS['mock_tracked_tempnam_files'];
$this->assertCount(1, $created);
$this->assertFileDoesNotExist($created[0]);
} finally {
$GLOBALS['mock_missing_functions'] = [];
$GLOBALS['mock_tempnam'] = false;
$GLOBALS['mock_tracked_tempnam_files'] = [];
}
}

public function testExtractThrowsWhenPayloadIsInvalid(): void
{
$GLOBALS['mock_file_get_contents_payload'] = ['invalid' => 'payload'];
Expand Down
Loading