-
Notifications
You must be signed in to change notification settings - Fork 8
feat: add mock_file_check_guard() for scope-based cleanup #96
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
Merged
atoomic
merged 1 commit into
cpan-authors:main
from
Koan-Bot:koan.atoomic/add-mock-file-check-guard
Apr 26, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| use strict; | ||
| use warnings; | ||
|
|
||
| use Test2::Bundle::Extended; | ||
| use Test2::Tools::Explain; | ||
|
|
||
| use Overload::FileCheck qw( | ||
| mock_file_check_guard mock_file_check unmock_file_check | ||
| CHECK_IS_TRUE CHECK_IS_FALSE FALLBACK_TO_REAL_OP | ||
| ); | ||
|
|
||
| my $fake = "/guard/test/file"; | ||
|
|
||
| # --- basic guard: mock is active inside scope, removed after --- | ||
| { | ||
| my $guard = mock_file_check_guard( '-e' => sub { CHECK_IS_TRUE } ); | ||
| isa_ok( $guard, 'Overload::FileCheck::Guard' ); | ||
| ok( -e $fake, "mocked -e returns true inside guard scope" ); | ||
| } | ||
| ok( !-e $fake, "-e falls back to real op after guard is destroyed" ); | ||
|
|
||
| # --- guard with cancel: mock persists after scope --- | ||
| { | ||
| my $guard = mock_file_check_guard( '-f' => sub { CHECK_IS_TRUE } ); | ||
| ok( -f $fake, "mocked -f returns true" ); | ||
| $guard->cancel; | ||
| } | ||
| # mock still active because we cancelled the guard | ||
| ok( -f $fake, "-f still mocked after cancelled guard" ); | ||
| unmock_file_check('-f'); # manual cleanup | ||
| ok( !-f $fake, "-f unmocked manually" ); | ||
|
|
||
| # --- guard handles double-destroy gracefully --- | ||
| { | ||
| my $guard = mock_file_check_guard( '-d' => sub { CHECK_IS_TRUE } ); | ||
| ok( -d $fake, "mocked -d" ); | ||
| # explicitly destroy, then let scope destroy again | ||
| $guard->DESTROY; | ||
| ok( !-d $fake, "-d unmocked after explicit DESTROY" ); | ||
| } | ||
| # second DESTROY from scope exit should not die | ||
| pass("double DESTROY did not die"); | ||
|
|
||
| # --- guard unmocks even if test dies (eval) --- | ||
| eval { | ||
| my $guard = mock_file_check_guard( '-e' => sub { CHECK_IS_TRUE } ); | ||
| ok( -e $fake, "mocked -e inside eval" ); | ||
| die "simulated test failure"; | ||
| }; | ||
| ok( !-e $fake, "-e unmocked after die inside eval" ); | ||
|
|
||
| # --- guard works with dash-less check names --- | ||
| { | ||
| my $guard = mock_file_check_guard( 'e' => sub { CHECK_IS_TRUE } ); | ||
| ok( -e $fake, "mocked with dash-less 'e'" ); | ||
| } | ||
| ok( !-e $fake, "unmocked after dash-less guard" ); | ||
|
|
||
| # --- guard with FALLBACK_TO_REAL_OP --- | ||
| { | ||
| my $guard = mock_file_check_guard( '-e' => sub { FALLBACK_TO_REAL_OP } ); | ||
| ok( !-e $fake, "FALLBACK_TO_REAL_OP falls through to real check" ); | ||
| } | ||
|
|
||
| done_testing; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.