This file is indexed.

/usr/share/perl5/Test/MockModule.pm is in libtest-mockmodule-perl 0.05-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# $Id: MockModule.pm,v 1.7 2005/03/24 22:23:38 simonflack Exp $
package Test::MockModule;
use strict qw/subs vars/;
use vars qw/$VERSION/;
use Scalar::Util qw/reftype weaken/;
use Carp;
$VERSION = '0.05';#sprintf'%d.%02d', q$Revision: 1.7 $ =~ /: (\d+)\.(\d+)/;

my %mocked;
sub new {
    my $class = shift;
    my ($package, %args) = @_;
    if ($package && (my $existing = $mocked{$package})) {
        return $existing;
    }

    croak "Cannot mock $package" if $package && $package eq $class;
    unless (_valid_package($package)) {
        $package = 'undef' unless defined $package;
        croak "Invalid package name $package";
    }

    unless ($args{no_auto} || ${"$package\::VERSION"}) {
        (my $load_package = "$package.pm") =~ s{::}{/}g;
        TRACE("$package is empty, loading $load_package");
        require $load_package;
    }

    TRACE("Creating MockModule object for $package");
    my $self = bless {
        _package => $package,
        _mocked  => {},
    }, $class;
    $mocked{$package} = $self;
    weaken $mocked{$package};
    return $self;
}

sub DESTROY {
    my $self = shift;
    $self->unmock_all;
}

sub get_package {
    my $self = shift;
    return $self->{_package};
}

sub mock {
    my $self = shift;

    while (my ($name, $value) = splice @_, 0, 2) {
        my $code = sub { };
        if (ref $value && reftype $value eq 'CODE') {
            $code = $value;
        } elsif (defined $value) {
            $code = sub {$value};
        }

        TRACE("$name: $code");
        croak "Invalid subroutine name: $name" unless _valid_subname($name);
        my $sub_name = _full_name($self, $name);
        if (!$self->{_mocked}{$name}) {
            TRACE("Storing existing $sub_name");
            $self->{_mocked}{$name} = 1;
            $self->{_orig}{$name}   = defined &{$sub_name} ? \&$sub_name
                : $self->{_package}->can($name);
        }
        TRACE("Installing mocked $sub_name");
        _replace_sub($sub_name, $code);
    }
}

sub original {
    my $self = shift;
    my ($name) = @_;
    return carp _full_name($self, $name) . " is not mocked"
            unless $self->{_mocked}{$name};
    return $self->{_orig}{$name};
}

sub unmock {
    my $self = shift;

    for my $name (@_) {
        croak "Invalid subroutine name: $name" unless _valid_subname($name);

        my $sub_name = _full_name($self, $name);
        unless ($self->{_mocked}{$name}) {
            carp $sub_name . " was not mocked";
            next;
        }

        TRACE("Restoring original $sub_name");
        _replace_sub($sub_name, $self->{_orig}{$name});
        delete $self->{_mocked}{$name};
        delete $self->{_orig}{$name};
    }
    return $self;
}

sub unmock_all {
    my $self = shift;
    foreach (keys %{$self->{_mocked}}) {
        $self->unmock($_);
    }
}

sub is_mocked {
    my $self = shift;
    my ($name) = shift;
    return $self->{_mocked}{$name};
}

sub _full_name {
    my ($self, $sub_name) = @_;
    sprintf "%s::%s", $self->{_package}, $sub_name;
}

sub _valid_package {
    defined($_[0]) && $_[0] =~ /^[a-z_]\w*(?:::\w+)*$/i;
}

sub _valid_subname {
    $_[0] =~ /^[a-z_]\w*$/i;
}

sub _replace_sub {
    my ($sub_name, $coderef) = @_;
    # from Test::MockObject
    local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
    if (defined $coderef) {
        *{$sub_name} = $coderef;
    } else {
        TRACE("removing subroutine: $sub_name");
        my ($package, $sub) = $sub_name =~ /(.*::)(.*)/;
        my %symbols = %{$package};

        # save a copy of all non-code slots
        my %slot;
        foreach (qw(ARRAY FORMAT HASH IO SCALAR)) {
            next unless defined(my $elem = *{$symbols{$sub}}{$_});
            $slot{$_} = $elem;
        }

        # clear the symbol table entry for the subroutine
        undef *$sub_name;

        # restore everything except the code slot
        return unless keys %slot;
        foreach (keys %slot) {
            *$sub_name = $slot{$_};
        }
    }
}

# Log::Trace stubs
sub TRACE {}
sub DUMP  {}

1;

=pod

=head1 NAME

Test::MockModule - Override subroutines in a module for unit testing

=head1 SYNOPSIS

   use Module::Name;
   use Test::MockModule;

   {
       my $module = new Test::MockModule('Module::Name');
       $module->mock('subroutine', sub { ... });
       Module::Name::subroutine(@args); # mocked
   }

   Module::Name::subroutine(@args); # original subroutine

=head1 DESCRIPTION

C<Test::MockModule> lets you temporarily redefine subroutines in other packages
for the purposes of unit testing.

A C<Test::MockModule> object is set up to mock subroutines for a given
module. The object remembers the original subroutine so it can be easily
restored. This happens automatically when all MockModule objects for the given
module go out of scope, or when you C<unmock()> the subroutine.

=head1 METHODS

=over 4

=item new($package[, %options])

Returns an object that will mock subroutines in the specified C<$package>.

If there is no C<$VERSION> defined in C<$package>, the module will be
automatically loaded. You can override this behaviour by setting the C<no_auto>
option:

    my $mock = new Test::MockModule('Module::Name', no_auto => 1);

=item get_package()

Returns the target package name for the mocked subroutines

=item is_mocked($subroutine)

Returns a boolean value indicating whether or not the subroutine is currently
mocked

=item mock($subroutine =E<gt> \E<amp>coderef)

Temporarily replaces one or more subroutines in the mocked module. A subroutine
can be mocked with a code reference or a scalar. A scalar will be recast as a
subroutine that returns the scalar.

The following statements are equivalent:

    $module->mock(purge => 'purged');
    $module->mock(purge => sub { return 'purged'});

    $module->mock(updated => [localtime()]);
    $module->mock(updated => sub { return [localtime()]});

However, C<undef> is a special case. If you mock a subroutine with C<undef> it
will install an empty subroutine

    $module->mock(purge => undef);
    $module->mock(purge => sub { });

rather than a subroutine that returns C<undef>:

    $module->mock(purge => sub { undef });

You can call C<mock()> for the same subroutine many times, but when you call
C<unmock()>, the original subroutine is restored (not the last mocked
instance).

=item original($subroutine)

Returns the original (unmocked) subroutine

=item unmock($subroutine [, ...])

Restores the original C<$subroutine>. You can specify a list of subroutines to
C<unmock()> in one go.

=item unmock_all()

Restores all the subroutines in the package that were mocked. This is
automatically called when all C<Test::MockObject> objects for the given package
go out of scope.

=back

=head1 SEE ALSO

L<Test::MockObject::Extends>

L<Sub::Override>

=head1 AUTHOR

Simon Flack E<lt>simonflk _AT_ cpan.orgE<gt>

=head1 COPYRIGHT

Copyright 2004 Simon Flack E<lt>simonflk _AT_ cpan.orgE<gt>.
All rights reserved

You may distribute under the terms of either the GNU General Public License or
the Artistic License, as specified in the Perl README file.

=cut