Skip to content

Commit

Permalink
Add a flag to enforce a minimum coverage threshold (closes #5).
Browse files Browse the repository at this point in the history
  • Loading branch information
d0x2f committed Oct 23, 2018
1 parent f5b392f commit 110cd4a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 6 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ $ docker pull d0x2f/clover-merge
usage: clover-merge [<options>] [<args>]
OPTIONS
--help, -? Display this help.
--mode, -m merge mode: additive, exclusive or inclusive (default)
--output, -o output file path
--enforce, -e Exit with failure if final coverage is below the given
threshold
--help, -? Display this help.
--mode, -m merge mode: additive, exclusive or inclusive (default)
--output, -o output file path
ARGUMENTS
paths input file paths
Expand Down
2 changes: 1 addition & 1 deletion clover-merge
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require_once 'vendor/autoload.php';

try {
$invocation = new \d0x2f\CloverMerge\Invocation($argv);
$invocation->execute();
exit ((int)(!$invocation->execute()));
} catch (\Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
63 changes: 63 additions & 0 deletions spec/Invocation.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,75 @@
allow('file_put_contents')->toBeCalled();
});
it('writes to the output file.', function () {
allow('printf')->toBeCalled();
expect('file_put_contents')->toBeCalled()->with(
__DIR__.'/../test_output/fixtures_result.xml'
)->once();
$this->closure();
});

it('Prints the coverage stats.', function () {
expect($this->closure)->toEcho("Files Discovered: 4\nFinal Coverage: 17/24 (70.83%)\n");
});
});
context('With an unsatisfied minimum coverage threshold.', function () {
beforeEach(function () {
allow(Utilities::class)->toReceive('::logWarning')->andReturn();
allow('file_put_contents')->toBeCalled();

$invocation = new Invocation([
'prog',
'-o', __DIR__.'/../test_output/fixtures_result.xml',
'-e', '90.0',
__DIR__.'/fixtures/file-without-package.xml'
]);
$this->closure = function () use ($invocation) {
return $invocation->execute();
};
});

it('Returns false.', function () {
allow('printf')->toBeCalled();
expect($this->closure())->toBe(false);
});

it('Prints the coverage stats.', function () {
expect($this->closure)->toEcho(
"Files Discovered: 1\n" .
"Final Coverage: 4/5 (80.00%)\n" .
"Coverage is below required threshold (80.00% < 90.00%).\n"
);
});
});
context('With a satisfied minimum coverage threshold.', function () {
beforeEach(function () {
allow(Utilities::class)->toReceive('::logWarning')->andReturn();
allow('file_put_contents')->toBeCalled();

$invocation = new Invocation([
'prog',
'-o', __DIR__.'/../test_output/fixtures_result.xml',
'-e', '50.0',
__DIR__.'/fixtures/file-without-package.xml'
]);
$this->closure = function () use ($invocation) {
return $invocation->execute();
};
});

it('Returns false.', function () {
allow('printf')->toBeCalled();
expect($this->closure())->toBe(true);
});

it('Prints the coverage stats.', function () {
expect($this->closure)->toEcho(
"Files Discovered: 1\n" .
"Final Coverage: 4/5 (80.00%)\n" .
"Coverage is above required threshold (80.00% > 50.00%).\n"
);
});
});
});
context('With mocked dependencies.', function () {
beforeEach(function () {
Expand Down
30 changes: 28 additions & 2 deletions src/Invocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class Invocation
*/
private $merge_mode;

/**
* Coverage success threshold.
*
* @var float
*/
private $coverage_threshold;

/**
* Parsed input XML documents.
*
Expand All @@ -41,6 +48,7 @@ public function __construct(array $argv)
$cli = Cli::create()
->opt('output:o', 'output file path', true)
->opt('mode:m', 'merge mode: additive, exclusive or inclusive (default)', false)
->opt('enforce:e', 'Exit with failure if final coverage is below the given threshold', false)
->arg('paths', 'input file paths', true);

try {
Expand All @@ -51,6 +59,7 @@ public function __construct(array $argv)

$this->output_path = $arguments->getOpt('output');
$this->merge_mode = $arguments->getOpt('mode', 'inclusive');
$this->coverage_threshold = floatval($arguments->getOpt('enforce', '0'));

if (!in_array($this->merge_mode, ['inclusive', 'exclusive', 'additive'])) {
throw new ArgumentException('Merge option must be one of: additive, exclusive or inclusive.');
Expand Down Expand Up @@ -85,10 +94,10 @@ public function __construct(array $argv)
/**
* Execute the invocation.
*
* @return void
* @return bool Success
* @throws FileException
*/
public function execute() : void
public function execute() : bool
{
$accumulator = new Accumulator($this->merge_mode);

Expand All @@ -113,5 +122,22 @@ public function execute() : void
}
printf("Files Discovered: %d\n", $files_discovered);
printf("Final Coverage: %d/%d (%.2f%%)\n", $covered_element_count, $element_count, $coverage_percentage);
$success = $coverage_percentage > $this->coverage_threshold;
if ($this->coverage_threshold > 0) {
if ($success) {
printf(
"Coverage is above required threshold (%.2f%% > %.2f%%).\n",
$coverage_percentage,
$this->coverage_threshold
);
} else {
printf(
"Coverage is below required threshold (%.2f%% < %.2f%%).\n",
$coverage_percentage,
$this->coverage_threshold
);
}
}
return $success;
}
}

0 comments on commit 110cd4a

Please sign in to comment.