Skip to content

Commit 2010ff3

Browse files
Closes #115
1 parent d033ec0 commit 2010ff3

File tree

6 files changed

+27
-7
lines changed

6 files changed

+27
-7
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes in PHPCOV are documented in this file using the [Keep a CHAN
88

99
* Added support for PHPUnit 13.1 and phpunit/php-code-coverage 14.0
1010
* Added `--openclover` CLI option for merging into OpenClover XML format
11+
* [#115](https://github.com/sebastianbergmann/phpcov/issues/115): Support for merging of serialized code coverage data files generated with different base paths
1112

1213
### Removed
1314

src/PatchCoverageCalculator.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
use const DIRECTORY_SEPARATOR;
1313
use function is_array;
14+
use function str_starts_with;
15+
use function strlen;
1416
use function substr;
1517
use SebastianBergmann\CodeCoverage\Data\ProcessedCodeCoverageData;
1618
use SebastianBergmann\Diff\Diff;
@@ -27,7 +29,7 @@ final class PatchCoverageCalculator
2729
*
2830
* @return array{numChangedLinesThatAreExecutable: non-negative-int, numChangedLinesThatWereExecuted: non-negative-int, changedLinesThatWereNotExecuted: array<non-empty-string, non-empty-list<positive-int>>}
2931
*/
30-
public function calculate(array $lineCoverage, array $patch, string $pathPrefix): array
32+
public function calculate(array $lineCoverage, array $patch, string $basePath, string $pathPrefix): array
3133
{
3234
$result = [
3335
'numChangedLinesThatAreExecutable' => 0,
@@ -61,7 +63,13 @@ public function calculate(array $lineCoverage, array $patch, string $pathPrefix)
6163
}
6264

6365
foreach ($changes as $file => $lines) {
64-
$key = $pathPrefix . $file;
66+
$fullPath = $pathPrefix . $file;
67+
68+
if ($basePath !== '' && str_starts_with($fullPath, $basePath . DIRECTORY_SEPARATOR)) {
69+
$key = substr($fullPath, strlen($basePath) + 1);
70+
} else {
71+
$key = $fullPath;
72+
}
6573

6674
foreach ($lines as $line) {
6775
if (isset($lineCoverage[$key][$line]) &&

src/cli/MergeCommand.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace SebastianBergmann\PHPCOV;
1111

12+
use const DIRECTORY_SEPARATOR;
1213
use const PHP_EOL;
1314
use function file_put_contents;
1415
use function is_dir;
@@ -94,17 +95,17 @@ public function run(Arguments $arguments): int
9495
print 'done' . PHP_EOL;
9596
}
9697

97-
$this->handleReports($merged['codeCoverage'], $merged['testResults'], $arguments);
98+
$this->handleReports($merged['codeCoverage'], $merged['testResults'], $merged['basePath'], $arguments);
9899

99100
return 0;
100101
}
101102

102103
/**
103104
* @param array<string, TestType> $testResults
104105
*/
105-
private function handleReports(ProcessedCodeCoverageData $codeCoverage, array $testResults, Arguments $arguments): void
106+
private function handleReports(ProcessedCodeCoverageData $codeCoverage, array $testResults, string $basePath, Arguments $arguments): void
106107
{
107-
$report = $this->buildReport($codeCoverage, $testResults);
108+
$report = $this->buildReport($codeCoverage, $testResults, $basePath);
108109

109110
if ($arguments->clover() !== null) {
110111
print 'Generating code coverage report in Clover XML format ... ';
@@ -187,8 +188,16 @@ private function handleReports(ProcessedCodeCoverageData $codeCoverage, array $t
187188
/**
188189
* @param array<string, TestType> $testResults
189190
*/
190-
private function buildReport(ProcessedCodeCoverageData $codeCoverage, array $testResults): Directory
191+
private function buildReport(ProcessedCodeCoverageData $codeCoverage, array $testResults, string $basePath): Directory
191192
{
193+
if ($basePath !== '') {
194+
$codeCoverage = clone $codeCoverage;
195+
196+
foreach ($codeCoverage->coveredFiles() as $relPath) {
197+
$codeCoverage->renameFile($relPath, $basePath . DIRECTORY_SEPARATOR . $relPath);
198+
}
199+
}
200+
192201
return (new Builder(new FileAnalyser(new ParsingSourceAnalyser, false, false)))->build($codeCoverage, $testResults);
193202
}
194203
}

src/cli/PatchCoverageCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ public function run(Arguments $arguments): int
4949
}
5050

5151
try {
52+
$data = (new Unserializer)->unserialize($arguments->coverage());
5253
$patchCoverage = (new PatchCoverageCalculator)->calculate(
53-
(new Unserializer)->unserialize($arguments->coverage())['codeCoverage']->lineCoverage(),
54+
$data['codeCoverage']->lineCoverage(),
5455
(new DiffParser)->parse(file_get_contents($arguments->patch())),
56+
$data['basePath'],
5557
$pathPrefix,
5658
);
5759
} catch (FileCouldNotBeReadException|InvalidCoverageDataException|VersionMismatchException $e) {
-27 Bytes
Binary file not shown.
-27 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)