From 7558aa99540277099b4b79cb9f826e468460c7ec Mon Sep 17 00:00:00 2001 From: Nicolas R Date: Thu, 16 Jan 2020 17:05:27 -0700 Subject: [PATCH] Make sure we can redefine a function in 'main' This is broken with SUPER 1.17, we need a version >1.20 This is bumping the version requirement for SUPER to 1.20* and also adding one exception for the package namespace 'main' --- Build.PL | 4 ++-- lib/Test/MockModule.pm | 2 +- t/main.t | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 t/main.t diff --git a/Build.PL b/Build.PL index 8d6bbf9..2515d6e 100644 --- a/Build.PL +++ b/Build.PL @@ -19,10 +19,10 @@ my $builder = Module::Build->new( 'Module::Build' => '0.38', }, requires => { - 'perl' => 5.006, + 'perl' => 5.006, 'Carp' => 0, 'Scalar::Util' => 0, - 'SUPER' => 0, + 'SUPER' => '1.20', }, build_requires => { 'Test::More' => 0.88, diff --git a/lib/Test/MockModule.pm b/lib/Test/MockModule.pm index 7358816..ee214eb 100644 --- a/lib/Test/MockModule.pm +++ b/lib/Test/MockModule.pm @@ -30,7 +30,7 @@ sub new { croak "Invalid package name $package"; } - unless ($package eq "CORE::GLOBAL" || $args{no_auto} || ${"$package\::VERSION"}) { + unless ($package eq "CORE::GLOBAL" || $package eq 'main' || $args{no_auto} || ${"$package\::VERSION"}) { (my $load_package = "$package.pm") =~ s{::}{/}g; TRACE("$package is empty, loading $load_package"); require $load_package; diff --git a/t/main.t b/t/main.t new file mode 100644 index 0000000..68c47f0 --- /dev/null +++ b/t/main.t @@ -0,0 +1,15 @@ +use warnings; +use strict; + +use Test::More; +use Test::Warnings; + +use Test::MockModule; + +sub fourofour { 404 } + +my $mocker = Test::MockModule->new('main')->redefine( fourofour => 200 ); + +is fourofour(), 200, "can mock a function in main # need SUPER > 1.17"; + +done_testing();