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 file is all over the place #3587
Comments
It should only be in the root directory (where |
@constup Thanks for your report. I will look for ways to tighten up the cachefile placement. @sebastianbergmann Yes, it was designed that way. However I have also seen it ending up in the 'root' of end-to-end test directories and depending on the IDE/wrapper script the mechanism might see a different root. It still happens, though:
I will think more about this coming days. Basically the cache is to speed up when testing the same collection over and over again, like I do a lot for PHPUnit. When using the ⏯ buttons in PHPStorm for example, caching this result makes no sense. In addition to the IDE having its own "run failed tests again/first" option. @sebastianbergmann if you have any wishes or suggestions to be included for this let me know.
This (already) works when there's a single location to see as root. It gets hard when people use custom configurations with |
The current implementation is still the MVP, basically:
Unless the user sets a fixed path in the config, the |
To be honest,
suggests to me that IDEs are running single tests wrong. This does not mean, though, that we should not make sure that |
@epdenouden Thanks! I merged this to |
Will do! |
I managed to merge it without conflict from diff --git a/src/Runner/DefaultTestResultCache.php b/src/Runner/DefaultTestResultCache.php
index 1385849c4..0ff80fc55 100644
--- a/src/Runner/DefaultTestResultCache.php
+++ b/src/Runner/DefaultTestResultCache.php
@@ -66,9 +66,14 @@ final class DefaultTestResultCache implements \Serializable, TestResultCache
*/
private $times = [];
- public function __construct($filename = null)
+ public function __construct($filepath = null)
{
- $this->cacheFilename = $filename ?? $_ENV['PHPUNIT_RESULT_CACHE'] ?? self::DEFAULT_RESULT_CACHE_FILENAME;
+ 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;
}
/**
diff --git a/src/TextUI/TestRunner.php b/src/TextUI/TestRunner.php
index 165089587..5655b6923 100644
--- a/src/TextUI/TestRunner.php
+++ b/src/TextUI/TestRunner.php
@@ -159,12 +159,24 @@ public function doRun(Test $suite, array $arguments = [], bool $exit = true): Te
}
if ($arguments['cacheResult']) {
- if (isset($arguments['cacheResultFile'])) {
- $cache = new DefaultTestResultCache($arguments['cacheResultFile']);
- } else {
- $cache = new DefaultTestResultCache;
+ 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']);
+
$this->addExtension(new ResultCacheExtension($cache));
} In However, with the changes shown above, running the tests for PHPUnit 8.1 results in the following files getting created:
Is this behaviour correct, @epdenouden, and we just need to add these files to |
I came to the conclusion that it is the right thing to do here to ignore the generated files. |
No it certainly isn't! The "we are running our self-tests" environment variable should prevent this. I will have a deeper look. Self-testing a testing framework properly just hurts my brain every now and then. Apologies for the fix, I should have run more tests on it. |
@sebastianbergmann Perhaps I am misinterpreting some comments from yesterday, but I do not see a lot of cache files being generated by |
You probably do not see them because of acd938f. |
Never mind, you're looking using |
I did not see those files on |
Just made a fresh branch of When running from another directory new caches get created, as expected. Here's the end result of running the For now it seems to work as expected. Don't hesitate to reopen the issue if a new scenario pops up! |
When I run a test with PHPUnit from PHPStorm I see them all over when I run a single test (test method). I think this cannot be desired behaviour, right? Using PHPStorm 2019.1 and PHPUnit 8.1.3. Based on the last comments from this discussion I am not sure if there was any action on it. |
Changes were made to remedy what you describe, @frederikbosch, but there is no release yet that has them. |
@sebastianbergmann and @epdenouden Will that changes also help for our situation, where the PHPUnit PHAR file is located on a share 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, leading to warning logs like:
In the course of analysing the problem we stumbled over
DefaultTestResultCache c'tor is never called with null (cf. phpunit/src/TextUI/TestRunner.php Line 167 in 41a5260
Should it be usable? As far as I understand our devs using PhpStorm IDE must 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 Of course I would be happy if global configuration via |
@sebastianbergmann What do you think of my aforementioned comment #3587 (comment) (I am using latest PHPUnit release 8.1.6 and thus unfortunately have the "fix" from 8.1.4 that may help @frederikbosch, but actually causes my problem...)? |
@reinholdfuereder If your problem is a different/new one then please open a new ticket. Thanks! |
Cache is enabled by default, which is not a bad thing. However, by running unit tests,
.phpunit.result.cache
file is being generated in each folder where the test is run. When running tests from the root of the project - that's not a big issue, since one file is generated in the root directory of the project. However, IDEs have integrations for unit tests where you can run a single test method or a class "on click". That translates to having multiple.phpunit.result.cache
files all over the directory structure - in each directory where a test is run.The solution is obvious - have a single location where the cache file is placed by default and prevent cache clutter in project's directory structure.
The text was updated successfully, but these errors were encountered: