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

use strict;
use warnings;

use TAP::Parser::IteratorFactory   ();
use TAP::Parser::Iterator::Process ();

use base 'TAP::Parser::SourceHandler';

TAP::Parser::IteratorFactory-&gt;register_handler(__PACKAGE__);

=head1 NAME

TAP::Parser::SourceHandler::Executable - Stream output from an executable TAP source

=head1 VERSION

Version 3.44

=cut

our $VERSION = '3.44';

=head1 SYNOPSIS

  use TAP::Parser::Source;
  use TAP::Parser::SourceHandler::Executable;

  my $source = TAP::Parser::Source-&gt;new-&gt;raw(['/usr/bin/ruby', 'mytest.rb']);
  $source-&gt;assemble_meta;

  my $class = 'TAP::Parser::SourceHandler::Executable';
  my $vote  = $class-&gt;can_handle( $source );
  my $iter  = $class-&gt;make_iterator( $source );

=head1 DESCRIPTION

This is an I&lt;executable&gt; L&lt;TAP::Parser::SourceHandler&gt; - it has 2 jobs:

1. Figure out if the L&lt;TAP::Parser::Source&gt; it's given is an executable
   command (L&lt;/can_handle&gt;).

2. Creates an iterator for executable commands (L&lt;/make_iterator&gt;).

Unless you're writing a plugin or subclassing L&lt;TAP::Parser&gt;, you
probably won't need to use this module directly.

=head1 METHODS

=head2 Class Methods

=head3 C&lt;can_handle&gt;

  my $vote = $class-&gt;can_handle( $source );

Only votes if $source looks like an executable file. Casts the
following votes:

  0.9  if it's a hash with an 'exec' key
  0.8  if it's a .bat file
  0.75 if it's got an execute bit set

=cut

sub can_handle {
    my ( $class, $src ) = @_;
    my $meta = $src-&gt;meta;

    if ( $meta-&gt;{is_file} ) {
        my $file = $meta-&gt;{file};

        return 0.85 if $file-&gt;{execute} &amp;&amp; $file-&gt;{binary};
        return 0.8 if $file-&gt;{lc_ext} eq '.bat';
        return 0.25 if $file-&gt;{execute};
    }
    elsif ( $meta-&gt;{is_hash} ) {
        return 0.9 if $src-&gt;raw-&gt;{exec};
    }

    return 0;
}

=head3 C&lt;make_iterator&gt;

  my $iterator = $class-&gt;make_iterator( $source );

Returns a new L&lt;TAP::Parser::Iterator::Process&gt; for the source.
C&lt;$source-E&lt;gt&gt;raw&gt; must be in one of the following forms:

  { exec =&gt; [ @exec ] }

  [ @exec ]

  $file

C&lt;croak&gt;s on error.

=cut

sub make_iterator {
    my ( $class, $source ) = @_;
    my $meta = $source-&gt;meta;

    my @command;
    if ( $meta-&gt;{is_hash} ) {
        @command = @{ $source-&gt;raw-&gt;{exec} || [] };
    }
    elsif ( $meta-&gt;{is_scalar} ) {
        @command = ${ $source-&gt;raw };
    }
    elsif ( $meta-&gt;{is_array} ) {
        @command = @{ $source-&gt;raw };
    }

    $class-&gt;_croak('No command found in $source-&gt;raw!') unless @command;

    $class-&gt;_autoflush( \*STDOUT );
    $class-&gt;_autoflush( \*STDERR );

    push @command, @{ $source-&gt;test_args || [] };

    return $class-&gt;iterator_class-&gt;new(
        {   command =&gt; \@command,
            merge   =&gt; $source-&gt;merge
        }
    );
}

=head3 C&lt;iterator_class&gt;

The class of iterator to use, override if you're sub-classing.  Defaults
to L&lt;TAP::Parser::Iterator::Process&gt;.

=cut

use constant iterator_class =&gt; 'TAP::Parser::Iterator::Process';

# Turns on autoflush for the handle passed
sub _autoflush {
    my ( $class, $flushed ) = @_;
    my $old_fh = select $flushed;
    $| = 1;
    select $old_fh;
}

1;

=head1 SUBCLASSING

Please see L&lt;TAP::Parser/SUBCLASSING&gt; for a subclassing overview.

=head2 Example

  package MyRubySourceHandler;

  use strict;

  use Carp qw( croak );
  use TAP::Parser::SourceHandler::Executable;

  use base 'TAP::Parser::SourceHandler::Executable';

  # expect $handler-&gt;(['mytest.rb', 'cmdline', 'args']);
  sub make_iterator {
    my ($self, $source) = @_;
    my @test_args = @{ $source-&gt;test_args };
    my $rb_file   = $test_args[0];
    croak("error: Ruby file '$rb_file' not found!") unless (-f $rb_file);
    return $self-&gt;SUPER::raw_source(['/usr/bin/ruby', @test_args]);
  }

=head1 SEE ALSO

L&lt;TAP::Object&gt;,
L&lt;TAP::Parser&gt;,
L&lt;TAP::Parser::IteratorFactory&gt;,
L&lt;TAP::Parser::SourceHandler&gt;,
L&lt;TAP::Parser::SourceHandler::Perl&gt;,
L&lt;TAP::Parser::SourceHandler::File&gt;,
L&lt;TAP::Parser::SourceHandler::Handle&gt;,
L&lt;TAP::Parser::SourceHandler::RawTAP&gt;

=cut
</pre></body></html>