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
|
#!/usr/bin/perl -w
# 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 @exist = qw{cherry banana apple};
my @not_there = qw{mum and dad};
ok mock_all_file_checks( \&my_custom_check ), 'mock_all_file_checks succeeds';
my $last_check_called;
sub my_custom_check {
my ( $check, $f ) = @_;
note "mocked check -$check called for ", $f;
$last_check_called = $check;
return CHECK_IS_TRUE if grep { $_ eq $f } @exist;
return CHECK_IS_FALSE if grep { $_ eq $f } @not_there;
# we have no idea about these files
return FALLBACK_TO_REAL_OP;
}
my $ALL_CHECKS = Overload::FileCheck::_get_filecheck_ops_map();
foreach my $c ( sort keys %$ALL_CHECKS ) {
next if $c =~ qr{^l?stat$}; # do not bother with these they are not file checks
my $do_check = sub {
my ($input) = @_;
return eval qq[ -$c \$input];
};
foreach my $f (@exist) {
ok( $do_check->($f), "-$c '$f' is true" );
}
is $last_check_called, $c, "last check called was -$c";
foreach my $f (@not_there) {
ok( !$do_check->($f), "-$c '$f' is false" );
}
is $last_check_called, $c, "last check called was -$c";
}
$last_check_called = '';
ok stat($0);
ok lstat($0);
is $last_check_called, '', "stat or lstat are not mocked";
ok unmock_all_file_checks(), "unmock_all_file_checks";
undef $last_check_called;
my $check = -e $^X;
ok $check, "check succeeds";
is $last_check_called, undef, 'custom function not called after unmock_all_file_checks';
done_testing;
|