Test-Without-Module-0.21/0000755000175000017500000000000014345031610014600 5ustar corioncorionTest-Without-Module-0.21/Texts/0000755000175000017500000000000014345031610015707 5ustar corioncorionTest-Without-Module-0.21/Texts/article.txt0000755000175000017500000001630613134206462020110 0ustar corioncorionTitle: Preventing a module from loading

I like modules that provide a dynamic fallback and degrade gracefully if some prerequisites are not available instead of requiring modules when they can do well without them.

But there is a problem - on my development machine, I have all these optional modules installed, but I want to test the behaviour of my code without the optional modules. So I want to set up tests where the optional modules seem not available. My preferred syntax for this is a pragma-like syntax :

use Test::Without::Module qw( HTML::Template ); use Test::Without::Module qr/^POE::/;

So, most of the magic will have to be installed in a sub called "import()" within my (to be written) module.

When you want to muck around with module loading, the only way in Perl seems to be to add a code reference into @INC. That code reference either returns a filehandle, from which the text will be loaded, or undef, which means that the next entry in @INC will be tried.

Things that didn't work :

BEGIN { @SAVED_INC = @INC; }; sub import { @INC = sub { # return undef if it's a blocked module # Look if the module is in @SAVED_INC # Return a filehandle to it }; };

This first variant worked quite well, until I came up to [cpan://Digest::MD5], which wants to load XS code. And the XS code loader looks through @INC, it dosen't respect coderefs in @INC, and thus, the loading of Digest::MD5 fails. Or rather, Digest::MD5 has a fallback to [cpan://Digest::Perl::MD5], which I didn't have installed. So this way will not work as soon as we use any module which uses XS code.

So I had to keep all existing directories in @INC, but there was no way to prevent Perl to look through the rest of @INC if my handler returned undef for a blocked module :

BEGIN { @SAVED_INC = @INC; }; sub import { @INC = sub { # return undef if it's a blocked module }; };

[demerphq] then suggested that I forget about a handler in @INC and muck instead with %INC and a custom import method, that would die whenever that module was imported into a new namespace.

sub import { $INC{$module} = 1; *{$module."::import"} = sub { die 'ugh'; }; };

But this version didn't work, because one could still require the module, and most checks whether a module is available rely on the meme

eval { require Optional::Module }; if ($@) { # module is not available };

But this put me on the right track, I would simply create a faked module on the fly, and return this faked module whenever I want to prevent a module from loading. I don't need to handle the case that a module is allowed, as the rest of @INC will take care of that.

sub import { unshift @INC, sub { # return dummy module filehandle if it's a blocked module }; };

There are now some technical pitfalls. First, [cpan://IO::String] does not work in an @INC-handler, seemingly Perl wants a real filehandle (or at least, [cpan://Acme::Intraweb] and [cpan://PAR] do it that way as well), so I have to create a tempfile for every faked module. That's not a real concern as my module is intended for testing anyway - efficiency is of no importance.

Second, what if a module has already been loaded? Then Perl won't go through @INC at all. So we have to scrub %INC as well and clean it of the unwanted modules, in case they have already been loaded.

After these tries, the algorithm to prevent a module from loading now looks like the following :

use vars qw( %forbidden ); sub import { my ($self,@forbidden_modules) = @_; scrub $module for @forbidden_modules; unshift @INC, sub { my (undef,$filename,undef) = @_; if (exists $forbidden{$filename}) { # return faked, failing module }; }; };

The complete module is appended below. If you have suggestions about the naming convention or the usage interface, I'd like to hear about them. If you have any hint on how to make my module into a lexical pragma (warnings.pm and strict.pm didn't offer a hint to me), I'll be even more interested.

package Test::Without::Module; use strict; use File::Temp; use Carp qw( croak ); use vars qw( %forbidden $VERSION ); $VERSION = 0.01; sub import { my ($self,@forbidden_modules) = @_; $forbidden{$_} = $_ for @forbidden_modules; # Scrub %INC, so that loaded modules disappear my ($module); for $module (@forbidden_modules) { scrub $module; }; # Move our handler to the front of the list @INC = grep { $_ ne \&fake_module } @INC; unshift @INC, \&fake_module; }; sub fake_module { my ($self,$module_file,$member_only) = @_; warn $@ if $@; my $modulename = file2module($module_file); # Deliver a faked, nonworking module if (grep { $modulename =~ $_ } keys %forbidden) { my $fh = File::Temp::tmpfile(); print $fh < and C. =begin testing no warnings 'once'; eval 'use Test::Without::Module qw( File::Temp )'; eval 'no Test::Without::Module qw( File::Temp )'; is_deeply( [keys %Test::Without::Module::forbidden],[],"Module list" ); eval { require File::Temp; }; is( $@, '', "unimport" ); =end testing =head1 BUGS =over 4 =item * There is no lexicalic scoping (yet) =back =head1 AUTHOR Max Maischein, Ecorion@cpan.orgE =head1 SEE ALSO L, L, L =cut Test-Without-Module-0.21/MANIFEST.SKIP0000755000175000017500000000024513134206462016506 0ustar corioncorion^.cvsignore ^.lwpcookies ^.releaserc ^blib/ ^Test-Without-Module-.* CVS/ ^pm_to_blib .tar.gz$ .old$ ^Makefile$ ^Releases/ ^MANIFEST.bak$ ^MYMETA.* ^.git ^.travis.ymlTest-Without-Module-0.21/Makefile.PL0000755000175000017500000000753513134206462016573 0ustar corioncorionuse ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. # Normalize version strings like 6.30_02 to 6.3002, # so that we can do numerical comparisons on it. my $eumm_version = $ExtUtils::MakeMaker::VERSION; $eumm_version =~ s/_//; my $module = 'Test::Without::Module'; (my $main_file = "lib/$module.pm") =~ s!::!/!g; (my $distname = $module) =~ s!::!-!g; my $content = do { local(*ARGV,$/)=[$main_file]; <> }; (my $main_version) = $content =~ m/ [^\n]* \$VERSION \s* = [^=] '([\d_.]+) [^\n]+ /gxms; my @tests = map {glob $_ } 't/*.t', 't/*/*.t'; my %module = ( 'NAME' => 'Test::Without::Module', 'VERSION_FROM' => 'lib/Test/Without/Module.pm', # finds $VERSION 'PREREQ_PM' => { 'Carp' => 0, }, # e.g., Module::Name => 1.1 ABSTRACT_FROM => 'lib/Test/Without/Module.pm', # retrieve abstract from module AUTHOR => 'Max Maischein ', META_MERGE => { "meta-spec" => { version => 2 }, resources => { repository => { web => 'https://github.com/Corion/test-without-module', url => 'git://github.com/Corion/test-without-module.git', type => 'git', }, bugtracker => 'http://rt.cpan.org/Public/Dist/Display.html?Name=' . $distname, license => 'http://dev.perl.org/licenses/', }, dynamic_config => 0, # we promise to keep META.* up-to-date x_static_install => 1, # we are pure Perl and don't do anything fancy provides => { $module => { file => $main_file, version => $main_version, } } }, BUILD_REQUIRES => { # Fairly long in core 'File::Find' => 0, 'File::Spec' => 0, 'Test::More' => 0, }, # Make the version metadata explicit 'LICENSE' => 'perl', ); sub get_module_info { %module } if ( ! caller ) { regen_README($main_file); #regen_EXAMPLES(); WriteMakefile1(get_module_info); } sub WriteMakefile1 { #Written by Alexandr Ciornii, version 0.21. Added by eumm-upgrade. my %params=@_; die "EXTRA_META is deprecated" if exists $params{EXTRA_META}; die "License not specified" if not exists $params{LICENSE}; if ($params{BUILD_REQUIRES} and $eumm_version < 6.5503) { #EUMM 6.5502 has problems with BUILD_REQUIRES $params{PREREQ_PM}={ %{$params{PREREQ_PM} || {}} , %{$params{BUILD_REQUIRES}} }; delete $params{BUILD_REQUIRES}; } delete $params{CONFIGURE_REQUIRES} if $eumm_version < 6.52; delete $params{MIN_PERL_VERSION} if $eumm_version < 6.48; delete $params{META_MERGE} if $eumm_version < 6.46; delete $params{META_ADD} if $eumm_version < 6.46; delete $params{LICENSE} if $eumm_version < 6.31; delete $params{AUTHOR} if $] < 5.005; delete $params{ABSTRACT_FROM} if $] < 5.005; delete $params{BINARY_LOCATION} if $] < 5.005; WriteMakefile(%params); } sub regen_README { eval { require Pod::Readme; my $parser = Pod::Readme->new(); # Read POD from Module.pm and write to README $parser->parse_from_file($_[0], 'README'); }; eval { require Pod::Markdown; my $parser = Pod::Markdown->new(); # Read POD from Module.pm and write to README $parser->parse_from_file($_[0]); open my $fh, '>', 'README.mkdn' or die "Couldn't open 'README.mkdn': $!"; print $fh <as_markdown; }; } 1; Test-Without-Module-0.21/t/0000755000175000017500000000000014345031610015043 5ustar corioncorionTest-Without-Module-0.21/t/06-missing-hidden-modules.t0000644000175000017500000000227314345031600022026 0ustar corioncorion use Test::Without::Module; use Test::More tests => 5; sub tryload { my $module = shift; my $failed = !eval "require $module; 1"; my $error = $@; $error =~ s/(\(\@INC contains: ).*\)/$1...)/; $error =~ s/\n+\z//; my $inc_status = !exists $INC{"$module.pm"} ? 'missing' : !defined $INC{"$module.pm"} ? 'undef' : !$INC{"$module.pm"} ? 'false' : '"'.$INC{"$module.pm"}.'"' ; return $failed, $error, $inc_status; } my ($failed,$error,$inc) = tryload( 'Nonexisting::Module' ); is $failed, 1, "Self-test, a non-existing module fails to load"; like $error, qr!^Can't locate Nonexisting/Module.pm in \@INC( \(you may need to install the Nonexisting::Module module\))? \(\@INC !, 'Self-test, error message shows @INC'; #diag $error; # Now, hide a module that has not been loaded: ok !$INC{'IO/Socket.pm'}, "Module 'IO/Socket.pm' has not been loaded yet"; Test::Without::Module->import('IO::Socket'); ($failed,$error,$inc) = tryload( 'IO::Socket' ); is $failed, 1, "a non-existing module fails to load"; like $error, qr!Can't locate IO/Socket.pm in \@INC( \(you may need to install the IO::Socket module\))? \(\@INC !, 'error message for hidden module shows @INC'; #diag $error; Test-Without-Module-0.21/t/embedded-Test-Without-Module.t0000755000175000017500000000344313134206462022575 0ustar corioncorion#!D:\Programme\indigoperl-5.6\bin\perl.exe -w use Test::More 'no_plan'; package Catch; sub TIEHANDLE { my($class, $var) = @_; return bless { var => $var }, $class; } sub PRINT { my($self) = shift; ${'main::'.$self->{var}} .= join '', @_; } sub OPEN {} # XXX Hackery in case the user redirects sub CLOSE {} # XXX STDERR/STDOUT. This is not the behavior we want. sub READ {} sub READLINE {} sub GETC {} sub BINMODE {} my $Original_File = 'D:lib\Test\Without\Module.pm'; package main; # pre-5.8.0's warns aren't caught by a tied STDERR. $SIG{__WARN__} = sub { $main::_STDERR_ .= join '', @_; }; tie *STDOUT, 'Catch', '_STDOUT_' or die $!; tie *STDERR, 'Catch', '_STDERR_' or die $!; SKIP: { # A header testing whether we find all prerequisites : # Check for module My::Module eval { require My::Module }; skip "Need module My::Module to run this test", 1 if $@; # Check for module Test::Without::Module eval { require Test::Without::Module }; skip "Need module Test::Without::Module to run this test", 1 if $@; # The original POD test undef $main::_STDOUT_; undef $main::_STDERR_; eval q{ my $example = sub { local $^W = 0; #line 109 lib/Test/Without/Module.pm use Test::Without::Module qw( My::Module ); # Now, loading of My::Module fails : eval { require My::Module; }; warn $@ if $@; # Now it works again eval q{ no Test::Without::Module qw( My::Module ) }; eval { require My::Module; }; print "Found My::Module" unless $@; ; } }; is($@, '', "example from line 109"); }; SKIP: { # A header testing whether we find all prerequisites : # The original POD test undef $main::_STDOUT_; undef $main::_STDERR_; }; Test-Without-Module-0.21/t/01-api.t0000755000175000017500000000012613134206462016225 0ustar corioncorion#!/usr/bin/perl -w use Test::More tests => 1; use_ok( 'Test::Without::Module' );Test-Without-Module-0.21/t/03-block-require-module.t0000755000175000017500000000153413134206462021511 0ustar corioncorion#!/usr/bin/perl -w use strict; use Symbol qw( delete_package ); use Test::More tests => 6; BEGIN { use_ok( "Test::Without::Module", qw( Digest::MD5 )); }; { use Test::Without::Module qw( Digest::MD5 ); eval { require Digest::MD5 }; use Test::Without::Module qw( Digest::MD5 ); ok( $@ ne '', "Loading raised error"); like( $@, qr!^(Can't locate Digest/MD5.pm in \@INC|Digest/MD5.pm did not return a true value at)!, "Hid module"); is_deeply( [sort keys %{Test::Without::Module::get_forbidden_list()}],[ qw[ Digest/MD5.pm ]],"Module list" ); delete_package( 'Digest::MD5' ); }; TODO: { local $TODO = 'Implement lexical scoping'; eval { require 'Digest::MD5' }; is( $@, '', "Local (require) confinement"); delete_package( 'Digest::MD5' ); eval q{ use Digest::MD5 }; is( $@, '', "Local (use) confinement"); };Test-Without-Module-0.21/t/02-block-use-module.t0000755000175000017500000000065513134206462020633 0ustar corioncorion#!/usr/bin/perl -w use strict; use Test::More tests => 4; BEGIN{ use_ok( "Test::Without::Module", qw( Digest::MD5 )); }; is_deeply( [sort keys %{Test::Without::Module::get_forbidden_list()}],[ qw[ Digest/MD5.pm ]],"Module list" ); eval q{ use Digest::MD5 }; ok( $@ ne '', 'Importing raises an error' ); like( $@, qr!^(Can't locate Digest/MD5.pm in \@INC|Digest/MD5.pm did not return a true value at)!, "Hid module");Test-Without-Module-0.21/t/05-redefine.t0000644000175000017500000000076713134206462017251 0ustar corioncorion#!/usr/bin/perl -w use strict; use Test::More tests => 1; use File::Find; my @warnings; BEGIN { $SIG{__WARN__} = sub { push @warnings, @_; }; }; use Data::Dumper; #BEGIN { diag $INC{"File/Find.pm"}; }; use Test::Without::Module qw(File::Find); #BEGIN { diag $INC{"File/Find.pm"}; }; no Test::Without::Module qw(File::Find); #diag $INC{"File/Find.pm"}; require File::Find; # diag Dumper \%INC; is_deeply "@warnings", "", "No warnings were issued upon re-allowing a module"; __END__ Test-Without-Module-0.21/t/04-import-export.t0000755000175000017500000000052713134206462020315 0ustar corioncorion#!/usr/bin/perl -w use Test::More tests => 3; use_ok( 'Test::Without::Module' ); use Test::Without::Module qw( File::Temp ); no Test::Without::Module qw( File::Temp ); is_deeply( [keys %{Test::Without::Module::get_forbidden_list()}],[],"Module list is empty" ); eval { $^W = 0; require File::Temp; }; is( $@, '', "unimport" ); Test-Without-Module-0.21/t/rt122551.t0000644000175000017500000000140714345031600016336 0ustar corioncorion#!/usr/bin/perl -w use strict; use Test::More tests => 2; BEGIN { if( ! eval { require Module::Load::Conditional; 1 }) { SKIP: { skip "Module::Load::Conditional not installed: $@", 2; }; }; }; use Test::Without::Module qw(Test::More); local $TODO = 'Module::Load::Conditional doesn\'t guard against failures in @INC hook'; my $lived = eval { my $res = Module::Load::Conditional::can_load( modules => { 'Test::More' => undef, } ); ok !$res, "We don't load Test::More"; 1; }; ok $lived or diag "Caught error $@"; diag "Test::Without::Module: $Test::Without::Module::VERSION"; diag "Module::Load::Conditional: $Module::Load::Conditional::VERSION"; done_testing; Test-Without-Module-0.21/README.mkdn0000644000175000017500000000572014345031607016422 0ustar corioncorion [![Build Status](https://travis-ci.org/Corion/test-without-module.svg?branch=master)](https://github.com/Corion/test-without-module) # NAME Test::Without::Module - Test fallback behaviour in absence of modules # SYNOPSIS use Test::Without::Module qw( My::Module ); # Now, loading of My::Module fails : eval { require My::Module; }; warn $@ if $@; # Now it works again eval q{ no Test::Without::Module qw( My::Module ) }; eval { require My::Module; }; print "Found My::Module" unless $@; # DESCRIPTION This module allows you to deliberately hide modules from a program even though they are installed. This is mostly useful for testing modules that have a fallback when a certain dependency module is not installed. ## EXPORT None. All magic is done via `use Test::Without::Module LIST` and `no Test::Without::Module LIST`. ## Test::Without::Module::get\_forbidden\_list This function returns a reference to a copy of the current hash of forbidden modules or an empty hash if none are currently forbidden. This is convenient if you are testing and/or debugging this module. # ONE LINER A neat trick for using this module from the command line was mentioned to me by NUFFIN and by Jerrad Pierce: perl -MTest::Without::Module=Some::Module -w -Iblib/lib t/SomeModule.t That way, you can easily see how your module or test file behaves when a certain module is unavailable. # BUGS - There is no lexical scoping # CREDITS Much improvement must be thanked to Aristotle from PerlMonks, he pointed me to a much less convoluted way to fake a module at [https://perlmonks.org?node=192635](https://perlmonks.org?node=192635). I also discussed with him an even more elegant way of overriding CORE::GLOBAL::require, but the parsing of the overridden subroutine didn't work out the way I wanted it - CORE::require didn't recognize barewords as such anymore. NUFFIN and Jerrad Pierce pointed out the convenient use from the command line to interactively watch the behaviour of the test suite and module in absence of a module. # AUTHOR Copyright (c) 2003-2014 Max Maischein, # LICENSE This module is released under the same terms as Perl itself. # REPOSITORY The public repository of this module is [https://github.com/Corion/test-without-module](https://github.com/Corion/test-without-module). # SUPPORT The public support forum of this module is [https://perlmonks.org/](https://perlmonks.org/). # BUG TRACKER Please report bugs in this module via the RT CPAN bug queue at [https://rt.cpan.org/Public/Dist/Display.html?Name=Test-Without-Module](https://rt.cpan.org/Public/Dist/Display.html?Name=Test-Without-Module) or via mail to [test-without-module-Bugs@rt.cpan.org](https://metacpan.org/pod/test-without-module-Bugs%40rt.cpan.org). # SEE ALSO [Devel::Hide](https://metacpan.org/pod/Devel%3A%3AHide), [Acme::Intraweb](https://metacpan.org/pod/Acme%3A%3AIntraweb), [PAR](https://metacpan.org/pod/PAR), [perlfunc](https://metacpan.org/pod/perlfunc) Test-Without-Module-0.21/META.yml0000644000175000017500000000147214345031610016055 0ustar corioncorion--- abstract: 'Test fallback behaviour in absence of modules' author: - 'Max Maischein ' build_requires: File::Find: '0' File::Spec: '0' Test::More: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 0 generated_by: 'ExtUtils::MakeMaker version 7.44, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Test-Without-Module no_index: directory: - t - inc provides: Test::Without::Module: file: lib/Test/Without/Module.pm version: '0.21' requires: Carp: '0' resources: license: http://dev.perl.org/licenses/ repository: git://github.com/Corion/test-without-module.git version: '0.21' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' x_static_install: 1 Test-Without-Module-0.21/lib/0000755000175000017500000000000014345031610015346 5ustar corioncorionTest-Without-Module-0.21/lib/Test/0000755000175000017500000000000014345031610016265 5ustar corioncorionTest-Without-Module-0.21/lib/Test/Without/0000755000175000017500000000000014345031610017730 5ustar corioncorionTest-Without-Module-0.21/lib/Test/Without/Module.pm0000755000175000017500000001016114345031600021514 0ustar corioncorionpackage Test::Without::Module; use strict; use Carp qw( croak ); use vars qw( $VERSION ); $VERSION = '0.21'; use vars qw(%forbidden); sub get_forbidden_list { \%forbidden }; sub import { my ($self,@forbidden_modules) = @_; my $forbidden = get_forbidden_list; for (@forbidden_modules) { my $file = module2file($_); $forbidden->{$file} = delete $INC{$file}; }; # Scrub %INC, so that loaded modules disappear for my $module (@forbidden_modules) { scrub( $module ); }; @INC = (\&fake_module, grep { !ref || $_ != \&fake_module } @INC); }; sub fake_module { my ($self,$module_file,$member_only) = @_; # Don't touch $@, or .al files will not load anymore???? if (exists get_forbidden_list->{$module_file}) { my $module_name = file2module($module_file); croak "Can't locate $module_file in \@INC (you may need to install the $module_name module) (\@INC contains: @INC)"; }; }; sub unimport { my ($self,@list) = @_; my $module; my $forbidden = get_forbidden_list; for $module (@list) { my $file = module2file($module); if (exists $forbidden->{$file}) { my $path = delete $forbidden->{$file}; if (defined $path) { $INC{ $file } = $path; } } else { croak "Can't allow non-forbidden module $module"; }; }; }; sub file2module { my ($mod) = @_; $mod =~ s!/!::!g; $mod =~ s!\.pm$!!; $mod; }; sub module2file { my ($mod) = @_; $mod =~ s!::|'!/!g; $mod .= ".pm"; $mod; }; sub scrub { my ($module) = @_; delete $INC{module2file($module)}; }; 1; =head1 NAME Test::Without::Module - Test fallback behaviour in absence of modules =head1 SYNOPSIS use Test::Without::Module qw( My::Module ); # Now, loading of My::Module fails : eval { require My::Module; }; warn $@ if $@; # Now it works again eval q{ no Test::Without::Module qw( My::Module ) }; eval { require My::Module; }; print "Found My::Module" unless $@; =head1 DESCRIPTION This module allows you to deliberately hide modules from a program even though they are installed. This is mostly useful for testing modules that have a fallback when a certain dependency module is not installed. =head2 EXPORT None. All magic is done via C and C. =head2 Test::Without::Module::get_forbidden_list This function returns a reference to a copy of the current hash of forbidden modules or an empty hash if none are currently forbidden. This is convenient if you are testing and/or debugging this module. =cut =head1 ONE LINER A neat trick for using this module from the command line was mentioned to me by NUFFIN and by Jerrad Pierce: perl -MTest::Without::Module=Some::Module -w -Iblib/lib t/SomeModule.t That way, you can easily see how your module or test file behaves when a certain module is unavailable. =head1 BUGS =over 4 =item * There is no lexical scoping =back =head1 CREDITS Much improvement must be thanked to Aristotle from PerlMonks, he pointed me to a much less convoluted way to fake a module at L. I also discussed with him an even more elegant way of overriding CORE::GLOBAL::require, but the parsing of the overridden subroutine didn't work out the way I wanted it - CORE::require didn't recognize barewords as such anymore. NUFFIN and Jerrad Pierce pointed out the convenient use from the command line to interactively watch the behaviour of the test suite and module in absence of a module. =head1 AUTHOR Copyright (c) 2003-2014 Max Maischein, Ecorion@cpan.orgE =head1 LICENSE This module is released under the same terms as Perl itself. =head1 REPOSITORY The public repository of this module is L. =head1 SUPPORT The public support forum of this module is L. =head1 BUG TRACKER Please report bugs in this module via the RT CPAN bug queue at L or via mail to L. =head1 SEE ALSO L, L, L, L =cut __END__ Test-Without-Module-0.21/META.json0000644000175000017500000000265714345031610016233 0ustar corioncorion{ "abstract" : "Test fallback behaviour in absence of modules", "author" : [ "Max Maischein " ], "dynamic_config" : 0, "generated_by" : "ExtUtils::MakeMaker version 7.44, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Test-Without-Module", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "File::Find" : "0", "File::Spec" : "0", "Test::More" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Carp" : "0" } } }, "provides" : { "Test::Without::Module" : { "file" : "lib/Test/Without/Module.pm", "version" : "0.21" } }, "release_status" : "stable", "resources" : { "license" : [ "http://dev.perl.org/licenses/" ], "repository" : { "type" : "git", "url" : "git://github.com/Corion/test-without-module.git", "web" : "https://github.com/Corion/test-without-module" } }, "version" : "0.21", "x_serialization_backend" : "JSON::PP version 4.11", "x_static_install" : 1 } Test-Without-Module-0.21/xt/0000755000175000017500000000000014345031610015233 5ustar corioncorionTest-Without-Module-0.21/xt/99-todo.t0000644000175000017500000000202713134206462016631 0ustar corioncorionuse Test::More; use File::Spec; use File::Find; use strict; # Check that all files do not contain any # lines with "XXX" - such markers should # either have been converted into Todo-stuff # or have been resolved. # The test was provided by Andy Lester. my @files; my $blib = File::Spec->catfile(qw(blib lib)); find(\&wanted, grep { -d } ($blib, 'bin')); plan tests => 2* @files; foreach my $file (@files) { source_file_ok($file); } sub wanted { push @files, $File::Find::name if /\.p(l|m|od)$/; } sub source_file_ok { my $file = shift; open( my $fh, "<$file" ) or die "Can't open $file: $!"; my @lines = <$fh>; close $fh; my $n = 0; for ( @lines ) { ++$n; s/^/$file ($n): /; } my @x = grep /XXX/, @lines; if ( !is( scalar @x, 0, "Looking for XXXes in $file" ) ) { diag( $_ ) for @x; } @x = grep /<<<|>>>/, @lines; if ( !is( scalar @x, 0, "Looking for <<<<|>>>> in $file" ) ) { diag( $_ ) for @x; } } Test-Without-Module-0.21/xt/99-changes.t0000644000175000017500000000142214345031600017265 0ustar corioncorion#!perl -w use warnings; use strict; use File::Find; use Test::More tests => 2; =head1 PURPOSE This test ensures that the Changes file mentions the current version and that a release date is mentioned as well =cut use lib '.'; use vars '%module'; require 'Makefile.PL'; # Loaded from Makefile.PL %module = get_module_info(); my $module = $module{NAME}; my $file = $module{ VERSION_FROM }; require $file; my $version = sprintf '%0.2f', $module->VERSION; my $changes = do { local $/; open my $fh, 'Changes' or die $!; <$fh> }; ok $changes =~ /^(.*$version.*)$/m, "We find version $version for $module"; my $changes_line = $1; ok $changes_line =~ /$version\s+20\d{2}-\d{2}-\d{2}/, "We find a release date on the same line" or diag $changes_line; Test-Without-Module-0.21/xt/99-manifest.t0000644000175000017500000000154613134206462017477 0ustar corioncorionuse strict; use Test::More; # Check that MANIFEST and MANIFEST.skip are sane : use File::Find; use File::Spec; my @files = qw( MANIFEST MANIFEST.SKIP ); plan tests => scalar @files * 4 +1 # MANIFEST existence check ; for my $file (@files) { ok(-f $file, "$file exists"); open F, "<$file" or die "Couldn't open $file : $!"; my @lines = ; is_deeply([grep(/^$/, @lines)],[], "No empty lines in $file"); is_deeply([grep(/^\s+$/, @lines)],[], "No whitespace-only lines in $file"); is_deeply([grep(/^\s*\S\s+$/, @lines)],[],"No trailing whitespace on lines in $file"); if ($file eq 'MANIFEST') { chomp @lines; is_deeply([grep { s/\s.*//; ! -f } @lines], [], "All files in $file exist") or do { diag "$_ is mentioned in $file but doesn't exist on disk" for grep { ! -f } @lines }; }; close F; }; Test-Without-Module-0.21/xt/99-unix-text.t0000644000175000017500000000140413134206462017627 0ustar corioncorionuse Test::More; # Check that all released module files are in # UNIX text format use File::Spec; use File::Find; use strict; my @files; my $blib = File::Spec->catfile(qw(blib lib)); find(\&wanted, grep { -d } ($blib, 'bin')); plan tests => scalar @files; foreach my $file (@files) { unix_file_ok($file); } sub wanted { push @files, $File::Find::name if /\.p(l|m|od)$/; } sub unix_file_ok { my ($filename) = @_; local $/; open F, "< $filename" or die "Couldn't open '$filename' : $!\n"; binmode F; my $content = ; my $i; my @lines = grep { /\x0D\x0A$/sm } map { sprintf "%s: %s\x0A", $i++, $_ } split /\x0A/, $content; unless (is(scalar @lines, 0,"'$filename' contains no windows newlines")) { diag $_ for @lines; }; close F; }; Test-Without-Module-0.21/xt/99-pod.t0000644000175000017500000000123213134206462016443 0ustar corioncorionuse Test::More; # Check our Pod # The test was provided by Andy Lester, # who stole it from Brian D. Foy # Thanks to both ! use File::Spec; use File::Find; use strict; eval { require Test::Pod; Test::Pod->import; }; my @files; if ($@) { plan skip_all => "Test::Pod required for testing POD"; } elsif ($Test::Pod::VERSION < 0.95) { plan skip_all => "Test::Pod 0.95 required for testing POD"; } else { my $blib = File::Spec->catfile(qw(blib lib)); find(\&wanted, grep { -d } ($blib, 'bin')); plan tests => scalar @files; foreach my $file (@files) { pod_file_ok($file); } } sub wanted { push @files, $File::Find::name if /\.p(l|m|od)$/; } Test-Without-Module-0.21/xt/99-compile.t0000644000175000017500000000140613134206462017314 0ustar corioncorion#!perl -w use warnings; use strict; use File::Find; use Test::More; BEGIN { eval 'use Capture::Tiny ":all"; 1'; if ($@) { plan skip_all => "Capture::Tiny needed for testing"; exit 0; }; }; plan 'no_plan'; my $last_version = undef; sub check { return if (! m{(\.pm|\.pl|\.psgi) \z}xmsi); my ($stdout, $stderr, $exit) = capture(sub { system( $^X, '-Mblib', '-wc', $_ ); }); s!\s*\z!! for ($stdout, $stderr); if( $exit ) { diag $exit; fail($_); } elsif( $stderr ne "$_ syntax OK") { diag $stderr; fail($_); } else { pass($_); }; } find({wanted => \&check, no_chdir => 1}, grep { -d $_ } 'blib', 'scripts', 'examples', 'bin', 'lib' ); Test-Without-Module-0.21/xt/99-synopsis.t0000644000175000017500000000327013134206462017554 0ustar corioncorionuse strict; use Test::More; use File::Spec; use File::Find; use File::Temp 'tempfile'; use Getopt::Long; GetOptions( 'verbose' => \my $verbose, ); my @files; my $blib = File::Spec->catfile(qw(blib lib)); find(\&wanted, grep { -d } ($blib, 'bin', 'scripts', 'examples')); plan tests => scalar @files; foreach my $file (@files) { synopsis_file_ok($file); } sub wanted { push @files, $File::Find::name if /\.p(l|m|od)$/; } sub synopsis_file_ok { my( $file ) = @_; my $name = "SYNOPSIS in $file compiles"; open my $fh, '<', $file or die "Couldn't read '$file': $!"; my $start; my $line = 0; my @synopsis = map { s!^\s\s!!; $_ } # outdent all code for here-docs grep { /^\s\s/ } # extract all verbatim (=code) stuff grep { /^=head1\s+SYNOPSIS$/.../^=/ } # extract Pod synopsis map { $line++; /^=head1\s+SYNOPSIS$/ and $start = $line+1; $_ } # remember start of synopsis <$fh>; if( $verbose ) { diag $_ for @synopsis }; if( @synopsis ) { my($tmpfh,$tempname) = tempfile(); print {$tmpfh} qq(#line $start "$file"\n); print {$tmpfh} join '', @synopsis; close $tmpfh; # flush it my $output = `$^X -Ilib -c $tempname 2>&1`; if( $output =~ /\ssyntax OK$/ ) { pass $name; } else { fail $name; diag $output; diag $_ for @synopsis; }; unlink $tempname or warn "Couldn't clean up $tempname: $!"; } else { SKIP: { skip "$file has no SYNOPSIS section", 1; }; }; }Test-Without-Module-0.21/xt/99-versions.t0000644000175000017500000000242213134206462017533 0ustar corioncorion#!perl -w # Stolen from ChrisDolan on use.perl.org # http://use.perl.org/comments.pl?sid=29264&cid=44309 use warnings; use strict; use File::Find; use Test::More; BEGIN { eval 'use File::Slurp; 1'; if ($@) { plan skip_all => "File::Slurp needed for testing"; exit 0; }; }; plan 'no_plan'; my $last_version = undef; sub check { return if (! m{blib/script/}xms && ! m{\.pm \z}xms); my $content = read_file($_); # only look at perl scripts, not sh scripts return if (m{blib/script/}xms && $content !~ m/\A \#![^\r\n]+?perl/xms); my @version_lines = $content =~ m/ ( [^\n]* \$VERSION \s* = [^=] [^\n]* ) /gxms; if (@version_lines == 0) { fail($_); } for my $line (@version_lines) { $line =~ s/^\s+//; $line =~ s/\s+$//; if (!defined $last_version) { $last_version = shift @version_lines; diag "Checking for $last_version"; pass($_); } else { is($line, $last_version, $_); } } } find({wanted => \&check, no_chdir => 1}, 'blib'); if (! defined $last_version) { fail('Failed to find any files with $VERSION'); } Test-Without-Module-0.21/xt/meta-lint.t0000644000175000017500000000164713134206462017326 0ustar corioncorion#!perl -w # Stolen from ChrisDolan on use.perl.org # http://use.perl.org/comments.pl?sid=29264&cid=44309 use warnings; use strict; use File::Find; use Test::More tests => 4; use Parse::CPAN::Meta; use CPAN::Meta::Validator; use lib '.'; use vars '%module'; require 'Makefile.PL'; # Loaded from Makefile.PL %module = get_module_info(); my $module = $module{NAME}; (my $file = $module) =~ s!::!/!g; require "$file.pm"; my $version = sprintf '%0.2f', $module->VERSION; for my $meta_file ('META.yml', 'META.json') { my $meta = Parse::CPAN::Meta->load_file($meta_file); my $cmv = CPAN::Meta::Validator->new( $meta ); if(! ok $cmv->is_valid, "$meta_file is valid" ) { diag $_ for $cmv->errors; }; # Also check that the declared version matches the version in META.* is $meta->{version}, $version, "$meta_file version matches module version ($version)"; }; Test-Without-Module-0.21/Changes0000755000175000017500000000576114345031600016106 0ustar corioncorionRevision history for Perl extension Test::Without::Module. Todo: - Add way to allow only core modules (suggested by SREZIC) 0.21 2022-12-10 - Harden test suite against changed error message coming with 5.38 0.20 2017-04-09 - Make tests more resilient against old versions of Carp.pm (Paul Howarth) Also addresses https://rt.cpan.org/Public/Bug/Display.html?id=121002 - Typo fixes (Paul Howarth) - Rework test suite, move author tests below xt/ 0.19 2017-03-30 - Make error message more like the original Perl error message (haarg) - Makefile.PL overhaul (by Abzal Serekov) - Generate README.md 0.18 2014-08-30 - Eliminate segfault on Perl 5.8 (by Graham Knop, Karen Etheridge, RT 98207) - Only add the @INC hook once (by Graham Knop, RT #91857) - Updated documentation 0.17 2009-01-18 - Made license explicit in the metadata (for those versions of EU:MM that support it) ! Changed module comparison from using a regular expression to string equality. That means that ugly actions at a distance by matching substrings shouldn't happen. 0.16 2008-10-20 - Add a test that exhibits the "redefined" warnings upon disallowing and then reallowing a module - Fixed the code so no more warnings get issued. [RT#40065] 0.15 2007-10-21 - Now really restored compatibility of the tests with Perl 5.005, using lexical filehandles does not work under 5.005... . No functional changes, no upgrade is necessary 0.14 2007-10-20 - Hopefully restored compatibility of the tests with Perl 5.005 this time, reported by SREZIC . No functional changes, no upgrade is necessary 0.13 2007-10-20 - Restored compatibility of the tests with Perl 5.005, reported by SREZIC . No functional changes, no upgrade is necessary 0.12 2007-10-20 - Fixed RT [rt.cpan.org #24735], reported by Slaven Rezic This seems to have been some really weird interaction or maybe eating of $@. 0.11 2007-08-19 - File::Slurp is now no prerequisite anymore - the test requiring it is now optional 0.10 2007-08-04 - Added missing File::Slurp prerequisite. Thanks to ANDK and the CPAN smoke testers to alert me to this - No functional changes. No upgrade from 0.09 is necessary. 0.09 2006-12-31 - Added more consistency tests - Added Jerrad Pierce to the list of those who mentioned the command line use. I forgot the usage NUFFIN had given, so both were genuinely new to me. 0.08 2006-12-29 - Removed superfluous File::Temp usage from module 0.07 2006-12-29 - No more inline pod tests - No code changes since 0.06 0.06 2003-03-20 - fixed the inline test pod - Now needs Test::Pod 0.95 (or skips the pod test) 0.03 2003-02-17 - Released on CPAN - Fixes so the pod for the fake module doesn't appear in the main pod. 0.02 2003-02-16 - Released on CPAN 0.01 Sat Feb 15 21:14:45 2003 - original version; created by h2xs 1.21 with options -Xn Test::Without::Module Test-Without-Module-0.21/README0000755000175000017500000000507414345031607015477 0ustar corioncorionNAME Test::Without::Module - Test fallback behaviour in absence of modules SYNOPSIS use Test::Without::Module qw( My::Module ); # Now, loading of My::Module fails : eval { require My::Module; }; warn $@ if $@; # Now it works again eval q{ no Test::Without::Module qw( My::Module ) }; eval { require My::Module; }; print "Found My::Module" unless $@; DESCRIPTION This module allows you to deliberately hide modules from a program even though they are installed. This is mostly useful for testing modules that have a fallback when a certain dependency module is not installed. EXPORT None. All magic is done via use Test::Without::Module LIST and no Test::Without::Module LIST. Test::Without::Module::get_forbidden_list This function returns a reference to a copy of the current hash of forbidden modules or an empty hash if none are currently forbidden. This is convenient if you are testing and/or debugging this module. ONE LINER A neat trick for using this module from the command line was mentioned to me by NUFFIN and by Jerrad Pierce: perl -MTest::Without::Module=Some::Module -w -Iblib/lib t/SomeModule.t That way, you can easily see how your module or test file behaves when a certain module is unavailable. BUGS * There is no lexical scoping CREDITS Much improvement must be thanked to Aristotle from PerlMonks, he pointed me to a much less convoluted way to fake a module at https://perlmonks.org?node=192635. I also discussed with him an even more elegant way of overriding CORE::GLOBAL::require, but the parsing of the overridden subroutine didn't work out the way I wanted it - CORE::require didn't recognize barewords as such anymore. NUFFIN and Jerrad Pierce pointed out the convenient use from the command line to interactively watch the behaviour of the test suite and module in absence of a module. AUTHOR Copyright (c) 2003-2014 Max Maischein, LICENSE This module is released under the same terms as Perl itself. REPOSITORY The public repository of this module is https://github.com/Corion/test-without-module. SUPPORT The public support forum of this module is https://perlmonks.org/. BUG TRACKER Please report bugs in this module via the RT CPAN bug queue at https://rt.cpan.org/Public/Dist/Display.html?Name=Test-Without-Module or via mail to test-without-module-Bugs@rt.cpan.org. SEE ALSO Devel::Hide, Acme::Intraweb, PAR, perlfunc Test-Without-Module-0.21/MANIFEST0000644000175000017500000000067313134206462015743 0ustar corioncorionChanges lib/Test/Without/Module.pm Makefile.PL MANIFEST MANIFEST.SKIP META.json META.yml README README.mkdn t/01-api.t t/02-block-use-module.t t/03-block-require-module.t t/04-import-export.t t/05-redefine.t t/06-missing-hidden-modules.t t/embedded-Test-Without-Module.t t/rt122551.t Texts/article.txt xt/99-changes.t xt/99-compile.t xt/99-manifest.t xt/99-pod.t xt/99-synopsis.t xt/99-todo.t xt/99-unix-text.t xt/99-versions.t xt/meta-lint.t