blib/lib/Hg/Lib/Server.pm

filter: Statementstmt 80.0% 108 / 135 Branchbran 67.3% 31 / 46 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 85.1% 23 / 27 Podpod 0.0% 0 / 9 totaltotal 72.8% 169 / 232 CC 29 SCAR 16.6
CC
29
Cov
76%
_get_hello
12.3
runcommand
12.0
get_chunk
7.2
CRAP 5.2
↑ Summary Build »
1 package Hg::Lib::Server;
2  
3 3 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use System::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Params::Validate ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use MooX::Types::MooseLike::Base qw[ :all ];
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use constant forceArray => sub { 'ARRAY' eq ref $_[0] ? $_[0] : [ $_[0] ] };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $_[0] ? : 29 1 30
16  
17 3 with 'MooX::Attributes::Shadow::Role';
18  
19 3 shadowable_attrs( qw[ hg args path configs encoding env ] );
20  
21 # path to hg executable; allow multiple components
22 3 has hg => (
23 is => 'ro',
24 default => 'hg',
25 coerce => forceArray,
26 isa => ArrayRef [Str],
27 );
28  
29 # arguments to hg
30 has args => (
31 is => 'ro',
32 coerce => forceArray,
33 isa => ArrayRef [Str],
34 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 3 );
36  
37 3 has path => (
38 is => 'ro',
39 predicate => 1,
40 );
41  
42 has configs => (
43 is => 'ro',
44 coerce => forceArray,
45 isa => ArrayRef [Str],
46 10 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 3 );
48  
49 # default encoding; set to that returned by the hg hello response
50 3 has encoding => (
51 is => 'rwp',
52 predicate => 1,
53 clearer => '_clear_encoding',
54 );
55  
56 has env => (
57 is => 'ro',
58 isa => HashRef,
59 10 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
60 3 );
61  
62 # the actual pipe object. the pipe should be lazily created when any
63 # handled method is used. $server->_get_hello *must* be called after
64 # pipe creation; trigger will do that. however, trigger is not called
65 # when default is used, only when attribute is set. so, have
66 # default call $server->open which calls setter.
67  
68 has _pipe => (
69  
70 is => 'rw',
71 init_arg => undef,
72 lazy => 1,
73 predicate => 1,
74 9 ▶ trigger => sub { $_[0]->_get_hello },
Subroutine __ANON__: called CC=1 SCAR=0.0
75 handles => [qw[ stdin stdout stderr pid close is_terminated ]],
76 3 ▶ default => sub { $_[0]->open },
Subroutine __ANON__: called CC=1 SCAR=0.0
77 3 );
78  
79  
80 3 has connect => (
81  
82 is => 'ro',
83 default => 0,
84  
85 );
86  
87 # constructed command line; does not include environment variables
88 has _cmdline => (
89 is => 'lazy',
90 init_arg => undef,
91 builder => sub {
92  
93 8 ▶ my $self = shift;
Subroutine _build__cmdline: called CC=2 SCAR=7.0
Subroutine __ANON__: called CC=2 SCAR=7.0
94  
95 my @cmd = (
96 8 @{ $self->hg },
97 qw[ --config ui.interactive=True
98 serve
99 --cmdserver pipe
100 ],
101 );
102  
103 8 ▶ push @cmd, '-R', $self->path if $self->has_path;
Branch: 2 branches
BranchTrueFalseTotal
if $self->has_path 0 8 8
if $self->has_path 0 1 1
104  
105 8 push @cmd, map { ( '--config' => $_ ) } @{ $self->configs };
106  
107 8 push @cmd, @{ $self->args };
108  
109 8 return \@cmd;
110 },
111  
112 3 );
113  
114  
115 3 has capabilities => (
116 is => 'rwp',
117 predicate => 1,
118 init_arg => undef,
119 );
120  
121 sub BUILD {
122  
123 10 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
124  
125 10 ▶ $self->open if $self->connect;
Branch: 1 branch
BranchTrueFalseTotal
if $self->connect 6 4 10
126  
127 }
128  
129 sub DEMOLISH {
130  
131 10 ▶ my $self = shift;
Subroutine DEMOLISH: called CC=2 SCAR=6.9
Pod undocumented
132  
133 10 ▶ $self->close if $self->_has_pipe;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_pipe 9 1 10
134  
135 }
136  
137 sub open {
138  
139 9 ▶ my $self = shift;
Subroutine open: called CC=2 SCAR=7.0
Pod undocumented
140  
141 9 my $env = $self->env;
142  
143 9 $env->{HGPLAIN} = 1;
144 9 ▶ $env->{HGENCODING} = $self->encoding
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_encoding 0 9 9
145 if $self->has_encoding;
146  
147 my $pipe
148 9 = System::Command->new( @{ $self->_cmdline }, { env => $self->env } );
149 9 $self->_pipe( $pipe );
150  
151 2 return $pipe;
152 }
153  
154 sub read {
155  
156 20 ▶ my $self = shift;
Subroutine read: called CC=1 SCAR=0.0
Pod undocumented
157  
158 # use aliased data in @_ to prevent copying
159 20 return $self->stdout->sysread( @_ );
160 }
161  
162 # always use aliased $_[0] as buffer to prevent copying
163 # call as get_chunk( $buf )
164 sub get_chunk {
165  
166 11 ▶ my $self = shift;
Subroutine get_chunk: called CC=6 SCAR=19.7
Pod undocumented
167  
168 # catch pipe errors from child
169 11 ▶ local $SIG{'PIPE'} = sub { croak( "SIGPIPE on read from server\n" ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
170  
171 11 my $nr = $self->read( $_[0], 5 );
172 10 ▶ croak( "error reading chunk header from server: $!\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nr 0 10 10
173 unless defined $nr;
174  
175 10 ▶ $nr > 0
Branch: 1 branch
BranchTrueFalseTotal
$nr > 0 or croak("unexpected end-of-file getting chunk header from server\n") 1 9 10
176 or croak( "unexpected end-of-file getting chunk header from server\n" );
177  
178 9 my ( $ch, $len ) = unpack( 'A[1] l>', $_[0] );
179  
180 9 ▶ if ( $ch =~ /IL/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /IL/) { } 0 9 9
181  
182 0 ▶ croak(
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
183 "get_chunk called incorrectly called in scalar context for channel $ch\n"
184 ) unless wantarray();
185  
186 0 return $ch, $len;
187 }
188  
189 else {
190  
191 9 ▶ $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") 1 8 9
192 or croak(
193 "unexpected end-of-file reading $len bytes from server channel $ch\n"
194 );
195  
196 8 return $ch;
197 }
198  
199 }
200  
201 # call as $self->write( $buf, [ $len ] )
202 sub write {
203  
204 1 ▶ my $self = shift;
Subroutine write: called CC=3 SCAR=11.7
Pod undocumented
205 1 ▶ my $len = @_ > 1 ? $_[1] : length( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 1 1
206 1 ▶ $self->stdin->syswrite( $_[0], $len ) == $len
Branch: 1 branch
BranchTrueFalseTotal
$self->stdin->syswrite($_[0], $len) == $len or croak("error writing $len bytes to server\n") 0 1 1
207 or croak( "error writing $len bytes to server\n" );
208 }
209  
210 sub writeblock {
211  
212 0 ▶ my $self = shift;
Subroutine writeblock: not called CC=1 SCAR=6.9
Pod undocumented
213  
214 0 $self->write( pack( "N/a*", $_[0] ) );
215 }
216  
217 sub _get_hello {
218  
219 9 ▶ my $self = shift;
Subroutine _get_hello: called CC=12 SCAR=25.1
220  
221 9 my $buf;
222 9 my $ch = $self->get_chunk( $buf );
223  
224 7 ▶ croak( "corrupt or incomplete hello message from server\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless $ch eq "o" and length $buf 2 5 7
Condition100%3/3
$ch eq "o" and length $buf
!ll&&!rl&&r
115
MC/DC100%2/2
$ch eq "o" and length $buf
$ch eq "o" length $buf
Truth table100%3/3
A and B
A= $ch eq "o"B= length $buf
ABresult
0X0
100
111
225 unless $ch eq 'o' && length $buf;
226  
227 5 ▶ my $requested_encoding = $self->has_encoding ? $self->encoding : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_encoding ? : 0 5 5
228 5 $self->_clear_encoding;
229  
230 5 for my $item ( split( "\n", $buf ) ) {
231  
232 8 my ( $field, $value ) = $item =~ /([a-z0-9]+):\s*(.*)/;
233  
234 8 ▶ if ( $field eq 'capabilities' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($field eq 'capabilities') { } 4 4 8
elsif ($field eq 'encoding') { } 4 0 4
235  
236 $self->_set_capabilities(
237 4 { map { $_ => 1 } split( ' ', $value ) } );
238 }
239  
240 elsif ( $field eq 'encoding' ) {
241  
242 4 ▶ croak( sprintf "requested encoding of %s; got %s",
Branch: 1 branch
BranchTrueFalseTotal
if defined $requested_encoding and $requested_encoding ne $value 0 4 4
Condition33%1/3
defined $requested_encoding and $requested_encoding ne $value
!ll&&!rl&&r
400
MC/DC0%0/2
defined $requested_encoding and $requested_encoding ne $value
defined $requested_encoding $requested_encoding ne $value
Truth table33%1/3
A and B
A= defined $requested_encodingB= $requested_encoding ne $value
ABresult
0X0
100
111
243 $requested_encoding, $value )
244 if defined $requested_encoding && $requested_encoding ne $value;
245  
246 4 $self->_set_encoding( $value );
247  
248 }
249  
250 # ignore anything else 'cause we don't know what it means
251  
252 }
253  
254 # make sure hello message meets minimum standards
255 5 ▶ croak( "server did not provide capabilities?\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_capabilities 1 4 5
256 unless $self->has_capabilities;
257  
258 croak( "server is missing runcommand capability\n" )
259 4 ▶ unless exists $self->capabilities->{runcommand};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->capabilities->{"runcommand"} 1 3 4
260  
261 3 ▶ croak( "server did not provide encoding?\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_encoding 1 2 3
262 unless $self->has_encoding;
263  
264 2 return;
265 }
266  
267 sub getencoding {
268  
269 1 ▶ my $self = shift;
Subroutine getencoding: called CC=3 SCAR=11.6
Pod undocumented
270  
271 1 $self->write( "getencoding\n" );
272  
273 1 my $buffer;
274 1 my ( $ch, $len ) = $self->get_chunk( $buffer );
275  
276 1 ▶ croak( "unexpected return message for getencoding on channel $ch\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless $ch eq "r" and length $buffer 0 1 1
Condition33%1/3
$ch eq "r" and length $buffer
!ll&&!rl&&r
001
MC/DC0%0/2
$ch eq "r" and length $buffer
$ch eq "r" length $buffer
Truth table33%1/3
A and B
A= $ch eq "r"B= length $buffer
ABresult
0X0
100
111
277 unless $ch eq 'r' && length( $buffer );
278  
279 1 return $buffer;
280  
281 }
282  
283 # $server->runcommand( args => [ $command, @args ],
284 # inchannels => \%callbacks,
285 # outchannels => \%callbacks )
286 sub runcommand {
287  
288 0 ▶ my $self = shift;
Subroutine runcommand: not called CC=3 SCAR=24.8
Pod undocumented
289  
290 0 my $opts = validate(
291 @_,
292 {
293 inchannels => {
294 type => HASHREF,
295 default => {}
296 },
297 outchannels => {
298 type => HASHREF,
299 default => {}
300 },
301 args => {
302 type => ARRAYREF,
303 default => {}
304 },
305 } );
306  
307 0 $self->write( "runcommand\n" );
308 0 $self->writeblock( join( "\0", @{ $opts->{args} } ) );
309  
310 # read from server until a return channel is specified
311 0 my $buffer;
312 0 while ( 1 ) {
313  
314 0 my ( $ch, $len ) = $self->get_chunk( $buffer );
315  
316 0 for ( $ch ) {
317  
318 0 when ( $opts->{inchannels} ) {
319  
320 0 $self->writeblock( $opts->{inchannels}{$ch}->( $buffer ) );
321 }
322  
323 0 when ( $opts->{outchannels} ) {
324  
325 0 $opts->{outchannels}{$ch}->( $buffer );
326 }
327  
328  
329 0 when ( 'r' ) {
330  
331 0 state $length_exp = length( pack( 'l>', 0 ) );
332 0 ▶ croak( sprintf "incorrect message length (got %d, expected %d)",
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer != $length_exp 0 0 0
333 length( $buffer ), $length_exp )
334 if length( $buffer ) != $length_exp;
335  
336 0 return unpack( 'l>', $buffer );
337 }
338  
339 0 when ( /[[:upper:]]/ ) {
340  
341 0 croak( "unexpected data on required channel $ch\n" );
342 }
343  
344 }
345  
346  
347 }
348  
349 }
350  
351 3 1;
352  
353 __END__
↑ Summary Build »