#!/usr/bin/perl -w

use strict;

use Test::Harness;
use Getopt::Long;
use Pod::Usage;
use File::Spec;

our $VERSION = "0.02";

my @ext = ();
my $shuffle = 0;
my $debug = 0;
my $dry = 0;
my $recurse = 0;
my @includes = ();
my @switches = ();

# Allow cuddling the paths with the -I
@ARGV = map { /^(-I)(.+)/ ? ($1,$2) : $_ } @ARGV;

Getopt::Long::Configure( "no_ignore_case" );
GetOptions(
    'b|blib'        => sub { unshift @includes, File::Spec->catfile( "blib", "lib" ) },
    'd|debug'	    => \$debug,
    'D|dry'         => \$dry,
    'h|help|?'      => sub {pod2usage({-verbose => 1, -input => \*DATA}); exit},
    'H|man'         => sub {pod2usage({-verbose => 2, -input => \*DATA}); exit},
    'I=s@'          => \@includes,
    'r|recurse'     => \$recurse,
    's|shuffle'     => \$shuffle,
    'T|taint'       => sub { unshift @switches, "-T" }, # Always want -T up front
    'v|verbose'     => \$Test::Harness::verbose,
    'V|version'     => sub { print_version(); exit; },
    'ext=s@'        => \@ext,
) or exit 1;

# Build up extensions regex
@ext = map { split /,/ } @ext;
s/^\.// foreach @ext;
@ext = ("t") unless @ext;
my $ext_regex = join( "|", map { quotemeta } @ext );
$ext_regex = qr/\.($ext_regex)$/;

# Build up TH switches
unshift( @switches, map { "-I$_" } @includes );
$Test::Harness::Switches = join( " ", @switches );
print "# \$Test::Harness::Switches: $Test::Harness::Switches\n" if $debug;

my @tests;
@ARGV = File::Spec->curdir unless @ARGV;
push( @tests, -d $_ ? all_in( $_ ) : $_ ) for @ARGV;

if ( @tests ) {
    shuffle(@tests) if $shuffle;
    if ( $dry ) {
        print join( "\n", @tests, "" );
    } else {
	print "# ", scalar @tests, " tests to run\n" if $debug;
        runtests(@tests);
    }
}

sub all_in {
    my $start = shift;

    my @hits = ();

    local *DH;
    if ( opendir( DH, $start ) ) {
        while ( my $file = readdir DH ) {
            next if $file eq File::Spec->updir || $file eq File::Spec->curdir;
            next if $file eq ".svn";
            next if $file eq "CVS";

            my $currfile = File::Spec->catfile( $start, $file );
            if ( -d $currfile ) {
                push( @hits, all_in( $currfile ) ) if $recurse;
            } else {
                push( @hits, $currfile ) if $currfile =~ $ext_regex;
            }
        }
    } else {
        warn "$start: $!\n";
    }

    return @hits;
}

sub shuffle {
    # Fisher-Yates shuffle
    my $i = @_;
    while ($i) {
        my $j = rand $i--;
        @_[$i, $j] = @_[$j, $i];
    }
}

sub print_version {
    print "prove v$VERSION, using Test::Harness $Test::Harness::VERSION\n";
}

__END__

=head1 NAME

prove -- A command-line tool for running tests against Test::Harness

=head1 SYNOPSIS

prove [options] [files/directories]

Options:

    -b  --blib      Adds blib/lib to the path for your tests, a la "use blib".
    -d  --debug	    Includes extra debugging information.
    -D  --dry       Dry run: Show the tests to run, but don't run them.
        --ext       Extensions (defaults to .t)
    -h  --help      Display this help
    -H  --man       Longer manpage for prove
    -I              Add libraries to @INC, as Perl's -I
    -r  --recurse   Recursively descend into directories.
    -s  --shuffle   Run the tests in a random order.
    -T  --taint     Run under taint mode
    -v  --verbose   Display standard output of test scripts while running them.
    -V  --version   Display version info

=head1 OVERVIEW

F<prove> is a command-line interface to the test-running functionality
of C<Test::Harness>.  With no arguments, it will run all tests in the
current directory.

Shell metacharacters may be used with command lines options and will be exanded 
via C<glob>.

=head1 PROVE VS. "MAKE TEST"

F<prove> has a number of advantages over C<make test> when doing development.

=over 4

=item * F<prove> is designed as a development tool

Perl users typically run the test harness through a makefile via
C<make test>.  That's fine for module distributions, but it's
suboptimal for a test/code/debug development cycle.

=item * F<prove> is granular 

F<prove> lets your run against only the files you want to check.
Running C<prove t/live/ t/master.t> checks every F<*.t> in F<t/live>,
plus F<t/master.t>.

=item * F<prove> has an easy verbose mode

To get full test program output from C<make test>, you must set
C<HARNESS_VERBOSE> in the environment.  F<prove> has a C<-v> option.

=item * F<prove> can run under taint mode

F<prove>'s C<-T> runs your tests under C<perl -T>.

=item * F<prove> can shuffle tests

You can use F<prove>'s C<--shuffle> option to try to excite problems
that don't show up when tests are run in the same order every time.

=item * Not everything is a module

More and more users are using Perl's testing tools outside the
context of a module distribution, and may not even use a makefile
at all.

=back

=head1 COMMAND LINE OPTIONS

=head2 -b, --blib

Adds blib/lib to the path for your tests, a la "use blib".

=head2 -d, --debug

Include debug information about how F<prove> is being run.  This
option doesn't show the output from the test scripts.  That's handled
by -v,--verbose.

=head2 -D, --dry

Dry run: Show the tests to run, but don't run them.

=head2 --ext

Extensions (defaults to .t)

=head2 -I

Add libraries to @INC, as Perl's -I

=head2 -r, --recurse

Descends into subdirectories of any directories specified, looking for tests.

=head2 -s, --shuffle

Sometimes tests are accidentally dependent on tests that have been
run before.  This switch will shuffle the tests to be run prior to
running them, thus ensuring that hidden dependencies in the test
order are likely to be revealed.  The author hopes the run the
algorithm on the preceding sentence to see if he can produce something
slightly less awkward.

=head2 -T, --taint

Runs test programs under perl's -T taint mode.

=head2 -v, --verbose

Display standard output of test scripts while running them.

=head2 -V, --version

Display version info.

=head1 BUGS

Please use the CPAN bug ticketing system at L<http://rt.cpan.org/>.
You can also mail bugs, fixes and enhancements to 
C<< <bug-test-harness@rt.cpan.org> >>.

=head1 TODO

=over 4

=item *

Shuffled tests must be recreatable

=item *

Add a flag to run prove under Devel::Cover

=back

=head1 AUTHORS

Andy Lester C<< <andy@petdance.com> >>

=head1 COPYRIGHT

Copyright 2003 by Andy Lester C<< <andy@petdance.com> >>.

This program is free software; you can redistribute it and/or 
modify it under the same terms as Perl itself.

See L<http://www.perl.com/perl/misc/Artistic.html>.

=cut
