Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a global way to assure nostrict ever happens #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion lib/Test/MockModule.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@ use SUPER;
# This is now auto-updated at release time by the github action
$VERSION = 'DEVELOP';

our $GLOBAL_STRICT_MODE;

sub import {
my ( $class, @args ) = @_;

# default if no args
$^H{'Test::MockModule/STRICT_MODE'} = 0;

foreach my $arg (@args) {
if ( $arg eq 'strict' ) {
if($arg eq 'global-strict' ) {
$GLOBAL_STRICT_MODE=1;
$^H{'Test::MockModule/STRICT_MODE'} = 1;
}
elsif ( $arg eq 'strict' ) {
$^H{'Test::MockModule/STRICT_MODE'} = 1;
} elsif ( $arg eq 'nostrict' ) {
$GLOBAL_STRICT_MODE && die "use Test::MockModule qw(nostrict) is illegal when GLOBAL_STRICT_MODE is being enforced";
$^H{'Test::MockModule/STRICT_MODE'} = 0;
} else {
carp "Test::MockModule unknown import option '$arg'";
Expand All @@ -31,6 +38,8 @@ sub _strict_mode {
while(my @fields = caller($depth++)) {
my $hints = $fields[10];
if($hints && grep { /^Test::MockModule\// } keys %{$hints}) {
$GLOBAL_STRICT_MODE && !$hints->{'Test::MockModule/STRICT_MODE'} && die "use Test::MockModule qw(nostrict) is illegal when GLOBAL_STRICT_MODE is being enforced";

return $hints->{'Test::MockModule/STRICT_MODE'};
}
}
Expand Down Expand Up @@ -328,6 +337,9 @@ Test::MockModule - Override subroutines in a module for unit testing
$module->mock('subroutine', sub { ... });
}

# Assure strict is ALWAYS used.
use Test::MockModule 'global-strict';

# Back in the strict scope, so mock() dies here
$module->mock('subroutine', sub { ... });

Expand Down Expand Up @@ -388,6 +400,19 @@ you think you're going to try and be clever by calling Test::MockModule's
C<import()> method at runtime then what happens in undefined, with results
differing from one version of perl to another. What larks!

=head1 GLOBAL STRICT MODE

If your particular test suite needs to assure that no developer ever accidentally
turns off strict, this is the mode for you

use Test::MockModule 'global-strict';

Setting this mode will cause any later invocation of nostrict to fail on compile.
Further, any use of mock at runtime will die if the 'nostrict' mode was invoked
prior to global-strict being initially set. While this seems like it might be
overkill, this can be important as the number of simultaneous developers
increases over time.

=head1 METHODS

=over 4
Expand Down
28 changes: 28 additions & 0 deletions t/global_strict_mode.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use strict;
use warnings;

use Test::More;

use lib 't/lib';

use Test::MockModule 'nostrict';

sub runtime_loose_mock {
my $m = Test::MockModule->new("lib");
$m->mock( "abc" => 1 ); # Should fail under T::MM strict mode.
return 1;
}

use Test::MockModule 'global-strict';

sub strict_off {
eval { Test::MockModule->import('nostrict') };
like( "$@", qr/is illegal when GLOBAL_STRICT_MODE /, "An import of Test::MockModule fails if they try to turn off strict after global-strict has been set." );
}

strict_off();

is( eval { runtime_loose_mock() }, undef, "runtime_loose_mock() fails at runtime" );
like( "$@", qr/is illegal when GLOBAL_STRICT_MODE/, "Runtime mock is caught even if nostrict is defined before global-strict is invoked" );

done_testing();