I've probably posted this before, but it's just here so I google it later and find it easily. Note that one difference between this and some other Test::Class driver scripts is that this script assumes that all packages in TEST_DIR are test class packages which should be loaded. Thus, I don't have to have a bunch of "use" lines in this script and forget when I've added another class.
Also, note that the else condition allows me to put the following in my .vimrc:
noremap ,T :!perl t/test_class_tests.t %
That lets me run a test class while I'm editing it.
#!/usr/bin/perl
use strict;
use warnings;
use File::Find::Rule;
use UNIVERSAL::require;
use constant TEST_DIR => 't/tests/';
use lib 'lib', 't/lib/', TEST_DIR;
sub file_to_module ($) {
my $file = shift;
$file =~ s/^@{[TEST_DIR]}//;
$file =~ s/\.pm$//;
$file =~ s{/}{::}g;
warn $file, $/;
return $file;
}
BEGIN {
# tests must be loaded in a begin block or they will not be run.
unless (@ARGV) {
foreach
my $file ( File::Find::Rule->file->name('*.pm')->in(TEST_DIR) )
{
( file_to_module $file )->require or die $@;
}
}
else {
my $file = shift;
unless ( -e $file ) {
die "Could not locate file ($file)";
}
( file_to_module $file )->require or die $@;
}
}
Test::Class->runtests;
First of all: unless? What were you thinking?
Secondly: sticking the TEST_DIR removal in file_to_module seems like a violation of separatation of concerns. Yeah, I know this script is small, but it still bugged me when I did it in a small script of my own. My suggestion would be to fix it the same way as I did:
use File::Spec::Functions qw( abs2rel );
#...
# file_to_module which does not concern itself with TEST_DIR
#...
( file_to_module abs2rel $file, TEST_DIR )->require or die $@;
The script could generally stand some File::Spec attention…