xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Harness/lib/TAP/Parser/SourceHandler/File.pm (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1package TAP::Parser::SourceHandler::File;
2
3use strict;
4use vars qw($VERSION @ISA);
5
6use TAP::Parser::SourceHandler    ();
7use TAP::Parser::IteratorFactory  ();
8use TAP::Parser::Iterator::Stream ();
9
10@ISA = qw(TAP::Parser::SourceHandler);
11
12TAP::Parser::IteratorFactory->register_handler(__PACKAGE__);
13
14=head1 NAME
15
16TAP::Parser::SourceHandler::File - Stream TAP from a text file.
17
18=head1 VERSION
19
20Version 3.26
21
22=cut
23
24$VERSION = '3.26';
25
26=head1 SYNOPSIS
27
28  use TAP::Parser::Source;
29  use TAP::Parser::SourceHandler::File;
30
31  my $source = TAP::Parser::Source->new->raw( \'file.tap' );
32  $source->assemble_meta;
33
34  my $class = 'TAP::Parser::SourceHandler::File';
35  my $vote  = $class->can_handle( $source );
36  my $iter  = $class->make_iterator( $source );
37
38=head1 DESCRIPTION
39
40This is a I<raw TAP stored in a file> L<TAP::Parser::SourceHandler> - it has 2 jobs:
41
421. Figure out if the I<raw> source it's given is a file containing raw TAP
43output.  See L<TAP::Parser::IteratorFactory> for more details.
44
452. Takes raw TAP from the text file given, and converts into an iterator.
46
47Unless you're writing a plugin or subclassing L<TAP::Parser>, you probably
48won't need to use this module directly.
49
50=head1 METHODS
51
52=head2 Class Methods
53
54=head3 C<can_handle>
55
56  my $vote = $class->can_handle( $source );
57
58Only votes if $source looks like a regular file.  Casts the following votes:
59
60  0.9 if it's a .tap file
61  0.9 if it has an extension matching any given in user config.
62
63=cut
64
65sub can_handle {
66    my ( $class, $src ) = @_;
67    my $meta   = $src->meta;
68    my $config = $src->config_for($class);
69
70    return 0 unless $meta->{is_file};
71    my $file = $meta->{file};
72    return 0.9 if $file->{lc_ext} eq '.tap';
73
74    if ( my $exts = $config->{extensions} ) {
75        return 0.9 if grep { lc($_) eq $file->{lc_ext} } @$exts;
76    }
77
78    return 0;
79}
80
81=head3 C<make_iterator>
82
83  my $iterator = $class->make_iterator( $source );
84
85Returns a new L<TAP::Parser::Iterator::Stream> for the source.  C<croak>s
86on error.
87
88=cut
89
90sub make_iterator {
91    my ( $class, $source ) = @_;
92
93    $class->_croak('$source->raw must be a scalar ref')
94      unless $source->meta->{is_scalar};
95
96    my $file = ${ $source->raw };
97    my $fh;
98    open( $fh, '<', $file )
99      or $class->_croak("error opening TAP source file '$file': $!");
100    return $class->iterator_class->new($fh);
101}
102
103=head3 C<iterator_class>
104
105The class of iterator to use, override if you're sub-classing.  Defaults
106to L<TAP::Parser::Iterator::Stream>.
107
108=cut
109
110use constant iterator_class => 'TAP::Parser::Iterator::Stream';
111
1121;
113
114__END__
115
116=head1 CONFIGURATION
117
118  {
119   extensions => [ @case_insensitive_exts_to_match ]
120  }
121
122=head1 SUBCLASSING
123
124Please see L<TAP::Parser/SUBCLASSING> for a subclassing overview.
125
126=head1 SEE ALSO
127
128L<TAP::Object>,
129L<TAP::Parser>,
130L<TAP::Parser::SourceHandler>,
131L<TAP::Parser::SourceHandler::Executable>,
132L<TAP::Parser::SourceHandler::Perl>,
133L<TAP::Parser::SourceHandler::Handle>,
134L<TAP::Parser::SourceHandler::RawTAP>
135
136=cut
137