File: FileTemp.pm

package info (click to toggle)
libtest-mockfile-perl 0.037-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 436 kB
  • sloc: perl: 4,110; makefile: 7
file content (126 lines) | stat: -rw-r--r-- 2,744 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package Test::MockFile::Plugin::FileTemp;

use strict;
use warnings;

use parent 'Test::MockFile::Plugin';

use Test::MockModule qw{strict};

use Carp qw(croak);

our $VERSION = '0.037';

sub register {
    my ($self) = @_;

    if ( $^V lt 5.28.0 ) {
        croak( __PACKAGE__ . " is only supported for Perl >= 5.28" );
    }

    foreach my $pkg (qw{ File::Temp File::Temp::Dir File::Temp::END File::Temp::Dir::DESTROY }) {
        Test::MockFile::authorized_strict_mode_for_package($pkg);
    }

    Test::MockFile::add_strict_rule_generic( \&_allow_file_temp_calls );

    my $mock = Test::MockModule->new('File::Temp');

    # tempfile
    $mock->redefine(
        tempfile => sub {
            my (@in) = @_;

            my @out = $mock->original('tempfile')->(@in);

            Test::MockFile::add_strict_rule_for_filename( $out[1] => 1 );

            return @out if wantarray;

            File::Temp::unlink0( $out[0], $out[1] );
            return $out[0];
        }
    );

    # tempdir
    $mock->redefine(
        tempdir => sub {
            my (@in) = @_;

            my $out = $mock->original('tempdir')->(@in);
            my $dir = "$out";

            Test::MockFile::add_strict_rule_for_filename( [ $dir, qr{^${dir}/} ] => 1 );

            return $out;
        }
    );

    # newdir
    $mock->redefine(
        newdir => sub {
            my (@args) = @_;

            my $out = $mock->original('newdir')->(@args);
            my $dir = "$out";

            Test::MockFile::add_strict_rule_for_filename( [ $dir, qr{^$dir/} ] => 1 );

            return $out;
        }
    );

    $self->{mock} = $mock;

    return $self;
}

sub _allow_file_temp_calls {
    my ($ctx) = @_;

    foreach my $stack_level ( 1 .. Test::MockFile::_STACK_ITERATION_MAX() ) {
        my @stack = caller($stack_level);
        last if !scalar @stack;
        last if !defined $stack[0];    # We don't know when this would ever happen.

        return 1 if $stack[0] eq 'File::Temp'    #
          || $stack[0] eq 'File::Temp::Dir';
    }

    return;
}

1;

=encoding utf8

=head1 NAME

Test::MockFile::Plugin::FileTemp - Plugin to allow File::Temp calls

=head1 SYNOPSIS

  use Test::MockFile 'strict', plugin => 'FileTemp';

  # using FileTemp plugin, all calls from FileTemp bypass the Test::MockFile strict mode

  my $dir = File::Temp->newdir();
  ok opendir( my $dh, "$dir" );
  ok open( my $f, '>', "$dir/myfile.txt" );

=head1 DESCRIPTION

L<Test::MockFile::Plugin::FileTemp> provides plugin to Test::MockFile
to authorize any calls from File::Temp package.

=head1 METHODS

=head2 register( $self )

Public method to register the plugin.

=head1 SEE ALSO

L<Test::MockFile>, L<Test::MockFile::Plugins>, L<Test::MockModule>

=cut