Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Perl test - Make sure all perl files will compile

by skx (Parson)
on Jul 11, 2006 at 23:06 UTC ( #560574=CUFP: print w/replies, xml ) Need Help??

I've had this code in one of my projects for a while, but after reading Re: What is YOUR Development Process? and point 2 in particular "Everything has an automated test that, at the bare minimum, verifies that it will compile." I figured it was worth sharing.

The code is designed to be dropped into a test/ directory which "make test" will then run.

#!/usr/bin/perl -w # # Test that every perl file we have passes the syntax check. # # Steve # -- # $Id: perl-syntax.t,v 1.2 2006/06/13 13:26:00 steve Exp $ use strict; use File::Find; use Test::More qw( no_plan ); # # Find all the files beneath the current directory, # and call 'checkFile' with the name. # find( { wanted => \&checkFile, no_chdir => 1 }, '.' ); # # Check a file. # # If this is a perl file then call "perl -c $name", otherwise # return # sub checkFile { # The file. my $file = $File::Find::name; # We don't care about directories return if ( ! -f $file ); # `` is a false positive. return if ( $file =~ /$/ ); # See if it is a perl file. my $isPerl = 0; # Read the file. open( INPUT, "<", $file ); foreach my $line ( <INPUT> ) { if ( ( $line =~ /\/usr\/bin\/perl/ ) || ( $line =~ /\/usr\/local\/bin\/perl/ ) ) { $isPerl = 1; } } close( INPUT ); # # Return if it wasn't a perl file. # return if ( ! $isPerl ); # # Now run 'perl -c $file' to see if we pass the syntax # check # my $retval = system( "perl -c $file 2>/dev/null >/dev/null" ); is( $retval, 0, "Perl file passes our syntax check: $file" ); }

Replies are listed 'Best First'.
Re: Perl test - Make sure all perl files will compile
by Tanktalus (Canon) on Jul 12, 2006 at 14:59 UTC

    A couple minor comments. First, your loop through the file - if you found what you want, you probably should break out. Second, rather than foreach, you probably want while - no point in reading in a huge file and then looping through it.

    Then I have to wonder why you're looking through the file at all. You only need to check the first line. And you probably should make the same check that perl does - that it starts with "#!" and has "perl" somewhere in the line:

    open( my $input, "<", $file ) or return; # can't read, can't test. P +erhaps mark as a failure? my $line = <$input>; return unless $line =~ /^#!\s*(.*perl\S*)/; my $desired_interpreter = $1 close( $input );

    Now I've captured the perl that this file wants to use, so we can use it in the system command.

    If you want to support pathological cases where there is leading cruft, you'll need to scan the rest of the file looking for the #!.*perl line, and then use the -x option to the perl interpreter - but I don't think that doing this in an automated manner is wise. So I suggest just looking at the first line, and discarding the rest.

      ++ - Thanks!

      Amazing how obvious it sounds now you've said it.

Re: Perl test - Make sure all perl files will compile
by osfameron (Hermit) on Jul 13, 2006 at 21:01 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://560574]
Approved by GrandFather
Front-paged by grinder
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2017-07-24 17:46 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (356 votes). Check out past polls.