Skip to content

Commit

Permalink
Add an option that allows you to hide all files where code coverage i…
Browse files Browse the repository at this point in the history
…s full in the code coverage output
  • Loading branch information
carlos-granados committed Oct 12, 2024
1 parent efb38f7 commit 2467f85
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Adapters/Laravel/Commands/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class TestCommand extends Command
{--compact : Indicates whether the compact printer should be used}
{--coverage : Indicates whether code coverage information should be collected}
{--min= : Indicates the minimum threshold enforcement for code coverage}
{--hide-full-coverage : Do not report any files where code coverage is 100%}
{--p|parallel : Indicates if the tests should run in parallel}
{--profile : Lists top 10 slowest tests}
{--recreate-databases : Indicates if the test databases should be re-created}
Expand Down Expand Up @@ -130,7 +131,8 @@ public function handle()
$this->newLine();
}

$coverage = Coverage::report($this->output);
$hideFullCoverage = (bool) $this->option('hide-full-coverage');
$coverage = Coverage::report($this->output, $hideFullCoverage);

$exitCode = (int) ($coverage < $this->option('min'));

Expand Down Expand Up @@ -216,6 +218,7 @@ protected function phpunitArguments($options)
&& $option != '-q'
&& $option != '--quiet'
&& $option != '--coverage'
&& $option != '--hide-full-coverage'
&& $option != '--compact'
&& $option != '--profile'
&& $option != '--ansi'
Expand Down Expand Up @@ -256,6 +259,7 @@ protected function paratestArguments($options)
&& $option != '--ansi'
&& $option != '--no-ansi'
&& ! Str::startsWith($option, '--min')
&& ! Str::startsWith($option, '--hide-full-coverage')
&& ! Str::startsWith($option, '-p')
&& ! Str::startsWith($option, '--parallel')
&& ! Str::startsWith($option, '--recreate-databases')
Expand Down
14 changes: 13 additions & 1 deletion src/Coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,16 @@ public static function usingXdebug(): bool
* Reports the code coverage report to the
* console and returns the result in float.
*/
public static function report(OutputInterface $output): float
public static function report(OutputInterface $output, bool $hideFullCoverage): float
{
$filesExcluded = $hideFullCoverage ? ' (files with full coverage not printed)' : '';
renderUsing($output);
render(<<<HTML
<div class="mx-2">
<span class="mb-1 font-bold">Code Coverage{$filesExcluded}:</span>
</div>
HTML);

if (! file_exists($reportPath = self::getPath())) {
if (self::usingXdebug()) {
$output->writeln(
Expand Down Expand Up @@ -110,6 +118,10 @@ public static function report(OutputInterface $output): float
? '100.0'
: number_format($file->percentageOfExecutedLines()->asFloat(), 1, '.', '');

if ($percentage === '100.0' && $hideFullCoverage) {
continue;
}

$uncoveredLines = '';

$percentageOfExecutedLinesAsString = $file->percentageOfExecutedLines()->asString();
Expand Down
1 change: 1 addition & 0 deletions tests/LaravelApp/app/Console/Commands/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class TestCommand extends BaseTestCommand
{--compact : Indicates whether the compact printer should be used}
{--coverage : Indicates whether code coverage information should be collected}
{--min= : Indicates the minimum threshold enforcement for code coverage}
{--hide-full-coverage : Do not report any files where code coverage is 100%}
{--p|parallel : Indicates if the tests should run in parallel}
{--profile : Lists top 10 slowest tests}
{--recreate-databases : Indicates if the test databases should be re-created}
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/Adapters/ArtisanTestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ class ArtisanTestCommandTest extends TestCase
public function testCoverage(): void
{
$output = $this->runTests(['./tests/LaravelApp/artisan', 'test', '--coverage', '--group', 'coverage']);
$this->assertStringContainsString('Code Coverage:', $output);
$this->assertStringContainsString('Models/User', $output);
$this->assertStringContainsString('0.0', $output);
$this->assertStringContainsString('Http/Controllers/Controller', $output);
$this->assertStringContainsString('100.0', $output);
$this->assertStringContainsString('Total: ', $output);

/**
Expand Down Expand Up @@ -50,6 +53,18 @@ public function testMinCoverage(): void
*/
}

#[Test]
public function testHideFullCoverage(): void
{
$output = $this->runTests(['./tests/LaravelApp/artisan', 'test', '--coverage', '--hide-full-coverage', '--group', 'coverage'], 0);
$this->assertStringContainsString('Code Coverage (files with full coverage not printed):', $output);
$this->assertStringContainsString('Models/User', $output);
$this->assertStringContainsString('0.0', $output);
$this->assertStringNotContainsString('Http/Controllers/Controller', $output);
$this->assertStringNotContainsString('100.0', $output);
$this->assertStringContainsString('Total: ', $output);
}

#[Test]
public function testAnsi(): void
{
Expand Down

0 comments on commit 2467f85

Please sign in to comment.