Test::Class and FindBin

I'm on a new team at the BBC and I was rather curious to note that I couldn't run the Test::Class tests by simply doing a prove -lv t/lib/Path/To/Test.pm. A bit of research reveals the culprit is FindBin, a module I've never been terribly happy with. Seems we have configuration information located relative to the $FindBin variable that module sets.

package Dynamite::Test::FindBin;

my $EXECUTABLE;
BEGIN { 
  $EXECUTABLE = $0;
  # point it at the test directory if we're not there
  if ( $0 !~ m{^t/[^./]+\.t$} ) {

    # FindBin requires a real filename
    $0 = (glob('t/[^/]+.t'))[0];
  }

  # In case something does a "use FindBin" before we get here
  FindBin->again if exists $INC{'FindBin.pm'};
}
use FindBin;
BEGIN { $0 = $EXECUTABLE };

1;

Seems FindBin uses $0 (not surprising) to do its magic. Regrettably, that assumption makes it hard to run a test class directly. This also explained why the test classes required driver *.t files, something which drives me up a wall. Now they're not longer needed, but I confess I feel like I need to take a shower and scrub myself really, really clean.

Leave a comment

About Ovid

user-pic Freelance Perl/Testing/Agile consultant and trainer. See http://www.allaroundtheworld.fr/ for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book! http://www.amazon.com/Beginning-Perl-Curtis-Poe/dp/1118013840/