diff --git a/MANIFEST b/MANIFEST index 0aa5071..04d654a 100644 --- a/MANIFEST +++ b/MANIFEST @@ -30,6 +30,7 @@ t/mock_stat.t t/new_dir_interface.t t/open-noclose.t t/open.t +t/open_strict.t t/opendir.t t/path.t t/plugin-filetemp.t @@ -43,8 +44,10 @@ t/runtime-bareword-filehandles.t t/stat-x.t t/strict-rules.t t/strict-rules_file-temp-example.t +t/strict-rules_scalar.t t/symlink.t t/sysopen.t +t/sysopen_strict.t t/Test-MockFile_file.t t/touch.t t/unlink.t diff --git a/lib/Test/MockFile.pm b/lib/Test/MockFile.pm index 91a921d..3e8d92a 100644 --- a/lib/Test/MockFile.pm +++ b/lib/Test/MockFile.pm @@ -452,7 +452,11 @@ Apply a rule to one or more files. =cut sub add_strict_rule_for_command { - my ( $command_rule, $action ) = @_; + my ( $command_rule, $action, $extra ) = @_; + + if ($extra) { + die q[Syntax not supported (extra arg) for 'add_strict_rule_for_command', please consider using 'add_strict_rule' instead.]; + } return add_strict_rule( $command_rule, undef, $action ); } @@ -525,7 +529,7 @@ sub _strict_mode_violation { # we don't need to check if it's a violation since something else should # have opened it first. open and sysopen, though, require special care. # - if (UNIVERSAL::isa( $filename, 'GLOB' )) { + if ( UNIVERSAL::isa( $filename, 'GLOB' ) ) { return if $command ne 'open' && $command ne 'sysopen'; } diff --git a/t/strict-rules_scalar.t b/t/strict-rules_scalar.t new file mode 100644 index 0000000..db1db42 --- /dev/null +++ b/t/strict-rules_scalar.t @@ -0,0 +1,41 @@ +#!/usr/bin/perl -w + +use strict; +use warnings; + +use Test2::Bundle::Extended; +use Test2::Tools::Explain; +use Test2::Plugin::NoWarnings; + +use Test::MockFile qw< strict >; # yeap it's strict + +ok( dies { -e "/no/mocked" }, q[-e "/no/mocked"] ); +ok( dies { -l "/no/mocked" }, q[-l "/no/mocked"] ); + +note "add_strict_rule_for_command for stat / lstat"; + +# incorrect +ok( dies { Test::MockFile::add_strict_rule_for_command( [qw{ lstat stat }] => '/this/path', 1 ) }, "command not supported" ); + +# correct +Test::MockFile::add_strict_rule_for_command( + [qw{ lstat stat }] => sub { + my ($ctx) = @_; + return 1 if $ctx->{filename} eq '/this/path'; + return; # continue to the next rule + } +); + +ok( dies { -e "/no/mocked" }, q[-e "/no/mocked"] ); +ok( dies { -l "/no/mocked" }, q[-l "/no/mocked"] ); +ok( lives { -l '/this/path' }, q[-l "/this/path" mocked] ); +ok( dies { -l "/another/mocked" }, q[-l "/another/mocked"] ); + +Test::MockFile::add_strict_rule( [qw{ lstat stat }] => '/another/path', 1 ); + +ok( dies { -e "/no/mocked" }, q[-e "/no/mocked"] ); +ok( dies { -l "/no/mocked" }, q[-l "/no/mocked"] ); +ok( lives { -l '/this/path' }, q[-l "/this/path" mocked] ); +ok( lives { -l '/another/path' }, q[-l "/another/path" mocked] ); + +done_testing;