<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package TAP::Harness;

use strict;
use warnings;
use Carp;

use File::Spec;
use File::Path;
use IO::Handle;

use base 'TAP::Base';

=head1 NAME

TAP::Harness - Run test scripts with statistics

=head1 VERSION

Version 3.38

=cut

our $VERSION = '3.38';

$ENV{HARNESS_ACTIVE}  = 1;
$ENV{HARNESS_VERSION} = $VERSION;

END {

    # For VMS.
    delete $ENV{HARNESS_ACTIVE};
    delete $ENV{HARNESS_VERSION};
}

=head1 DESCRIPTION

This is a simple test harness which allows tests to be run and results
automatically aggregated and output to STDOUT.

=head1 SYNOPSIS

 use TAP::Harness;
 my $harness = TAP::Harness-&gt;new( \%args );
 $harness-&gt;runtests(@tests);

=cut

my %VALIDATION_FOR;
my @FORMATTER_ARGS;

sub _error {
    my $self = shift;
    return $self-&gt;{error} unless @_;
    $self-&gt;{error} = shift;
}

BEGIN {

    @FORMATTER_ARGS = qw(
      directives verbosity timer failures comments errors stdout color
      show_count normalize
    );

    %VALIDATION_FOR = (
        lib =&gt; sub {
            my ( $self, $libs ) = @_;
            $libs = [$libs] unless 'ARRAY' eq ref $libs;

            return [ map {"-I$_"} @$libs ];
        },
        switches          =&gt; sub { shift; shift },
        exec              =&gt; sub { shift; shift },
        merge             =&gt; sub { shift; shift },
        aggregator_class  =&gt; sub { shift; shift },
        formatter_class   =&gt; sub { shift; shift },
        multiplexer_class =&gt; sub { shift; shift },
        parser_class      =&gt; sub { shift; shift },
        scheduler_class   =&gt; sub { shift; shift },
        formatter         =&gt; sub { shift; shift },
        jobs              =&gt; sub { shift; shift },
        test_args         =&gt; sub { shift; shift },
        ignore_exit       =&gt; sub { shift; shift },
        rules             =&gt; sub { shift; shift },
        rulesfile         =&gt; sub { shift; shift },
        sources           =&gt; sub { shift; shift },
        version           =&gt; sub { shift; shift },
        trap              =&gt; sub { shift; shift },
    );

    for my $method ( sort keys %VALIDATION_FOR ) {
        no strict 'refs';
        if ( $method eq 'lib' || $method eq 'switches' ) {
            *{$method} = sub {
                my $self = shift;
                unless (@_) {
                    $self-&gt;{$method} ||= [];
                    return wantarray
                      ? @{ $self-&gt;{$method} }
                      : $self-&gt;{$method};
                }
                $self-&gt;_croak("Too many arguments to method '$method'")
                  if @_ &gt; 1;
                my $args = shift;
                $args = [$args] unless ref $args;
                $self-&gt;{$method} = $args;
                return $self;
            };
        }
        else {
            *{$method} = sub {
                my $self = shift;
                return $self-&gt;{$method} unless @_;
                $self-&gt;{$method} = shift;
            };
        }
    }

    for my $method (@FORMATTER_ARGS) {
        no strict 'refs';
        *{$method} = sub {
            my $self = shift;
            return $self-&gt;formatter-&gt;$method(@_);
        };
    }
}

##############################################################################

=head1 METHODS

=head2 Class Methods

=head3 C&lt;new&gt;

 my %args = (
    verbosity =&gt; 1,
    lib     =&gt; [ 'lib', 'blib/lib', 'blib/arch' ],
 )
 my $harness = TAP::Harness-&gt;new( \%args );

The constructor returns a new C&lt;TAP::Harness&gt; object. It accepts an
optional hashref whose allowed keys are:

=over 4

=item * C&lt;verbosity&gt;

Set the verbosity level:

     1   verbose        Print individual test results to STDOUT.
     0   normal
    -1   quiet          Suppress some test output (mostly failures 
                        while tests are running).
    -2   really quiet   Suppress everything but the tests summary.
    -3   silent         Suppress everything.

=item * C&lt;timer&gt;

