Skip to content

Commit c77aaa6

Browse files
authored
Merge pull request #47 from anne-gaelle123inkt/master
add the support of multi coverage file for the baseline command + rai…
2 parents c57a48a + e847a93 commit c77aaa6

File tree

14 files changed

+98
-54
lines changed

14 files changed

+98
-54
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ Note: if no `--reportGitlab`, `--reportCheckstyle` or `--reportText` is set, it
107107
The third required argument and `--report` has been removed, and should be replaced by:
108108
`--reportGitlab=<file>`, `--reportCheckstyle=<file>` or `--reportText=<file>`
109109

110+
# Migrating from 2 to 3
111+
The baseline now also supports multiple coverage files, as the inspect command.
112+
An error is also raised if the baseline custom entry doesn't match the coverage.
113+
110114
## About us
111115

112116
At 123inkt (Part of Digital Revolution B.V.), every day more than 50 development professionals are working on improving our internal ERP

src/Command/BaselineCommand.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use RuntimeException;
1313
use SplFileInfo;
1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Input\InputArgument;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Input\InputOption;
1718
use Symfony\Component\Console\Output\OutputInterface;
@@ -25,26 +26,34 @@ protected function configure(): void
2526
{
2627
$this->setName("baseline")
2728
->setDescription("Generate phpfci.xml based on a given coverage.xml")
28-
->addArgument('coverage', InputOption::VALUE_REQUIRED, 'Path to phpunit\'s coverage.xml')
29-
->addArgument('config', InputOption::VALUE_REQUIRED, 'Path to write the configuration file')
29+
->addArgument('coverage', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Path to phpunit\'s coverage.xml')
30+
->addOption('config', '', InputOption::VALUE_REQUIRED, 'Path to write the configuration file')
3031
->addOption('threshold', '', InputOption::VALUE_REQUIRED, 'Minimum coverage threshold, defaults to 100', 100)
3132
->addOption('baseDir', '', InputOption::VALUE_REQUIRED, 'Base directory from where to determine the relative config paths');
3233
}
3334

3435
protected function execute(InputInterface $input, OutputInterface $output): int
3536
{
36-
$configArgument = $input->getArgument('config');
37+
$configArgument = $input->getOption('config');
3738
if (is_array($configArgument)) {
3839
if (count($configArgument) === 0) {
3940
throw new RuntimeException('Missing config argument');
4041
}
4142
$configArgument = reset($configArgument);
4243
}
4344

44-
$outputPath = new SplFileInfo((string)$configArgument);
45-
$baseDir = $input->getOption('baseDir') ?? $outputPath->getPath();
46-
$threshold = $input->getOption('threshold');
47-
$coverageFilePath = FileUtil::getExistingFile($input->getArgument('coverage'));
45+
$outputPath = new SplFileInfo((string)$configArgument);
46+
$baseDir = $input->getOption('baseDir') ?? $outputPath->getPath();
47+
$threshold = $input->getOption('threshold');
48+
$coveragesFilepath = [];
49+
$coverageArgument = $input->getArgument('coverage');
50+
if (is_array($coverageArgument) === false) {
51+
$output->writeln('Coverage argument should be an array');
52+
return Command::FAILURE;
53+
}
54+
foreach ($coverageArgument as $coverageFilepath) {
55+
$coveragesFilepath[] = FileUtil::getExistingFile($coverageFilepath);
56+
}
4857

4958
if (is_string($baseDir) === false) {
5059
$output->writeln("--baseDir argument is not valid. Expecting string argument");
@@ -59,8 +68,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5968
}
6069

6170
// default to 100% coverage
62-
$config = new InspectionConfig($baseDir, (int)$threshold, false);
63-
$metrics = MetricsFactory::getFileMetrics(DOMDocumentFactory::getDOMDocument($coverageFilePath));
71+
$config = new InspectionConfig($baseDir, (int)$threshold, false);
72+
$domDocuments = [];
73+
foreach ($coveragesFilepath as $coverageFilepath) {
74+
$domDocuments[] = DOMDocumentFactory::getDOMDocument($coverageFilepath);
75+
}
76+
$metrics = MetricsFactory::getFilesMetrics($domDocuments);
6477

6578
// analyzer
6679
$failures = (new MetricsAnalyzer($metrics, $config))->analyze();

src/Lib/Metrics/Inspection/CustomCoverageAboveGlobalInspection.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ public function inspect(?PathInspectionConfig $fileConfig, FileMetric $metric):
2121
}
2222

