An Unexpected Test Result

I spent several hours debugging a catastrophic test suite failure today. As our test classes take over an hour to run and the failure takes place near the end of the test run, it was a very annoying issue to debug. Unfortunately, the test class in question passed when run by itself, but not when run in the entire suite. That usually means the global state had been altered in an unexpected way, and boy had it! Seems the test was failing because it was trying to run a method that a completely separate test class had -- and those classes weren't related by inheritance. It was very confusing until I finally realized that someone had mistyped a package name at the top of the test class. A few minutes later I had a new test which verified the package name ... and found ten other misspelled package names (out of 325 packages).

You'll need to adjust this to taste (and it assumes that the first thing which looks like package declaration is the package declaration).

#!/usr/bin/env perl

use Test::Most 'bail';
use File::Find;

sub expected_package_name($) {
  my $file = shift;
  $file =~ s{^t/lib/}{};
  $file =~ s{\.pm$}{};
  $file =~ s/\//::/g;
  return $file;
}

sub found_package_name($) {
  my $file = shift;

  # we assume first package name found is actual
  open my $fh, '<', $file or die "Could not open $file for reading: $!";
  my $package;
  while ( my $line = <$fh> ) {
    next unless $line =~ /^\s*package\s+((?:\w+)(::\w+)*)/;
    return $1;
  }
}

my @files;
find(
  { no_chdir => 1,
    wanted   => sub {
      my $file = $File::Find::name;
      return if !-f $file || $file !~ /\.pm$/ || $file =~ /\.svn/;
      push @files =>
        [ $file, found_package_name $file, expected_package_name $file];
    },
  },
  't/lib/Tests'
);
plan tests => scalar @files;
for my $file (@files) {
  my ( $file, $have, $want ) = @$file;
  is $have, $want, "Package name correct for $file";
}

1 Comment

I wrote a Perl-Critic policy to do something very similar to that. Take a look at Perl::Critic::Policy::Modules::RequireFilenameMatchesPackage

Leave a comment

About Ovid

user-pic Have Perl; Will Travel. Freelance Perl/Testing/Agile consultant. Photo by http://www.circle23.com/. Warning: that site is not safe for work. The photographer is a good friend of mine, though, and it's appropriate to credit his work.