Append run time for each test to output. Uses L&lt;Time::HiRes&gt; if
available.

=item * C&lt;failures&gt;

Show test failures (this is a no-op if C&lt;verbose&gt; is selected).

=item * C&lt;comments&gt;

Show test comments (this is a no-op if C&lt;verbose&gt; is selected).

=item * C&lt;show_count&gt;

Update the running test count during testing.

=item * C&lt;normalize&gt;

Set to a true value to normalize the TAP that is emitted in verbose modes.

=item * C&lt;lib&gt;

Accepts a scalar value or array ref of scalar values indicating which
paths to allowed libraries should be included if Perl tests are
executed. Naturally, this only makes sense in the context of tests
written in Perl.

=item * C&lt;switches&gt;

Accepts a scalar value or array ref of scalar values indicating which
switches should be included if Perl tests are executed. Naturally, this
only makes sense in the context of tests written in Perl.

=item * C&lt;test_args&gt;

A reference to an C&lt;@INC&gt; style array of arguments to be passed to each
test program.

  test_args =&gt; ['foo', 'bar'],

if you want to pass different arguments to each test then you should
pass a hash of arrays, keyed by the alias for each test:

  test_args =&gt; {
    my_test    =&gt; ['foo', 'bar'],
    other_test =&gt; ['baz'],
  }

=item * C&lt;color&gt;

Attempt to produce color output.

=item * C&lt;exec&gt;

Typically, Perl tests are run through this. However, anything which
spits out TAP is fine. You can use this argument to specify the name of
the program (and optional switches) to run your tests with:

  exec =&gt; ['/usr/bin/ruby', '-w']

You can also pass a subroutine reference in order to determine and
return the proper program to run based on a given test script. The
subroutine reference should expect the TAP::Harness object itself as the
first argument, and the file name as the second argument. It should
return an array reference containing the command to be run and including
the test file name. It can also simply return C&lt;undef&gt;, in which case
TAP::Harness will fall back on executing the test script in Perl:

    exec =&gt; sub {
        my ( $harness, $test_file ) = @_;

        # Let Perl tests run.
        return undef if $test_file =~ /[.]t$/;
        return [ qw( /usr/bin/ruby -w ), $test_file ]
          if $test_file =~ /[.]rb$/;
      }

If the subroutine returns a scalar with a newline or a filehandle, it
will be interpreted as raw TAP or as a TAP stream, respectively.

=item * C&lt;merge&gt;

If C&lt;merge&gt; is true the harness will create parsers that merge STDOUT
and STDERR together for any processes they start.

=item * C&lt;sources&gt;

I&lt;NEW to 3.18&gt;.

If set, C&lt;sources&gt; must be a hashref containing the names of the
L&lt;TAP::Parser::SourceHandler&gt;s to load and/or configure.  The values are a
hash of configuration that will be accessible to the source handlers via
L&lt;TAP::Parser::Source/config_for&gt;.

For example:

  sources =&gt; {
    Perl =&gt; { exec =&gt; '/path/to/custom/perl' },
    File =&gt; { extensions =&gt; [ '.tap', '.txt' ] },
    MyCustom =&gt; { some =&gt; 'config' },
  }

The C&lt;sources&gt; parameter affects how C&lt;source&gt;, C&lt;tap&gt; and C&lt;exec&gt; parameters
are handled.

For more details, see the C&lt;sources&gt; parameter in L&lt;TAP::Parser/new&gt;,
L&lt;TAP::Parser::Source&gt;, and L&lt;TAP::Parser::IteratorFactory&gt;.

=item * C&lt;aggregator_class&gt;

The name of the class to use to aggregate test results. The default is
L&lt;TAP::Parser::Aggregator&gt;.

=item * C&lt;version&gt;

I&lt;NEW to 3.22&gt;.

Assume this TAP version for L&lt;TAP::Parser&gt; instead of default TAP
version 12.

=item * C&lt;formatter_class&gt;

The name of the class to use to format output. The default is
L&lt;TAP::Formatter::Console&gt;, or L&lt;TAP::Formatter::File&gt; if the output
isn't a TTY.