2323
$globalCoverage = $this->config->getMinimumCoverage();
24-
$customCoverage = $fileConfig->getMinimumCoverage();
2524

26-
// custom coverage is lower than global coverage, and file is above global coverage
27-
if ($customCoverage <= $globalCoverage && $metric->getCoverage() >= $globalCoverage) {
25+
if ($metric->getCoverage() >= $globalCoverage) {
2826
return new Failure($metric, $fileConfig->getMinimumCoverage(), Failure::UNNECESSARY_CUSTOM_COVERAGE);
2927
}
3028

src/Lib/Metrics/Inspection/BelowCustomCoverageInspection.php renamed to src/Lib/Metrics/Inspection/DifferentCustomCoverageInspection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010
/**
1111
* File coverage is below custom coverage
1212
*/
13-
class BelowCustomCoverageInspection extends AbstractInspection
13+
class DifferentCustomCoverageInspection extends AbstractInspection
1414
{
1515
public function inspect(?PathInspectionConfig $fileConfig, FileMetric $metric): ?Failure
1616
{
17-
if ($fileConfig !== null && $metric->getCoverage() < $fileConfig->getMinimumCoverage()) {
17+
if ($fileConfig !== null && (int)floor($metric->getCoverage()) < $fileConfig->getMinimumCoverage()) {
1818
return new Failure($metric, $fileConfig->getMinimumCoverage(), Failure::CUSTOM_COVERAGE_TOO_LOW);
1919
}
2020

21+
if ($fileConfig !== null && (int)floor($metric->getCoverage()) > $fileConfig->getMinimumCoverage()) {
22+
return new Failure($metric, $fileConfig->getMinimumCoverage(), Failure::CUSTOM_COVERAGE_TOO_HIGH);
23+
}
24+
2125
return null;
2226
}
2327
}

src/Lib/Metrics/MetricsAnalyzer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace DigitalRevolution\CodeCoverageInspection\Lib\Metrics;
55

66
use DigitalRevolution\CodeCoverageInspection\Lib\Metrics\Inspection\AbstractInspection;
7-
use DigitalRevolution\CodeCoverageInspection\Lib\Metrics\Inspection\BelowCustomCoverageInspection;
7+
use DigitalRevolution\CodeCoverageInspection\Lib\Metrics\Inspection\DifferentCustomCoverageInspection;
88
use DigitalRevolution\CodeCoverageInspection\Lib\Metrics\Inspection\BelowGlobalCoverageInspection;
99
use DigitalRevolution\CodeCoverageInspection\Lib\Metrics\Inspection\CustomCoverageAboveGlobalInspection;
1010
use DigitalRevolution\CodeCoverageInspection\Lib\Metrics\Inspection\UncoveredMethodsInspection;
@@ -31,10 +31,10 @@ public function __construct(array $metrics, InspectionConfig $config)
3131
$this->config = $config;
3232

3333
$this->inspections = [
34-
new BelowCustomCoverageInspection($config),
34+
new CustomCoverageAboveGlobalInspection($config),
35+
new DifferentCustomCoverageInspection($config),
3536
new BelowGlobalCoverageInspection($config),
36-
new UncoveredMethodsInspection($config),
37-
new CustomCoverageAboveGlobalInspection($config)
37+
new UncoveredMethodsInspection($config)
3838
];
3939
}
4040

src/Model/Metric/Failure.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Failure
99
public const CUSTOM_COVERAGE_TOO_LOW = 2;
1010
public const UNNECESSARY_CUSTOM_COVERAGE = 3;
1111
public const MISSING_METHOD_COVERAGE = 4;
12+
public const CUSTOM_COVERAGE_TOO_HIGH = 5;
1213

1314
private FileMetric $metric;
1415
private int $minimumCoverage;

src/Renderer/RendererHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public static function renderReason(InspectionConfig $config, Failure $failure):
1919
case Failure::CUSTOM_COVERAGE_TOO_LOW:
2020
$message = "Custom file coverage is configured at %s%%. Current coverage is at %s%%. Improve coverage for this class.";
2121

22+
return sprintf($message, (string)$failure->getMinimumCoverage(), (string)$failure->getMetric()->getCoverage());
23+
case Failure::CUSTOM_COVERAGE_TOO_HIGH:
24+
$message = "Custom file coverage is configured at %s%%. Current coverage is at %s%%. Edit the phpfci baseline for this class.";
25+
2226
return sprintf($message, (string)$failure->getMinimumCoverage(), (string)$failure->getMetric()->getCoverage());
2327
case Failure::MISSING_METHOD_COVERAGE:
2428
$message = "File coverage is above %s%%, but method(s) `%s` has/have no coverage at all.";

tests/Functional/Command/BaselineCommand/BaselineCommandTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ class BaselineCommandTest extends TestCase
2020
/** @var vfsStreamDirectory */
2121
private $fileSystem;
2222

23-
protected function setUp(): void
24-
{
25-
parent::setUp();
26-
$this->fileSystem = vfsStream::setup('output');
27-
}
28-
2923
/**
3024
* @throws Exception
3125
*/
@@ -39,7 +33,7 @@ public function testBaselineCommand(): void
3933

4034
// prepare command
4135
$command = new BaselineCommand();
42-
$input = new ArgvInput(['phpfci', '--baseDir', $baseDir, $coveragePath, $output]);
36+
$input = new ArgvInput(['phpfci', $coveragePath, '--baseDir', $baseDir, '--config', $output]);
4337
$output = new BufferedOutput();
4438

4539
// run test case
@@ -53,4 +47,10 @@ public function testBaselineCommand(): void
5347

5448
static::assertSame($expected, $result);
5549
}
50+
51+
protected function setUp(): void
52+
{
53+
parent::setUp();
54+
$this->fileSystem = vfsStream::setup('output');
55+
}
5656
}

