yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

Check file existence before filemtime

Open markhuot opened this issue 1 year ago • 7 comments

Calling filemtime with a nonexistent file will cause a warning, even with the @ error suppression. This avoids the warning by checking file existence before the modification time

Q A
Is bugfix? ✔️
New feature?
Breaks BC?
Fixed issues

Note, this shows up for me when using https://pestphp.com because any test that emits a warning is marked as ! instead of . So for something like https://craftcms.com, which uses the ->exists during bootstrapping you end up with every test marked with !.


Before:

$./vendor/bin/pest tests/ActingAsTest.php -vvv --display-warnings

   WARN  Tests\ActingAsTest
  ! it logs in users by factory → filemtime(): stat failed for /Users/markhuot/Sites/craft-pest-core/storage/runtime/cache/8e/CraftCMS8e0694a5966c89b99615fb42a4a74296.bin 0.28s
  ✓ it logs in users by email address                                                                 0.01s
  ✓ it logs in user objects                                                                           0.01s
  ! it logs in admins via shorthand → filemtime(): stat failed for /Users/markhuot/Sites/craft-pest-core/storage/runtime/cache/ba/CraftCMSbasePath.bin 0.21s
  ✓ it throws on missing users
  ✓ it should not be logged in on subsequent tests
  ! it acts as a user on get requests → filemtime(): stat failed for /Users/markhuot/Sites/craft-pest-core/storage/runtime/cache/62/CraftCMS62f10a620b432ee970868362b84379c4.bin 0.02s
  ! it creates admin users → filemtime(): stat failed for /Users/markhuot/Sites/craft-pest-core/storage/runtime/cache/62/CraftCMS62f10a620b432ee970868362b84379c4.bin 0.03s
  ! it resets globals during twig parsing → filemtime(): stat failed for /Users/markhuot/Sites/craft-pest-core/storage/runtime/cache/90/CraftCMS90d3d81bc326013c006a844c726840fb.bin 0.02s

After:

$./vendor/bin/pest tests/ActingAsTest.php -vvv --display-warnings

   PASS  Tests\ActingAsTest
  ✓ it logs in users by factory                                                                       0.24s
  ✓ it logs in users by email address                                                                 0.02s
  ✓ it logs in user objects                                                                           0.01s
  ✓ it logs in admins via shorthand                                                                   0.24s
  ✓ it throws on missing users                                                                        0.01s
  ✓ it should not be logged in on subsequent tests
  ✓ it acts as a user on get requests                                                                 0.02s
  ✓ it creates admin users                                                                            0.02s
  ✓ it resets globals during twig parsing                                                             0.02s

  Tests:    9 passed (10 assertions)
  Duration: 0.70s

markhuot avatar Sep 30 '24 21:09 markhuot

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 64.95%. Comparing base (3c75ff1) to head (e7df4bd).

Additional details and impacted files
@@            Coverage Diff            @@
##             master   #20260   +/-   ##
=========================================
  Coverage     64.95%   64.95%           
- Complexity    11396    11398    +2     
=========================================
  Files           430      430           
  Lines         36925    36925           
=========================================
  Hits          23984    23984           
  Misses        12941    12941           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Sep 30 '24 21:09 codecov[bot]

Why these tests report suppressed warnings?

rob006 avatar Oct 01 '24 08:10 rob006

and again the same topic #17556

ailmanki avatar Oct 01 '24 08:10 ailmanki

Is there a way to tell pest to respect @? It's PhpUnit wrapper as far as I know, so should be possible to configure it.

samdark avatar Oct 01 '24 10:10 samdark

Interesting. Thanks for the link @ailmanki, I hadn't thought of,

It is needed. file_exists($cacheFile) && filemtime($cacheFile) > time() is not atomic so you will have race condition: during file_exists($cacheFile) call file exists, but it is deleted right after it and does not exist enymore during filemtime($cacheFile) call. And since we can't rely on file_exists($cacheFile) it is redundant - original code is correct. source: https://github.com/yiisoft/yii2/issues/17556#issuecomment-530005815

@samdark, yup Pest is a PHPUnit wrapper and PHPUnit reports the same warnings. There are currently no ways to ignore warnings in PHPUnit, but I could see about PR'ing a change. I'm not sure if the issue is actually lower though… should PHP even emit warnings when using error suppression?

Edit: opened a discussion over in Pest's discussion forum to see if anyone over there has thoughts: https://github.com/pestphp/pest/discussions/1283

markhuot avatar Oct 01 '24 16:10 markhuot

@markhuot Do you have any of these options enabled: https://docs.phpunit.de/en/10.5/error-handling.html#ignoring-issue-suppression ?

rob006 avatar Oct 01 '24 16:10 rob006

@rob006, I've tried setting ignoreSuppressionOfWarnings="false" ignoreSuppressionOfPhpWarnings="false" on the <source> and didn't see any difference. I also tried the same with true and still no difference.

markhuot avatar Oct 01 '24 16:10 markhuot

@rob006, I've tried setting ignoreSuppressionOfWarnings="false" ignoreSuppressionOfPhpWarnings="false" on the <source> and didn't see any difference. I also tried the same with true and still no difference.

By default it should just work fine. Link by Rob clearly says:

By default, the error handler registered by PHPUnit’s test runner respects the suppression operator (@). This means that issues triggered using @trigger_error(), for example, will not be reported by the default progress and result printers.

So it should work.

One thing am not sure and haven't checked is the PHPUnit version where this is applicable outside the latest.

mtangoo avatar Dec 06 '24 12:12 mtangoo