mirror of
https://github.com/pestphp/pest.git
synced 2026-12-06 15:52:16 +01:00
fix(tia): drop cached coverage of tests that no longer exist (#2000)
* fix(tia): drop cached coverage of tests that no longer exist A deleted or renamed test never runs again, so its hits stayed in coverage.bin.gz and --min passed on a real coverage drop. Fixes #1957 * Update deleted coverage success snapshot --------- Co-authored-by: Tiago Santos <tsantos@lynixt.pt> Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
@@ -117,17 +117,12 @@ final class CoverageMerger
|
||||
private static function stripCurrentTestsFromCached(CodeCoverage $cached, CodeCoverage $current): void
|
||||
{
|
||||
$currentIds = self::collectTestIds($current);
|
||||
|
||||
if ($currentIds === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$cachedData = $cached->getData();
|
||||
|
||||
$staleIndexes = [];
|
||||
|
||||
foreach ($cachedData->testIds() as $index => $id) {
|
||||
if (in_array($id, $currentIds, true)) {
|
||||
if (in_array($id, $currentIds, true) || ! self::testExists($id)) {
|
||||
$staleIndexes[$index] = true;
|
||||
}
|
||||
}
|
||||
@@ -157,6 +152,19 @@ final class CoverageMerger
|
||||
$cachedData->setLineCoverage($lineCoverage);
|
||||
}
|
||||
|
||||
private static function testExists(string $id): bool
|
||||
{
|
||||
$name = explode('#', $id, 2)[0];
|
||||
|
||||
if (! str_contains($name, '::')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
[$class, $method] = explode('::', $name, 2);
|
||||
|
||||
return method_exists($class, $method);
|
||||
}
|
||||
|
||||
private static function dropLinesMissingFromCurrent(CodeCoverage $cached, CodeCoverage $current): void
|
||||
{
|
||||
$currentLines = $current->getData(true)->lineCoverage();
|
||||
|
||||
@@ -1916,6 +1916,9 @@
|
||||
✓ output format → it returns a 32-character hex xxh128 hash
|
||||
✓ output format → it returns a stable hash for empty content
|
||||
|
||||
WARN Tests\Unit\Plugins\Tia\DeletedTestCoverage
|
||||
- cached hits of a deleted test are dropped while a replayed test stays → Coverage is not available
|
||||
|
||||
PASS Tests\Unit\Plugins\Tia\FileState
|
||||
✓ keysWithPrefix() → it lists keys matching the prefix
|
||||
✓ keysWithPrefix() → it ignores in-flight temporary files from concurrent writes
|
||||
@@ -2294,4 +2297,4 @@
|
||||
✓ pass with dataset with ('my-datas-set-value')
|
||||
✓ within describe → pass with dataset with ('my-datas-set-value')
|
||||
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 36 skipped, 1626 passed (3592 assertions)
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 37 skipped, 1626 passed (3592 assertions)
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Pest\Plugins\Tia;
|
||||
use Pest\Plugins\Tia\Contracts\State;
|
||||
use Pest\Plugins\Tia\CoverageMerger;
|
||||
use Pest\Plugins\Tia\FileState;
|
||||
use Pest\Support\Container;
|
||||
use Pest\Support\Coverage;
|
||||
use Pest\Support\Str;
|
||||
use SebastianBergmann\CodeCoverage\CodeCoverage;
|
||||
use SebastianBergmann\CodeCoverage\Driver\Selector;
|
||||
use SebastianBergmann\CodeCoverage\Filter;
|
||||
|
||||
test('cached hits of a deleted test are dropped while a replayed test stays', function (): void {
|
||||
$source = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-deleted-coverage-'.bin2hex(random_bytes(4)).'.php';
|
||||
$kept = Str::class.'::endsWith';
|
||||
$keptDataset = Str::class.'::endsWith#0';
|
||||
$gone = Str::class.'::testThatWasDeleted';
|
||||
|
||||
$cached = coverageWith([
|
||||
0 => $gone,
|
||||
1 => $kept,
|
||||
2 => $keptDataset,
|
||||
], [
|
||||
$source => [
|
||||
9 => [0 => 1],
|
||||
14 => [1 => 1],
|
||||
19 => [2 => 1],
|
||||
],
|
||||
]);
|
||||
$current = coverageWith([], []);
|
||||
|
||||
$merged = mergeCoverage($cached, $current);
|
||||
$lines = $merged->getData(true)->lineCoverage()[$source];
|
||||
|
||||
expect($lines[9])->toBe([])
|
||||
->and($lines[14])->toBe([1 => 1])
|
||||
->and($lines[19])->toBe([2 => 1]);
|
||||
|
||||
if (is_file($source)) {
|
||||
unlink($source);
|
||||
}
|
||||
})->skip(! Coverage::isAvailable(), 'Coverage is not available');
|
||||
|
||||
function coverageWith(array $testIds, array $lines): CodeCoverage
|
||||
{
|
||||
$filter = new Filter;
|
||||
$coverage = new CodeCoverage(new Selector()->forLineCoverage($filter), $filter);
|
||||
$data = $coverage->getData(true);
|
||||
$data->setTestIds($testIds);
|
||||
$data->setLineCoverage($lines);
|
||||
$coverage->setData($data);
|
||||
|
||||
return $coverage;
|
||||
}
|
||||
|
||||
function mergeCoverage(CodeCoverage $cached, CodeCoverage $current): CodeCoverage
|
||||
{
|
||||
$directory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-coverage-state-'.bin2hex(random_bytes(4));
|
||||
mkdir($directory);
|
||||
$report = $directory.DIRECTORY_SEPARATOR.'coverage.php';
|
||||
$container = Container::getInstance();
|
||||
$previous = $container->get(State::class);
|
||||
$container->add(State::class, new FileState($directory));
|
||||
|
||||
file_put_contents($report, '<?php return unserialize('.var_export(serialize($current), true).");\n");
|
||||
$compressed = gzencode(serialize($cached));
|
||||
$state = $container->get(State::class);
|
||||
assert($state instanceof FileState);
|
||||
$state->write(Tia::KEY_COVERAGE_CACHE, $compressed === false ? serialize($cached) : $compressed);
|
||||
$state->write(Tia::KEY_COVERAGE_MARKER, '');
|
||||
|
||||
try {
|
||||
CoverageMerger::applyIfMarked($report);
|
||||
|
||||
$merged = require $report;
|
||||
assert($merged instanceof CodeCoverage);
|
||||
|
||||
return $merged;
|
||||
} finally {
|
||||
$container->add(State::class, $previous);
|
||||
|
||||
if (is_file($report)) {
|
||||
unlink($report);
|
||||
}
|
||||
|
||||
foreach (glob($directory.DIRECTORY_SEPARATOR.'*') ?: [] as $file) {
|
||||
@unlink($file);
|
||||
}
|
||||
|
||||
@rmdir($directory);
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ test('parallel', function () use ($run): void {
|
||||
$output = $run('--exclude-group=integration');
|
||||
$output = implode("\n", array_slice(explode("\n", (string) $output), -10));
|
||||
|
||||
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 28 skipped, 1607 passed (3534 assertions)';
|
||||
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 29 skipped, 1607 passed (3534 assertions)';
|
||||
|
||||
expect($output)
|
||||
->toContain("Tests: {$expected}")
|
||||
|
||||
Reference in New Issue
Block a user