tests/Functional/Command/InspectCommand/Data/checkstyle.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
<error line="1" column="0" severity="error" message="Project per file coverage is configured at 80%. Current coverage is at 70%. Improve coverage for this class." source="phpunit-file-coverage-inspection"/>
55
</file>
66
<file name="/home/workspace/test/directory/standard-below-threshold.php">
7-
<error line="1" column="0" severity="error" message="Custom file coverage is configured at 90%. Current coverage is at 80%. Improve coverage for this class." source="phpunit-file-coverage-inspection"/>
7+
<error line="1" column="0" severity="error" message="Custom file coverage is configured at 90%. Current coverage is at 60%. Improve coverage for this class." source="phpunit-file-coverage-inspection"/>
8+
</file>
9+
<file name="/home/workspace/test/directory/standard-above-threshold.php">
10+
<error line="1" column="0" severity="error" message="A custom file coverage is configured at 90%, but the current file coverage 90% exceeds the project coverage 80%. Remove `/home/workspace/test/directory/standard-above-threshold.php` from phpfci.xml custom-coverage rules." source="phpunit-file-coverage-inspection"/>
811
</file>
912
<file name="/home/workspace/test/case/below-threshold.php">
1013
<error line="1" column="0" severity="error" message="Custom file coverage is configured at 55%. Current coverage is at 50%. Improve coverage for this class." source="phpunit-file-coverage-inspection"/>

tests/Functional/Command/InspectCommand/Data/coverage.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@
6262
conditionals="0"
6363
coveredconditionals="0"
6464
statements="10"
65-
coveredstatements="8"
65+
coveredstatements="6"
6666
elements="0"
6767
coveredelements="0"/>
6868
<line num="1" type="method" name="methodName" count="1"/>
69-
<line num="2" type="stmt" count="1"/>
69+
<line num="2" type="stmt" count="0"/>
7070
<line num="3" type="stmt" count="1"/>
71-
<line num="4" type="stmt" count="1"/>
71+
<line num="4" type="stmt" count="0"/>
7272
<line num="5" type="stmt" count="1"/>
7373
<line num="6" type="stmt" count="1"/>
7474
<line num="7" type="stmt" count="1"/>

0 commit comments

Comments
 (0)