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

Check file existence before filemtime #20260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

markhuot
Copy link
Contributor

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

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
Copy link

codecov bot commented Sep 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rob006
Copy link
Contributor

rob006 commented Oct 1, 2024

Why these tests report suppressed warnings?

@ailmanki
Copy link

ailmanki commented Oct 1, 2024

and again the same topic
#17556

@samdark
Copy link
Member

samdark commented Oct 1, 2024

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.

@markhuot
Copy link
Contributor Author

markhuot commented Oct 1, 2024

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: #17556 (comment)

@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: pestphp/pest#1283

@rob006
Copy link
Contributor

rob006 commented Oct 1, 2024

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

@markhuot
Copy link
Contributor Author

markhuot commented Oct 1, 2024

@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.

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

Successfully merging this pull request may close these issues.

4 participants