summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/lib/TAP/Parser/Source.pm
blob: 9263e9e5442c2b08bc8b57061e959a5abcaf0fef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package TAP::Parser::Source;

use strict;
use vars qw($VERSION @ISA);

use TAP::Object                  ();
use TAP::Parser::IteratorFactory ();

@ISA = qw(TAP::Object);

# Causes problem on MacOS and shouldn't be necessary anyway
#$SIG{CHLD} = sub { wait };

=head1 NAME

TAP::Parser::Source - Stream output from some source

=head1 VERSION

Version 3.17

=cut

$VERSION = '3.17';

=head1 SYNOPSIS

  use TAP::Parser::Source;
  my $source = TAP::Parser::Source->new;
  my $stream = $source->source(['/usr/bin/ruby', 'mytest.rb'])->get_stream;

=head1 DESCRIPTION

Takes a command and hopefully returns a stream from it.

=head1 METHODS

=head2 Class Methods

=head3 C<new>

 my $source = TAP::Parser::Source->new;

Returns a new C<TAP::Parser::Source> object.

=cut

# new() implementation supplied by TAP::Object

sub _initialize {
    my ( $self, $args ) = @_;
    $self->{switches} = [];
    _autoflush( \*STDOUT );
    _autoflush( \*STDERR );
    return $self;
}

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

=head2 Instance Methods

=head3 C<source>

 my $source = $source->source;
 $source->source(['./some_prog some_test_file']);

 # or
 $source->source(['/usr/bin/ruby', 't/ruby_test.rb']);

Getter/setter for the source.  The source should generally consist of an array
reference of strings which, when executed via L<&IPC::Open3::open3|IPC::Open3>,
should return a filehandle which returns successive rows of TAP.  C<croaks> if
it doesn't get an arrayref.

=cut

sub source {
    my $self = shift;
    return $self->{source} unless @_;
    unless ( 'ARRAY' eq ref $_[0] ) {
        $self->_croak('Argument to &source must be an array reference');
    }
    $self->{source} = shift;
    return $self;
}

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

=head3 C<get_stream>

 my $stream = $source->get_stream;

Returns a L<TAP::Parser::Iterator> stream of the output generated by executing
C<source>.  C<croak>s if there was no command found.

Must be passed an object that implements a C<make_iterator> method.
Typically this is a TAP::Parser instance.

=cut

sub get_stream {
    my ( $self, $factory ) = @_;
    my @command = $self->_get_command
      or $self->_croak('No command found!');

    return $factory->make_iterator(
        {   command => \@command,
            merge   => $self->merge
        }
    );
}

sub _get_command { return @{ shift->source || [] } }

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

=head3 C<merge>

  my $merge = $source->merge;

Sets or returns the flag that dictates whether STDOUT and STDERR are merged.

=cut

sub merge {
    my $self = shift;
    return $self->{merge} unless @_;
    $self->{merge} = shift;
    return $self;
}

# Turns on autoflush for the handle passed
sub _autoflush {
    my $flushed = shift;
    my $old_fh  = select $flushed;
    $| = 1;
    select $old_fh;
}

1;

=head1 SUBCLASSING

Please see L<TAP::Parser/SUBCLASSING> for a subclassing overview.

=head2 Example

  package MyRubySource;

  use strict;
  use vars '@ISA';

  use Carp qw( croak );
  use TAP::Parser::Source;

  @ISA = qw( TAP::Parser::Source );

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

=head1 SEE ALSO

L<TAP::Object>,
L<TAP::Parser>,
L<TAP::Parser::Source::Perl>,

=cut