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
|
#!/usr/bin/perl
# Copyright (c) 2018, cPanel, LLC.
# All rights reserved.
# http://cpanel.net
#
# This is free software; you can redistribute it and/or modify it under the
# same terms as Perl itself. See L<perlartistic>.
use strict;
use warnings;
use Test2::Bundle::Extended;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;
use Overload::FileCheck q/:all/;
my $CURRENT_FH = 0;
my @STAT_CALLED_WITH;
my $IS_MOCKED;
my $TOTAL_CALL = 0;
sub mystat {
my ( $stat_or_lstat, $f ) = @_;
note "call MYSTAT '", $stat_or_lstat, "' for file: ", $f, " def ? ", defined $f ? "defined" : "undef";
push @STAT_CALLED_WITH, "$f"; # stringify
++$TOTAL_CALL;
# if this our CURRENT_FH call return a file with a size
return stat_as_file( size => 1234 ) if $f eq $CURRENT_FH;
# otherwise return a null file
return stat_as_file( size => 0 );
}
sub test {
my @array;
my $x = boom($0);
push @array, $x;
push @array, boom($0); # Bizarre copy of ARRAY in list assignment
push @array, boom($0);
return \@array;
}
note "Test unmocked";
is test(), [ $0, $0, $0 ], "check - unmocked";
note "Mocking all FileCheck using mock_all_from_stat";
$IS_MOCKED = 1;
ok mock_all_from_stat( \&mystat ), "mocking stat";
my $a = test();
is scalar @$a, 3, "3 elements in array";
is $a, [ $0, $0, $0 ], "check - mocked - array with two elements as expected";
is $TOTAL_CALL, 3, "mystat was called 3 times";
done_testing;
sub boom {
my ($path) = @_;
open my $fh, '<', $path;
$CURRENT_FH = "$fh"; # stringify
@STAT_CALLED_WITH = ();
note "... -f fh && -s _", $fh;
my $not_null = -f $fh && -s _;
if ($IS_MOCKED) {
is scalar @STAT_CALLED_WITH, 1, "only perform a single stat call";
is \@STAT_CALLED_WITH, [$CURRENT_FH], "stat was called with our current GLOB";
}
return $not_null ? $path : undef;
}
__END__
|