File Coverage

blib/lib/Test/MockFile/Plugin/FileTemp.pm
Criterion Covered Total %
statement 49 50 98.0
branch 7 10 70.0
condition 1 3 33.3
subroutine 10 10 100.0
pod 1 1 100.0
total 68 74 91.8


line stmt bran cond sub pod time code
1             package Test::MockFile::Plugin::FileTemp;
2              
3 3     3   19 use strict;
  3         7  
  3         134  
4 3     3   17 use warnings;
  3         5  
  3         182  
5              
6 3     3   17 use parent 'Test::MockFile::Plugin';
  3         6  
  3         26  
7              
8 3     3   2255 use Test::MockModule qw{strict};
  3         23006  
  3         23  
9              
10 3     3   173 use Carp qw(croak);
  3         6  
  3         1807  
11              
12             our $VERSION = '0.037';
13              
14             sub register {
15 3     3 1 6 my ($self) = @_;
16              
17 3 50       157 if ( $^V lt 5.28.0 ) {
18 0         0 croak( __PACKAGE__ . " is only supported for Perl >= 5.28" );
19             }
20              
21 3         14 foreach my $pkg (qw{ File::Temp File::Temp::Dir File::Temp::END File::Temp::Dir::DESTROY }) {
22 12         31 Test::MockFile::authorized_strict_mode_for_package($pkg);
23             }
24              
25 3         14 Test::MockFile::add_strict_rule_generic( \&_allow_file_temp_calls );
26              
27 3         14 my $mock = Test::MockModule->new('File::Temp');
28              
29             # tempfile
30             $mock->redefine(
31             tempfile => sub {
32 3     3   10363 my (@in) = @_;
33              
34 3         18 my @out = $mock->original('tempfile')->(@in);
35              
36 3         95 Test::MockFile::add_strict_rule_for_filename( $out[1] => 1 );
37              
38 3 100       14 return @out if wantarray;
39              
40 2         13 File::Temp::unlink0( $out[0], $out[1] );
41 2         83 return $out[0];
42             }
43 3         41838 );
44              
45             # tempdir
46             $mock->redefine(
47             tempdir => sub {
48 4     4   974 my (@in) = @_;
49              
50 4         26 my $out = $mock->original('tempdir')->(@in);
51 4         163 my $dir = "$out";
52              
53 4         100 Test::MockFile::add_strict_rule_for_filename( [ $dir, qr{^${dir}/} ] => 1 );
54              
55 4         21 return $out;
56             }
57 3         354 );
58              
59             # newdir
60             $mock->redefine(
61             newdir => sub {
62 1     1   676 my (@args) = @_;
63              
64 1         7 my $out = $mock->original('newdir')->(@args);
65 1         109 my $dir = "$out";
66              
67 1         35 Test::MockFile::add_strict_rule_for_filename( [ $dir, qr{^$dir/} ] => 1 );
68              
69 1         5 return $out;
70             }
71 3         217 );
72              
73 3         209 $self->{mock} = $mock;
74              
75 3         24 return $self;
76             }
77              
78             sub _allow_file_temp_calls {
79 11     11   51 my ($ctx) = @_;
80              
81 11         33 foreach my $stack_level ( 1 .. Test::MockFile::_STACK_ITERATION_MAX() ) {
82 97         390 my @stack = caller($stack_level);
83 97 100       192 last if !scalar @stack;
84 86 50       146 last if !defined $stack[0]; # We don't know when this would ever happen.
85              
86 86 50 33     352 return 1 if $stack[0] eq 'File::Temp' #
87             || $stack[0] eq 'File::Temp::Dir';
88             }
89              
90 11         36 return;
91             }
92              
93             1;
94              
95             =encoding utf8
96              
97             =head1 NAME
98              
99             Test::MockFile::Plugin::FileTemp - Plugin to allow File::Temp calls
100              
101             =head1 SYNOPSIS
102              
103             use Test::MockFile 'strict', plugin => 'FileTemp';
104              
105             # using FileTemp plugin, all calls from FileTemp bypass the Test::MockFile strict mode
106              
107             my $dir = File::Temp->newdir();
108             ok opendir( my $dh, "$dir" );
109             ok open( my $f, '>', "$dir/myfile.txt" );
110              
111             =head1 DESCRIPTION
112              
113             L provides plugin to Test::MockFile
114             to authorize any calls from File::Temp package.
115              
116             =head1 METHODS
117              
118             =head2 register( $self )
119              
120             Public method to register the plugin.
121              
122             =head1 SEE ALSO
123              
124             L, L, L
125              
126             =cut