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

use 5.006;

use strict;
use warnings;

use constant IS_WIN32 =&gt; ( $^O =~ /^(MS)?Win32$/ );
use constant IS_VMS =&gt; ( $^O eq 'VMS' );

use TAP::Harness                     ();
use TAP::Parser::Aggregator          ();
use TAP::Parser::Source              ();
use TAP::Parser::SourceHandler::Perl ();

use Text::ParseWords qw(shellwords);

use Config;
use base 'Exporter';

# $ML $Last_ML_Print

BEGIN {
    eval q{use Time::HiRes 'time'};
    our $has_time_hires = !$@;
}

=head1 NAME

Test::Harness - Run Perl standard test scripts with statistics

=head1 VERSION

Version 3.42

=cut

our $VERSION = '3.42';

# Backwards compatibility for exportable variable names.
*verbose  = *Verbose;
*switches = *Switches;
*debug    = *Debug;

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

END {

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

our @EXPORT    = qw(&amp;runtests);
our @EXPORT_OK = qw(&amp;execute_tests $verbose $switches);

our $Verbose = $ENV{HARNESS_VERBOSE} || 0;
our $Debug   = $ENV{HARNESS_DEBUG}   || 0;
our $Switches = '-w';
our $Columns = $ENV{HARNESS_COLUMNS} || $ENV{COLUMNS} || 80;
$Columns--;    # Some shells have trouble with a full line of text.
our $Timer      = $ENV{HARNESS_TIMER}       || 0;
our $Color      = $ENV{HARNESS_COLOR}       || 0;
our $IgnoreExit = $ENV{HARNESS_IGNORE_EXIT} || 0;

=head1 SYNOPSIS

  use Test::Harness;

  runtests(@test_files);

=head1 DESCRIPTION

Although, for historical reasons, the L&lt;Test::Harness&gt; distribution
takes its name from this module it now exists only to provide
L&lt;TAP::Harness&gt; with an interface that is somewhat backwards compatible
with L&lt;Test::Harness&gt; 2.xx. If you're writing new code consider using
L&lt;TAP::Harness&gt; directly instead.

Emulation is provided for C&lt;runtests&gt; and C&lt;execute_tests&gt; but the
pluggable 'Straps' interface that previous versions of L&lt;Test::Harness&gt;
supported is not reproduced here. Straps is now available as a stand
alone module: L&lt;Test::Harness::Straps&gt;.

See L&lt;TAP::Parser&gt;, L&lt;TAP::Harness&gt; for the main documentation for this
distribution.

=head1 FUNCTIONS

The following functions are available.

=head2 runtests( @test_files )

This runs all the given I&lt;@test_files&gt; and divines whether they passed
or failed based on their output to STDOUT (details above).  It prints
out each individual test which failed along with a summary report and
a how long it all took.

It returns true if everything was ok.  Otherwise it will C&lt;die()&gt; with
one of the messages in the DIAGNOSTICS section.

=cut

sub _has_taint {
    my $test = shift;
    return TAP::Parser::SourceHandler::Perl-&gt;get_taint(
        TAP::Parser::Source-&gt;shebang($test) );
}

sub _aggregate {
    my ( $harness, $aggregate, @tests ) = @_;

    # Don't propagate to our children
    local $ENV{HARNESS_OPTIONS};

    _apply_extra_INC($harness);
    _aggregate_tests( $harness, $aggregate, @tests );
}

# Make sure the child sees all the extra junk in @INC
sub _apply_extra_INC {
    my $harness = shift;

    $harness-&gt;callback(
        parser_args =&gt; sub {
            my ( $args, $test ) = @_;
            push @{ $args-&gt;{switches} }, map {"-I$_"} _filtered_inc();
        }
    );
}

sub _aggregate_tests {
    my ( $harness, $aggregate, @tests ) = @_;
    $aggregate-&gt;start();
    $harness-&gt;aggregate_tests( $aggregate, @tests );
    $aggregate-&gt;stop();

}

sub runtests {
    my @tests = @_;

    # shield against -l
    local ( $\, $, );

    my $harness   = _new_harness();
    my $aggregate = TAP::Parser::Aggregator-&gt;new();

    local $ENV{PERL_USE_UNSAFE_INC} = 1 if not exists $ENV{PERL_USE_UNSAFE_INC};
    _aggregate( $harness, $aggregate, @tests );

    $harness-&gt;formatter-&gt;summary($aggregate);

    my $total  = $aggregate-&gt;total;
    my $passed = $aggregate-&gt;passed;
    my $failed = $aggregate-&gt;failed;

    my @parsers = $aggregate-&gt;parsers;

    my $num_bad = 0;
    for my $parser (@parsers) {
        $num_bad++ if $parser-&gt;has_problems;
    }

    die(sprintf(
            "Failed %d/%d test programs. %d/%d subtests failed.\n",
            $num_bad, scalar @parsers, $failed, $total
        )
    ) if $num_bad;

    return $total &amp;&amp; $total == $passed;
}

sub _canon {
    my @list   = sort { $a &lt;=&gt; $b } @_;
    my @ranges = ();
    my $count  = scalar @list;
    my $pos    = 0;

    while ( $pos &lt; $count ) {
        my $end = $pos + 1;
        $end++ while $end &lt; $count &amp;&amp; $list[$end] &lt;= $list[ $end - 1 ] + 1;
        push @ranges, ( $end == $pos + 1 )
          ? $list[$pos]
          : join( '-', $list[$pos], $list[ $end - 1 ] );
        $pos = $end;
    }

    return join( ' ', @ranges );
}

sub _new_harness {
    my $sub_args = shift || {};

    my ( @lib, @switches );
    my @opt = map { shellwords($_) } grep { defined } $Switches, $ENV{HARNESS_PERL_SWITCHES};
    while ( my $opt = shift @opt ) {
        if ( $opt =~ /^ -I (.*) $ /x ) {
            push @lib, length($1) ? $1 : shift @opt;
        }
        else {
            push @switches, $opt;
        }
    }

    # Do things the old way on VMS...
    push @lib, _filtered_inc() if IS_VMS;

    # If $Verbose isn't numeric default to 1. This helps core.
    my $verbosity = ( $Verbose ? ( $Verbose !~ /\d/ ) ? 1 : $Verbose : 0 );

    my $args = {
        timer       =&gt; $Timer,
        directives  =&gt; our $Directives,
        lib         =&gt; \@lib,
        switches    =&gt; \@switches,
        color       =&gt; $Color,
        verbosity   =&gt; $verbosity,
        ignore_exit =&gt; $IgnoreExit,
    };

    $args-&gt;{stdout} = $sub_args-&gt;{out}
      if exists $sub_args-&gt;{out};

    my $class = $ENV{HARNESS_SUBCLASS} || 'TAP::Harness';
    if ( defined( my $env_opt = $ENV{HARNESS_OPTIONS} ) ) {
        for my $opt ( split /:/, $env_opt ) {
            if ( $opt =~ /^j(\d*)$/ ) {
                $args-&gt;{jobs} = $1 || 9;
            }
            elsif ( $opt eq 'c' ) {
                $args-&gt;{color} = 1;
            }
            elsif ( $opt =~ m/^f(.*)$/ ) {
                my $fmt = $1;
                $fmt =~ s/-/::/g;
                $args-&gt;{formatter_class} = $fmt;
            }
            elsif ( $opt =~ m/^a(.*)$/ ) {
                my $archive = $1;
                $class = "TAP::Harness::Archive";
                $args-&gt;{archive} = $archive;
            }
            else {
                die "Unknown HARNESS_OPTIONS item: $opt\n";
            }
        }
    }

    return TAP::Harness-&gt;_construct( $class, $args );
}

# Get the parts of @INC which are changed from the stock list AND
# preserve reordering of stock directories.
sub _filtered_inc {
    my @inc = grep { !ref } @INC;    #28567

    if (IS_VMS) {

        # VMS has a 255-byte limit on the length of %ENV entries, so
        # toss the ones that involve perl_root, the install location
        @inc = grep !/perl_root/i, @inc;

    }
    elsif (IS_WIN32) {

        # Lose any trailing backslashes in the Win32 paths
        s/[\\\/]+$// for @inc;
    }

    my @default_inc = _default_inc();

    my @new_inc;
    my %seen;
    for my $dir (@inc) {
        next if $seen{$dir}++;

        if ( $dir eq ( $default_inc[0] || '' ) ) {
            shift @default_inc;
        }
        else {
            push @new_inc, $dir;
        }

        shift @default_inc while @default_inc and $seen{ $default_inc[0] };
    }

    return @new_inc;
}

{

    # Cache this to avoid repeatedly shelling out to Perl.
    my @inc;

    sub _default_inc {
        return @inc if @inc;

        local $ENV{PERL5LIB};
        local $ENV{PERLLIB};

        my $perl = $ENV{HARNESS_PERL} || $^X;

        # Avoid using -l for the benefit of Perl 6
        chomp( @inc = `"$perl" -e "print join qq[\\n], \@INC, q[]"` );
        return @inc;
    }
}

sub _check_sequence {
    my @list = @_;
    my $prev;
    while ( my $next = shift @list ) {
        return if defined $prev &amp;&amp; $next &lt;= $prev;
        $prev = $next;
    }

    return 1;
}

sub execute_tests {
    my %args = @_;

    my $harness   = _new_harness( \%args );
    my $aggregate = TAP::Parser::Aggregator-&gt;new();

    my %tot = (
        bonus       =&gt; 0,
        max         =&gt; 0,
        ok          =&gt; 0,
        bad         =&gt; 0,
        good        =&gt; 0,
        files       =&gt; 0,
        tests       =&gt; 0,
        sub_skipped =&gt; 0,
        todo        =&gt; 0,
        skipped     =&gt; 0,
        bench       =&gt; undef,
    );

    # Install a callback so we get to see any plans the
    #Â&nbsp;harness executes.
    $harness-&gt;callback(
        made_parser =&gt; sub {
            my $parser = shift;
            $parser-&gt;callback(
                plan =&gt; sub {
                    my $plan = shift;
                    if ( $plan-&gt;directive eq 'SKIP' ) {
                        $tot{skipped}++;
                    }
                }
            );
        }
    );

    local $ENV{PERL_USE_UNSAFE_INC} = 1 if not exists $ENV{PERL_USE_UNSAFE_INC};
    _aggregate( $harness, $aggregate, @{ $args{tests} } );

    $tot{bench} = $aggregate-&gt;elapsed;
    my @tests = $aggregate-&gt;descriptions;

    # TODO: Work out the circumstances under which the files
    # and tests totals can differ.
    $tot{files} = $tot{tests} = scalar @tests;

    my %failedtests = ();
    my %todo_passed = ();

    for my $test (@tests) {
        my ($parser) = $aggregate-&gt;parsers($test);

        my @failed = $parser-&gt;failed;

        my $wstat         = $parser-&gt;wait;
        my $estat         = $parser-&gt;exit;
        my $planned       = $parser-&gt;tests_planned;
        my @errors        = $parser-&gt;parse_errors;
        my $passed        = $parser-&gt;passed;
        my $actual_passed = $parser-&gt;actual_passed;

        my $ok_seq = _check_sequence( $parser-&gt;actual_passed );

        # Duplicate exit, wait status semantics of old version
        $estat ||= '' unless $wstat;
        $wstat ||= '';

        $tot{max} += ( $planned || 0 );
        $tot{bonus} += $parser-&gt;todo_passed;
        $tot{ok} += $passed &gt; $actual_passed ? $passed : $actual_passed;
        $tot{sub_skipped} += $parser-&gt;skipped;
        $tot{todo}        += $parser-&gt;todo;

        if ( @failed || $estat || @errors ) {
            $tot{bad}++;

            my $huh_planned = $planned ? undef : '??';
            my $huh_errors  = $ok_seq  ? undef : '??';

            $failedtests{$test} = {
                'canon' =&gt; $huh_planned
                  || $huh_errors
                  || _canon(@failed)
                  || '??',
                'estat'  =&gt; $estat,
                'failed' =&gt; $huh_planned
                  || $huh_errors
                  || scalar @failed,
                'max' =&gt; $huh_planned || $planned,
                'name'  =&gt; $test,
                'wstat' =&gt; $wstat
            };
        }
        else {
            $tot{good}++;
        }

        my @todo = $parser-&gt;todo_passed;
        if (@todo) {
            $todo_passed{$test} = {
                'canon'  =&gt; _canon(@todo),
                'estat'  =&gt; $estat,
                'failed' =&gt; scalar @todo,
                'max'    =&gt; scalar $parser-&gt;todo,
                'name'   =&gt; $test,
                'wstat'  =&gt; $wstat
            };
        }
    }

    return ( \%tot, \%failedtests, \%todo_passed );
}

=head2 execute_tests( tests =&gt; \@test_files, out =&gt; \*FH )

Runs all the given C&lt;@test_files&gt; (just like C&lt;runtests()&gt;) but
doesn't generate the final report.  During testing, progress
information will be written to the currently selected output
filehandle (usually C&lt;STDOUT&gt;), or to the filehandle given by the
C&lt;out&gt; parameter.  The I&lt;out&gt; is optional.

Returns a list of two values, C&lt;$total&gt; and C&lt;$failed&gt;, describing the
results.  C&lt;$total&gt; is a hash ref summary of all the tests run.  Its
keys and values are this:

    bonus           Number of individual todo tests unexpectedly passed
    max             Number of individual tests ran
    ok              Number of individual tests passed
    sub_skipped     Number of individual tests skipped
    todo            Number of individual todo tests

    files           Number of test files ran
    good            Number of test files passed
    bad             Number of test files failed
    tests           Number of test files originally given
    skipped         Number of test files skipped

If C&lt;&lt; $total-&gt;{bad} == 0 &gt;&gt; and C&lt;&lt; $total-&gt;{max} &gt; 0 &gt;&gt;, you've
got a successful test.

C&lt;$failed&gt; is a hash ref of all the test scripts that failed.  Each key
is the name of a test script, each value is another hash representing
how that script failed.  Its keys are these:

    name        Name of the test which failed
    estat       Script's exit value
    wstat       Script's wait status
    max         Number of individual tests
    failed      Number which failed
    canon       List of tests which failed (as string).

C&lt;$failed&gt; should be empty if everything passed.

=cut

1;
__END__

=head1 EXPORT

C&lt;&amp;runtests&gt; is exported by C&lt;Test::Harness&gt; by default.

C&lt;&amp;execute_tests&gt;, C&lt;$verbose&gt;, C&lt;$switches&gt; and C&lt;$debug&gt; are
exported upon request.

=head1 ENVIRONMENT VARIABLES THAT TAP::HARNESS::COMPATIBLE SETS

C&lt;Test::Harness&gt; sets these before executing the individual tests.

=over 4

=item C&lt;HARNESS_ACTIVE&gt;

This is set to a true value.  It allows the tests to determine if they
are being executed through the harness or by any other means.

=item C&lt;HARNESS_VERSION&gt;

This is the version of C&lt;Test::Harness&gt;.

=back

=head1 ENVIRONMENT VARIABLES THAT AFFECT TEST::HARNESS

=over 4

=item C&lt;HARNESS_PERL_SWITCHES&gt;

Setting this adds perl command line switches to each test file run.

For example, C&lt;HARNESS_PERL_SWITCHES=-T&gt; will turn on taint mode.
C&lt;HARNESS_PERL_SWITCHES=-MDevel::Cover&gt; will run C&lt;Devel::Cover&gt; for
each test.

C&lt;-w&gt; is always set.  You can turn this off in the test with C&lt;BEGIN {
$^W = 0 }&gt;.

=item C&lt;HARNESS_TIMER&gt;

Setting this to true will make the harness display the number of
milliseconds each test took.  You can also use F&lt;prove&gt;'s C&lt;--timer&gt;
switch.

=item C&lt;HARNESS_VERBOSE&gt;

If true, C&lt;Test::Harness&gt; will output the verbose results of running
its tests.  Setting C&lt;$Test::Harness::verbose&gt; will override this,
or you can use the C&lt;-v&gt; switch in the F&lt;prove&gt; utility.

=item C&lt;HARNESS_OPTIONS&gt;

Provide additional options to the harness. Currently supported options are:

=over

=item C&lt;&lt; j&lt;n&gt; &gt;&gt;

Run &lt;n&gt; (default 9) parallel jobs.

=item C&lt;&lt; c &gt;&gt;

Try to color output. See L&lt;TAP::Formatter::Base/"new"&gt;.

=item C&lt;&lt; a&lt;file.tgz&gt; &gt;&gt;

Will use L&lt;TAP::Harness::Archive&gt; as the harness class, and save the TAP to
C&lt;file.tgz&gt;

=item C&lt;&lt; fPackage-With-Dashes &gt;&gt;

Set the formatter_class of the harness being run. Since the C&lt;HARNESS_OPTIONS&gt;
is seperated by C&lt;:&gt;, we use C&lt;-&gt; instead.

=back

Multiple options may be separated by colons:

    HARNESS_OPTIONS=j9:c make test

=item C&lt;HARNESS_SUBCLASS&gt;

Specifies a TAP::Harness subclass to be used in place of TAP::Harness.

=item C&lt;HARNESS_SUMMARY_COLOR_SUCCESS&gt;

Determines the L&lt;Term::ANSIColor&gt; for the summary in case it is successful.
This color defaults to C&lt;'green'&gt;.

=item C&lt;HARNESS_SUMMARY_COLOR_FAIL&gt;

Determines the L&lt;Term::ANSIColor&gt; for the failure in case it is successful.
This color defaults to C&lt;'red'&gt;.

=back

=head1 Taint Mode

Normally when a Perl program is run in taint mode the contents of the
C&lt;PERL5LIB&gt; environment variable do not appear in C&lt;@INC&gt;.

Because C&lt;PERL5LIB&gt; is often used during testing to add build
directories to C&lt;@INC&gt; C&lt;Test::Harness&gt; passes the names of any
directories found in C&lt;PERL5LIB&gt; as -I switches. The net effect of this
is that C&lt;PERL5LIB&gt; is honoured even in taint mode.

=head1 SEE ALSO

L&lt;TAP::Harness&gt;

=head1 BUGS

Please report any bugs or feature requests to
C&lt;bug-test-harness at rt.cpan.org&gt;, or through the web interface at
L&lt;http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Harness&gt;.  I will be 
notified, and then you'll automatically be notified of progress on your bug 
as I make changes.

=head1 AUTHORS

Andy Armstrong  C&lt;&lt; &lt;andy@hexten.net&gt; &gt;&gt;

L&lt;Test::Harness&gt; 2.64 (maintained by Andy Lester and on which this
module is based) has this attribution:

    Either Tim Bunce or Andreas Koenig, we don't know. What we know for
    sure is, that it was inspired by Larry Wall's F&lt;TEST&gt; script that came
    with perl distributions for ages. Numerous anonymous contributors
    exist.  Andreas Koenig held the torch for many years, and then
    Michael G Schwern.

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2007-2011, Andy Armstrong C&lt;&lt; &lt;andy@hexten.net&gt; &gt;&gt;. All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L&lt;perlartistic&gt;.

</pre></body></html>