File Coverage

blib/lib/TAP/Harness.pm
Criterion Covered Total %
statement 254 278 91.3
branch 84 104 80.7
condition 18 23 78.2
subroutine 35 36 97.2
pod 6 6 100.0
total 397 447 88.8


line stmt bran cond sub pod time code
1             package TAP::Harness;
2              
3 21     21   337256 use strict;
  21         54  
  21         663  
4 21     21   135 use warnings;
  21         53  
  21         696  
5 21     21   263 use Carp;
  21         57  
  21         1918  
6              
7 21     21   144 use File::Spec;
  21         46  
  21         717  
8 21     21   128 use File::Path;
  21         51  
  21         1259  
9 21     21   11237 use IO::Handle;
  21         108892  
  21         1099  
10              
11 21     21   174 use base 'TAP::Base';
  21         56  
  21         10775  
12              
13             =head1 NAME
14              
15             TAP::Harness - Run test scripts with statistics
16              
17             =head1 VERSION
18              
19             Version 3.40_01
20              
21             =cut
22              
23             our $VERSION = '3.40_01';
24              
25             $ENV{HARNESS_ACTIVE} = 1;
26             $ENV{HARNESS_VERSION} = $VERSION;
27              
28             END {
29              
30             # For VMS.
31 21     21   7549 delete $ENV{HARNESS_ACTIVE};
32 21         551 delete $ENV{HARNESS_VERSION};
33             }
34              
35             =head1 DESCRIPTION
36              
37             This is a simple test harness which allows tests to be run and results
38             automatically aggregated and output to STDOUT.
39              
40             =head1 SYNOPSIS
41              
42             use TAP::Harness;
43             my $harness = TAP::Harness->new( \%args );
44             $harness->runtests(@tests);
45              
46             =cut
47              
48             my %VALIDATION_FOR;
49             my @FORMATTER_ARGS;
50              
51             sub _error {
52 164     164   334 my $self = shift;
53 164 50       921 return $self->{error} unless @_;
54 0         0 $self->{error} = shift;
55             }
56              
57             BEGIN {
58              
59 21     21   124 @FORMATTER_ARGS = qw(
60             directives verbosity timer failures comments errors stdout color
61             show_count normalize
62             );
63              
64             %VALIDATION_FOR = (
65             lib => sub {
66 48         165 my ( $self, $libs ) = @_;
67 48 100       230 $libs = [$libs] unless 'ARRAY' eq ref $libs;
68              
69 48         201 return [ map {"-I$_"} @$libs ];
  9         56  
70             },
71 47         105 switches => sub { shift; shift },
  47         119  
72 7         24 exec => sub { shift; shift },
  7         24  
73 1         4 merge => sub { shift; shift },
  1         4  
74 1         3 aggregator_class => sub { shift; shift },
  1         2  
75 6         14 formatter_class => sub { shift; shift },
  6         16  
76 1         2 multiplexer_class => sub { shift; shift },
  1         3  
77 1         2 parser_class => sub { shift; shift },
  1         3  
78 1         2 scheduler_class => sub { shift; shift },
  1         2  
79 0         0 formatter => sub { shift; shift },
  0         0  
80 3         36 jobs => sub { shift; shift },
  3         10  
81 2         10 test_args => sub { shift; shift },
  2         10  
82 43         94 ignore_exit => sub { shift; shift },
  43         113  
83 0         0 rules => sub { shift; shift },
  0         0  
84 1         3 rulesfile => sub { shift; shift },
  1         2  
85 2         9 sources => sub { shift; shift },
  2         8  
86 0         0 version => sub { shift; shift },
  0         0  
87 0         0 trap => sub { shift; shift },
  0         0  
88 21         803 );
89              
90 21         290 for my $method ( sort keys %VALIDATION_FOR ) {
91 21     21   200 no strict 'refs';
  21         54  
  21         5317  
92 378 100 100     1609 if ( $method eq 'lib' || $method eq 'switches' ) {
93 42         249 *{$method} = sub {
94 533     533   3768 my $self = shift;
95 533 100       1555 unless (@_) {
96 436   100     1523 $self->{$method} ||= [];
97             return wantarray
98 216         971 ? @{ $self->{$method} }
99 436 100       2034 : $self->{$method};
100             }
101 97 100       329 $self->_croak("Too many arguments to method '$method'")
102             if @_ > 1;
103 96         193 my $args = shift;
104 96 100       359 $args = [$args] unless ref $args;
105 96         365 $self->{$method} = $args;
106 96         274 return $self;
107 42         193 };
108             }
109             else {
110 336         2135 *{$method} = sub {
111 3234     3234   7017 my $self = shift;
112 3234 100       16840 return $self->{$method} unless @_;
113 720         3024 $self->{$method} = shift;
114 336         1391 };
115             }
116             }
117              
118 21         70 for my $method (@FORMATTER_ARGS) {
119 21     21   168 no strict 'refs';
  21         56  
  21         2061  
120 210         56044 *{$method} = sub {
121 14     14   47 my $self = shift;
122 14         51 return $self->formatter->$method(@_);
123 210         801 };
124             }
125             }
126              
127             ##############################################################################
128              
129             =head1 METHODS
130              
131             =head2 Class Methods
132              
133             =head3 C
134              
135             my %args = (
136             verbosity => 1,
137             lib => [ 'lib', 'blib/lib', 'blib/arch' ],
138             )
139             my $harness = TAP::Harness->new( \%args );
140              
141             The constructor returns a new C object. It accepts an
142             optional hashref whose allowed keys are:
143              
144             =over 4
145              
146             =item * C
147              
148             Set the verbosity level:
149              
150             1 verbose Print individual test results to STDOUT.
151             0 normal
152             -1 quiet Suppress some test output (mostly failures
153             while tests are running).
154             -2 really quiet Suppress everything but the tests summary.
155             -3 silent Suppress everything.
156              
157             =item * C
158              
159             Append run time for each test to output. Uses L if
160             available.
161              
162             =item * C
163              
164             Show test failures (this is a no-op if C is selected).
165              
166             =item * C
167              
168             Show test comments (this is a no-op if C is selected).
169              
170             =item * C
171              
172             Update the running test count during testing.
173              
174             =item * C
175              
176             Set to a true value to normalize the TAP that is emitted in verbose modes.
177              
178             =item * C
179              
180             Accepts a scalar value or array ref of scalar values indicating which
181             paths to allowed libraries should be included if Perl tests are
182             executed. Naturally, this only makes sense in the context of tests
183             written in Perl.
184              
185             =item * C
186              
187             Accepts a scalar value or array ref of scalar values indicating which
188             switches should be included if Perl tests are executed. Naturally, this
189             only makes sense in the context of tests written in Perl.
190              
191             =item * C
192              
193             A reference to an C<@INC> style array of arguments to be passed to each
194             test program.
195              
196             test_args => ['foo', 'bar'],
197              
198             if you want to pass different arguments to each test then you should
199             pass a hash of arrays, keyed by the alias for each test:
200              
201             test_args => {
202             my_test => ['foo', 'bar'],
203             other_test => ['baz'],
204             }
205              
206             =item * C
207              
208             Attempt to produce color output.
209              
210             =item * C
211              
212             Typically, Perl tests are run through this. However, anything which
213             spits out TAP is fine. You can use this argument to specify the name of
214             the program (and optional switches) to run your tests with:
215              
216             exec => ['/usr/bin/ruby', '-w']
217              
218             You can also pass a subroutine reference in order to determine and
219             return the proper program to run based on a given test script. The
220             subroutine reference should expect the TAP::Harness object itself as the
221             first argument, and the file name as the second argument. It should
222             return an array reference containing the command to be run and including
223             the test file name. It can also simply return C, in which case
224             TAP::Harness will fall back on executing the test script in Perl:
225              
226             exec => sub {
227             my ( $harness, $test_file ) = @_;
228              
229             # Let Perl tests run.
230             return undef if $test_file =~ /[.]t$/;
231             return [ qw( /usr/bin/ruby -w ), $test_file ]
232             if $test_file =~ /[.]rb$/;
233             }
234              
235             If the subroutine returns a scalar with a newline or a filehandle, it
236             will be interpreted as raw TAP or as a TAP stream, respectively.
237              
238             =item * C
239              
240             If C is true the harness will create parsers that merge STDOUT
241             and STDERR together for any processes they start.
242              
243             =item * C
244              
245             I.
246              
247             If set, C must be a hashref containing the names of the
248             Ls to load and/or configure. The values are a
249             hash of configuration that will be accessible to the source handlers via
250             L.
251              
252             For example:
253              
254             sources => {
255             Perl => { exec => '/path/to/custom/perl' },
256             File => { extensions => [ '.tap', '.txt' ] },
257             MyCustom => { some => 'config' },
258             }
259              
260             The C parameter affects how C, C and C parameters
261             are handled.
262              
263             For more details, see the C parameter in L,
264             L, and L.
265              
266             =item * C
267              
268             The name of the class to use to aggregate test results. The default is
269             L.
270              
271             =item * C
272              
273             I.
274              
275             Assume this TAP version for L instead of default TAP
276             version 12.
277              
278             =item * C
279              
280             The name of the class to use to format output. The default is
281             L, or L if the output
282             isn't a TTY.
283              
284             =item * C
285              
286             The name of the class to use to multiplex tests during parallel testing.
287             The default is L.
288              
289             =item * C
290              
291             The name of the class to use to parse TAP. The default is
292             L.
293              
294             =item * C
295              
296             The name of the class to use to schedule test execution. The default is
297             L.
298              
299             =item * C
300              
301             If set C must be an object that is capable of formatting the
302             TAP output. See L for an example.
303              
304             =item * C
305              
306             If parse errors are found in the TAP output, a note of this will be
307             made in the summary report. To see all of the parse errors, set this
308             argument to true:
309              
310             errors => 1
311              
312             =item * C
313              
314             If set to a true value, only test results with directives will be
315             displayed. This overrides other settings such as C or
316             C.
317              
318             =item * C
319              
320             If set to a true value instruct C to ignore exit and wait
321             status from test scripts.
322              
323             =item * C
324              
325             The maximum number of parallel tests to run at any time. Which tests
326             can be run in parallel is controlled by C. The default is to
327             run only one test at a time.
328              
329             =item * C
330              
331             A reference to a hash of rules that control which tests may be executed in
332             parallel. If no rules are declared and L is available,
333             C attempts to load rules from a YAML file specified by the
334             C parameter. If no rules file exists, the default is for all
335             tests to be eligible to be run in parallel.
336              
337             Here some simple examples. For the full details of the data structure
338             and the related glob-style pattern matching, see
339             L.
340              
341             # Run all tests in sequence, except those starting with "p"
342             $harness->rules({
343             par => 't/p*.t'
344             });
345              
346             # Equivalent YAML file
347             ---
348             par: t/p*.t
349              
350             # Run all tests in parallel, except those starting with "p"
351             $harness->rules({
352             seq => [
353             { seq => 't/p*.t' },
354             { par => '**' },
355             ],
356             });
357              
358             # Equivalent YAML file
359             ---
360             seq:
361             - seq: t/p*.t
362             - par: **
363              
364             # Run some startup tests in sequence, then some parallel tests than some
365             # teardown tests in sequence.
366             $harness->rules({
367             seq => [
368             { seq => 't/startup/*.t' },
369             { par => ['t/a/*.t','t/b/*.t','t/c/*.t'], }
370             { seq => 't/shutdown/*.t' },
371             ],
372              
373             });
374              
375             # Equivalent YAML file
376             ---
377             seq:
378             - seq: t/startup/*.t
379             - par:
380             - t/a/*.t
381             - t/b/*.t
382             - t/c/*.t
383             - seq: t/shutdown/*.t
384              
385             This is an experimental feature and the interface may change.
386              
387             =item * C
388              
389             This specifies where to find a YAML file of test scheduling rules. If not
390             provided, it looks for a default file to use. It first checks for a file given
391             in the C environment variable, then it checks for
392             F and then F.
393              
394             =item * C
395              
396             A filehandle for catching standard output.
397              
398             =item * C
399              
400             Attempt to print summary information if run is interrupted by
401             SIGINT (Ctrl-C).
402              
403             =back
404              
405             Any keys for which the value is C will be ignored.
406              
407             =cut
408              
409             # new supplied by TAP::Base
410              
411             {
412             my @legal_callback = qw(
413             parser_args
414             made_parser
415             before_runtests
416             after_runtests
417             after_test
418             );
419              
420             my %default_class = (
421             aggregator_class => 'TAP::Parser::Aggregator',
422             formatter_class => 'TAP::Formatter::Console',
423             multiplexer_class => 'TAP::Parser::Multiplexer',
424             parser_class => 'TAP::Parser',
425             scheduler_class => 'TAP::Parser::Scheduler',
426             );
427              
428             sub _initialize {
429 93     93   385 my ( $self, $arg_for ) = @_;
430 93   100     463 $arg_for ||= {};
431              
432 93         814 $self->SUPER::_initialize( $arg_for, \@legal_callback );
433 93         655 my %arg_for = %$arg_for; # force a shallow copy
434              
435 93         1351 for my $name ( sort keys %VALIDATION_FOR ) {
436 1674         3240 my $property = delete $arg_for{$name};
437 1674 100       4264 if ( defined $property ) {
438 164         478 my $validate = $VALIDATION_FOR{$name};
439              
440 164         607 my $value = $self->$validate($property);
441 164 50       609 if ( $self->_error ) {
442 0         0 $self->_croak;
443             }
444 164         774 $self->$name($value);
445             }
446             }
447              
448 93 100       724 $self->jobs(1) unless defined $self->jobs;
449              
450 93 50       536 if ( ! defined $self->rules ) {
451 93         457 $self->_maybe_load_rulesfile;
452             }
453              
454             local $default_class{formatter_class} = 'TAP::Formatter::File'
455 93 50 100     1324 unless -t ( $arg_for{stdout} || \*STDOUT ) && !$ENV{HARNESS_NOTTY};
      33        
456              
457 93         666 while ( my ( $attr, $class ) = each %default_class ) {
458 465   66     1701 $self->$attr( $self->$attr() || $class );
459             }
460              
461 93 50       371 unless ( $self->formatter ) {
462              
463             # This is a little bodge to preserve legacy behaviour. It's
464             # pretty horrible that we know which args are destined for
465             # the formatter.
466 93         316 my %formatter_args = ( jobs => $self->jobs );
467 93         377 for my $name (@FORMATTER_ARGS) {
468 930 100       2805 if ( defined( my $property = delete $arg_for{$name} ) ) {
469 185         517 $formatter_args{$name} = $property;
470             }
471             }
472              
473             $self->formatter(
474 93         334 $self->_construct( $self->formatter_class, \%formatter_args )
475             );
476             }
477              
478 91 100       657 if ( my @props = sort keys %arg_for ) {
479 1         10 $self->_croak("Unknown arguments to TAP::Harness::new (@props)");
480             }
481              
482 90         914 return $self;
483             }
484              
485             sub _maybe_load_rulesfile {
486 93     93   345 my ($self) = @_;
487              
488             my ($rulesfile) = defined $self->rulesfile ? $self->rulesfile :
489             defined($ENV{HARNESS_RULESFILE}) ? $ENV{HARNESS_RULESFILE} :
490 93 100       359 grep { -r } qw(./testrules.yml t/testrules.yml);
  182 100       1991  
491              
492 93 100 66     513 if ( defined $rulesfile && -r $rulesfile ) {
493 4 50       11 if ( ! eval { require CPAN::Meta::YAML; 1} ) {
  4         42  
  4         17  
494 0         0 warn "CPAN::Meta::YAML required to process $rulesfile" ;
495 0         0 return;
496             }
497 4 50       24 my $layer = $] lt "5.008" ? "" : ":encoding(UTF-8)";
498 4 50   1   162 open my $fh, "<$layer", $rulesfile
  1         9  
  1         3  
  1         10  
499             or die "Couldn't open $rulesfile: $!";
500 4         11972 my $yaml_text = do { local $/; <$fh> };
  4         22  
  4         92  
501 4 50       79 my $yaml = CPAN::Meta::YAML->read_string($yaml_text)
502             or die CPAN::Meta::YAML->errstr;
503 4         1097 $self->rules( $yaml->[0] );
504             }
505 93         341 return;
506             }
507             }
508              
509             ##############################################################################
510              
511             =head2 Instance Methods
512              
513             =head3 C
514              
515             $harness->runtests(@tests);
516              
517             Accepts an array of C<@tests> to be run. This should generally be the
518             names of test files, but this is not required. Each element in C<@tests>
519             will be passed to C as a C. See
520             L for more information.
521              
522             It is possible to provide aliases that will be displayed in place of the
523             test name by supplying the test as a reference to an array containing
524             C<< [ $test, $alias ] >>:
525              
526             $harness->runtests( [ 't/foo.t', 'Foo Once' ],
527             [ 't/foo.t', 'Foo Twice' ] );
528              
529             Normally it is an error to attempt to run the same test twice. Aliases
530             allow you to overcome this limitation by giving each run of the test a
531             unique name.
532              
533             Tests will be run in the order found.
534              
535             If the environment variable C is defined it
536             should name a directory into which a copy of the raw TAP for each test
537             will be written. TAP is written to files named for each test.
538             Subdirectories will be created as needed.
539              
540             Returns a L containing the test results.
541              
542             =cut
543              
544             sub runtests {
545 44     44 1 88178 my ( $self, @tests ) = @_;
546              
547 44         237 my $aggregate = $self->_construct( $self->aggregator_class );
548              
549 44         388 $self->_make_callback( 'before_runtests', $aggregate );
550 44         329 $aggregate->start;
551             my $finish = sub {
552 42     42   125 my $interrupted = shift;
553 42         314 $aggregate->stop;
554 42         1329 $self->summary( $aggregate, $interrupted );
555 42         854 $self->_make_callback( 'after_runtests', $aggregate );
556 44         1481 };
557             my $run = sub {
558 44     44   262 $self->aggregate_tests( $aggregate, @tests );
559 42         206 $finish->();
560 44         319 };
561              
562 44 50       275 if ( $self->trap ) {
563             local $SIG{INT} = sub {
564 0     0   0 print "\n";
565 0         0 $finish->(1);
566 0         0 exit;
567 0         0 };
568 0         0 $run->();
569             }
570             else {
571 44         152 $run->();
572             }
573              
574 42         1658 return $aggregate;
575             }
576              
577             =head3 C
578              
579             $harness->summary( $aggregator );
580              
581             Output the summary for a L.
582              
583             =cut
584              
585             sub summary {
586 42     42 1 266 my ( $self, @args ) = @_;
587 42         189 $self->formatter->summary(@args);
588             }
589              
590             sub _after_test {
591 105     105   451 my ( $self, $aggregate, $job, $parser ) = @_;
592              
593 105         686 $self->_make_callback( 'after_test', $job->as_array_ref, $parser );
594 105         530 $aggregate->add( $job->description, $parser );
595             }
596              
597             sub _bailout {
598 2     2   9 my ( $self, $result ) = @_;
599 2         10 my $explanation = $result->explanation;
600 2 50       1855 die "FAILED--Further testing stopped"
601             . ( $explanation ? ": $explanation\n" : ".\n" );
602             }
603              
604             sub _aggregate_parallel {
605 1     1   6 my ( $self, $aggregate, $scheduler ) = @_;
606              
607 1         6 my $jobs = $self->jobs;
608 1         6 my $mux = $self->_construct( $self->multiplexer_class );
609              
610             RESULT: {
611              
612             # Keep multiplexer topped up
613 1         4 FILL:
614 5         21 while ( $mux->parsers < $jobs ) {
615 6         31 my $job = $scheduler->get_job;
616              
617             # If we hit a spinner stop filling and start running.
618 6 100 66     32 last FILL if !defined $job || $job->is_spinner;
619              
620 1         8 my ( $parser, $session ) = $self->make_parser($job);
621 1         16 $mux->add( $parser, [ $session, $job ] );
622             }
623              
624 5 50       23 if ( my ( $parser, $stash, $result ) = $mux->next ) {
625 5         18 my ( $session, $job ) = @$stash;
626 5 50       12 if ( defined $result ) {
627 5         26 $session->result($result);
628 5 100       14 $self->_bailout($result) if $result->is_bailout;
629             }
630             else {
631              
632             # End of parser. Automatically removed from the mux.
633 0         0 $self->finish_parser( $parser, $session );
634 0         0 $self->_after_test( $aggregate, $job, $parser );
635 0         0 $job->finish;
636             }
637 4         19 redo RESULT;
638             }
639             }
640              
641 0         0 return;
642             }
643              
644             sub _aggregate_single {
645 78     78   261 my ( $self, $aggregate, $scheduler ) = @_;
646              
647             JOB:
648 78         454 while ( my $job = $scheduler->get_job ) {
649 106 50       524 next JOB if $job->is_spinner;
650              
651 106         732 my ( $parser, $session ) = $self->make_parser($job);
652              
653 106         1132 while ( defined( my $result = $parser->next ) ) {
654 435         2553 $session->result($result);
655 435 100       2248 if ( $result->is_bailout ) {
656              
657             # Keep reading until input is exhausted in the hope
658             # of allowing any pending diagnostics to show up.
659 1         14 1 while $parser->next;
660 1         12 $self->_bailout($result);
661             }
662             }
663              
664 105         851 $self->finish_parser( $parser, $session );
665 105         943 $self->_after_test( $aggregate, $job, $parser );
666 105         656 $job->finish;
667             }
668              
669 77         410 return;
670             }
671              
672             =head3 C
673              
674             $harness->aggregate_tests( $aggregate, @tests );
675              
676             Run the named tests and display a summary of result. Tests will be run
677             in the order found.
678              
679             Test results will be added to the supplied L.
680             C may be called multiple times to run several sets of
681             tests. Multiple C instances may be used to pass results
682             to a single aggregator so that different parts of a complex test suite
683             may be run using different C settings. This is useful, for
684             example, in the case where some tests should run in parallel but others
685             are unsuitable for parallel execution.
686              
687             my $formatter = TAP::Formatter::Console->new;
688             my $ser_harness = TAP::Harness->new( { formatter => $formatter } );
689             my $par_harness = TAP::Harness->new(
690             { formatter => $formatter,
691             jobs => 9
692             }
693             );
694             my $aggregator = TAP::Parser::Aggregator->new;
695              
696             $aggregator->start();
697             $ser_harness->aggregate_tests( $aggregator, @ser_tests );
698             $par_harness->aggregate_tests( $aggregator, @par_tests );
699             $aggregator->stop();
700             $formatter->summary($aggregator);
701              
702             Note that for simpler testing requirements it will often be possible to
703             replace the above code with a single call to C.
704              
705             Each element of the C<@tests> array is either:
706              
707             =over
708              
709             =item * the source name of a test to run
710              
711             =item * a reference to a [ source name, display name ] array
712              
713             =back
714              
715             In the case of a perl test suite, typically I are simply the file
716             names of the test scripts to run.
717              
718             When you supply a separate display name it becomes possible to run a
719             test more than once; the display name is effectively the alias by which
720             the test is known inside the harness. The harness doesn't care if it
721             runs the same test more than once when each invocation uses a
722             different name.
723              
724             =cut
725              
726             sub aggregate_tests {
727 79     79 1 331 my ( $self, $aggregate, @tests ) = @_;
728              
729 79         303 my $jobs = $self->jobs;
730 79         387 my $scheduler = $self->make_scheduler(@tests);
731              
732             # #12458
733 79 100       505 local $ENV{HARNESS_IS_VERBOSE} = 1
734             if $self->formatter->verbosity > 0;
735              
736             # Formatter gets only names.
737 79         336 $self->formatter->prepare( map { $_->description } $scheduler->get_all );
  107         492  
738              
739 79 100       288 if ( $self->jobs > 1 ) {
740 1         8 $self->_aggregate_parallel( $aggregate, $scheduler );
741             }
742             else {
743 78         372 $self->_aggregate_single( $aggregate, $scheduler );
744             }
745              
746 77         879 return;
747             }
748              
749             sub _add_descriptions {
750 84     84   11286 my $self = shift;
751              
752             # Turn unwrapped scalars into anonymous arrays and copy the name as
753             # the description for tests that have only a name.
754 122 100       896 return map { @$_ == 1 ? [ $_->[0], $_->[0] ] : $_ }
755 84 100       289 map { 'ARRAY' eq ref $_ ? $_ : [$_] } @_;
  122         754  
756             }
757              
758             =head3 C
759              
760             Called by the harness when it needs to create a
761             L. Override in a subclass to provide an
762             alternative scheduler. C is passed the list of tests
763             that was passed to C.
764              
765             =cut
766              
767             sub make_scheduler {
768 79     79 1 312 my ( $self, @tests ) = @_;
769 79         474 return $self->_construct(
770             $self->scheduler_class,
771             tests => [ $self->_add_descriptions(@tests) ],
772             rules => $self->rules
773             );
774             }
775              
776             =head3 C
777              
778             Gets or sets the number of concurrent test runs the harness is
779             handling. By default, this value is 1 -- for parallel testing, this
780             should be set higher.
781              
782             =cut
783              
784             ##############################################################################
785              
786             sub _get_parser_args {
787 107     107   463 my ( $self, $job ) = @_;
788 107         478 my $test_prog = $job->filename;
789 107         443 my %args = ();
790              
791 107 100       730 $args{sources} = $self->sources if $self->sources;
792              
793 107         295 my @switches;
794 107 50       557 @switches = $self->lib if $self->lib;
795 107 50       499 push @switches => $self->switches if $self->switches;
796 107         449 $args{switches} = \@switches;
797 107         610 $args{spool} = $self->_open_spool($test_prog);
798 107         449 $args{merge} = $self->merge;
799 107         533 $args{ignore_exit} = $self->ignore_exit;
800 107 50       506 $args{version} = $self->version if $self->version;
801              
802 107 100       529 if ( my $exec = $self->exec ) {
803             $args{exec}
804 7 100       61 = ref $exec eq 'CODE'
805             ? $exec->( $self, $test_prog )
806             : [ @$exec, $test_prog ];
807 7 100 100     133 if ( not defined $args{exec} ) {
    100          
808 1         9 $args{source} = $test_prog;
809             }
810             elsif ( ( ref( $args{exec} ) || "" ) ne "ARRAY" ) {
811 2         14 $args{source} = delete $args{exec};
812             }
813             }
814             else {
815 100         356 $args{source} = $test_prog;
816             }
817              
818 107 100       449 if ( defined( my $test_args = $self->test_args ) ) {
819              
820 2 100       16 if ( ref($test_args) eq 'HASH' ) {
821              
822             # different args for each test
823 1 50       7 if ( exists( $test_args->{ $job->description } ) ) {
824 1         7 $test_args = $test_args->{ $job->description };
825             }
826             else {
827 0         0 $self->_croak( "TAP::Harness Can't find test_args for "
828             . $job->description );
829             }
830             }
831              
832 2         7 $args{test_args} = $test_args;
833             }
834              
835 107         554 return \%args;
836             }
837              
838             =head3 C
839              
840             Make a new parser and display formatter session. Typically used and/or
841             overridden in subclasses.
842              
843             my ( $parser, $session ) = $harness->make_parser;
844              
845             =cut
846              
847             sub make_parser {
848 107     107 1 386 my ( $self, $job ) = @_;
849              
850 107         674 my $args = $self->_get_parser_args($job);
851 107         714 $self->_make_callback( 'parser_args', $args, $job->as_array_ref );
852 107         763 my $parser = $self->_construct( $self->parser_class, $args );
853              
854 107         1503 $self->_make_callback( 'made_parser', $parser, $job->as_array_ref );
855 107         1207 my $session = $self->formatter->open_test( $job->description, $parser );
856              
857 107         2228 return ( $parser, $session );
858             }
859              
860             =head3 C
861              
862             Terminate use of a parser. Typically used and/or overridden in
863             subclasses. The parser isn't destroyed as a result of this.
864              
865             =cut
866              
867             sub finish_parser {
868 105     105 1 424 my ( $self, $parser, $session ) = @_;
869              
870 105         788 $session->close_test;
871 105         1712 $self->_close_spool($parser);
872              
873 105         273 return $parser;
874             }
875              
876             sub _open_spool {
877 108     108   2165 my $self = shift;
878 108         545 my $test = shift;
879              
880 108 100       720 if ( my $spool_dir = $ENV{PERL_TEST_HARNESS_DUMP_TAP} ) {
881              
882 2         32 my $spool = File::Spec->catfile( $spool_dir, $test );
883              
884             # Make the directory
885 2         78 my ( $vol, $dir, undef ) = File::Spec->splitpath($spool);
886 2         47 my $path = File::Spec->catpath( $vol, $dir, '' );
887 2         18 eval { mkpath($path) };
  2         1058  
888 2 50       19 $self->_croak($@) if $@;
889              
890 2         33 my $spool_handle = IO::Handle->new;
891 2 100       210 open( $spool_handle, ">$spool" )
892             or $self->_croak(" Can't write $spool ( $! ) ");
893              
894 1         12 return $spool_handle;
895             }
896              
897 106         405 return;
898             }
899              
900             sub _close_spool {
901 106     106   346 my $self = shift;
902 106         359 my ($parser) = @_;
903              
904 106 100       546 if ( my $spool_handle = $parser->delete_spool ) {
905 2 100       78 close($spool_handle)
906             or $self->_croak(" Error closing TAP spool file( $! ) \n ");
907             }
908              
909 105         323 return;
910             }
911              
912             sub _croak {
913 4     4   83 my ( $self, $message ) = @_;
914 4 50       23 unless ($message) {
915 0         0 $message = $self->_error;
916             }
917 4         79 $self->SUPER::_croak($message);
918              
919 0         0 return;
920             }
921              
922             1;
923              
924             __END__