Skip to content

Commit 769b3d6

Browse files
committed
Make the collected data cache test pass on Windows and slow runners
The temporary directory has to go through FileHelper::normalizePath(), as the analysed files do, or the index keys never match on Windows. A full analysis records its own time, so the comparison between a partial save and a fresh save of the same data leaves that frame out.
1 parent 35e4c77 commit 769b3d6

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

‎tests/PHPStan/Analyser/ResultCache/ResultCacheManagerCollectedDataTest.php‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Analyser\ResultCache;
44

5+
use Nette\Utils\Strings;
56
use Override;
67
use PHPStan\Analyser\AnalyserResult;
78
use PHPStan\Command\Output;
@@ -20,14 +21,15 @@
2021
use Symfony\Component\Console\Output\NullOutput;
2122
use Symfony\Component\Console\Style\SymfonyStyle as SymfonyConsoleStyle;
2223
use function array_keys;
24+
use function basename;
2325
use function count;
2426
use function file_get_contents;
2527
use function file_put_contents;
2628
use function is_file;
2729
use function md5_file;
2830
use function mkdir;
31+
use function ord;
2932
use function rmdir;
30-
use function str_replace;
3133
use function strlen;
3234
use function strpos;
3335
use function substr;
@@ -47,11 +49,12 @@ class ResultCacheManagerCollectedDataTest extends PHPStanTestCase
4749
protected function setUp(): void
4850
{
4951
parent::setUp();
50-
$this->directory = sys_get_temp_dir() . '/phpstan-collected-data-' . uniqid();
52+
$fileHelper = self::getContainer()->getByType(FileHelper::class);
53+
$this->directory = $fileHelper->normalizePath(sys_get_temp_dir() . '/phpstan-collected-data-' . uniqid());
5154
// the cache file must not be in the analysed directory, or it counts as a scanned file
5255
mkdir($this->directory . '/src', 0777, true);
5356
foreach (['a', 'b', 'c'] as $name) {
54-
$file = $this->directory . '/src/' . $name . '.php';
57+
$file = $fileHelper->normalizePath($this->directory . '/src/' . $name . '.php');
5558
file_put_contents($file, '<?php class Cache' . $name . " {}\n");
5659
$this->files[] = $file;
5760
}
@@ -87,7 +90,7 @@ public function testCachedEntriesAreIndexedAndReadBack(): void
8790
$this->assertNotFalse($contents);
8891
foreach ($lazy->getCachedIndex() as $file => [$offset, $length]) {
8992
$entry = substr($contents, $offset, $length);
90-
$this->assertStringContainsString('"' . str_replace($this->directory . '/', '', $file) . '"', $entry);
93+
$this->assertStringContainsString('"src/' . basename($file) . '"', $entry);
9194
$this->assertStringEndsWith('}', $entry);
9295
}
9396
}
@@ -123,9 +126,9 @@ public function testPartialSaveEqualsFreshSaveOfMergedData(): void
123126
$expected[$this->files[1]] = $freshB[$this->files[1]];
124127
$this->assertSame($expected, $processed->getAnalyserResult()->getCollectedData());
125128

126-
$partial = md5_file($this->cacheFilePath());
129+
$partial = $this->cacheContentsWithoutTime();
127130
$this->saveFullAnalysis($expected);
128-
$this->assertSame($partial, md5_file($this->cacheFilePath()));
131+
$this->assertSame($partial, $this->cacheContentsWithoutTime());
129132
}
130133

131134
public function testDamagedValueDiscardsTheCacheWhenRead(): void
@@ -186,7 +189,7 @@ private function collectedData(array $names): array
186189
{
187190
$data = [];
188191
foreach ($names as $name) {
189-
$data[$this->directory . '/src/' . $name . '.php'] = [MethodWithoutImpurePointsCollector::class => ['usage in ' . $name]];
192+
$data[$this->files[ord($name) - ord('a')]] = [MethodWithoutImpurePointsCollector::class => ['usage in ' . $name]];
190193
}
191194

192195
return $data;
@@ -236,6 +239,17 @@ private function analyserResult(array $collectedData): AnalyserResult
236239
);
237240
}
238241

242+
/**
243+
* A full analysis records its own time, which a partial save keeps from the previous one.
244+
*/
245+
private function cacheContentsWithoutTime(): string
246+
{
247+
$contents = file_get_contents($this->cacheFilePath());
248+
$this->assertNotFalse($contents);
249+
250+
return Strings::replace($contents, '~^lastFullAnalysisTime \d+\ni:\d+;~m', '');
251+
}
252+
239253
private function breakFirstCollectedDataValue(): void
240254
{
241255
$contents = file_get_contents($this->cacheFilePath());

0 commit comments

Comments
 (0)