Skip to content

Commit 9104d82

Browse files
committed
Merge branch 'main' of github.com:permafrost-dev/coverage-check into main
2 parents c84e8a5 + 920f283 commit 9104d82

File tree

6 files changed

+33
-8
lines changed

6 files changed

+33
-8
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ If you don't specify the `--require/-r` flag, only the percentage of code covera
3939
./vendor/bin/coverage-check clover.xml --require=50
4040
./vendor/bin/coverage-check clover.xml -r 80.5
4141
./vendor/bin/coverage-check clover.xml -m statement -r 75
42+
./vendor/bin/coverage-check clover.xml --precision=1
4243
```
4344

4445
## Available Options
@@ -47,6 +48,7 @@ If you don't specify the `--require/-r` flag, only the percentage of code covera
4748
| --- | --- |
4849
| `--coverage-only` or `-C` | Only display the code coverage value |
4950
| `--metric` or `-m` `<name>` | Use the specified metric field for calculating coverage. Valid values are `element` _(default)_, `method`, or `statement` |
51+
| `--precision` or `-p` `<value>` | Use the specified precision when calculating the code coverage percentage, where `<value>` is an integer _(default: 4)_ |
5052
| `--require` or `-r` `<value>` | Enforce a minimum code coverage value, where `<value>` is an integer or decimal value |
5153

5254
## Metric fields
@@ -104,7 +106,7 @@ jobs:
104106
run: ./vendor/bin/phpunit --coverage-clover clover.xml
105107

106108
- name: Enforce 75% code coverage
107-
run: ./vendor/bin/coverage-check clover.xml --require=75
109+
run: ./vendor/bin/coverage-check clover.xml --require=75 --precision=2
108110
```
109111
110112
## Testing

src/Commands/CheckCoverageCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected function configure(): void
2929
->addOption('require', 'r', InputOption::VALUE_REQUIRED, 'Require a minimum code coverage percentage', null)
3030
->addOption('metric', 'm', InputOption::VALUE_REQUIRED, 'Use a specific metric field (element, statement, or method)')
3131
->addOption('coverage-only', 'C', InputOption::VALUE_NONE, 'Display only the code coverage percentage')
32+
->addOption('precision', 'p', InputOption::VALUE_REQUIRED, 'Precision to use when rounding the code coverage percentage', 4)
3233
->setDescription('Checks a clover-format coverage file for a minimum coverage percentage and optionally enforces it.');
3334
}
3435

src/Configuration/Configuration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ class Configuration
1919
/** @var string */
2020
public $metricField;
2121

22-
public function __construct(string $filename, bool $requireMode, $required, bool $displayCoverageOnly, string $metricField)
22+
/** @var int */
23+
public $precision;
24+
25+
public function __construct(string $filename, bool $requireMode, $required, bool $displayCoverageOnly, string $metricField, int $precision)
2326
{
2427
$this->filename = $filename;
2528
$this->required = $required;
2629
$this->requireMode = $requireMode;
2730
$this->displayCoverageOnly = $displayCoverageOnly;
2831
$this->metricField = rtrim(strtolower($metricField), 's');
32+
$this->precision = $precision;
2933
}
3034

3135
public function validate(): void

src/Configuration/ConfigurationFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ public static function create(InputInterface $input): Configuration
1212
$requireMode = $input->hasOption('require') && $input->getOption('require') !== null;
1313
$percentage = $requireMode ? (float)$input->getOption('require') : null;
1414
$displayCoverageOnly = $input->hasOption('coverage-only') && $input->getOption('coverage-only') === true;
15+
$precision = $input->hasOption('precision') ? (int)$input->getOption('precision') : 4;
1516
$metricField = 'element';
1617

1718
if ($input->hasOption('metric') && $input->getOption('metric') !== null) {
1819
$metricField = $input->getOption('metric');
1920
}
2021

21-
return new Configuration($filename, $requireMode, $percentage, $displayCoverageOnly, $metricField);
22+
return new Configuration($filename, $requireMode, $percentage, $displayCoverageOnly, $metricField, $precision);
2223
}
2324
}

src/CoverageChecker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function getCoveragePercent(): float
3939
$totalElements = $this->getMetricFieldSum($metrics, $totalName);
4040
$checkedElements = $this->getMetricFieldSum($metrics, $coveredName);
4141

42-
return round(($checkedElements / $totalElements) * 100, 4);
42+
return round(($checkedElements / $totalElements) * 100, $this->config->precision);
4343
}
4444

4545
public function check(float $minPercentage): bool

tests/CoverageCheckerTest.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,33 @@ class CoverageCheckerTest extends TestCase
1111
/** @test */
1212
public function it_gets_the_coverage_percentage()
1313
{
14-
$config = new Configuration(__DIR__ . '/data/coverage-clover.xml', false, false, false, 'element');
14+
$config = new Configuration(__DIR__ . '/data/coverage-clover.xml', false, false, false, 'element', 4);
1515
$checker = new CoverageChecker(__DIR__ . '/data/coverage-clover.xml', $config);
1616

1717
$this->assertEquals(89.8765, round($checker->getCoveragePercent(), 4));
1818
}
1919

20+
/** @test */
21+
public function it_gets_the_coverage_percentage_using_the_specified_precision()
22+
{
23+
$map = [
24+
0 => 90.0,
25+
1 => 89.9,
26+
2 => 89.88,
27+
];
28+
29+
foreach ($map as $precision => $value) {
30+
$config = new Configuration(__DIR__ . '/data/coverage-clover.xml', false, false, false, 'element', $precision);
31+
$checker = new CoverageChecker(__DIR__ . '/data/coverage-clover.xml', $config);
32+
33+
$this->assertEquals($value, $checker->getCoveragePercent());
34+
}
35+
}
36+
2037
/** @test */
2138
public function it_checks_for_a_minimum_coverage_percentage()
2239
{
23-
$config = new Configuration(__DIR__ . '/data/coverage-clover.xml', false, false, false, 'element');
40+
$config = new Configuration(__DIR__ . '/data/coverage-clover.xml', false, false, false, 'element', 4);
2441
$checker = new CoverageChecker(__DIR__ . '/data/coverage-clover.xml', $config);
2542

2643
$this->assertTrue($checker->check(75));
@@ -30,7 +47,7 @@ public function it_checks_for_a_minimum_coverage_percentage()
3047
/** @test */
3148
public function it_gets_the_coverage_percentage_for_the_statement_metric()
3249
{
33-
$config = new Configuration(__DIR__ . '/data/coverage-clover.xml', false, false, false, 'statement');
50+
$config = new Configuration(__DIR__ . '/data/coverage-clover.xml', false, false, false, 'statement', 4);
3451
$checker = new CoverageChecker(__DIR__ . '/data/coverage-clover.xml', $config);
3552

3653
$this->assertEquals(90.6279, round($checker->getCoveragePercent(), 4));
@@ -39,7 +56,7 @@ public function it_gets_the_coverage_percentage_for_the_statement_metric()
3956
/** @test */
4057
public function it_gets_the_coverage_percentage_for_the_method_metric()
4158
{
42-
$config = new Configuration(__DIR__ . '/data/coverage-clover.xml', false, false, false, 'method');
59+
$config = new Configuration(__DIR__ . '/data/coverage-clover.xml', false, false, false, 'method', 4);
4360
$checker = new CoverageChecker(__DIR__ . '/data/coverage-clover.xml', $config);
4461

4562
$this->assertEquals(87.4439, $checker->getCoveragePercent());

0 commit comments

Comments
 (0)