Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

petdance (2468)

petdance
  andy@petdance.com
http://www.perlbuzz.com/
AOL IM: petdance (Add Buddy, Send Message)
Yahoo! ID: petdance (Add User, Send Message)
Jabber: petdance@gmail.com

I'm Andy Lester, and I like to test stuff. I also write for the Perl Journal, and do tech edits on books. Sometimes I write code, too.

Journal of petdance (2468)

Thursday October 10, 2002
11:39 AM

Enforcing strict & warning as a .t file

[ #8300 ]
Acme found that he had files that didn't use strict. I found that, too, and I wrote a .t file for it. Here it is:

#!/usr/bin/perl -w

use strict;
use File::Spec;
use File::Find::Rule;

use Test::More 'no_plan';

my $base = (shift || $ENV{TWROOT} || '.');

my $rule = File::Find::Rule->new;
$rule->or( $rule->new->directory->name('CVS')->prune->discard,
           $rule->new->file->name( '*.pl','*.pm','*.t' ) );
my @files = $rule->in( $base );

for my $file ( @files ) {
    check( $file );
}

sub check {
    my $filename = shift;

    my $dispname = File::Spec->abs2rel( $filename, $base );

    local $/ = undef;

    open( my $fh, $filename ) or return fail( "Couldn't open $dispname: $!" );
    my $text = <$fh>;
    close $fh;

    # Search for strict and warnings.  You can get around this just by
    # having the text in comments, but that's not the point.
    ok( $text =~ /use strict;/,             "$dispname uses strict" );
    ok( $text =~ /use warnings;|perl -w/,   "$dispname uses warnings" );
}

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I was about to ask why you didn't use like() instead of ok(). Then I realized exactly how verbose a failure message would be. Good thinking.

  • I almost posted my code - which is eerily similar to yours. I actually count the number of files in a BEGIN block and hence get a test plan, but the code is basically the same. Ain't testing great ;-)
    • I almost posted my code - which is eerily similar to yours. I actually count the number of files in a BEGIN block and hence get a test plan, but the code is basically the same. Ain't testing great ;-)

      Yes, testing IS great. :-)

      I don't count the files because I don't know how many tests per file I'm going to have, and I may actually have a different number of tests for each file.

      One of the things I was talking with Farmer Schwern about at YAPC::NA was the possibility of having a count of tests, with a

      --

      --
      xoa

  • How about catching the cheating programmers who put the string "use strict" somewhere in their program?
    #use strict;
    #Andy says to use strict, but i don't like it
    Module::Info can do that :)
    • It's a known issue. Note:

      # Search for strict and warnings.  You can get around this just by
      # having the text in comments, but that's not the point.
      ok( $text =~ /use strict;/,             "$dispname uses strict" );
      ok( $text =~ /use warnings;|perl -w/,   "$dispname uses warnings" );

      The code is to catch mistakes, not malfeasance. There's also nothing stopping you from doing

      use strict;
      no strict;

      At that point, it's a management issue, not a technical one.

      --

      --
      xoa