From: Vincent Pit Date: Mon, 20 Aug 2012 17:03:06 +0000 (+0200) Subject: Update how author tests load their dependencies X-Git-Tag: rt80388~7 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=82dde567fe6ace7774ceec15edeb513dc5f287e8;p=perl%2Fmodules%2FVariable-Magic.git Update how author tests load their dependencies Also make sure that the kwalitee test unlinks its byproducts. --- diff --git a/MANIFEST b/MANIFEST index 8d8762f..9887264 100644 --- a/MANIFEST +++ b/MANIFEST @@ -41,8 +41,10 @@ t/41-clone.t t/80-leaks.t t/91-pod.t t/92-pod-coverage.t +t/93-pod-spelling.t t/95-portability-files.t t/99-kwalitee.t +t/lib/VPIT/TestHelpers.pm t/lib/Variable/Magic/TestDestroyRequired.pm t/lib/Variable/Magic/TestGlobalDestruction.pm t/lib/Variable/Magic/TestScopeEnd.pm diff --git a/t/16-huf.t b/t/16-huf.t index af9e740..0f9215c 100644 --- a/t/16-huf.t +++ b/t/16-huf.t @@ -7,22 +7,17 @@ use Test::More; use Variable::Magic qw; -if (!VMG_UVAR) { - plan skip_all => 'No nice uvar magic for this perl'; -} - -{ - local $@; - if (eval { require Hash::Util::FieldHash; 1 }) { - plan tests => 2 * 5 + 7 + 1; - defined and diag "Using Hash::Util::FieldHash $_" - for $Hash::Util::FieldHash::VERSION; - } else { - plan skip_all => 'Hash::Util::FieldHash required for testing uvar interaction' - } +use lib 't/lib'; +use VPIT::TestHelpers; + +if (VMG_UVAR) { + load_or_skip('Hash::Util::FieldHash', undef, [ ], + 'required for testing uvar interaction'); + plan tests => 2 * 5 + 7 + 1; +} else { + skip_all 'No nice uvar magic for this perl'; } -use lib 't/lib'; use Variable::Magic::TestWatcher; my $wiz = init_watcher [ qw ], 'huf'; diff --git a/t/34-glob.t b/t/34-glob.t index 1fe3df7..2e1a441 100644 --- a/t/34-glob.t +++ b/t/34-glob.t @@ -5,14 +5,14 @@ use warnings; use Test::More; +use lib 't/lib'; +use VPIT::TestHelpers; + BEGIN { - local $@; - if (eval "use Symbol qw; 1") { - plan tests => 2 * 17 + 1; - defined and diag "Using Symbol $_" for $Symbol::VERSION; - } else { - plan skip_all => "Symbol::gensym required for testing magic for globs"; - } + load_or_skip('Symbol', undef, [ 'gensym' ], + 'required for testing magic for globs'); + + plan tests => 2 * 17 + 1; } use Variable::Magic qw; diff --git a/t/91-pod.t b/t/91-pod.t index f1e1d3e..3b6c320 100644 --- a/t/91-pod.t +++ b/t/91-pod.t @@ -5,6 +5,12 @@ use warnings; use Test::More; -eval "use Test::Pod 1.14"; -plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; +use lib 't/lib'; +use VPIT::TestHelpers; + +load_or_skip('Test::Pod', '1.14', [ ], + 'required for testing POD syntax'); + +eval 'use Test::Pod'; # Make Kwalitee test happy + all_pod_files_ok(); diff --git a/t/92-pod-coverage.t b/t/92-pod-coverage.t index 2f7bde2..4ee6317 100644 --- a/t/92-pod-coverage.t +++ b/t/92-pod-coverage.t @@ -5,6 +5,12 @@ use warnings; use Test::More; -eval "use Test::Pod::Coverage 1.04"; -plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; +use lib 't/lib'; +use VPIT::TestHelpers; + +load_or_skip('Test::Pod::Coverage', '1.04', [ ], + 'required for testing POD coverage'); + +eval 'use Test::Pod::Coverage'; # Make Kwalitee test happy + all_pod_coverage_ok( { also_private => [ qr/^_/, qr/^CLONE(_SKIP)?$/ ] } ); diff --git a/t/93-pod-spelling.t b/t/93-pod-spelling.t new file mode 100644 index 0000000..acb2587 --- /dev/null +++ b/t/93-pod-spelling.t @@ -0,0 +1,14 @@ +#!perl -T + +use strict; +use warnings; + +use Test::More; + +use lib 't/lib'; +use VPIT::TestHelpers; + +load_or_skip('Test::Pod::Spelling::CommonMistakes', '1.0', [ ], + 'required for testing POD spelling'); + +all_pod_files_ok(); diff --git a/t/95-portability-files.t b/t/95-portability-files.t index ab541f3..55a9005 100644 --- a/t/95-portability-files.t +++ b/t/95-portability-files.t @@ -5,6 +5,10 @@ use warnings; use Test::More; -eval "use Test::Portability::Files"; -plan skip_all => "Test::Portability::Files required for testing filenames portability" if $@; +use lib 't/lib'; +use VPIT::TestHelpers; + +load_or_skip('Test::Portability::Files', undef, [ ], + 'required for testing filenames portability'); + run_tests(); diff --git a/t/99-kwalitee.t b/t/99-kwalitee.t index 4a66457..a448a64 100644 --- a/t/99-kwalitee.t +++ b/t/99-kwalitee.t @@ -5,14 +5,18 @@ use warnings; use Test::More; -eval { require Parse::RecDescent; 'Parse::RecDescent'->VERSION('1.967006') } - or plan skip_all => 'Parse::RecDescent version 1.967006 or greater required'; +use lib 't/lib'; +use VPIT::TestHelpers; -eval { require Module::ExtractUse; 'Module::ExtractUse'->VERSION('0.24') } - or plan skip_all => 'Module::ExtractUse version 0.24 or greater required'; +my $guard = VPIT::TestHelpers::Guard->new( + sub { unlink for glob 'Debian_CPANTS.txt*' } +); -eval { require Test::Kwalitee; 1 } - or plan skip_all => 'Test::Kwalitee required'; +my $desc = 'required to test kwalitee'; + +load_or_skip('Parse::RecDescent', '1.967006', undef, $desc); +load_or_skip('Module::ExtractUse', '0.24', undef, $desc); +load_or_skip('Test::Kwalitee', undef, undef, $desc); SKIP: { eval { Test::Kwalitee->import(); }; diff --git a/t/lib/VPIT/TestHelpers.pm b/t/lib/VPIT/TestHelpers.pm new file mode 100644 index 0000000..18f2b17 --- /dev/null +++ b/t/lib/VPIT/TestHelpers.pm @@ -0,0 +1,68 @@ +package VPIT::TestHelpers; + +use strict; +use warnings; + +my %exports = ( + load_or_skip => \&load_or_skip, + skip_all => \&skip_all, +); + +sub import { + my $pkg = caller; + while (my ($name, $code) = each %exports) { + no strict 'refs'; + *{$pkg.'::'.$name} = $code; + } +} + +sub skip_all { + my ($msg) = @_; + require Test::More; + Test::More::plan(skip_all => $msg); +} + +sub diag { + require Test::More; + Test::More::diag($_) for @_; +} + +our $TODO; +local $TODO; + +sub load_or_skip { + my ($pkg, $ver, $imports, $desc) = @_; + my $spec = $ver && $ver !~ /^[0._]*$/ ? "$pkg $ver" : $pkg; + local $@; + if (eval "use $spec (); 1") { + $ver = do { no strict 'refs'; ${"${pkg}::VERSION"} }; + $ver = 'undef' unless defined $ver; + if ($imports) { + my @imports = @$imports; + my $caller = (caller 0)[0]; + local $@; + my $res = eval <<"IMPORTER"; +package + $caller; +BEGIN { \$pkg->import(\@imports) } +1; +IMPORTER + skip_all "Could not import '@imports' from $pkg $ver: $@" unless $res; + } + diag "Using $pkg $ver"; + } else { + skip_all "$spec $desc"; + } +} + +package VPIT::TestHelpers::Guard; + +sub new { + my ($class, $code) = @_; + + bless { code => $code }, $class; +} + +sub DESTROY { $_[0]->{code}->() } + +1; diff --git a/t/lib/Variable/Magic/TestThreads.pm b/t/lib/Variable/Magic/TestThreads.pm index f3ebc5d..a11bae7 100644 --- a/t/lib/Variable/Magic/TestThreads.pm +++ b/t/lib/Variable/Magic/TestThreads.pm @@ -7,11 +7,7 @@ use Config qw<%Config>; use Variable::Magic qw; -sub skipall { - my ($msg) = @_; - require Test::More; - Test::More::plan(skip_all => $msg); -} +use VPIT::TestHelpers; sub diag { require Test::More; @@ -21,31 +17,17 @@ sub diag { sub import { shift; - skipall 'This Variable::Magic isn\'t thread safe' unless VMG_THREADSAFE; + skip_all 'This Variable::Magic isn\'t thread safe' unless VMG_THREADSAFE; my $force = $ENV{PERL_VARIABLE_MAGIC_TEST_THREADS} ? 1 : !1; - skipall 'This perl wasn\'t built to support threads' + skip_all 'This perl wasn\'t built to support threads' unless $Config{useithreads}; - skipall 'perl 5.13.4 required to test thread safety' + skip_all 'perl 5.13.4 required to test thread safety' unless $force or "$]" >= 5.013_004; - my $t_v = $force ? '0' : '1.67'; - my $has_threads = do { - local $@; - eval "use threads $t_v; 1"; - }; - skipall "threads $t_v required to test thread safety" unless $has_threads; - - my $ts_v = $force ? '0' : '1.14'; - my $has_threads_shared = do { - local $@; - eval "use threads::shared $ts_v; 1"; - }; - skipall "threads::shared $ts_v required to test thread safety" - unless $has_threads_shared; - - defined and diag "Using threads $_" for $threads::VERSION; - defined and diag "Using threads::shared $_" for $threads::shared::VERSION; + my $desc = 'required to test thread safety'; + load_or_skip('threads', $force ? '0' : '1.67', [ ], $desc); + load_or_skip('threads::shared', $force ? '0' : '1.14', [ ], $desc); my %exports = ( spawn => \&spawn,