16fb12b70Safresh1package TAP::Harness::Env;
26fb12b70Safresh1
36fb12b70Safresh1use strict;
46fb12b70Safresh1use warnings;
56fb12b70Safresh1
66fb12b70Safresh1use constant IS_VMS => ( $^O eq 'VMS' );
76fb12b70Safresh1use TAP::Object;
86fb12b70Safresh1use Text::ParseWords qw/shellwords/;
96fb12b70Safresh1
10*eac174f2Safresh1our $VERSION = '3.44';
116fb12b70Safresh1
126fb12b70Safresh1# Get the parts of @INC which are changed from the stock list AND
136fb12b70Safresh1# preserve reordering of stock directories.
146fb12b70Safresh1sub _filtered_inc_vms {
156fb12b70Safresh1    my @inc = grep { !ref } @INC;    #28567
166fb12b70Safresh1
176fb12b70Safresh1    # VMS has a 255-byte limit on the length of %ENV entries, so
186fb12b70Safresh1    # toss the ones that involve perl_root, the install location
196fb12b70Safresh1    @inc = grep { !/perl_root/i } @inc;
206fb12b70Safresh1
216fb12b70Safresh1    my @default_inc = _default_inc();
226fb12b70Safresh1
236fb12b70Safresh1    my @new_inc;
246fb12b70Safresh1    my %seen;
256fb12b70Safresh1    for my $dir (@inc) {
266fb12b70Safresh1        next if $seen{$dir}++;
276fb12b70Safresh1
286fb12b70Safresh1        if ( $dir eq ( $default_inc[0] || '' ) ) {
296fb12b70Safresh1            shift @default_inc;
306fb12b70Safresh1        }
316fb12b70Safresh1        else {
326fb12b70Safresh1            push @new_inc, $dir;
336fb12b70Safresh1        }
346fb12b70Safresh1
356fb12b70Safresh1        shift @default_inc while @default_inc and $seen{ $default_inc[0] };
366fb12b70Safresh1    }
376fb12b70Safresh1    return @new_inc;
386fb12b70Safresh1}
396fb12b70Safresh1
406fb12b70Safresh1# Cache this to avoid repeatedly shelling out to Perl.
416fb12b70Safresh1my @inc;
426fb12b70Safresh1
436fb12b70Safresh1sub _default_inc {
446fb12b70Safresh1    return @inc if @inc;
456fb12b70Safresh1
466fb12b70Safresh1    local $ENV{PERL5LIB};
476fb12b70Safresh1    local $ENV{PERLLIB};
486fb12b70Safresh1
496fb12b70Safresh1    my $perl = $ENV{HARNESS_PERL} || $^X;
506fb12b70Safresh1
516fb12b70Safresh1    # Avoid using -l for the benefit of Perl 6
526fb12b70Safresh1    chomp( @inc = `"$perl" -e "print join qq[\\n], \@INC, q[]"` );
536fb12b70Safresh1    return @inc;
546fb12b70Safresh1}
556fb12b70Safresh1
566fb12b70Safresh1sub create {
576fb12b70Safresh1	my $package = shift;
586fb12b70Safresh1    my %input = %{ shift || {} };
596fb12b70Safresh1
606fb12b70Safresh1    my @libs         = @{ delete $input{libs}     || [] };
616fb12b70Safresh1    my @raw_switches = @{ delete $input{switches} || [] };
626fb12b70Safresh1    my @opt
636fb12b70Safresh1      = ( @raw_switches, shellwords( $ENV{HARNESS_PERL_SWITCHES} || '' ) );
646fb12b70Safresh1    my @switches;
656fb12b70Safresh1    while ( my $opt = shift @opt ) {
666fb12b70Safresh1        if ( $opt =~ /^ -I (.*) $ /x ) {
676fb12b70Safresh1            push @libs, length($1) ? $1 : shift @opt;
686fb12b70Safresh1        }
696fb12b70Safresh1        else {
706fb12b70Safresh1            push @switches, $opt;
716fb12b70Safresh1        }
726fb12b70Safresh1    }
736fb12b70Safresh1
746fb12b70Safresh1    # Do things the old way on VMS...
756fb12b70Safresh1    push @libs, _filtered_inc_vms() if IS_VMS;
766fb12b70Safresh1
776fb12b70Safresh1    # If $Verbose isn't numeric default to 1. This helps core.
786fb12b70Safresh1    my $verbose
796fb12b70Safresh1      = $ENV{HARNESS_VERBOSE}
806fb12b70Safresh1      ? $ENV{HARNESS_VERBOSE} !~ /\d/
816fb12b70Safresh1          ? 1
826fb12b70Safresh1          : $ENV{HARNESS_VERBOSE}
836fb12b70Safresh1      : 0;
846fb12b70Safresh1
856fb12b70Safresh1    my %args = (
866fb12b70Safresh1        lib         => \@libs,
876fb12b70Safresh1        timer       => $ENV{HARNESS_TIMER} || 0,
886fb12b70Safresh1        switches    => \@switches,
896fb12b70Safresh1        color       => $ENV{HARNESS_COLOR} || 0,
906fb12b70Safresh1        verbosity   => $verbose,
916fb12b70Safresh1        ignore_exit => $ENV{HARNESS_IGNORE_EXIT} || 0,
926fb12b70Safresh1    );
936fb12b70Safresh1
94b8851fccSafresh1    my $class = delete $input{harness_class} || $ENV{HARNESS_SUBCLASS} || 'TAP::Harness';
956fb12b70Safresh1    if ( defined( my $env_opt = $ENV{HARNESS_OPTIONS} ) ) {
966fb12b70Safresh1        for my $opt ( split /:/, $env_opt ) {
976fb12b70Safresh1            if ( $opt =~ /^j(\d*)$/ ) {
986fb12b70Safresh1                $args{jobs} = $1 || 9;
996fb12b70Safresh1            }
1006fb12b70Safresh1            elsif ( $opt eq 'c' ) {
1016fb12b70Safresh1                $args{color} = 1;
1026fb12b70Safresh1            }
1036fb12b70Safresh1            elsif ( $opt =~ m/^f(.*)$/ ) {
1046fb12b70Safresh1                my $fmt = $1;
1056fb12b70Safresh1                $fmt =~ s/-/::/g;
1066fb12b70Safresh1                $args{formatter_class} = $fmt;
1076fb12b70Safresh1            }
1086fb12b70Safresh1            elsif ( $opt =~ m/^a(.*)$/ ) {
1096fb12b70Safresh1                my $archive = $1;
1106fb12b70Safresh1                $class = 'TAP::Harness::Archive';
1116fb12b70Safresh1                $args{archive} = $archive;
1126fb12b70Safresh1            }
1136fb12b70Safresh1            else {
1146fb12b70Safresh1                die "Unknown HARNESS_OPTIONS item: $opt\n";
1156fb12b70Safresh1            }
1166fb12b70Safresh1        }
1176fb12b70Safresh1    }
1186fb12b70Safresh1    return TAP::Object->_construct($class, { %args, %input });
1196fb12b70Safresh1}
1206fb12b70Safresh1
1216fb12b70Safresh11;
1226fb12b70Safresh1
1236fb12b70Safresh1=head1 NAME
1246fb12b70Safresh1
1256fb12b70Safresh1TAP::Harness::Env - Parsing harness related environmental variables where appropriate
1266fb12b70Safresh1
1276fb12b70Safresh1=head1 VERSION
1286fb12b70Safresh1
129*eac174f2Safresh1Version 3.44
1306fb12b70Safresh1
1316fb12b70Safresh1=head1 SYNOPSIS
1326fb12b70Safresh1
133b8851fccSafresh1 my $harness = TAP::Harness::Env->create(\%extra_args)
1346fb12b70Safresh1
1356fb12b70Safresh1=head1 DESCRIPTION
1366fb12b70Safresh1
137b8851fccSafresh1This module implements the environmental variables that L<Test::Harness> uses with TAP::Harness, and instantiates the appropriate class with the appropriate arguments.
1386fb12b70Safresh1
139b8851fccSafresh1=head1 METHODS
1406fb12b70Safresh1
1416fb12b70Safresh1=over 4
1426fb12b70Safresh1
143b8851fccSafresh1=item * create( \%args )
1446fb12b70Safresh1
145b8851fccSafresh1This function reads the environment and generates an appropriate argument hash from it. If given any arguments in C<%extra_args>, these will override the environmental defaults. In accepts C<harness_class> (which defaults to C<TAP::Harness>), and any argument the harness class accepts.
146b8851fccSafresh1
147b8851fccSafresh1=back
148b8851fccSafresh1
149b8851fccSafresh1=head1 ENVIRONMENTAL VARIABLES
150b8851fccSafresh1
151b8851fccSafresh1=over 4
152b8851fccSafresh1
153b8851fccSafresh1=item C<HARNESS_PERL_SWITCHES>
154b8851fccSafresh1
155b8851fccSafresh1Setting this adds perl command line switches to each test file run.
156b8851fccSafresh1
157b8851fccSafresh1For example, C<HARNESS_PERL_SWITCHES=-T> will turn on taint mode.
158b8851fccSafresh1C<HARNESS_PERL_SWITCHES=-MDevel::Cover> will run C<Devel::Cover> for
159b8851fccSafresh1each test.
160b8851fccSafresh1
161b8851fccSafresh1=item C<HARNESS_VERBOSE>
162b8851fccSafresh1
163b8851fccSafresh1If true, C<TAP::Harness> will output the verbose results of running
164b8851fccSafresh1its tests.
165b8851fccSafresh1
166b8851fccSafresh1=item C<HARNESS_SUBCLASS>
167b8851fccSafresh1
168b8851fccSafresh1Specifies a TAP::Harness subclass to be used in place of TAP::Harness.
169b8851fccSafresh1
170b8851fccSafresh1=item C<HARNESS_OPTIONS>
171b8851fccSafresh1
172b8851fccSafresh1Provide additional options to the harness. Currently supported options are:
173b8851fccSafresh1
174b8851fccSafresh1=over
175b8851fccSafresh1
176b8851fccSafresh1=item C<< j<n> >>
177b8851fccSafresh1
178b8851fccSafresh1Run <n> (default 9) parallel jobs.
179b8851fccSafresh1
180b8851fccSafresh1=item C<< c >>
181b8851fccSafresh1
182b8851fccSafresh1Try to color output. See L<TAP::Formatter::Base/"new">.
183b8851fccSafresh1
184b8851fccSafresh1=item C<< a<file.tgz> >>
185b8851fccSafresh1
186b8851fccSafresh1Will use L<TAP::Harness::Archive> as the harness class, and save the TAP to
187b8851fccSafresh1C<file.tgz>
188b8851fccSafresh1
189b8851fccSafresh1=item C<< fPackage-With-Dashes >>
190b8851fccSafresh1
191b8851fccSafresh1Set the formatter_class of the harness being run. Since the C<HARNESS_OPTIONS>
192*eac174f2Safresh1is separated by C<:>, we use C<-> instead.
193b8851fccSafresh1
194b8851fccSafresh1=back
195b8851fccSafresh1
196b8851fccSafresh1Multiple options may be separated by colons:
197b8851fccSafresh1
198b8851fccSafresh1    HARNESS_OPTIONS=j9:c make test
199b8851fccSafresh1
200b8851fccSafresh1=item C<HARNESS_TIMER>
201b8851fccSafresh1
202b8851fccSafresh1Setting this to true will make the harness display the number of
203b8851fccSafresh1milliseconds each test took.  You can also use F<prove>'s C<--timer>
204b8851fccSafresh1switch.
205b8851fccSafresh1
206b8851fccSafresh1=item C<HARNESS_COLOR>
207b8851fccSafresh1
208b8851fccSafresh1Attempt to produce color output.
209b8851fccSafresh1
210b8851fccSafresh1=item C<HARNESS_IGNORE_EXIT>
211b8851fccSafresh1
212b8851fccSafresh1If set to a true value instruct C<TAP::Parser> to ignore exit and wait
213b8851fccSafresh1status from test scripts.
2146fb12b70Safresh1
2156fb12b70Safresh1=back
216