dvdoug / behat-code-coverage

Behat Extension to generate code coverage reports for Behat tests
https://behat.cc
BSD 2-Clause "Simplified" License
54 stars 10 forks source link

Issue with PCOV and Xdebug: "No code coverage driver is available. XDEBUG_MODE=coverage or xdebug.mode=coverage has to be set" #25

Closed yakov255 closed 1 month ago

yakov255 commented 2 months ago

I have both Xdebug and Pcov installed And i want to use Pcov for coverage generation In my setup xdebug configured to not start with each request

xdebug.start_with_request=no

in my behat.yml i have disabled branch and path coverage by set:

    DVDoug\Behat\CodeCoverage\Extension:
      branchAndPathCoverage: false 

But behat-code-coverage always trying to init xdebug for coverage

this occurs because in source code in file \DVDoug\Behat\CodeCoverage\Extension config variable $branchPathConfig not passed to \DVDoug\Behat\CodeCoverage\Extension::initCodeCoverage method

if i do manual change that line - evething statrs works as excepcted: code coverage working with xdebug

Index: src/Extension.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/Extension.php b/src/Extension.php
--- a/src/Extension.php (revision dd02e423e3437f6cf8edaf69426ddf5805834767)
+++ b/src/Extension.php (date 1725270805642)
@@ -192,7 +192,7 @@

         $canCollectCodeCoverage = true;
         try {
-            $this->initCodeCoverage(new Filter(), $filterConfig, null, $cacheDir, $output);
+            $this->initCodeCoverage(new Filter(), $filterConfig, $branchPathConfig, $cacheDir, $output);

             $codeCoverageDefinition = $container->getDefinition(CodeCoverage::class);
             $filterDefinition = $container->getDefinition(Filter::class);

Can we fix this in code?

dvdoug commented 1 month ago

Hi @yakov255

I can't reproduce this, and at the moment I can't see how the scenario you've outlined could happen for a couple of reasons:

1) Although the param is not passed in on that line, that's only used as a dry-run to check that a driver can be found. The actual init that's used when running the suite is a couple of lines later on https://github.com/dvdoug/behat-code-coverage/blob/master/src/Extension.php#L200, where it is indeed set

2) If both pcov and Xdebug are available, pcov is the one that's used, not Xdebug. That's done over at https://github.com/sebastianbergmann/php-code-coverage/blob/main/src/Driver/Selector.php#L25 and it's pretty explicit

Are you able to create a repo with a full example that I can download and run to see this behaviour?

yakov255 commented 1 month ago

Hi @dvdoug!

I make repository for reproduce bug: behat-pcov-reproduce-bug

And while I was creating a repository for playback, I discovered that the problem is related to the version of the phpunit package and the phpunit/php-code-coverage package

I found that my project uses phpunit 9 and the problem is reproducible with this version

However, with phpunit versions 10 and 11, the code coverage report is generated correctly

This is because the package phpunit/php-code-coverage before version 10 used the exception \SebastianBergmann\CodeCoverage\Driver\Xdebug3NotEnabledException Which is not caught by extension

And since version 10, the package has been using \SebastianBergmann\CodeCoverage\Driver\XdebugNotEnabledException

As a result, I suggest:

  1. Respect the branchAndPathCoverage flag as I originally suggested (do not call `forLineAndPathCoverage' if flag is set)

  2. Add the exception Xdebug3NotEnabledException to the initCodeCoverage method This fix compatibility with phpunit 9

Or we can also go the other way: Add a minimal dependency on phpunit/php-code-coverage to version 10 (now "phpunit/php-code-coverage": "^9.2.16||^10.0",)

dvdoug commented 1 month ago

Thanks, I'll take a look