sebastianbergmann / phpunit

The PHP Unit Testing framework.
https://phpunit.de/
BSD 3-Clause "New" or "Revised" License
19.68k stars 2.2k forks source link

cli coverage output directory #5044

Closed bkdotcom closed 1 month ago

bkdotcom commented 2 years ago
Q A
PHPUnit version PHPUnit 9.5.24
PHP version 8.2.0-dev
Installation Method Composer

Summary

vendor/bin/phpunit --coverage-html someRelativeDir someRelativeDir should be relative to the current command line working directory

calling \chdir(); within the unit tests should not affect location specified via command line

Current behavior

calling chdir('somenewdir'); within unit tests effects where coverage is written


edit/note: It should be noted that in my case, the adverse chdir() was being called within my defined bootstrap file

sebastianbergmann commented 1 month ago

I cannot reproduce this.

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="src/Issue5044.php"
>
    <testsuites>
        <testsuite name="default">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

    <source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
        <include>
            <directory>src</directory>
        </include>
    </source>
</phpunit>

tests/Issue5044Test.php

<?php declare(strict_types=1);
namespace PHPUnit\TestFixture\Issue5044;

use PHPUnit\Framework\TestCase;

final class Issue5044Test extends TestCase
{
    public function testOne(): void
    {
        chdir('/tmp');

        $this->assertTrue((new Issue5044)->doSomething());
    }
}

src/Issue5044.php

<?php declare(strict_types=1);
namespace PHPUnit\TestFixture\Issue5044;

final class Issue5044
{
    public function doSomething(): true
    {
        return true;
    }
}
❯ echo $PWD
/home/sb/5044
❯ ll
total 68K
-rw-r--r--. 1 sb sb  66 Sep  8 17:58 composer.json
-rw-r--r--. 1 sb sb 59K Sep  8 17:58 composer.lock
-rw-r--r--. 1 sb sb 547 Sep  8 18:02 phpunit.xml
drwxr-xr-x. 1 sb sb  26 Sep  8 18:02 src
drwxr-xr-x. 1 sb sb  34 Sep  8 18:02 tests
drwxr-xr-x. 1 sb sb 130 Sep  8 17:58 vendor
❯ ./vendor/bin/phpunit --coverage-html coverage
PHPUnit 11.3.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.11 with PCOV 1.0.11
Configuration: /home/sb/5044/phpunit.xml

.                                                                   1 / 1 (100%)

Time: 00:00.015, Memory: 10.00 MB

OK (1 test, 1 assertion)

Generating code coverage report in HTML format ... done [00:00.005]
❯ ll
total 68K
-rw-r--r--. 1 sb sb  66 Sep  8 17:58 composer.json
-rw-r--r--. 1 sb sb 59K Sep  8 17:58 composer.lock
drwxr-xr-x. 1 sb sb 110 Sep  8 18:05 coverage
-rw-r--r--. 1 sb sb 547 Sep  8 18:02 phpunit.xml
drwxr-xr-x. 1 sb sb  26 Sep  8 18:02 src
drwxr-xr-x. 1 sb sb  34 Sep  8 18:02 tests
drwxr-xr-x. 1 sb sb 130 Sep  8 17:58 vendor