blib/lib/TAP/Parser/Iterator/PherkinStream.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 110 CC 8 SCAR 31.5
CC
8
Cov
0%
_next
56.0
_finish
6.0
_initialize
2.0
CRAP 23.4
1 use v5.14;
2 use warnings;
3  
4 package TAP::Parser::Iterator::PherkinStream 0.87;
5  
6 =head1 NAME
7  
8 TAP::Parser::Iterator::PherkinStream - Stream with TAP from async BDD process
9  
10 =head1 VERSION
11  
12 version 0.87
13  
14 =cut
15  
16  
17 use base 'TAP::Parser::Iterator';
18  
19 use IO::Select;
20  
21 sub _initialize {
22 my ($self, $out_fh, $err_fh, $pherkin, $child_pid) = @_;
23  
24 $self->{pherkin} = $pherkin;
25 $self->{child_pid} = $child_pid;
26 $self->{sel} = IO::Select->new($out_fh, $err_fh);
27 $self->{out_fh} = $out_fh;
28 $self->{err_fh} = $err_fh;
29  
30 return $self;
31 }
32  
33 sub _finish {
34 my $self = shift;
35  
36 $self->{pherkin}->_post_run();
37 if ($self->{child_pid}) {
38 waitpid $self->{child_pid}, 0; # reap child process
39 $self->{wait} = $?;
40 $self->{exit} = $? >> 8;
41 }
42  
43 return $self;
44 }
45  
46 sub wait { shift->{wait} }
47 sub exit { shift->{exit} }
48  
49 sub _next {
50 my $self = shift;
51  
52 my @buf = ();
53 my $part = '';
54 return sub {
55 return shift @buf if @buf;
56  
57 while (my @ready = $self->{sel}->can_read) {
58 for my $fh (@ready) {
59 my $stderr = '';
60  
61 READ:
62 {
63 my $got = sysread $fh, my ($chunk), 2048;
64 if ($got == 0) {
65 $self->{sel}->remove($fh);
66 }
67 elsif ($fh == $self->{err_fh}) {
68 $stderr .= $chunk;
69 my @lines = split(/\n/, $stderr, -1);
70 $stderr = pop @lines;
71  
72 for my $line (@lines) {
73 utf8::decode($line);
74 print STDERR $line . "\n";
75 }
76 goto READ if $got == 2048;
77  
78 utf8::decode($stderr)
79 or die 'Subprocess provided non-utf8 data';
80 print STDERR $stderr . "\n";
81 }
82 else {
83 $part .= $chunk;
84 push @buf, split(/\n/, $part, -1);
85 $part = pop @buf;
86  
87 my $rv = shift @buf;
88 if (defined $rv) {
89 utf8::decode($rv)
90 or die 'Subprocess provided non-utf8 data';
91 return $rv;
92 }
93 }
94 }
95 }
96 }
97  
98 if ($part) {
99 $part = '';
100 return $part;
101 }
102  
103 $self->_finish;
104 return;
105 };
106 }
107  
108 sub next_raw {
109 my $self = shift;
110 $self->{_next} ||= $self->_next;
111 return $self->{_next}->();
112 }
113  
114 sub get_select_handles {
115 my $self = shift;
116  
117 # return our handle in case it's a socket or pipe (select()-able)
118 return ( $self->{out_fh}, $self->{err_fh});
119 }
120  
121  
122  
123 1;