Skip to content

Commit

Permalink
Merge pull request #314 from carlos-granados/hide-full-coverage
Browse files Browse the repository at this point in the history
Add an option that allows you to hide all files where code coverage is full in the code coverage output
  • Loading branch information
nunomaduro authored Jan 23, 2025
2 parents 940a4f4 + c144384 commit 2bac222
Show file tree
Hide file tree
Showing 4 changed files with 26 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}
{--quiet-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('quiet-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 != '--quiet-coverage'
&& $option != '--compact'
&& $option != '--profile'
&& $option != '--ansi'
Expand Down Expand Up @@ -260,6 +263,7 @@ protected function paratestArguments($options)
&& $option != '--ansi'
&& $option != '--no-ansi'
&& ! Str::startsWith($option, '--min')
&& ! Str::startsWith($option, '--quiet-coverage')
&& ! Str::startsWith($option, '-p')
&& ! Str::startsWith($option, '--parallel')
&& ! Str::startsWith($option, '--recreate-databases')
Expand Down
6 changes: 5 additions & 1 deletion src/Coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ 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
{
if (! file_exists($reportPath = self::getPath())) {
if (self::usingXdebug()) {
Expand Down Expand Up @@ -110,6 +110,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}
{--quiet-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 test_coverage(): void
{
$output = $this->runTests(['./tests/LaravelApp/artisan', 'test', '--coverage', '--group', 'coverage']);
$this->assertStringContainsString('Code Coverage:', $output);

Check failure on line 18 in tests/Unit/Adapters/ArtisanTestCommandTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - ubuntu-latest - Pest 3.7.2 - prefer-lowest

Failed asserting that '\n

Check failure on line 18 in tests/Unit/Adapters/ArtisanTestCommandTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - ubuntu-latest - Pest 3.7.2 - prefer-stable

Failed asserting that '\n

Check failure on line 18 in tests/Unit/Adapters/ArtisanTestCommandTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - ubuntu-latest - Pest 3.7.2 - prefer-lowest

Failed asserting that '\n

Check failure on line 18 in tests/Unit/Adapters/ArtisanTestCommandTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - ubuntu-latest - Pest 3.7.2 - prefer-stable

Failed asserting that '\n

Check failure on line 18 in tests/Unit/Adapters/ArtisanTestCommandTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 - ubuntu-latest - Pest 3.7.2 - prefer-stable

Failed asserting that '\n
$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 test_min_coverage(): void
*/
}

#[Test]
public function test_hide_full_coverage(): void
{
$output = $this->runTests(['./tests/LaravelApp/artisan', 'test', '--coverage', '--quiet-coverage', '--group', 'coverage'], 0);
$this->assertStringContainsString('Code Coverage (files with full coverage not printed):', $output);

Check failure on line 60 in tests/Unit/Adapters/ArtisanTestCommandTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - ubuntu-latest - Pest 3.7.2 - prefer-lowest

Failed asserting that '\n

Check failure on line 60 in tests/Unit/Adapters/ArtisanTestCommandTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - ubuntu-latest - Pest 3.7.2 - prefer-stable

Failed asserting that '\n

Check failure on line 60 in tests/Unit/Adapters/ArtisanTestCommandTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - ubuntu-latest - Pest 3.7.2 - prefer-lowest

Failed asserting that '\n

Check failure on line 60 in tests/Unit/Adapters/ArtisanTestCommandTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - ubuntu-latest - Pest 3.7.2 - prefer-stable

Failed asserting that '\n

Check failure on line 60 in tests/Unit/Adapters/ArtisanTestCommandTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 - ubuntu-latest - Pest 3.7.2 - prefer-stable

Failed asserting that '\n
$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 test_ansi(): void
{
Expand Down

0 comments on commit 2bac222

Please sign in to comment.