-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add utime() override for mocked files #196
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
Open
Koan-Bot
wants to merge
1
commit into
cpanel:master
Choose a base branch
from
atoomic:koan.atoomic/implement-utime
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+263
−0
Open
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,164 @@ | ||
| #!/usr/bin/perl -w | ||
|
|
||
| use strict; | ||
| use warnings; | ||
|
|
||
| use Test2::Bundle::Extended; | ||
| use Test2::Tools::Explain; | ||
| use Test2::Plugin::NoWarnings; | ||
| use Test2::Tools::Exception qw< lives dies >; | ||
|
|
||
| use File::Temp qw< tempfile >; | ||
| use Cwd (); | ||
| use Errno qw< ENOENT >; | ||
|
|
||
| # Create tempfiles before Test::MockFile overrides are installed, | ||
| # so File::Temp's internal stat/chmod calls hit real CORE functions. | ||
| my ( $passthrough_tempfile, $nostrict_tempfile ); | ||
| BEGIN { | ||
| ( undef, $passthrough_tempfile ) = tempfile( UNLINK => 0 ); | ||
| ( undef, $nostrict_tempfile ) = tempfile( UNLINK => 0 ); | ||
| } | ||
|
|
||
| use Test::MockFile qw< nostrict >; | ||
|
|
||
| subtest( | ||
| 'utime on mocked file' => sub { | ||
| my $file = Test::MockFile->file( '/foo/bar', 'content' ); | ||
| ok( -f '/foo/bar', 'File exists' ); | ||
|
|
||
| my $new_atime = 1000000; | ||
| my $new_mtime = 2000000; | ||
|
|
||
| is( utime( $new_atime, $new_mtime, '/foo/bar' ), 1, 'utime returns 1 for success' ); | ||
|
|
||
| my @stat = stat('/foo/bar'); | ||
| is( $stat[8], $new_atime, 'atime was updated' ); | ||
| is( $stat[9], $new_mtime, 'mtime was updated' ); | ||
| } | ||
| ); | ||
|
|
||
| subtest( | ||
| 'utime updates ctime to current time' => sub { | ||
| my $file = Test::MockFile->file( '/foo/baz', 'content' ); | ||
|
|
||
| my $before = time; | ||
| utime( 1000, 2000, '/foo/baz' ); | ||
| my $after = time; | ||
|
|
||
| my @stat = stat('/foo/baz'); | ||
| ok( $stat[10] >= $before && $stat[10] <= $after, 'ctime was updated to current time' ); | ||
| } | ||
| ); | ||
|
|
||
| subtest( | ||
| 'utime with undef uses current time' => sub { | ||
| my $file = Test::MockFile->file( '/foo/undef_test', 'content' ); | ||
|
|
||
| my $before = time; | ||
| is( utime( undef, undef, '/foo/undef_test' ), 1, 'utime with undef returns 1' ); | ||
| my $after = time; | ||
|
|
||
| my @stat = stat('/foo/undef_test'); | ||
| ok( $stat[8] >= $before && $stat[8] <= $after, 'atime set to current time when undef' ); | ||
| ok( $stat[9] >= $before && $stat[9] <= $after, 'mtime set to current time when undef' ); | ||
| } | ||
| ); | ||
|
|
||
| subtest( | ||
| 'utime on multiple mocked files' => sub { | ||
| my $file1 = Test::MockFile->file( '/multi/a', 'aaa' ); | ||
| my $file2 = Test::MockFile->file( '/multi/b', 'bbb' ); | ||
|
|
||
| is( utime( 5000, 6000, '/multi/a', '/multi/b' ), 2, 'utime returns 2 for two files' ); | ||
|
|
||
| my @stat_a = stat('/multi/a'); | ||
| my @stat_b = stat('/multi/b'); | ||
| is( $stat_a[8], 5000, 'file a atime updated' ); | ||
| is( $stat_a[9], 6000, 'file a mtime updated' ); | ||
| is( $stat_b[8], 5000, 'file b atime updated' ); | ||
| is( $stat_b[9], 6000, 'file b mtime updated' ); | ||
| } | ||
| ); | ||
|
|
||
| subtest( | ||
| 'utime on nonexistent mocked file' => sub { | ||
| my $file = Test::MockFile->file('/no/exist'); | ||
| ok( !-f '/no/exist', 'File does not exist' ); | ||
|
|
||
| $! = 0; | ||
| is( utime( 1000, 2000, '/no/exist' ), 0, 'utime returns 0 for nonexistent file' ); | ||
| is( $! + 0, ENOENT, '$! is set to ENOENT' ); | ||
| } | ||
| ); | ||
|
|
||
| subtest( | ||
| 'utime on mocked directory' => sub { | ||
| my $dir = Test::MockFile->dir('/mydir'); | ||
| ok( mkdir('/mydir'), 'Created directory' ); | ||
| ok( -d '/mydir', 'Directory exists' ); | ||
|
|
||
| is( utime( 3000, 4000, '/mydir' ), 1, 'utime on directory returns 1' ); | ||
|
|
||
| my @stat = stat('/mydir'); | ||
| is( $stat[8], 3000, 'dir atime updated' ); | ||
| is( $stat[9], 4000, 'dir mtime updated' ); | ||
| } | ||
| ); | ||
|
|
||
| subtest( | ||
| 'utime with no files returns 0' => sub { | ||
| is( utime( 1000, 2000 ), 0, 'utime with no files returns 0' ); | ||
| } | ||
| ); | ||
|
|
||
| subtest( | ||
| 'utime on mix of mocked and unmocked files' => sub { | ||
| my $filename = __FILE__; | ||
| my $mocked = Cwd::getcwd() . "/$filename"; | ||
| my $unmocked = '/foo_DOES_NOT_EXIST.znxc'; | ||
|
|
||
| my $file = Test::MockFile->file( $filename, 'whatevs' ); | ||
|
|
||
| like( | ||
| dies( sub { utime 1000, 2000, $filename, $unmocked } ), | ||
| qr/^\QYou called utime() on a mix of mocked ($mocked) and unmocked files ($unmocked)\E/xms, | ||
| 'Cannot mix mocked and unmocked files with utime', | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| subtest( | ||
| 'utime on unmocked file passes through' => sub { | ||
| my $new_atime = 1000000; | ||
| my $new_mtime = 2000000; | ||
|
|
||
| is( utime( $new_atime, $new_mtime, $passthrough_tempfile ), 1, 'utime on real file returns 1' ); | ||
|
|
||
| my @stat = CORE::stat($passthrough_tempfile); | ||
| is( $stat[8], $new_atime, 'real file atime was updated' ); | ||
| is( $stat[9], $new_mtime, 'real file mtime was updated' ); | ||
|
|
||
| CORE::unlink $passthrough_tempfile; | ||
| } | ||
| ); | ||
|
|
||
| subtest( | ||
| 'utime on unmocked file while mocked files exist' => sub { | ||
| my $mock = Test::MockFile->file( '/mocked/for_utime', 'data' ); | ||
|
|
||
| my $new_atime = 3000000; | ||
| my $new_mtime = 4000000; | ||
|
|
||
| is( utime( $new_atime, $new_mtime, $nostrict_tempfile ), 1, 'utime on unmocked file returns 1' ); | ||
|
|
||
| my @stat = CORE::stat($nostrict_tempfile); | ||
| is( $stat[8], $new_atime, 'unmocked file atime was updated' ); | ||
| is( $stat[9], $new_mtime, 'unmocked file mtime was updated' ); | ||
|
|
||
| CORE::unlink $nostrict_tempfile; | ||
| } | ||
| ); | ||
|
|
||
| done_testing(); | ||
| exit; |
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,45 @@ | ||
| #!/usr/bin/perl -w | ||
|
|
||
| use strict; | ||
| use warnings; | ||
|
|
||
| use Test2::Bundle::Extended; | ||
| use Test2::Plugin::NoWarnings; | ||
| use Test2::Tools::Exception qw< dies lives >; | ||
|
|
||
| use File::Temp qw< tempfile >; | ||
|
|
||
| my ( $temp_fh, $tempfile ); | ||
| BEGIN { ( $temp_fh, $tempfile ) = tempfile( UNLINK => 0 ); close $temp_fh; } | ||
|
|
||
| use Test::MockFile; | ||
|
|
||
| subtest( | ||
| 'utime on unmocked file in strict mode dies' => sub { | ||
| like( | ||
| dies( sub { utime 1000, 2000, $tempfile } ), | ||
| qr/\Qutime\E/, | ||
| 'utime on unmocked file in strict mode triggers violation', | ||
| ); | ||
|
|
||
| CORE::unlink($tempfile); | ||
| } | ||
| ); | ||
|
|
||
| subtest( | ||
| 'utime on mocked file in strict mode succeeds' => sub { | ||
| my $file = Test::MockFile->file( '/strict/test', 'content' ); | ||
|
|
||
| ok( | ||
| lives( sub { utime 1000, 2000, '/strict/test' } ), | ||
| 'utime on mocked file in strict mode works', | ||
| ) or note $@; | ||
|
|
||
| my @stat = stat('/strict/test'); | ||
| is( $stat[8], 1000, 'atime set correctly in strict mode' ); | ||
| is( $stat[9], 2000, 'mtime set correctly in strict mode' ); | ||
| } | ||
| ); | ||
|
|
||
| done_testing(); | ||
| exit; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is good but I think we should have also preserved the call and check later when
$mocked_files{$file}is undefined then we should also fall back to theCORE::utimeCan we extend coverage when mock is enabled (no strict) to call
utimeon amockedfile but also on anone mockedfile to cover that scenario line 2462 whenmy $mock = $mocked_files{$file};is undefThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Added:
Per-file
if (!$mock)fallback inside the foreach loop (lines 2464-2468) — mirrors the__chmodpattern. Falls through toCORE::utimeif a file has no mock object.New test:
utime on unmocked file while mocked files exist— creates a mocked file at/mocked/for_utime, then calls utime on a real tempfile. Verifies the passthrough works when mocks are active but the target file is unmocked.Tempfiles are created in
BEGINbeforeuse Test::MockFileto avoidFile::Temp's internal stat/chmod hitting our overrides.Force-pushed as a single squashed commit (678bd07).