Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Is there a better way to get all the t/.t test files an array?

by tphyahoo (Vicar)
on Jun 28, 2005 at 17:05 UTC ( [id://470718]=perlquestion: print w/replies, xml ) Need Help??

tphyahoo has asked for the wisdom of the Perl Monks concerning the following question:

Salutations, brother monks.

I have just started using Test::Harness, love it. The first thing you have to do, it appears, is push all your test files onto an array so you can runtests(). Rather than do this manually, I am grabbing all the files that end with .t in a directory, using File::Find.

This works, but it seemed rather long. Is there is a more elegant/shorter way to accomplish the same thing?

use strict; use Test::Harness; use File::Find; my @tests; my @test_files = get_files('./t'); push @tests, @test_files; runtests(@tests); sub get_files { my $directory = shift; opendir (DIR, $directory) or die "couldn't open directory: $direct +ory"; my @myfiles; my @directories_to_search = ("$directory"); find( sub { my $name = $File::Find::name; if ( $name =~ /\.t$/i ) { $name =~ s|\\|/|; # substitute forward slash for back slas +hes, icky but don't know a better way. #print "name: $name\n"; push @myfiles, $name; } }, @directories_to_search); return @myfiles; }
Update: followup at Re: Testing for Beginners

Replies are listed 'Best First'.
Re: Is there a better way to get all the t/.t test files an array?
by davido (Cardinal) on Jun 28, 2005 at 17:49 UTC

    To simply grab a list of filenames that match some expression within a directory, just use glob:

    sub get_files { chdir( $_[0] ); return glob( $_[1] ); }

    Usage:

    my( @files ) = get_files( '/path/to/files', '*.t' );

    This will change your current working directory.


    Dave

Re: Is there a better way to get all the t/.t test files an array?
by revdiablo (Prior) on Jun 28, 2005 at 17:56 UTC

    If you don't need a recursive search, davido's idea to use glob is preferable. If you do, however, File::Find is a fine solution. Your sub can be cleaned up a bit, though:

    sub get_files { my $directory = shift; my @files; find(sub { return unless /\.t$/; my $n = $File::Find::name; $n =~ tr{\\}{/}; push @files, $n; }, $directory); return @files; }
Re: Is there a better way to get all the t/.t test files an array?
by donarb (Beadle) on Jul 01, 2005 at 06:44 UTC
    You can also test your code within a module directory. Then you can let Perl's make system find all of your .t files for you and test with a simple 'make test' on the command line.
      Sounds promising, but I don't really understand. Could you link to the relevant information describing how to do this? I use modules, but I am not familiar with the MakeMaker / Module::Build type infrastructure for distribution. (High on the list of things to learn.)
Re: Is there a better way to get all the t/.t test files an array?
by adrianh (Chancellor) on Jun 29, 2005 at 15:56 UTC

    Untested:

    use Test::Harness; use File::Find::Rule; runtests( File::Find::Rule->file->name('*.t')->in('./t'));

    On the off chance that you're considering creating a command line tool for running test scripts, you might want to look at prove (part of recent Test::Harness distributions) and Test::Verbose.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://470718]
Approved by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-19 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found