Skip to content
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

Coverage XML can't handle files with just newlines #859

Closed
rieschl opened this issue Jul 27, 2021 · 7 comments
Closed

Coverage XML can't handle files with just newlines #859

rieschl opened this issue Jul 27, 2021 · 7 comments

Comments

@rieschl
Copy link
Sponsor

rieschl commented Jul 27, 2021

Q A
php-code-coverage version 9.2.6
PHP version 7.4.21
Driver Xdebug
Xdebug version (if used) 3.0.4
Installation Method Composer
Usage Method PHPUnit
PHPUnit version (if used) 9.5.7

When trying to create an XML coverage report --coverage-xml with PHPUnit with some .phtml files files with just newlines in the coverage configuration directory, PHPUnit exits with error code 2 and the message:
Generating code coverage report in PHPUnit XML format ... Call to a member function getLine() on null.

When using another coverage report or removing the directory with the phtml files in the config file, or removing the empty file the error is gone.
I'm not quite sure how to debug this, as running PHPUnit with --verbose --debug doesn't print any additional lines.

This is the coverage section of the phpunit.xml file:

    <coverage>
        <include>
            <directory>src</directory>
            <directory suffix=".phtml">templates</directory>
        </include>
    </coverage>

Thanks!

Edit: edited the description to reflect the correct problem scenario

@sebastianbergmann
Copy link
Owner

Thank you for your report.

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

@rieschl
Copy link
Sponsor Author

rieschl commented Jul 27, 2021

Ah, thanks for making me try to reproduce that 🙂
The specific scenario of the library I'm testing got me off-track. It's really much more simple than described above:
The XML coverage driver can't handle empty files (files with just a newline - completely empty files are okay), regardless of the file extension.

Consider this example:
phpunit.xml:

<phpunit>
    <testsuites>
        <testsuite name="CoverageBug">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
    <coverage>
        <include>
            <directory>src</directory>
        </include>
    </coverage>
</phpunit>

tests/UselessTest.php:

<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

class UselessTest extends TestCase
{
    public function testNothing(): void
    {
        self::assertTrue(true);
    }
}

and a file with just a newline in /src/:
src/Empty.php


It doesn't matter if it's a LF, CRLF or CR or if there are multiple newlines in the file.

That will trigger the above error:

$ php -dxdebug.mode=coverage vendor/bin/phpunit --coverage-xml=cov.xml
PHPUnit 9.5.7 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 00:01.011, Memory: 8.00 MB

OK (1 test, 1 assertion)

Generating code coverage report in PHPUnit XML format ... Call to a member function getLine() on null

Does that help or should I push a git repo with above content?
Thanks!

@rieschl rieschl changed the title Coverage XML with .phtml files throws null-pointer exception Coverage XML can't handle files with just newlines Jul 27, 2021
@sebastianbergmann
Copy link
Owner

$ php -dxdebug.mode=coverage vendor/bin/phpunit --coverage-xml=cov.xml

Sidenote: the parameter for the --coverage-xml option is a directory, not a file. cov.xml looks to me like you are expecting it to be a file.

Does that help or should I push a git repo with above content?

That should help. @theseer Might be easier/quicker if you have a look.

@sebastianbergmann
Copy link
Owner

I have created a reproducing test case. Thank you, @rieschl, for investing the time to minimize your test case.

Of course, the test case currently fails:

1) SebastianBergmann\CodeCoverage\Report\Xml\XmlTest::testForFileWithSingleEmptyLine
Error: Call to a member function getLine() on null

/usr/local/src/php-code-coverage/vendor/theseer/tokenizer/src/Tokenizer.php:122
/usr/local/src/php-code-coverage/vendor/theseer/tokenizer/src/Tokenizer.php:89
/usr/local/src/php-code-coverage/src/Report/Xml/Source.php:33
/usr/local/src/php-code-coverage/src/Report/Xml/Facade.php:174
/usr/local/src/php-code-coverage/src/Report/Xml/Facade.php:126
/usr/local/src/php-code-coverage/src/Report/Xml/Facade.php:74
/usr/local/src/php-code-coverage/tests/tests/XmlTest.php:78
/usr/local/src/php-code-coverage/vendor/phpunit/phpunit/src/Framework/TestCase.php:1341
/usr/local/src/php-code-coverage/vendor/phpunit/phpunit/src/Framework/TestCase.php:808
/usr/local/src/php-code-coverage/vendor/phpunit/phpunit/src/Framework/TestRunner.php:115
/usr/local/src/php-code-coverage/vendor/phpunit/phpunit/src/Framework/TestCase.php:547
/usr/local/src/php-code-coverage/vendor/phpunit/phpunit/src/Framework/TestSuite.php:513
/usr/local/src/php-code-coverage/vendor/phpunit/phpunit/src/Framework/TestSuite.php:513
/usr/local/src/php-code-coverage/vendor/phpunit/phpunit/src/Framework/TestSuite.php:513
/usr/local/src/php-code-coverage/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:438
/usr/local/src/php-code-coverage/vendor/phpunit/phpunit/src/TextUI/Application.php:109
/usr/local/src/php-code-coverage/vendor/phpunit/phpunit/src/TextUI/Application.php:77

@sebastianbergmann
Copy link
Owner

This is not a bug in this library, but in one of its dependencies. I have opened theseer/tokenizer#13 to track this.

@theseer
Copy link
Contributor

theseer commented Jul 28, 2021

This is not a bug in this library, but in one of its dependencies. I have opened theseer/tokenizer#13 to track this.

Should be fixed with the just released version 1.2.1.

@rieschl
Copy link
Sponsor Author

rieschl commented Jul 28, 2021

Thanks for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants