Lib/Server/Pipe.pm

filter: Statementstmt 28.2% 22 / 78 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 40.0% 8 / 20 Podpod n/a totaltotal 20.9% 30 / 143 CC 19 SCAR 40.0
CC
19
Cov
19%
_check_on_child
156.0
get_chunk
30.0
__ANON__
6.0
CRAP 54.7
↑ Summary Lib/Server.pm »
1 package Hg::Lib::Server::Pipe;
2  
3 1 ▶use Symbol 'gensym';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 #use IPC::Open3 qw[ open3 ];
5 1 ▶use IPC::Open2 qw[ open2 ];
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use POSIX qw[ :sys_wait_h ];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use MooX::Types::MooseLike::Base qw[ :all ];
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶sub forceArray {
Subroutine forceArray: called CC=1 SCAR=3.5
16 0 ▶ sub { 'ARRAY' eq ref $_[0] ? $_[0] : [ $_[0] ] }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $_[0] ? : 0 0 0
17 3 }
18  
19  
20 with 'MooX::Attributes::Shadow::Role';
21  
22 shadowable_attrs( qw[ hg args path configs ] );
23  
24 has _pid => (
25 is => 'rwp',
26 predicate => 1,
27 clearer => 1,
28 init_arg => undef
29 );
30 has _write => ( is => 'rwp', init_arg => undef );
31 has _read => ( is => 'rwp', init_arg => undef );
32 has _error => ( is => 'rwp', init_arg => undef );
33 has cmd => ( is => 'rwp' );
34  
35 # path to hg executable
36 has hg => (
37 is => 'ro',
38 default => sub { 'hg' },
39 coerce => forceArray,
40 isa => sub {
41 is_Str( $_ )
42 or die( "'hg' attribute must be string\n" )
43 foreach @{ shift() };
44 },
45 );
46  
47 # arguments to hg
48 has args => (
49 is => 'ro',
50 coerce => forceArray,
51 default => sub { [] },
52 );
53  
54 has path => (
55 is => 'ro',
56 predicate => 1,
57 );
58  
59 has configs => (
60 is => 'ro',
61 coerce => forceArray,
62 default => sub { [] },
63 );
64  
65 has cmd => (
66 is => 'lazy',
67 init_arg => undef,
68 );
69  
70  
71 0 ▶sub _build_cmd {
Subroutine _build_cmd: not called CC=2 SCAR=17.9
72  
73 0 my $self = shift;
74  
75 my @cmd = (
76 0 @{ $self->hg },
77 qw[ --config ui.interactive=True
78 serve
79 --cmdserver pipe
80 ],
81 );
82  
83 0 ▶ push @cmd, '-R', $self->path if $self->has_path;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_path 0 0 0
84  
85 0 push @cmd, map { ( '--config' => $_ ) } @{ $self->configs };
86  
87 0 push @cmd, @{ $self->args };
88  
89 0 return \@cmd;
90 }
91  
92 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
93  
94 0 shift()->open;
95  
96 }
97  
98 0 ▶sub open {
Subroutine open: not called CC=1 SCAR=6.9
99  
100 0 my $self = shift;
101  
102 0 my ( $write, $read );
103 0 my $error = gensym();
104  
105 0 my $pid;
106  
107 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
108  
109 0 $pid = open2( $read, $write, @{ $self->cmd } );
110 # $pid = open3( $write, $read, $error, @{ $self->cmd } );
111  
112 # there's probably not enough time elapsed between starting
113 # the child process and checking for its existence, but this
114 # doesn't cost much
115 0 _check_on_child( $pid, status => 'alive' );
116  
117 }
118 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
119  
120 0 croak( $_ );
121  
122 0 };
123  
124  
125 0 $self->_set__pid( $pid );
126 0 $self->_set__write( $write );
127 0 $self->_set__read( $read );
128 0 $self->_set__error( $error );
129  
130 }
131  
132 0 ▶sub DEMOLISH {
Subroutine DEMOLISH: not called CC=1 SCAR=6.9
133  
134 0 shift()->close;
135  
136 }
137  
138 0 ▶sub read {
Subroutine read: not called CC=1 SCAR=6.9
139  
140 0 my $self = shift;
141  
142 # use aliased data in @_ to prevent copying
143 0 return $self->_read->sysread( @_ );
144 }
145  
146 # always use aliased $_[0] as buffer to prevent copying
147 # call as get_chunk( $buf )
148 0 ▶sub get_chunk {
Subroutine get_chunk: not called CC=5 SCAR=34.0
149  
150 0 my $self = shift;
151  
152 # catch pipe errors from child
153 0 ▶ local $SIG{'PIPE'} = sub { croak( "SIGPIPE on read from server\n" ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
154  
155 0 my $nr = $self->read( $_[0], 5 );
156 0 ▶ croak( "error reading chunk header from server: $!\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nr 0 0 0
157 unless defined $nr;
158  
159 0 ▶ $nr > 0
Branch: 1 branch
BranchTrueFalseTotal
$nr > 0 or croak("unexpected end-of-file getting chunk header from server\n") 0 0 0
160 or croak( "unexpected end-of-file getting chunk header from server\n" );
161  
162 0 my ( $ch, $len ) = unpack( 'A[1] l>', $_[0] );
163  
164 0 ▶ if ( $ch =~ /IL/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /IL/) { } 0 0 0
165  
166 0 return $ch, $len;
167  
168 }
169  
170 else {
171  
172 0 ▶ $self->read( $_[0], $len ) == $len
Branch: 1 branch
BranchTrueFalseTotal
$self->read($_[0], $len) == $len or croak("unexpected end-of-file reading $len bytes from server channel $ch\n") 0 0 0
173 or croak(
174 "unexpected end-of-file reading $len bytes from server channel $ch\n"
175 );
176  
177 0 return $ch;
178 }
179  
180 }
181  
182 0 ▶sub close {
Subroutine close: not called CC=2 SCAR=17.9
183  
184 0 my $self = shift;
185  
186 # if the command server was created, see if it's
187 # still hanging around
188 0 ▶ if ( $self->_has_pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_pid) 0 0 0
189  
190 0 $self->_write->close;
191  
192 0 _check_on_child( $self->_pid, status => 'exit', wait => 1 );
193  
194 0 $self->_clear_pid;
195 }
196  
197 0 return;
198  
199 }
200  
201 0 ▶sub _check_on_child {
Subroutine _check_on_child: not called CC=12 SCAR=50.5
202  
203 0 my $pid = shift;
204 0 my %opt = @_;
205  
206 0 ▶ my $flags = WUNTRACED | ( $opt{wait} ? 0 : WNOHANG );
Branch: 1 branch
BranchTrueFalseTotal
$opt{'wait'} ? : 0 0 0
207 0 my $status = waitpid( $pid, $flags );
208  
209 # if the child exitted, it had better have been a clean death;
210 # anything else is not ok.
211 0 ▶ if ( $pid == $status ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == $status) 0 0 0
212  
213 0 ▶ die( "unexpected exit of child with status ",
Branch: 1 branch
BranchTrueFalseTotal
if WIFEXITED($?) and WEXITSTATUS($?) != 0 0 0 0
Condition0%0/3
WIFEXITED($?) and WEXITSTATUS($?) != 0
!ll&&!rl&&r
000
MC/DC0%0/2
WIFEXITED($?) and WEXITSTATUS($?) != 0
WIFEXITED($?) WEXITSTATUS($?) != 0
Truth table0%0/3
A and B
A= WIFEXITED($?)B= WEXITSTATUS($?) != 0
ABresult
0X0
100
111
214 WEXITSTATUS( $? ), "\n" )
215 if WIFEXITED( $? ) && WEXITSTATUS( $? ) != 0;
216  
217 0 ▶ die( "unexpected exit of child with signal ",
Branch: 1 branch
BranchTrueFalseTotal
if WIFSIGNALED($?) 0 0 0
218 WTERMSIG( $? ), "\n" )
219 if WIFSIGNALED( $? );
220  
221 }
222  
223 0 ▶ if ( $opt{status} eq 'alive' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt{'status'} eq 'alive') { } 0 0 0
elsif ($opt{'status'} eq 'exit') { } 0 0 0
224  
225 0 ▶ die( "unexpected exit of child\n" )
Branch: 1 branch
BranchTrueFalseTotal
if $pid == $status or -1 == $status 0 0 0
Condition0%0/3
$pid == $status or -1 == $status
l!l&&r!l&&!r
000
MC/DC0%0/2
$pid == $status or -1 == $status
$pid == $status -1 == $status
Truth table0%0/3
A or B
A= $pid == $statusB= -1 == $status
ABresult
1X1
011
000
226 if $pid == $status || -1 == $status;
227  
228 }
229  
230 elsif ( $opt{status} eq 'exit' ) {
231  
232 # is the child still alive
233 0 ▶ die( "child still alive\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless $pid == $status or -1 == $status 0 0 0
Condition0%0/3
$pid == $status or -1 == $status
l!l&&r!l&&!r
000
MC/DC0%0/2
$pid == $status or -1 == $status
$pid == $status -1 == $status
Truth table0%0/3
A or B
A= $pid == $statusB= -1 == $status
ABresult
1X1
011
000
234 unless $pid == $status || -1 == $status;
235  
236 }
237  
238 else {
239  
240 0 die( "internal error: unknown child status requested\n" );
241  
242 }
243  
244 }
245 1;
↑ Summary Lib/Server.pm »