#!/usr/bin/perl -w

use strict;

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

our $VERSION = "0.01";

my @exclude;
my @ext = ();
my $shuffle;
my $dry = 0;
my $recurse = 0;
my @thswitches = ();

Getopt::Long::Configure( "no_ignore_case" );
GetOptions(
    'help|?'        => sub {pod2usage({-verbose => 1, -input => \*DATA}); exit},
    'man'           => sub {pod2usage({-verbose => 2, -input => \*DATA}); exit},
    'shuffle'       => \$shuffle,
    'dry'           => \$dry,
    'recurse'       => \$recurse,
    'b|blib'        => sub { push( @thswitches, "-I" . File::Spec->catfile( "blib", "lib" ) ) },
    'T|taint'       => sub { unshift( @thswitches, "-T" ) }, # Always want -T up front
    'v|verbose'     => \$Test::Harness::verbose,
    'V|version'     => sub { print_version(); exit; },
    'x|exclude=s@'  => sub { warn "--exclude option is unimplemented\n" },
    'ext=s@'        => \@ext,
);

# 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
$Test::Harness::Switches = join( " ", @thswitches );

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 {
        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:

    -v  --verbose   Display standard output of test scripts while running them.
    -T  --taint     Run under taint mode
        --shuffle   Run the tests in a random order.
    -x  --exclude   Tests to exclude (unimplemented)
        --ext       Extensions (defaults to .t)
    -r  --recurse   Recursively descend into directories.
    -d  --dry       Dry run: Show the tests to run, but don't run them.
    -b  --blib      Adds blib/lib to the path for your tests, a la "use blib".
    -V  --version   Display version info
    -h  --help      Display this help
        --man       Longer manpage for prove

=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 COMMAND LINE OPTIONS

=head2 -T

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

=head2 -x, --exclude

Use this switch to specify which files will not be run as tests.  If used, the
program assumes all files ending in C<.t> are tests and will skip those that
match C<--exclude>.  To run all tests but exclude the tests used in the
previous example:

    prove -x 10item.t -x 10tax.t -x 10tender.t

=head2 --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 --recurse

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

=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 *

-I flag for includes

=item * 

Shuffled tests must be recreatable

=item *

Allow excluding tests

=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