=item * C&lt;multiplexer_class&gt;

The name of the class to use to multiplex tests during parallel testing.
The default is L&lt;TAP::Parser::Multiplexer&gt;.

=item * C&lt;parser_class&gt;

The name of the class to use to parse TAP. The default is
L&lt;TAP::Parser&gt;.

=item * C&lt;scheduler_class&gt;

The name of the class to use to schedule test execution. The default is
L&lt;TAP::Parser::Scheduler&gt;.

=item * C&lt;formatter&gt;

If set C&lt;formatter&gt; must be an object that is capable of formatting the
TAP output. See L&lt;TAP::Formatter::Console&gt; for an example.

=item * C&lt;errors&gt;

If parse errors are found in the TAP output, a note of this will be
made in the summary report. To see all of the parse errors, set this
argument to true:

  errors =&gt; 1

=item * C&lt;directives&gt;

If set to a true value, only test results with directives will be
displayed. This overrides other settings such as C&lt;verbose&gt; or
C&lt;failures&gt;.

=item * C&lt;ignore_exit&gt;

If set to a true value instruct C&lt;TAP::Parser&gt; to ignore exit and wait
status from test scripts.

=item * C&lt;jobs&gt;

The maximum number of parallel tests to run at any time.  Which tests
can be run in parallel is controlled by C&lt;rules&gt;.  The default is to
run only one test at a time.

=item * C&lt;rules&gt;

A reference to a hash of rules that control which tests may be executed in
parallel. If no rules are declared and L&lt;CPAN::Meta::YAML&gt; is available,
C&lt;TAP::Harness&gt; attempts to load rules from a YAML file specified by the
C&lt;rulesfile&gt; parameter. If no rules file exists, the default is for all
tests to be eligible to be run in parallel.

