Skip to content

Do not run tests when code coverage analysis is requested but code coverage data cannot be collected #6237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Runner/CodeCoverage.php
Original file line number Diff line number Diff line change
@@ -72,18 +72,18 @@ public static function instance(): self
return self::$instance;
}

public function init(Configuration $configuration, CodeCoverageFilterRegistry $codeCoverageFilterRegistry, bool $extensionRequiresCodeCoverageCollection): void
public function init(Configuration $configuration, CodeCoverageFilterRegistry $codeCoverageFilterRegistry, bool $extensionRequiresCodeCoverageCollection): CodeCoverageInitializationStatus
{
$codeCoverageFilterRegistry->init($configuration);

if (!$configuration->hasCoverageReport() && !$extensionRequiresCodeCoverageCollection) {
return;
return CodeCoverageInitializationStatus::NOT_REQUESTED;
}

$this->activate($codeCoverageFilterRegistry->get(), $configuration->pathCoverage());

if (!$this->isActive()) {
return;
return CodeCoverageInitializationStatus::FAILED;
}

if ($configuration->hasCoverageCacheDirectory()) {
@@ -154,6 +154,8 @@ public function init(Configuration $configuration, CodeCoverageFilterRegistry $c
$statistics['cacheMisses'],
);
}

return CodeCoverageInitializationStatus::SUCCESSFULLY;
}

/**
17 changes: 17 additions & 0 deletions src/Runner/CodeCoverageInitializationStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Runner;

enum CodeCoverageInitializationStatus
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
enum CodeCoverageInitializationStatus
/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
enum CodeCoverageInitializationStatus

?

{
case NOT_REQUESTED;
case SUCCESSFULLY;
case FAILED;
}
20 changes: 13 additions & 7 deletions src/TextUI/Application.php
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@
use PHPUnit\Runner\Baseline\Reader;
use PHPUnit\Runner\Baseline\Writer;
use PHPUnit\Runner\CodeCoverage;
use PHPUnit\Runner\CodeCoverageInitializationStatus;
use PHPUnit\Runner\DeprecationCollector\Facade as DeprecationCollector;
use PHPUnit\Runner\DirectoryDoesNotExistException;
use PHPUnit\Runner\ErrorHandler;
@@ -201,7 +202,7 @@ public function run(array $argv): int
$this->execute(new ShowHelpCommand(Result::FAILURE));
}

CodeCoverage::instance()->init(
$coverageInitializationStatus = CodeCoverage::instance()->init(
$configuration,
CodeCoverageFilterRegistry::instance(),
$extensionRequiresCodeCoverageCollection,
@@ -220,13 +221,18 @@ public function run(array $argv): int
$timer = new Timer;
$timer->start();

$runner = new TestRunner;
if (
$coverageInitializationStatus === CodeCoverageInitializationStatus::NOT_REQUESTED ||
$coverageInitializationStatus === CodeCoverageInitializationStatus::SUCCESSFULLY
) {
$runner = new TestRunner;

$runner->run(
$configuration,
$resultCache,
$testSuite,
);
$runner->run(
$configuration,
$resultCache,
$testSuite,
);
}

$duration = $timer->stop();

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Don't run tests when coverage driver is not loaded
--SKIPIF--
<?php declare(strict_types=1);
if (extension_loaded('xdebug') || extension_loaded('pcov')) {
print 'skip: No debug driver should be loaded.';
}
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--coverage-html';
$_SERVER['argv'][] = 'my_coverage_folder';

require_once __DIR__ . '/../../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s
Configuration: %s

There was 1 PHPUnit test runner warning:

1) No code coverage driver available

No tests executed!

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Don't run tests when wrong xdebug mode is set
--SKIPIF--
<?php declare(strict_types=1);
if (!extension_loaded('xdebug')) {
print 'skip: Extension xdebug must be loaded.';
}
--ENV--
XDEBUG_MODE=debug
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--coverage-html';
$_SERVER['argv'][] = 'my_coverage_folder';

require_once __DIR__ . '/../../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s
Configuration: %s

There was 1 PHPUnit test runner warning:

1) XDEBUG_MODE=coverage (environment variable) or xdebug.mode=coverage (PHP configuration setting) has to be set

No tests executed!

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Don't run tests when coverage driver is not loaded
--SKIPIF--
<?php declare(strict_types=1);
if (extension_loaded('xdebug') || extension_loaded('pcov')) {
print 'skip: No debug driver should be loaded.';
}
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = \realpath(__DIR__ . '/../../_files/coverage/coverage-no-tests-when-missing-coverage-driver.phpt');

require_once __DIR__ . '/../../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s

. 1 / 1 (100%)

Time: %s, Memory: %s

OK (1 test, 1 assertion)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Don't run tests when wrong xdebug mode is set
--SKIPIF--
<?php declare(strict_types=1);
if (!extension_loaded('xdebug')) {
print 'skip: Extension xdebug must be loaded.';
}
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = \realpath(__DIR__ . '/../../_files/coverage/coverage-no-tests-when-wrong-xdebug-mode.phpt');

require_once __DIR__ . '/../../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s

. 1 / 1 (100%)

Time: %s, Memory: %s

OK (1 test, 1 assertion)