Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

A Better 't/00-load.t'

by Ovid (Cardinal)
on Oct 03, 2007 at 08:02 UTC ( #642327=sourcecode: print w/ replies, xml ) Need Help??

Category: Testing
Author/Contact Info Ovid
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"
+);
}

Comment on A Better 't/00-load.t'
Download Code

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://642327]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-07-26 11:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls