Description: |
In test suites, you often see a test program named something like t/00-load.t. The leading '00' generally ensures that this test is run before others. Usually this test is designed to load all of your packages and ensure that they compile. There's no point in continuing testing if they don't. Unfortunately, it's easy to forget to add a new module to the list if you forget about it. I do that all the time and I hate that.
The following 'improved' load tester has the following features:
- Finds and loads your modules automatically
- Is taint safe
- Should run on Perls as old as 5.004 (I think)
- Cross-platform
- Calculates the test plan for you
- Optionally verifies that all packages have same version number
|
#!/usr/bin/perl -wT
use strict;
use File::Spec;
use File::Find;
use Test::More;
use constant DISTRIBUTION => 'TAP::Parser';
# Set this to true if all packages have the same version number
use constant CHECK_VERSION => 1;
sub file_to_pm {
my ( $dir, $file ) = @_;
$file =~ s/\.pm$// || return; # we only care about .pm files
$file =~ s{\\}{/}g; # to make win32 happy
$dir =~ s{\\}{/}g; # to make win32 happy
$file =~ s/^$dir//;
my $_package = join '::' => grep $_ => File::Spec->splitdir($file)
+;
# untaint that puppy!
my ($package) = $_package =~ /^([\w]+(?:::[\w]+)*)$/;
return DISTRIBUTION eq $package ? () : $package;
}
BEGIN {
my $dir = 'lib';
my @classes;
find(
{ no_chdir => 1, # keeps it taint safe
wanted => sub {
-f && /\.pm$/
&& push @classes => file_to_pm( $dir, $File::Find::n
+ame );
}
},
$dir,
);
my $tests_per_class = CHECK_VERSION ? 2 : 1;
plan tests => $tests_per_class + $tests_per_class * @classes;
foreach my $class ( DISTRIBUTION, sort @classes ) {
use_ok $class or BAIL_OUT("Could not load $class");
if (CHECK_VERSION) {
is $class->VERSION, DISTRIBUTION->VERSION,
"... and $class should have the correct version";
}
}
diag("Testing Test::Harness $Test::Harness::VERSION, Perl $], $^X"
+);
}
|