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

PHPUNIT_RESULT_CACHE env variable is not considered anymore #3714

Open
reinholdfuereder opened this issue Jun 6, 2019 · 4 comments
Open
Assignees
Labels
feature/test-runner CLI test runner type/bug Something is broken

Comments

@reinholdfuereder
Copy link

Q A
PHPUnit version 8.1.6, but presumably since 8.1.4
PHP version 7.2
Installation Method PHAR

(Filed because of #3587 (comment))

Since the fix for #3587 (in fact see commit 795abc1) released in PHPUnit 8.1.4 it is no more possible to configure the '.phpunit.result.cache' location globally (via PHPUNIT_RESULT_CACHE env variable).

The reason why we would like to use such a global configuration possibility (instead of asking each developer to configure each of her PhpStorm PHPUnit runner options when executing single tests) is that we are using a PHPUnit PHAR file located on a (CIFS) mount and execution of single PHPUnit Tests via PhpStorm IDE (without configuration file) will always try to create the '.phpunit.result.cache' next to the shared PHPUnit PHAR file (on the share) that succeeds only the very first time and subsequently always leads to rather irritating warning logs like:

Testing started at 15:55 ...
[sftp://myuser@myhost.acme.com:22]:/usr/bin/php /mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar --bootstrap /home/data/myuser/unittests/unit/bootstrap.php --no-configuration TestFramework\\Util\\TraitUtilTest /home/data/myuser/unittests/unit/testframework/test/TestFramework/Util/TraitUtilTest.php --teamcity
PHPUnit 8.1.5 by Sebastian Bergmann and contributors.


PHP Warning:  file_put_contents(/mnt/data/irgendwas/components/phpunit/8.x/.phpunit.result.cache): failed to open stream: Permission denied in phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/Runner/DefaultTestResultCache.php on line 107
PHP Stack trace:
PHP   1. {main}() /mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar:0
PHP   2. PHPUnit\TextUI\Command::main() /mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar:619
PHP   3. PHPUnit\TextUI\Command->run() phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/TextUI/Command.php:163
PHP   4. PHPUnit\TextUI\TestRunner->doRun() phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/TextUI/Command.php:207
PHP   5. PHPUnit\Runner\ResultCacheExtension->executeAfterLastTest() phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/TextUI/TestRunner.php:617
PHP   6. PHPUnit\Runner\ResultCacheExtension->flush() phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/Runner/ResultCacheExtension.php:89
PHP   7. PHPUnit\Runner\DefaultTestResultCache->persist() phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/Runner/ResultCacheExtension.php:29

PHP   8. PHPUnit\Runner\DefaultTestResultCache->saveToFile() phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/Runner/DefaultTestResultCache.php:84
PHP   9. file_put_contents() phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/Runner/DefaultTestResultCache.php:107

Warning: file_put_contents(/mnt/data/irgendwas/components/phpunit/8.x/.phpunit.result.cache): failed to open stream: Permission denied in phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/Runner/DefaultTestResultCache.php on line 107

Call Stack:
    0.0672     868992   1. {main}() /mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar:0
    1.6985    9865904   2. PHPUnit\TextUI\Command::main() /mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar:619
    1.6985    9866016   3. PHPUnit\TextUI\Command->run() phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/TextUI/Command.php:163
    1.9119   49307264   4. PHPUnit\TextUI\TestRunner->doRun() phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/TextUI/Command.php:207
    1.9224   49411600   5. PHPUnit\Runner\ResultCacheExtension->executeAfterLastTest() phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/TextUI/TestRunner.php:617
    1.9224   49411600   6. PHPUnit\Runner\ResultCacheExtension->flush() phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/Runner/ResultCacheExtension.php:89
    1.9224   49411600   7. PHPUnit\Runner\DefaultTestResultCache->persist() phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/Runner/ResultCacheExtension.php:29
    1.9224   49411600   8. PHPUnit\Runner\DefaultTestResultCache->saveToFile() phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/Runner/DefaultTestResultCache.php:84
    1.9225   49415744   9. file_put_contents() phar:///mnt/data/irgendwas/components/phpunit/8.x/phpunit.phar/phpunit/Runner/DefaultTestResultCache.php:107



Time: 1.92 seconds, Memory: 56.25 MB

OK (13 tests, 13 assertions)

Process finished with exit code 0

In the course of analysing the problem we stumbled over $_ENV['PHPUNIT_RESULT_CACHE']:

public function __construct(?string $filepath = null)
{
if ($filepath !== null && \is_dir($filepath)) {
// cache path provided, use default cache filename in that location
$filepath = $filepath . \DIRECTORY_SEPARATOR . self::DEFAULT_RESULT_CACHE_FILENAME;
}
$this->cacheFilename = $filepath ?? $_ENV['PHPUNIT_RESULT_CACHE'] ?? self::DEFAULT_RESULT_CACHE_FILENAME;
}

... that we initially had hoped to configure (globally on the 'myhost.acme.com') to set to result cache file directory to user-specific non-shared value: however, since the DefaultTestResultCache c'tor is never called with null anymore, cf.:
if ($arguments['cacheResult']) {
if (!isset($arguments['cacheResultFile'])) {
if (isset($arguments['configuration']) && $arguments['configuration'] instanceof Configuration) {
$cacheLocation = $arguments['configuration']->getFilename();
} else {
$cacheLocation = $_SERVER['PHP_SELF'];
}
$arguments['cacheResultFile'] = null;
$cacheResultFile = \realpath($cacheLocation);
if ($cacheResultFile !== false) {
$arguments['cacheResultFile'] = \dirname($cacheResultFile);
}
}
$cache = new DefaultTestResultCache($arguments['cacheResultFile']);

... please note especially
$cacheLocation = $_SERVER['PHP_SELF'];

..., this environment variable cannot be used or is not considered anymore. Actually I think it is not usable anymore since commit 795abc1

(Please note that this problem has not occurred with the previously used PHPUnit version 8.1.2, but presumably there we will have had the problem of #3587, I guess...)

As far as I understand our devs using PhpStorm IDE would now need to always configure something to avoid these warnings, and the most convenient approach might be: (1) removing all existing PHPUnit runners and (2) configuring the PHPUnit runner template with the option to disable the PHPUnit result cache file via --do-not-cache-result (that is like PHPUNIT_RESULT_CACHE env variable also NOT yet documented https://phpunit.readthedocs.io/en/8.1/textui.html by the way) or alternatively to specify the location via --cache-result-file? Or is there a better way?

However, of course I would be very happy if global configuration via $_ENV['PHPUNIT_RESULT_CACHE'] is supported again, so that our devs would not need to do anything...

@epdenouden
Copy link
Contributor

@sebastianbergmann @reinholdfuereder I will be able to spend time on PHPUnit again, I've been rather absent due to change in job.

@reinholdfuereder
Copy link
Author

@epdenouden Thanks in advance and looking forward to this little fix!

By the way, below is a quick (naive?) attempt written in a non-PHP-editor (so it may be buggy), but it should show one possible approach:

  • Mind that I am not sure whether the PHPUNIT_RESULT_CACHE env variable should actually be "only" a directory (I used that in my approach below), or really a full path including filename?

TestRunner.php

        if ($arguments['cacheResult']) {
            $cacheLocation = $arguments['cacheResultFile'];
            if (!isset($cacheLocation)) {
                if (isset($arguments['configuration']) && $arguments['configuration'] instanceof Configuration) {
                    $configurationFilename = \realpath($arguments['configuration']->getFilename());
                    if ($configurationFilename !== false) {
                        $cacheLocation = \dirname($configurationFilename);
                    }
                }
            }
            $cache = new DefaultTestResultCache($cacheLocation); // Note (that shall not be committed): $cacheLocation can be null again
            $this->addExtension(new ResultCacheExtension($cache));
        }

DefaultTestResultCache.php

    public function __construct(?string $cacheDirname = null)
    {
        if ($cacheDirname == null || !\is_dir($cacheDirname)) {
            $cacheDirname = $_ENV['PHPUNIT_RESULT_CACHE'] ?? $_SERVER['PHP_SELF']; // TODO: or should it be $_SERVER['PHPUNIT_RESULT_CACHE']?
        }
        $this->cacheFilename = $cacheDirname . \DIRECTORY_SEPARATOR . self::DEFAULT_RESULT_CACHE_FILENAME;
    }

@reinholdfuereder
Copy link
Author

While this is IMHO still a bug, this is no more critical (for us), because PhpStorm with versions >= 2019.2.5 (?) implemented the workaround of always adding "--cache-result-file"...

@sebastianbergmann sebastianbergmann added feature/test-runner CLI test runner type/bug Something is broken labels Feb 12, 2020
@Chi-teck
Copy link

Chi-teck commented Mar 7, 2020

That does not just happen in PhpStorm. I experience the same when run tests with sudo -uww-data. And that causes a permission problem. Setting $_ENV['PHPUNIT_RESULT_CACHE'] does not change the location of the cache file as supposed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/test-runner CLI test runner type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

4 participants