Here some simple examples. For the full details of the data structure
and the related glob-style pattern matching, see
L&lt;TAP::Parser::Scheduler/"Rules data structure"&gt;.

    # Run all tests in sequence, except those starting with "p"
    $harness-&gt;rules({
        par =&gt; 't/p*.t'
    });

    # Equivalent YAML file
    ---
    par: t/p*.t

    # Run all tests in parallel, except those starting with "p"
    $harness-&gt;rules({
        seq =&gt; [
                  { seq =&gt; 't/p*.t' },
                  { par =&gt; '**'     },
               ],
    });

    # Equivalent YAML file
    ---
    seq:
        - seq: t/p*.t
        - par: **

    # Run some  startup tests in sequence, then some parallel tests than some
    # teardown tests in sequence.
    $harness-&gt;rules({
        seq =&gt; [
            { seq =&gt; 't/startup/*.t' },
            { par =&gt; ['t/a/*.t','t/b/*.t','t/c/*.t'], }
            { seq =&gt; 't/shutdown/*.t' },
        ],

    });

    # Equivalent YAML file
    ---
    seq:
        - seq: t/startup/*.t
        - par:
            - t/a/*.t
            - t/b/*.t
            - t/c/*.t
        - seq: t/shutdown/*.t

This is an experimental feature and the interface may change.

=item * C&lt;rulesfiles&gt;

This specifies where to find a YAML file of test scheduling rules.  If not
provided, it looks for a default file to use.  It first checks for a file given
in the C&lt;HARNESS_RULESFILE&gt; environment variable, then it checks for
F&lt;testrules.yml&gt; and then F&lt;t/testrules.yml&gt;.

=item * C&lt;stdout&gt;

A filehandle for catching standard output.

=item * C&lt;trap&gt;

Attempt to print summary information if run is interrupted by
SIGINT (Ctrl-C).

=back

Any keys for which the value is C&lt;undef&gt; will be ignored.

=cut

# new supplied by TAP::Base

{
    my @legal_callback = qw(
      parser_args
      made_parser
      before_runtests
      after_runtests
      after_test
    );

    my %default_class = (
        aggregator_class  =&gt; 'TAP::Parser::Aggregator',
        formatter_class   =&gt; 'TAP::Formatter::Console',
        multiplexer_class =&gt; 'TAP::Parser::Multiplexer',
        parser_class      =&gt; 'TAP::Parser',
        scheduler_class   =&gt; 'TAP::Parser::Scheduler',
    );

    sub _initialize {
        my ( $self, $arg_for ) = @_;
        $arg_for ||= {};

        $self-&gt;SUPER::_initialize( $arg_for, \@legal_callback );
        my %arg_for = %$arg_for;    # force a shallow copy

        for my $name ( sort keys %VALIDATION_FOR ) {
            my $property = delete $arg_for{$name};
            if ( defined $property ) {
                my $validate = $VALIDATION_FOR{$name};

                my $value = $self-&gt;$validate($property);
                if ( $self-&gt;_error ) {
                    $self-&gt;_croak;
                }
                $self-&gt;$name($value);
            }
        }

        $self-&gt;jobs(1) unless defined $self-&gt;jobs;

        if ( ! defined $self-&gt;rules ) {
            $self-&gt;_maybe_load_rulesfile;
        }

        local $default_class{formatter_class} = 'TAP::Formatter::File'
          unless -t ( $arg_for{stdout} || \*STDOUT ) &amp;&amp; !$ENV{HARNESS_NOTTY};

        while ( my ( $attr, $class ) = each %default_class ) {
            $self-&gt;$attr( $self-&gt;$attr() || $class );
        }

        unless ( $self-&gt;formatter ) {

            # This is a little bodge to preserve legacy behaviour. It's
            # pretty horrible that we know which args are destined for
            # the formatter.
            my %formatter_args = ( jobs =&gt; $self-&gt;jobs );
            for my $name (@FORMATTER_ARGS) {
                if ( defined( my $property = delete $arg_for{$name} ) ) {
                    $formatter_args{$name} = $property;
                }
            }

            $self-&gt;formatter(
                $self-&gt;_construct( $self-&gt;formatter_class, \%formatter_args )
            );
        }

        if ( my @props = sort keys %arg_for ) {
            $self-&gt;_croak("Unknown arguments to TAP::Harness::new (@props)");
        }

        return $self;
    }

    sub _maybe_load_rulesfile {
        my ($self) = @_;

        my ($rulesfile) =   defined $self-&gt;rulesfile ? $self-&gt;rulesfile :
                            defined($ENV{HARNESS_RULESFILE}) ? $ENV{HARNESS_RULESFILE} :
                            grep { -r } qw(./testrules.yml t/testrules.yml);

        if ( defined $rulesfile &amp;&amp; -r $rulesfile ) {
            if ( ! eval { require CPAN::Meta::YAML; 1} ) {
               warn "CPAN::Meta::YAML required to process $rulesfile" ;
               return;
            }
            my $layer = $] lt "5.008" ? "" : ":encoding(UTF-8)";
            open my $fh, "&lt;$layer", $rulesfile
                or die "Couldn't open $rulesfile: $!";
            my $yaml_text = do { local $/; &lt;$fh&gt; };
            my $yaml = CPAN::Meta::YAML-&gt;read_string($yaml_text)
                or die CPAN::Meta::YAML-&gt;errstr;
            $self-&gt;rules( $yaml-&gt;[0] );
        }
        return;
    }
}

##############################################################################

=head2 Instance Methods

=head3 C&lt;runtests&gt;

    $harness-&gt;runtests(@tests);

Accepts an array of C&lt;@tests&gt; to be run. This should generally be the
names of test files, but this is not required. Each element in C&lt;@tests&gt;
will be passed to C&lt;TAP::Parser::new()&gt; as a C&lt;source&gt;. See
L&lt;TAP::Parser&gt; for more information.

It is possible to provide aliases that will be displayed in place of the
test name by supplying the test as a reference to an array containing
C&lt;&lt; [ $test, $alias ] &gt;&gt;:

    $harness-&gt;runtests( [ 't/foo.t', 'Foo Once' ],
                        [ 't/foo.t', 'Foo Twice' ] );

Normally it is an error to attempt to run the same test twice. Aliases
allow you to overcome this limitation by giving each run of the test a
unique name.

Tests will be run in the order found.

If the environment variable C&lt;PERL_TEST_HARNESS_DUMP_TAP&gt; is defined it
should name a directory into which a copy of the raw TAP for each test
will be written. TAP is written to files named for each test.
Subdirectories will be created as needed.

Returns a L&lt;TAP::Parser::Aggregator&gt; containing the test results.

=cut

sub runtests {
    my ( $self, @tests ) = @_;

    my $aggregate = $self-&gt;_construct( $self-&gt;aggregator_class );

    $self-&gt;_make_callback( 'before_runtests', $aggregate );
    $aggregate-&gt;start;
    my $finish = sub {
        my $interrupted = shift;
        $aggregate-&gt;stop;
        $self-&gt;summary( $aggregate, $interrupted );
        $self-&gt;_make_callback( 'after_runtests', $aggregate );
    };
    my $run = sub {
        $self-&gt;aggregate_tests( $aggregate, @tests );
        $finish-&gt;();
    };

    if ( $self-&gt;trap ) {
        local $SIG{INT} = sub {
            print "\n";
            $finish-&gt;(1);
            exit;
        };
        $run-&gt;();
    }
    else {
        $run-&gt;();
    }

    return $aggregate;
}

=head3 C&lt;summary&gt;

  $harness-&gt;summary( $aggregator );

Output the summary for a L&lt;TAP::Parser::Aggregator&gt;.

=cut

sub summary {
    my ( $self, @args ) = @_;
    $self-&gt;formatter-&gt;summary(@args);
}

sub _after_test {
    my ( $self, $aggregate, $job, $parser ) = @_;

    $self-&gt;_make_callback( 'after_test', $job-&gt;as_array_ref, $parser );
    $aggregate-&gt;add( $job-&gt;description, $parser );
}

sub _bailout {
    my ( $self, $result ) = @_;
    my $explanation = $result-&gt;explanation;
    die "FAILED--Further testing stopped"
      . ( $explanation ? ": $explanation\n" : ".\n" );
}

sub _aggregate_parallel {
    my ( $self, $aggregate, $scheduler ) = @_;

    my $jobs = $self-&gt;jobs;
    my $mux  = $self-&gt;_construct( $self-&gt;multiplexer_class );

    RESULT: {

        # Keep multiplexer topped up
        FILL:
        while ( $mux-&gt;parsers &lt; $jobs ) {
            my $job = $scheduler-&gt;get_job;

            # If we hit a spinner stop filling and start running.
            last FILL if !defined $job || $job-&gt;is_spinner;

            my ( $parser, $session ) = $self-&gt;make_parser($job);
            $mux-&gt;add( $parser, [ $session, $job ] );
        }

        if ( my ( $parser, $stash, $result ) = $mux-&gt;next ) {
            my ( $session, $job ) = @$stash;
            if ( defined $result ) {
                $session-&gt;result($result);
                $self-&gt;_bailout($result) if $result-&gt;is_bailout;
            }
            else {

                # End of parser. Automatically removed from the mux.
                $self-&gt;finish_parser( $parser, $session );
                $self-&gt;_after_test( $aggregate, $job, $parser );
                $job-&gt;finish;
            }
            redo RESULT;
        }
    }

    return;
}

sub _aggregate_single {
    my ( $self, $aggregate, $scheduler ) = @_;

    JOB:
    while ( my $job = $scheduler-&gt;get_job ) {
        next JOB if $job-&gt;is_spinner;

        my ( $parser, $session ) = $self-&gt;make_parser($job);

        while ( defined( my $result = $parser-&gt;next ) ) {
            $session-&gt;result($result);
            if ( $result-&gt;is_bailout ) {

                # Keep reading until input is exhausted in the hope
                # of allowing any pending diagnostics to show up.
                1 while $parser-&gt;next;
                $self-&gt;_bailout($result);
            }
        }

        $self-&gt;finish_parser( $parser, $session );
        $self-&gt;_after_test( $aggregate, $job, $parser );
        $job-&gt;finish;
    }

    return;
}

=head3 C&lt;aggregate_tests&gt;

  $harness-&gt;aggregate_tests( $aggregate, @tests );

Run the named tests and display a summary of result. Tests will be run
in the order found.

Test results will be added to the supplied L&lt;TAP::Parser::Aggregator&gt;.
C&lt;aggregate_tests&gt; may be called multiple times to run several sets of
tests. Multiple C&lt;Test::Harness&gt; instances may be used to pass results
to a single aggregator so that different parts of a complex test suite
may be run using different C&lt;TAP::Harness&gt; settings. This is useful, for
example, in the case where some tests should run in parallel but others
are unsuitable for parallel execution.

    my $formatter   = TAP::Formatter::Console-&gt;new;
    my $ser_harness = TAP::Harness-&gt;new( { formatter =&gt; $formatter } );
    my $par_harness = TAP::Harness-&gt;new(
        {   formatter =&gt; $formatter,
            jobs      =&gt; 9
        }
    );
    my $aggregator = TAP::Parser::Aggregator-&gt;new;

    $aggregator-&gt;start();
    $ser_harness-&gt;aggregate_tests( $aggregator, @ser_tests );
    $par_harness-&gt;aggregate_tests( $aggregator, @par_tests );
    $aggregator-&gt;stop();
    $formatter-&gt;summary($aggregator);

Note that for simpler testing requirements it will often be possible to
replace the above code with a single call to C&lt;runtests&gt;.

Each element of the C&lt;@tests&gt; array is either:

=over

=item * the source name of a test to run

=item * a reference to a [ source name, display name ] array

=back

In the case of a perl test suite, typically I&lt;source names&gt; are simply the file
names of the test scripts to run.

When you supply a separate display name it becomes possible to run a
test more than once; the display name is effectively the alias by which
the test is known inside the harness. The harness doesn't care if it
runs the same test more than once when each invocation uses a
different name.

=cut

sub aggregate_tests {
    my ( $self, $aggregate, @tests ) = @_;

    my $jobs      = $self-&gt;jobs;
    my $scheduler = $self-&gt;make_scheduler(@tests);

    # #12458
    local $ENV{HARNESS_IS_VERBOSE} = 1
      if $self-&gt;formatter-&gt;verbosity &gt; 0;

    # Formatter gets only names.
    $self-&gt;formatter-&gt;prepare( map { $_-&gt;description } $scheduler-&gt;get_all );

    if ( $self-&gt;jobs &gt; 1 ) {
        $self-&gt;_aggregate_parallel( $aggregate, $scheduler );
    }
    else {
        $self-&gt;_aggregate_single( $aggregate, $scheduler );
    }

    return;
}

sub _add_descriptions {
    my $self = shift;

    # Turn unwrapped scalars into anonymous arrays and copy the name as
    # the description for tests that have only a name.
    return map { @$_ == 1 ? [ $_-&gt;[0], $_-&gt;[0] ] : $_ }
      map { 'ARRAY' eq ref $_ ? $_ : [$_] } @_;
}

=head3 C&lt;make_scheduler&gt;

Called by the harness when it needs to create a
L&lt;TAP::Parser::Scheduler&gt;. Override in a subclass to provide an
alternative scheduler. C&lt;make_scheduler&gt; is passed the list of tests
that was passed to C&lt;aggregate_tests&gt;.

=cut

sub make_scheduler {
    my ( $self, @tests ) = @_;
    return $self-&gt;_construct(
        $self-&gt;scheduler_class,
        tests =&gt; [ $self-&gt;_add_descriptions(@tests) ],
        rules =&gt; $self-&gt;rules
    );
}

=head3 C&lt;jobs&gt;

Gets or sets the number of concurrent test runs the harness is
handling.  By default, this value is 1 -- for parallel testing, this
should be set higher.

=cut

##############################################################################

sub _get_parser_args {
    my ( $self, $job ) = @_;
    my $test_prog = $job-&gt;filename;
    my %args      = ();

    $args{sources} = $self-&gt;sources if $self-&gt;sources;

    my @switches;
    @switches = $self-&gt;lib if $self-&gt;lib;
    push @switches =&gt; $self-&gt;switches if $self-&gt;switches;
    $args{switches}    = \@switches;
    $args{spool}       = $self-&gt;_open_spool($test_prog);
    $args{merge}       = $self-&gt;merge;
    $args{ignore_exit} = $self-&gt;ignore_exit;
    $args{version}     = $self-&gt;version if $self-&gt;version;

    if ( my $exec = $self-&gt;exec ) {
        $args{exec}
          = ref $exec eq 'CODE'
          ? $exec-&gt;( $self, $test_prog )
          : [ @$exec, $test_prog ];
        if ( not defined $args{exec} ) {
            $args{source} = $test_prog;
        }
        elsif ( ( ref( $args{exec} ) || "" ) ne "ARRAY" ) {
            $args{source} = delete $args{exec};
        }
    }
    else {
        $args{source} = $test_prog;
    }

    if ( defined( my $test_args = $self-&gt;test_args ) ) {

        if ( ref($test_args) eq 'HASH' ) {

            # different args for each test
            if ( exists( $test_args-&gt;{ $job-&gt;description } ) ) {
                $test_args = $test_args-&gt;{ $job-&gt;description };
            }
            else {
                $self-&gt;_croak( "TAP::Harness Can't find test_args for "
                      . $job-&gt;description );
            }
        }

        $args{test_args} = $test_args;
    }

    return \%args;
}

=head3 C&lt;make_parser&gt;

Make a new parser and display formatter session. Typically used and/or
overridden in subclasses.

    my ( $parser, $session ) = $harness-&gt;make_parser;

=cut

sub make_parser {
    my ( $self, $job ) = @_;

    my $args = $self-&gt;_get_parser_args($job);
    $self-&gt;_make_callback( 'parser_args', $args, $job-&gt;as_array_ref );
    my $parser = $self-&gt;_construct( $self-&gt;parser_class, $args );

    $self-&gt;_make_callback( 'made_parser', $parser, $job-&gt;as_array_ref );
    my $session = $self-&gt;formatter-&gt;open_test( $job-&gt;description, $parser );

    return ( $parser, $session );
}

=head3 C&lt;finish_parser&gt;

Terminate use of a parser. Typically used and/or overridden in
subclasses. The parser isn't destroyed as a result of this.

=cut

sub finish_parser {
    my ( $self, $parser, $session ) = @_;

    $session-&gt;close_test;
    $self-&gt;_close_spool($parser);

    return $parser;
}

sub _open_spool {
    my $self = shift;
    my $test = shift;

    if ( my $spool_dir = $ENV{PERL_TEST_HARNESS_DUMP_TAP} ) {

        my $spool = File::Spec-&gt;catfile( $spool_dir, $test );

        # Make the directory
        my ( $vol, $dir, undef ) = File::Spec-&gt;splitpath($spool);
        my $path = File::Spec-&gt;catpath( $vol, $dir, '' );
        eval { mkpath($path) };
        $self-&gt;_croak($@) if $@;

        my $spool_handle = IO::Handle-&gt;new;
        open( $spool_handle, "&gt;$spool" )
          or $self-&gt;_croak(" Can't write $spool ( $! ) ");

        return $spool_handle;
    }

    return;
}

sub _close_spool {
    my $self = shift;
    my ($parser) = @_;

    if ( my $spool_handle = $parser-&gt;delete_spool ) {
        close($spool_handle)
          or $self-&gt;_croak(" Error closing TAP spool file( $! ) \n ");
    }

    return;
}

sub _croak {
    my ( $self, $message ) = @_;
    unless ($message) {
        $message = $self-&gt;_error;
    }
    $self-&gt;SUPER::_croak($message);

    return;
}

1;

__END__

##############################################################################

=head1 CONFIGURING

C&lt;TAP::Harness&gt; is designed to be easy to configure.

=head2 Plugins

C&lt;TAP::Parser&gt; plugins let you change the way TAP is I&lt;input&gt; to and I&lt;output&gt;
from the parser.

L&lt;TAP::Parser::SourceHandler&gt;s handle TAP I&lt;input&gt;.  You can configure them
and load custom handlers using the C&lt;sources&gt; parameter to L&lt;/new&gt;.

L&lt;TAP::Formatter&gt;s handle TAP I&lt;output&gt;.  You can load custom formatters by
using the C&lt;formatter_class&gt; parameter to L&lt;/new&gt;.  To configure a formatter,
you currently need to instantiate it outside of L&lt;TAP::Harness&gt; and pass it in
with the C&lt;formatter&gt; parameter to L&lt;/new&gt;.  This I&lt;may&gt; be addressed by adding
a I&lt;formatters&gt; parameter to L&lt;/new&gt; in the future.

=head2 C&lt;Module::Build&gt;

L&lt;Module::Build&gt; version C&lt;0.30&gt; supports C&lt;TAP::Harness&gt;.

To load C&lt;TAP::Harness&gt; plugins, you'll need to use the C&lt;tap_harness_args&gt;
parameter to C&lt;new&gt;, typically from your C&lt;Build.PL&gt;.  For example:

  Module::Build-&gt;new(
      module_name        =&gt; 'MyApp',
      test_file_exts     =&gt; [qw(.t .tap .txt)],
      use_tap_harness    =&gt; 1,
      tap_harness_args   =&gt; {
          sources =&gt; {
              MyCustom =&gt; {},
              File =&gt; {
                  extensions =&gt; ['.tap', '.txt'],
              },
          },
          formatter_class =&gt; 'TAP::Formatter::HTML',
      },
      build_requires     =&gt; {
          'Module::Build' =&gt; '0.30',
          'TAP::Harness'  =&gt; '3.18',
      },
  )-&gt;create_build_script;

See L&lt;/new&gt;

=head2 C&lt;ExtUtils::MakeMaker&gt;

L&lt;ExtUtils::MakeMaker&gt; does not support L&lt;TAP::Harness&gt; out-of-the-box.

=head2 C&lt;prove&gt;

L&lt;prove&gt; supports C&lt;TAP::Harness&gt; plugins, and has a plugin system of its
own.  See L&lt;prove/FORMATTERS&gt;, L&lt;prove/SOURCE HANDLERS&gt; and L&lt;App::Prove&gt;
for more details.

=head1 WRITING PLUGINS

If you can't configure C&lt;TAP::Harness&gt; to do what you want, and you can't find
an existing plugin, consider writing one.

The two primary use cases supported by L&lt;TAP::Harness&gt; for plugins are I&lt;input&gt;
and I&lt;output&gt;:

=over 2

=item Customize how TAP gets into the parser

To do this, you can either extend an existing L&lt;TAP::Parser::SourceHandler&gt;,
or write your own.  It's a pretty simple API, and they can be loaded and
configured using the C&lt;sources&gt; parameter to L&lt;/new&gt;.

=item Customize how TAP results are output from the parser

To do this, you can either extend an existing L&lt;TAP::Formatter&gt;, or write your
own.  Writing formatters are a bit more involved than writing a
I&lt;SourceHandler&gt;, as you'll need to understand the L&lt;TAP::Parser&gt; API.  A
good place to start is by understanding how L&lt;/aggregate_tests&gt; works.

Custom formatters can be loaded configured using the C&lt;formatter_class&gt;
parameter to L&lt;/new&gt;.

=back

=head1 SUBCLASSING

If you can't configure C&lt;TAP::Harness&gt; to do exactly what you want, and writing
a plugin isn't an option, consider extending it.  It is designed to be (mostly)
easy to subclass, though the cases when sub-classing is necessary should be few
and far between.

=head2 Methods

The following methods are ones you may wish to override if you want to
subclass C&lt;TAP::Harness&gt;.

=over 4

=item L&lt;/new&gt;

=item L&lt;/runtests&gt;

=item L&lt;/summary&gt;

=back

=cut

=head1 REPLACING

If you like the C&lt;prove&gt; utility and L&lt;TAP::Parser&gt; but you want your
own harness, all you need to do is write one and provide C&lt;new&gt; and
C&lt;runtests&gt; methods. Then you can use the C&lt;prove&gt; utility like so:

 prove --harness My::Test::Harness

Note that while C&lt;prove&gt; accepts a list of tests (or things to be
tested), C&lt;new&gt; has a fairly rich set of arguments. You'll probably want
to read over this code carefully to see how all of them are being used.

=head1 SEE ALSO

L&lt;Test::Harness&gt;

=cut

# vim:ts=4:sw=4:et:sta
</pre></body></html>