4 use vars qw($VERSION @ISA);
8 use TAP::Parser::Utils qw( split_shell );
11 use App::Prove::State;
14 @ISA = qw(TAP::Object);
18 App::Prove - Implements the C<prove> command.
30 L<Test::Harness> provides a command, C<prove>, which runs a TAP based
31 test suite and prints a report. The C<prove> command is a minimal
32 wrapper around an instance of this module.
38 my $app = App::Prove->new;
39 $app->process_args(@ARGV);
44 use constant IS_WIN32 => ( $^O =~ /^(MS)?Win32$/ );
45 use constant IS_VMS => $^O eq 'VMS';
46 use constant IS_UNIXY => !( IS_VMS || IS_WIN32 );
48 use constant STATE_FILE => IS_UNIXY ? '.prove' : '_prove';
49 use constant RC_FILE => IS_UNIXY ? '.proverc' : '_proverc';
51 use constant PLUGINS => 'App::Prove::Plugin';
57 archive argv blib show_count color directives exec failures fork
58 formatter harness includes modules plugins jobs lib merge parse quiet
59 really_quiet recurse backwards shuffle taint_fail taint_warn timer
60 verbose warnings_fail warnings_warn show_help show_man show_version
61 test_args state dry extension ignore_exit rules state_manager
63 for my $attr (@ATTR) {
67 $self->{$attr} = shift if @_;
68 return $self->{$attr};
79 Create a new C<App::Prove>. Optionally a hash ref of attribute
80 initializers may be passed.
84 # new() implementation supplied by TAP::Object
88 my $args = shift || {};
91 for my $key (qw( argv rc_opts includes modules state plugins rules )) {
94 $self->{harness_class} = 'TAP::Harness';
96 for my $attr (@ATTR) {
97 if ( exists $args->{$attr} ) {
99 # TODO: Some validation here
100 $self->{$attr} = $args->{$attr};
104 my %env_provides_default = (
105 HARNESS_TIMER => 'timer',
108 while ( my ( $env, $attr ) = each %env_provides_default ) {
109 $self->{$attr} = 1 if $ENV{$env};
111 $self->state_manager(
112 $self->state_class->new( { store => STATE_FILE } ) );
117 =head3 C<state_class>
119 Returns the name of the class used for maintaining state. This class should
120 either subclass from C<App::Prove::State> or provide an identical interface.
122 =head3 C<state_manager>
124 Getter/setter for the an instane of the C<state_class>.
129 return 'App::Prove::State';
132 =head3 C<add_rc_file>
134 $prove->add_rc_file('myproj/.proverc');
136 Called before C<process_args> to prepend the contents of an rc file to
142 my ( $self, $rc_file ) = @_;
145 open RC, "<$rc_file" or croak "Can't read $rc_file ($!)";
146 while ( defined( my $line = <RC> ) ) {
147 push @{ $self->{rc_opts} }, grep $_ && $_ !~ /^#/,
148 $line =~ m{ ' ([^']*) ' | " ([^"]*) " | (\#.*) | (\S*) }xg;
153 =head3 C<process_args>
155 $prove->process_args(@args);
157 Processes the command-line arguments. Attributes will be set
158 appropriately. Any filenames may be found in the C<argv> attribute.
160 Dies on invalid arguments.
168 unshift @rc, glob '~/' . RC_FILE if IS_UNIXY;
170 # Preprocess meta-args.
172 while ( defined( my $arg = shift ) ) {
173 if ( $arg eq '--norc' ) {
176 elsif ( $arg eq '--rc' ) {
177 defined( my $rc = shift )
178 or croak "Missing argument to --rc";
181 elsif ( $arg =~ m{^--rc=(.+)$} ) {
189 # Everything after the arisdottle '::' gets passed as args to
191 if ( defined( my $stop_at = _first_pos( '::', @args ) ) ) {
192 my @test_args = splice @args, $stop_at;
194 $self->{test_args} = \@test_args;
197 # Grab options from RC files
198 $self->add_rc_file($_) for grep -f, @rc;
199 unshift @args, @{ $self->{rc_opts} };
201 if ( my @bad = map {"-$_"} grep {/^-(man|help)$/} @args ) {
202 die "Long options should be written with two dashes: ",
203 join( ', ', @bad ), "\n";
210 Getopt::Long::Configure( 'no_ignore_case', 'bundling' );
212 # Don't add coderefs to GetOptions
214 'v|verbose' => \$self->{verbose},
215 'f|failures' => \$self->{failures},
216 'l|lib' => \$self->{lib},
217 'b|blib' => \$self->{blib},
218 's|shuffle' => \$self->{shuffle},
219 'color!' => \$self->{color},
220 'colour!' => \$self->{color},
221 'count!' => \$self->{show_count},
222 'c' => \$self->{color},
223 'D|dry' => \$self->{dry},
224 'ext=s' => \$self->{extension},
225 'harness=s' => \$self->{harness},
226 'ignore-exit' => \$self->{ignore_exit},
227 'formatter=s' => \$self->{formatter},
228 'r|recurse' => \$self->{recurse},
229 'reverse' => \$self->{backwards},
230 'fork' => \$self->{fork},
231 'p|parse' => \$self->{parse},
232 'q|quiet' => \$self->{quiet},
233 'Q|QUIET' => \$self->{really_quiet},
234 'e|exec=s' => \$self->{exec},
235 'm|merge' => \$self->{merge},
236 'I=s@' => $self->{includes},
237 'M=s@' => $self->{modules},
238 'P=s@' => $self->{plugins},
239 'state=s@' => $self->{state},
240 'directives' => \$self->{directives},
241 'h|help|?' => \$self->{show_help},
242 'H|man' => \$self->{show_man},
243 'V|version' => \$self->{show_version},
244 'a|archive=s' => \$self->{archive},
245 'j|jobs=i' => \$self->{jobs},
246 'timer' => \$self->{timer},
247 'T' => \$self->{taint_fail},
248 't' => \$self->{taint_warn},
249 'W' => \$self->{warnings_fail},
250 'w' => \$self->{warnings_warn},
251 'rules=s@' => $self->{rules},
252 ) or croak('Unable to continue');
254 # Stash the remainder of argv for later
255 $self->{argv} = [@ARGV];
264 return $_ if $_[$_] eq $want;
270 my ( $self, $verbosity ) = @_;
272 eval('use Pod::Usage 1.12 ()');
273 if ( my $err = $@ ) {
274 die 'Please install Pod::Usage for the --help option '
275 . '(or try `perldoc prove`.)'
279 Pod::Usage::pod2usage( { -verbose => $verbosity } );
287 return -t STDOUT && !IS_WIN32;
295 if ( defined $self->color ? $self->color : $self->_color_default ) {
298 if ( !defined $self->show_count ) {
299 $args{show_count} = 1;
302 $args{show_count} = $self->show_count;
305 if ( $self->archive ) {
306 $self->require_harness( archive => 'TAP::Harness::Archive' );
307 $args{archive} = $self->archive;
310 if ( my $jobs = $self->jobs ) {
314 if ( my $fork = $self->fork ) {
318 if ( my $harness_opt = $self->harness ) {
319 $self->require_harness( harness => $harness_opt );
322 if ( my $formatter = $self->formatter ) {
323 $args{formatter_class} = $formatter;
326 if ( $self->ignore_exit ) {
327 $args{ignore_exit} = 1;
330 if ( $self->taint_fail && $self->taint_warn ) {
331 die '-t and -T are mutually exclusive';
334 if ( $self->warnings_fail && $self->warnings_warn ) {
335 die '-w and -W are mutually exclusive';
338 for my $a (qw( lib switches )) {
339 my $method = "_get_$a";
340 my $val = $self->$method();
341 $args{$a} = $val if defined $val;
344 # Handle verbose, quiet, really_quiet flags
345 my %verb_map = ( verbose => 1, quiet => -1, really_quiet => -2, );
347 my @verb_adj = grep {$_} map { $self->$_() ? $verb_map{$_} : 0 }
350 die "Only one of verbose, quiet or really_quiet should be specified\n"
353 $args{verbosity} = shift @verb_adj || 0;
355 for my $a (qw( merge failures timer directives )) {
356 $args{$a} = 1 if $self->$a();
359 $args{errors} = 1 if $self->parse;
361 # defined but zero-length exec runs test files as binaries
362 $args{exec} = [ split( /\s+/, $self->exec ) ]
363 if ( defined( $self->exec ) );
365 if ( defined( my $test_args = $self->test_args ) ) {
366 $args{test_args} = $test_args;
369 if ( @{ $self->rules } ) {
371 for ( @{ $self->rules } ) {
375 elsif (/^seq=(.*)/) {
376 push @rules, { seq => $1 };
379 $args{rules} = { par => [@rules] };
382 return ( \%args, $self->{harness_class} );
386 my ( $self, $class, @search ) = @_;
388 croak "Bad module name $class"
389 unless $class =~ /^ \w+ (?: :: \w+ ) *$/x;
391 for my $pfx (@search) {
392 my $name = join( '::', $pfx, $class );
393 eval "require $name";
394 return $name unless $@;
397 eval "require $class";
398 return $class unless $@;
402 sub _load_extension {
403 my ( $self, $class, @search ) = @_;
406 if ( $class =~ /^(.*?)=(.*)/ ) {
408 @args = split( /,/, $2 );
411 if ( my $name = $self->_find_module( $class, @search ) ) {
412 $name->import(@args);
415 croak "Can't load module $class";
419 sub _load_extensions {
420 my ( $self, $ext, @search ) = @_;
421 $self->_load_extension( $_, @search ) for @$ext;
426 Perform whatever actions the command line args specified. The C<prove>
427 command line tool consists of the following code:
431 my $app = App::Prove->new;
432 $app->process_args(@ARGV);
433 exit( $app->run ? 0 : 1 ); # if you need the exit code
440 if ( $self->show_help ) {
443 elsif ( $self->show_man ) {
446 elsif ( $self->show_version ) {
447 $self->print_version;
449 elsif ( $self->dry ) {
450 print "$_\n" for $self->_get_tests;
454 $self->_load_extensions( $self->modules );
455 $self->_load_extensions( $self->plugins, PLUGINS );
457 local $ENV{TEST_VERBOSE} = 1 if $self->verbose;
459 return $self->_runtests( $self->_get_args, $self->_get_tests );
468 my $state = $self->state_manager;
469 my $ext = $self->extension;
470 $state->extension($ext) if defined $ext;
471 if ( defined( my $state_switch = $self->state ) ) {
472 $state->apply_switch(@$state_switch);
475 my @tests = $state->get_tests( $self->recurse, @{ $self->argv } );
477 $self->_shuffle(@tests) if $self->shuffle;
478 @tests = reverse @tests if $self->backwards;
484 my ( $self, $args, $harness_class, @tests ) = @_;
485 my $harness = $harness_class->new($args);
487 my $state = $self->state_manager;
491 $state->observe_test(@_);
496 after_runtests => sub {
501 my $aggregator = $harness->runtests(@tests);
503 return !$aggregator->has_errors;
510 # notes that -T or -t must be at the front of the switches!
511 if ( $self->taint_fail ) {
512 push @switches, '-T';
514 elsif ( $self->taint_warn ) {
515 push @switches, '-t';
517 if ( $self->warnings_fail ) {
518 push @switches, '-W';
520 elsif ( $self->warnings_warn ) {
521 push @switches, '-w';
524 push @switches, split_shell( $ENV{HARNESS_PERL_SWITCHES} );
526 return @switches ? \@switches : ();
536 push @libs, 'blib/lib', 'blib/arch';
538 if ( @{ $self->includes } ) {
539 push @libs, @{ $self->includes };
543 @libs = map { File::Spec->rel2abs($_) } @libs;
546 return @libs ? \@libs : ();
552 # Fisher-Yates shuffle
556 @_[ $i, $j ] = @_[ $j, $i ];
561 =head3 C<require_harness>
563 Load a harness replacement class.
565 $prove->require_harness($for => $class_name);
569 sub require_harness {
570 my ( $self, $for, $class ) = @_;
572 my ($class_name) = $class =~ /^(\w+(?:::\w+)*)/;
574 # Emulate Perl's -MModule=arg1,arg2 behaviour
575 $class =~ s!^(\w+(?:::\w+)*)=(.*)$!$1 split(/,/,q{$2})!;
578 die "$class_name is required to use the --$for feature: $@" if $@;
580 $self->{harness_class} = $class_name;
585 =head3 C<print_version>
587 Display the version numbers of the loaded L<TAP::Harness> and the
595 "TAP::Harness v%s and Perl v%vd\n",
596 $TAP::Harness::VERSION, $^V
604 # vim:ts=4:sw=4:et:sta
610 After command line parsing the following attributes reflect the values
611 of the corresponding command line switches. They may be altered before
660 =item C<really_quiet>
672 =item C<show_version>
688 =item C<warnings_fail>
690 =item C<warnings_warn>