Skip to content

Commit

Permalink
Detect incorrect usage of add_strict_rule_for_command
Browse files Browse the repository at this point in the history
Fixes #181:

Improve add_strict_rule_for_command usage
and detect  incorrect usage of add_strict_rule_for_command
  • Loading branch information
atoomic authored and toddr committed Jul 20, 2023
1 parent 067007a commit d68311b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/Test/MockFile.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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';
}

Expand Down
41 changes: 41 additions & 0 deletions t/strict-rules_scalar.t
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit d68311b

Please sign in to comment.