blib/lib/YATT/Lite/Test/LangServerClient.pm

filter: Statementstmt 20.4% 28 / 137 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 32.1% 9 / 28 Podpod 0.0% 0 / 18 totaltotal 15.1% 37 / 244 CC 28 SCAR 34.2
CC
28
Cov
15%
wait_response
90.0
read_message
72.0
wait_exit
30.0
CRAP 30.6
« blib/lib/YATT/Lite/WebMVC0/SiteApp/CGI.pm ↑ Summary samples/basic/1/app.psgi »
1 package YATT::Lite::Test::LangServerClient;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use IPC::Open2;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use POSIX qw(WNOHANG);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 YATT::Lite::Test::LangServerClient - drive YATT::Lite::LanguageServer over pipes (for tests)
15  
16 =head1 SYNOPSIS
17  
18 my $cl = YATT::Lite::Test::LangServerClient->new(
19 server => "$FindBin::Bin/../Lite/LanguageServer.pm",
20 );
21 my $res = $cl->call(initialize => {rootUri => "file://$root", capabilities => {}});
22  
23 # Several frames in ONE write (what eglot does: didChange + definition):
24 my $req = $cl->make_request('textDocument/definition', {...});
25 $cl->send_messages($cl->make_notification('textDocument/didOpen', {...}), $req);
26 my $def = $cl->wait_response($req->{id});
27  
28 my ($shutdown_res, $exit_code) = $cl->shutdown;
29  
30 =head1 DESCRIPTION
31  
32 Spawns C<LanguageServer.pm --quiet server> as a child process and speaks
33 JSON-RPC (Content-Length framing) with it. Every read is bounded by
34 C<timeout> seconds so a stalled server fails a test instead of hanging it.
35 Notifications from the server (publishDiagnostics...) are queued and can be
36 inspected with C<take_notifications>.
37  
38 =cut
39  
40 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
41 0 my ($class, %opts) = @_;
42 0 my $self = bless {
43 timeout => 20,
44 args => [qw(--quiet server)],
45 _buf => '',
46 _next_id => 1,
47 _notifications => [],
48 _others => [],
49 %opts,
50 }, $class;
51 defined $self->{server}
52 0 ▶ or croak "server (path to LanguageServer.pm) is required";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"server"} or croak("server (path to LanguageServer.pm) is required") 0 0 0
53 0 $self->spawn;
54 0 $self;
55 }
56  
57 0 ▶sub spawn {
Subroutine spawn: not called CC=3 SCAR=24.8
Pod undocumented
58 0 my ($self) = @_;
59 0 ▶ my @inc = map {"-I$_"} grep {defined and not ref} @INC;
Condition0%0/3
defined $_ && !ref($_)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && !ref($_)
defined $_ !ref($_)
Truth table0%0/3
A && not(B)
A= defined $_B= !ref($_)
ABresult
0X0
100
111
60 0 my ($out, $in);
61 0 my $pid = open2($out, $in, $^X, @inc, $self->{server}, @{$self->{args}});
62 0 binmode $_ for $in, $out;
63 0 $in->autoflush(1);
64 0 @{$self}{qw(pid in out sel)} = ($pid, $in, $out, IO::Select->new($out));
65 0 $self;
66 }
67  
68 0 ▶sub pid { $_[0]{pid} }
Subroutine pid: not called CC=1 SCAR=6.9
Pod undocumented
69  
70 #========================================
71 # Message construction
72 #========================================
73  
74 0 ▶sub frame {
Subroutine frame: not called CC=2 SCAR=17.9
Pod undocumented
75 0 my ($self, $msg) = @_;
76 0 ▶ my $body = ref $msg ? encode_json($msg) : $msg;
Branch: 1 branch
BranchTrueFalseTotal
ref $msg ? : 0 0 0
77 0 "Content-Length: " . length($body) . "\r\n\r\n" . $body;
78 }
79  
80 0 ▶sub make_request {
Subroutine make_request: not called CC=2 SCAR=17.9
Pod undocumented
81 0 my ($self, $method, $params, $id) = @_;
82 0 ▶ $id //= $self->{_next_id}++;
Condition0%0/2
$id //= $self->{"_next_id"}++
l!l
00
MC/DC0%0/1
$id //= $self->{"_next_id"}++
$id
Truth table0%0/2
A
A= $id
Aresult
11
00
83 0 +{jsonrpc => '2.0', id => $id, method => $method, params => $params};
84 }
85  
86 0 ▶sub make_notification {
Subroutine make_notification: not called CC=1 SCAR=6.9
Pod undocumented
87 0 my ($self, $method, $params) = @_;
88 0 +{jsonrpc => '2.0', method => $method, params => $params};
89 }
90  
91 #========================================
92 # Sending
93 #========================================
94  
95 # All messages are joined and written with ONE syswrite, so the server
96 # sees them in a single read (pipelined frames).
97 0 ▶sub send_messages {
Subroutine send_messages: not called CC=1 SCAR=6.9
Pod undocumented
98 0 my ($self, @msgs) = @_;
99 0 $self->send_raw(join "", map {$self->frame($_)} @msgs);
100 }
101  
102 0 ▶sub send_raw {
Subroutine send_raw: not called CC=3 SCAR=24.8
Pod undocumented
103 0 my ($self, $bytes) = @_;
104 0 my $off = 0;
105 0 ▶ while ($off < length $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off < length $bytes) 0 0 0
106 0 my $cnt = syswrite($self->{in}, $bytes, length($bytes) - $off, $off);
107 0 ▶ croak "write to language server failed: $!" unless defined $cnt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cnt 0 0 0
108 0 $off += $cnt;
109 }
110 0 $off;
111 }
112  
113 0 ▶sub request {
Subroutine request: not called CC=1 SCAR=6.9
Pod undocumented
114 0 my ($self, $method, $params) = @_;
115 0 my $req = $self->make_request($method, $params);
116 0 $self->send_messages($req);
117 0 $req->{id};
118 }
119  
120 0 ▶sub notify {
Subroutine notify: not called CC=1 SCAR=6.9
Pod undocumented
121 0 my ($self, $method, $params) = @_;
122 0 $self->send_messages($self->make_notification($method, $params));
123 }
124  
125 # request + wait_response
126 0 ▶sub call {
Subroutine call: not called CC=1 SCAR=6.9
Pod undocumented
127 0 my ($self, $method, $params) = @_;
128 0 my $id = $self->request($method, $params);
129 0 $self->wait_response($id);
130 }
131  
132 #========================================
133 # Receiving
134 #========================================
135  
136 # Read one framed message before $deadline (absolute time).
137 # Returns undef on timeout or EOF.
138 0 ▶sub read_message {
Subroutine read_message: not called CC=8 SCAR=42.8
Pod undocumented
139 0 my ($self, $deadline) = @_;
140 0 ▶ $deadline //= time + $self->{timeout};
Condition0%0/2
$deadline //= &YATT::Lite::Test::LangServerClient::time() + $self->{"timeout"}
l!l
00
MC/DC0%0/1
$deadline //= &YATT::Lite::Test::LangServerClient::time() + $self->{"timeout"}
$deadline
Truth table0%0/2
A
A= $deadline
Aresult
11
00
141 0 while (1) {
142 0 ▶ if ((my $sep = index($self->{_buf}, "\r\n\r\n")) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $sep = index($self->{"_buf"}, "\r\n\r\n")) >= 0) 0 0 0
143 0 my $header = substr($self->{_buf}, 0, $sep);
144 0 ▶ my ($len) = $header =~ /Content-Length:\s*(\d+)/i
Branch: 1 branch
BranchTrueFalseTotal
my($len) = $header =~ /Content-Length:\s*(\d+)/i or croak("bad header from language server: $header") 0 0 0
145 or croak "bad header from language server: $header";
146 0 ▶ if (length($self->{_buf}) >= $sep + 4 + $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"_buf"} >= $sep + 4 + $len) 0 0 0
147 0 substr($self->{_buf}, 0, $sep + 4, '');
148 0 my $body = substr($self->{_buf}, 0, $len, '');
149 0 return decode_json($body);
150 }
151 }
152 0 my $left = $deadline - time;
153 0 ▶ return undef if $left <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $left <= 0 0 0 0
154 0 ▶ $self->{sel}->can_read($left) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"sel"}->can_read($left) or return undef 0 0 0
155 0 my $cnt = sysread($self->{out}, $self->{_buf}, 65536, length $self->{_buf});
156 0 ▶ return undef unless $cnt;
Branch: 1 branch
BranchTrueFalseTotal
unless $cnt 0 0 0
157 }
158 }
159  
160 # Wait for the response to request $id. Other responses and all
161 # notifications are queued meanwhile. Returns undef on timeout/EOF.
162 0 ▶sub wait_response {
Subroutine wait_response: not called CC=9 SCAR=45.0
Pod undocumented
163 0 my ($self, $id, $timeout) = @_;
164 0 for my $i (0 .. $#{$self->{_others}}) {
165 0 my $m = $self->{_others}[$i];
166 0 ▶ if (defined $m->{id} and $m->{id} eq $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $m->{"id"} and $m->{"id"} eq $id) 0 0 0
Condition0%0/3
defined $m->{"id"} and $m->{"id"} eq $id
!ll&&!rl&&r
000
MC/DC0%0/2
defined $m->{"id"} and $m->{"id"} eq $id
defined $m->{"id"} $m->{"id"} eq $id
Truth table0%0/3
A and B
A= defined $m->{"id"}B= $m->{"id"} eq $id
ABresult
0X0
100
111
167 0 splice @{$self->{_others}}, $i, 1;
168 0 return $m;
169 }
170 }
171 0 ▶ my $deadline = time + ($timeout // $self->{timeout});
Condition0%0/3
$timeout // $self->{"timeout"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$timeout // $self->{"timeout"}
$timeout $self->{"timeout"}
Truth table0%0/3
A // B
A= $timeoutB= $self->{"timeout"}
ABresult
1X1
011
000
172 0 ▶ while (defined (my $m = $self->read_message($deadline))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $m = $self->read_message($deadline))) 0 0 0
173 0 ▶ if (defined $m->{method}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $m->{'method'}) { } 0 0 0
elsif (defined $m->{'id'}) { } 0 0 0
174 0 push @{$self->{_notifications}}, $m;
175 } elsif (defined $m->{id}) {
176 0 ▶ return $m if $m->{id} eq $id;
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"id"} eq $id 0 0 0
177 0 push @{$self->{_others}}, $m;
178 } else {
179 # error response without id (e.g. ParseError). Keep for inspection.
180 0 push @{$self->{_others}}, $m;
181 }
182 }
183 0 return undef;
184 }
185  
186 0 ▶sub notifications { @{$_[0]{_notifications}} }
Subroutine notifications: not called CC=1 SCAR=6.9
Pod undocumented
187  
188 0 ▶sub take_notifications {
Subroutine take_notifications: not called CC=1 SCAR=6.9
Pod undocumented
189 0 my ($self) = @_;
190 0 my @n = @{$self->{_notifications}};
191 0 @{$self->{_notifications}} = ();
192 0 @n;
193 }
194  
195 0 ▶sub other_messages { @{$_[0]{_others}} }
Subroutine other_messages: not called CC=1 SCAR=6.9
Pod undocumented
196  
197 #========================================
198 # Termination
199 #========================================
200  
201 # Returns ($shutdown_response, $exit_code). $exit_code is undef when
202 # the server did not exit within timeout (it is then killed).
203 0 ▶sub shutdown {
Subroutine shutdown: not called CC=1 SCAR=6.9
Pod undocumented
204 0 my ($self) = @_;
205 0 my $res = $self->call('shutdown', undef);
206 0 $self->notify('exit', undef);
207 0 my $code = $self->wait_exit;
208 0 ($res, $code);
209 }
210  
211 0 ▶sub wait_exit {
Subroutine wait_exit: not called CC=5 SCAR=34.0
Pod undocumented
212 0 my ($self) = @_;
213 0 ▶ my $pid = delete $self->{pid} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $pid = delete $self->{"pid"} or return undef 0 0 0
214 0 my $deadline = time + $self->{timeout};
215 0 ▶ while (time < $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (&YATT::Lite::Test::LangServerClient::time() < $deadline) 0 0 0
216 0 my $got = waitpid($pid, WNOHANG);
217 0 ▶ return $? >> 8 if $got == $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $got == $pid 0 0 0
218 0 ▶ return undef if $got < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $got < 0 0 0 0
219 0 select(undef, undef, undef, 0.05);
220 }
221 0 kill TERM => $pid;
222 0 waitpid($pid, 0);
223 0 return undef;
224 }
225  
226 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
227 0 my ($self) = @_;
228 0 ▶ if (my $pid = delete $self->{pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = delete $self->{"pid"}) 0 0 0
229 0 kill TERM => $pid;
230 0 waitpid($pid, 0);
231 }
232 }
233  
234 1 1;
« blib/lib/YATT/Lite/WebMVC0/SiteApp/CGI.pm ↑ Summary samples/basic/1/app.psgi »