Git - inhibit commit if modules don't compile

Sometimes I forget to run Perl base tests for my modules, before committing changes. As to what happens, I end up committing something that doesn't quite compile ;) Luckily Git has a pre-commit hook one can use to run at least the "compile" tests. The following aborts the commit if the t/00*.t tests (usually the "do all modules compile?" tests) in the repository don't run correctly; stick it in .git/hooks/pre-commit:
#!/usr/bin/perl
# Runs modules' "compiles" tests before committing
# Dies (halting commit) if they don't compile
print "pre-commit => testing..\n";
do {qx{
  prove -Ilib t/00*.t
}} or die <<'DIEMSG';
pre-commit => ERRORS:
$@
DIEMSG
print "pre-commit => test OK\n";
This is an example of a 00-load.t file, that can literally be dropped-in the t/ directory:
use strict;
use warnings;
use Test::More;
use File::Find::Rule;
 
my @files = File::Find::Rule->name('*.pm')->in('lib');
plan tests => ~ ~ @files;
 
for (@files) {
  s/^lib.//;
  s/.pm$//;
  s{[\\/]}{::}g;
 
  ok(
    eval "require $_; 1",
    "loaded $_ with no problems",
  );
}
File::Find::Rule is one of the golden gems found on CPAN, rclamp++!

Till soon,
-marco-

Leave a comment

About Marco Fontani

user-pic Slicing and splicing onions