t/20-gerrit-for-each-patchset.t

filter: Statementstmt 42.4% 99 / 233 Branchbran 8.3% 2 / 24 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 50.0% 24 / 48 Podpod n/a totaltotal 39.8% 127 / 319 CC 16 SCAR 21.4
CC
16
Cov
39%
test_for_each_patchset
42.0
mock_query
30.0
__ANON__
12.0
CRAP 8.5
« blib/lib/Gerrit/Client/ForEach.pm ↑ Summary
1 #!/usr/bin/env perl
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use AnyEvent::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Dir::Self;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Env::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Gerrit::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Gerrit::Client::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Sub::Override;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use Test::Warn;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # This would normally be loaded internally as needed, but we need to
26 # load it now for mocking
27 5 ▶use Gerrit::Client::ForEach;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Pipe ends used for communication between parent and child
30 5 my %pipe;
31  
32 # hash of commits reviewed (used for mocks)
33 my %reviewed_commits;
34  
35 0 ▶sub clear_reviewed_commits {
Subroutine clear_reviewed_commits: not called CC=1 SCAR=6.9
36 0 %reviewed_commits = ();
37 }
38  
39 # mock git commands which succeed but don't do much
40 0 ▶sub mock_gits_ok {
Subroutine mock_gits_ok: not called CC=1 SCAR=6.9
41 return [
42 Sub::Override->new(
43 0 ▶ 'Gerrit::Client::ForEach::_git_bare_clone_cmd' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 my ( undef, $giturl, $gitdir ) = @_;
45 0 return ( 'git', 'init', '--bare', $gitdir );
46 }
47 ),
48 Sub::Override->new(
49 0 ▶ 'Gerrit::Client::ForEach::_git_clone_cmd' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 my ( undef, $giturl, $gitdir ) = @_;
51 0 return ( 'git', 'init', $gitdir );
52 }
53 ),
54 Sub::Override->new(
55 0 ▶ 'Gerrit::Client::ForEach::_git_fetch_cmd' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 my ( undef, $giturl, $gitdir, $ref ) = @_;
57 0 return ( 'perl', '-e1' );
58 }
59 ),
60 Sub::Override->new(
61 0 ▶ 'Gerrit::Client::ForEach::_git_reset_cmd' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 my ( undef, $ref ) = @_;
63 0 return ( 'perl', '-e1' );
64 }
65 ),
66 Sub::Override->new(
67 0 ▶ 'Gerrit::Client::ForEach::_mark_commit_reviewed' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
68 0 my (undef, $event) = @_;
69 0 my $rev = $event->{patchSet}{revision};
70 0 ▶ if (!$rev) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rev) 0 0 0
71 0 die "bad event: " . Dumper($event);
72 }
73 0 diag "revision $event->{patchSet}{revision} now reviewed\n";
74 0 $reviewed_commits{$event->{patchSet}{revision}} = 1;
75 }
76 ),
77 Sub::Override->new(
78 0 ▶ 'Gerrit::Client::ForEach::_is_commit_reviewed' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
79 0 my (undef, $event) = @_;
80 0 my $rev = $event->{patchSet}{revision};
81 0 ▶ if (!$rev) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rev) 0 0 0
82 0 die "bad event: ".Dumper($event);
83 }
84 0 ▶ if ($reviewed_commits{$rev}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reviewed_commits{$rev}) 0 0 0
85 0 diag "revision $rev is already reviewed\n";
86 0 return 1;
87 }
88 0 return 0;
89 }
90 0 ),
91 ];
92 }
93  
94 # some test changes and patch sets
95 5 my $test_rev1 = 'a765609f8b97fd8c9c29d7576d46b8eba99c11ac';
96 5 my $test_rev2 = 'bbb5609f8b97fd8c9c29d7576d46b8eba99c11ac';
97 5 my $test_rev3 = 'ccc5609f8b97fd8c9c29d7576d46b8eba99c11ac';
98 5 my $test_rev4 = 'ddd5609f8b97fd8c9c29d7576d46b8eba99c11ac';
99 5 my $test_rev5 = 'eee5609f8b97fd8c9c29d7576d46b8eba99c11ac';
100 5 my $test_rev6 = 'fff5609f8b97fd8c9c29d7576d46b8eba99c11ac';
101  
102 5 my $test_ps1 =
103 { number => 1, revision => $test_rev1, ref => 'refs/changes/02/2/1' };
104 5 my $test_ps2 =
105 { number => 2, revision => $test_rev2, ref => 'refs/changes/01/1/2' };
106 5 my $test_ps3 =
107 { number => 3, revision => $test_rev3, ref => 'refs/changes/06/6/3' };
108 5 my $test_ps4 =
109 { number => 4, revision => $test_rev4, ref => 'refs/changes/07/7/4' };
110 5 my $test_ps5 =
111 { number => 5, revision => $test_rev5, ref => 'refs/changes/07/7/5' };
112 5 my $test_ps6 =
113 { number => 6, revision => $test_rev6, ref => 'refs/changes/07/7/6' };
114  
115 5 my $test_change1 = {
116 project => "prj1",
117 branch => "master",
118 id => "id1",
119 subject => 'Some commit',
120 currentPatchSet => $test_ps1,
121 };
122 5 my $test_change2 = {
123 project => "prj2",
124 branch => "master",
125 id => "id2",
126 subject => 'Some other commit',
127 currentPatchSet => $test_ps2,
128 };
129 5 my $test_change3 = {
130 project => "prj1",
131 branch => "master",
132 id => "id3",
133 subject => 'A great commit',
134 currentPatchSet => $test_ps3,
135 };
136 5 my $test_change4 = {
137 project => "prj2",
138 branch => "master",
139 id => "id4",
140 subject => 'A poor commit',
141 currentPatchSet => $test_ps4,
142 };
143 5 my $test_change5 = {
144 project => "prj2",
145 branch => "master",
146 id => "id5",
147 subject => 'The best commit',
148 currentPatchSet => $test_ps5,
149 };
150  
151 5 my $test_change_unwanted = {
152 project => "prj3",
153 branch => "master",
154 id => "iduw",
155 subject => 'An unwanted commit',
156 currentPatchSet => $test_ps6,
157 unwanted => 1,
158 };
159  
160 5 my %test_change_by_id = (
161 id1 => $test_change1,
162 id2 => $test_change2,
163 id3 => $test_change3,
164 id4 => $test_change4,
165 id5 => $test_change5,
166 iduw => $test_change_unwanted,
167 );
168  
169 0 ▶sub patchset_created_json {
Subroutine patchset_created_json: not called CC=1 SCAR=6.9
170 0 my ($change) = @_;
171  
172 # events do not include the currentPatchSet within change
173 0 $change = dclone $change;
174 0 my $patchset = delete $change->{currentPatchSet};
175 0 return encode_json(
176 { type => 'patchset-created',
177 change => $change,
178 patchSet => $patchset
179 }
180 );
181 }
182  
183 5 my %MOCK_QUERY;
184  
185 0 ▶sub mock_query {
Subroutine mock_query: not called CC=5 SCAR=34.0
186 0 my ( $query, %args ) = @_;
187 0 ▶ my $results = shift @{ $MOCK_QUERY{$query} || [] };
Condition0%0/2
$MOCK_QUERY{$query} || []
l!l
00
MC/DC0%0/1
$MOCK_QUERY{$query} || []
$MOCK_QUERY{$query}
Truth table0%0/2
A
A= $MOCK_QUERY{$query}
Aresult
11
00
188 0 ▶ $results ||= [];
Condition0%0/2
$results ||= []
l!l
00
MC/DC0%0/1
$results ||= []
$results
Truth table0%0/2
A
A= $results
Aresult
11
00
189 0 my @new_results;
190 0 foreach my $r ( @{$results} ) {
191 0 my $new_r = dclone $r;
192 0 ▶ if ( !$args{current_patch_set} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"current_patch_set"}) 0 0 0
193 0 delete $new_r->{currentPatchSet};
194 }
195 0 push @new_results, $new_r;
196 }
197 0 $args{on_success}->( @new_results );
198 0 return;
199 }
200  
201 0 ▶sub record_event {
Subroutine record_event: not called CC=1 SCAR=6.9
202 0 my ( $change, $patchset ) = @_;
203 $pipe{write}->push_write(
204 0 json => {
205 change => $change,
206 patchset => $patchset,
207 wd => "$CWD",
208 }
209 );
210 0 return;
211 }
212  
213 0 ▶sub test_for_each_patchset {
Subroutine test_for_each_patchset: not called CC=6 SCAR=37.4
214 0 my ( $testname, %args ) = @_;
215  
216 # in cmd mode, we're limited in the event checking we can do
217 0 my $cmd = $args{on_patchset_cmd};
218  
219 0 local %ENV = %ENV;
220 0 my $dir = File::Temp->newdir(
221 'perl-gerrit-client-test.XXXXXX',
222 TMPDIR => 1,
223 CLEANUP => 1
224 );
225 0 ok( $dir, "$testname tempdir created" );
226 0 Env::Path->PATH->Prepend("$dir");
227  
228 # test an explicitly set query
229 0 local $MOCK_QUERY{'quux'} = [ ([ $test_change1, $test_change2 ]) x 3 ];
230 0 my $mock_query =
231 Sub::Override->new( 'Gerrit::Client::query' => \&mock_query );
232 0 my $mock_git = mock_gits_ok();
233  
234 0 Gerrit::Client::Test::create_mock_command(
235 name => 'ssh',
236 directory => $dir,
237 sequence => [
238  
239 # simulate various events from a long-lived connection which
240 # drops every now and again
241 { delay => 1,
242 exitcode => 255,
243 stdout => patchset_created_json($test_change3) . "\n"
244 },
245 { delay => 1,
246 exitcode => 255,
247 stdout => patchset_created_json($test_change_unwanted) . "\n"
248 },
249 { delay => 1,
250 exitcode => 255,
251 stdout => patchset_created_json($test_change4) . "\n"
252 },
253 { delay => 120,
254 exitcode => 0,
255 stdout => patchset_created_json($test_change5) . "\n"
256 }
257 ],
258 );
259  
260 0 my $cv = AE::cv();
261  
262 0 my @events;
263  
264 # make sure we eventually give up if something goes wrong
265 0 ▶ my $timeout_timer = AE::timer( 120, 0, sub { my $cnt = @events; $cv->croak("timed out after $cnt event(s)") } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
266 0 my $done_timer;
267 my $guard;
268  
269 0 my %readreq;
270 %readreq = (
271 0 ▶ json => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
272 0 my ( $h, $data ) = @_;
273 0 push @events, $data;
274  
275 # simulate cancelling the loop after 4 events
276 0 ▶ if ( @events >= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@events >= 4) 0 0 0
277 0 undef $guard;
278 0 ▶ $done_timer = AE::timer( 1, 0, sub { $cv->send(); undef $done_timer } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
279 }
280 0 $h->push_read(%readreq);
281 }
282 0 );
283  
284 0 $pipe{read}->push_read(%readreq);
285  
286 $guard = Gerrit::Client::for_each_patchset(
287 url => 'ssh://gerrit.example.com/',
288 workdir => "$dir/work",
289 0 ▶ wanted => sub { return !$_[0]->{unwanted} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
290 0 query => 'quux',
291 %args,
292 );
293  
294 0 diag "'connection lost' warnings may be printed, don't be alarmed, this is expected...";
295 0 $cv->recv();
296  
297 0 is( scalar(@events), 4, "$testname got expected number of events" );
298  
299 # events may occur in any order, so sort them before comparison
300 0 ▶ if (!$cmd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmd) 0 0 0
301 0 @events = sort { $a->{change}{id} cmp $b->{change}{id} } @events;
302 }
303  
304 0 my %seen_wd;
305 my %seen_id;
306 0 foreach my $e (@events) {
307  
308 # there should be a unique temporary work directory for each
309 # event, all of which no longer exist
310 0 my $wd = delete $e->{wd};
311 0 ok( !$seen_wd{$wd}, "$testname unique working directory: $wd" );
312 0 ok( !-e $wd, "$testname working directory $wd was cleaned up" );
313 0 $seen_wd{$wd} = 1;
314  
315 0 ▶ next if ($cmd);
Branch: 1 branch
BranchTrueFalseTotal
if $cmd 0 0 0
316  
317 0 my $id = $e->{change}{id};
318 0 ok( !$seen_id{$id}, "$testname unique event $id" );
319 0 $seen_id{$id} = 1;
320  
321 0 my %test_change = %{ $test_change_by_id{$id} };
322 0 my $test_patchset = delete $test_change{currentPatchSet};
323 is_deeply( $e->{change}, \%test_change, "$testname change $id looks ok" )
324 0 ▶ || diag explain $e->{change};
Branch: 1 branch
BranchTrueFalseTotal
is_deeply($$e{"change"}, \%test_change, "$testname change $id looks ok") or diag(explain($$e{"change"})) 0 0 0
325 is_deeply( $e->{patchset}, $test_patchset,
326 "$testname patchset $id looks ok" )
327 0 ▶ || diag explain $e->{patchset};
Branch: 1 branch
BranchTrueFalseTotal
is_deeply($$e{"patchset"}, $test_patchset, "$testname patchset $id looks ok") or diag(explain($$e{"patchset"})) 0 0 0
328 }
329  
330 0 return;
331 }
332  
333 0 ▶sub test_for_each_patchset_inproc {
Subroutine test_for_each_patchset_inproc: not called CC=1 SCAR=6.9
334 0 my ( $r, $w ) = portable_pipe();
335 0 local $pipe{read} = AnyEvent::Handle->new( fh => $r );
336 0 local $pipe{write} = AnyEvent::Handle->new( fh => $w );
337 0 test_for_each_patchset( 'inproc', on_patchset => \&record_event );
338 }
339  
340 0 ▶sub test_for_each_patchset_forksub {
Subroutine test_for_each_patchset_forksub: not called CC=1 SCAR=6.9
341 0 my ( $r, $w ) = portable_pipe();
342 0 local $pipe{read} = AnyEvent::Handle->new( fh => $r );
343 0 local $pipe{write} = AnyEvent::Handle->new( fh => $w );
344 0 test_for_each_patchset( 'fork', on_patchset_fork => \&record_event );
345 }
346  
347 0 ▶sub test_for_each_patchset_cmd {
Subroutine test_for_each_patchset_cmd: not called CC=1 SCAR=6.9
348 0 my $script = __DIR__ . '/' . basename(__FILE__);
349  
350 0 my ( $r, $w ) = portable_pipe();
351 0 local $pipe{read} = AnyEvent::Handle->new( fh => $r );
352 0 local $pipe{write} = AnyEvent::Handle->new( fh => $w );
353 0 ▶ my $server = tcp_server undef, undef, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
354 0 my ($fh, $host, $port) = @_;
355  
356 0 my $handle;
357 $handle = AnyEvent::Handle->new(
358 fh => $fh,
359 0 ▶ on_read => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
360 0 my ($h) = @_;
361 0 $pipe{write}->push_write( delete $h->{rbuf} );
362 },
363 0 ▶ on_eof => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
364 0 undef $handle;
365 },
366 0 );
367 0 ▶ }, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
368 0 my (undef, undef, $port) = @_;
369 0 $ENV{TEST_SOCKET_PORT} = $port;
370 0 return 0;
371 0 };
372  
373 0 my @cmd = ($Config{perlpath}, $script, 'record_event');
374 0 diag "patchset command: @cmd";
375  
376 0 test_for_each_patchset( 'cmd', on_patchset_cmd => \@cmd );
377 }
378  
379 5 ▶sub record_event_from_child {
Subroutine record_event_from_child: called CC=2 SCAR=7.0
380 5 ▶ my $port = $ENV{TEST_SOCKET_PORT} || die;
Condition50%1/2
$ENV{'TEST_SOCKET_PORT'} || die()
l!l
50
MC/DC0%0/1
$ENV{'TEST_SOCKET_PORT'} || die()
$ENV{'TEST_SOCKET_PORT'}
Truth table50%1/2
A
A= $ENV{'TEST_SOCKET_PORT'}
Aresult
11
00
381 5 my $cv = AE::cv();
382 tcp_connect(
383 'localhost',
384 $port,
385 5 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
386 5 my ($fh) = @_;
387 5 local $pipe{write} = AnyEvent::Handle->new( fh => $fh );
388 5 $pipe{write}->push_write( json => { wd => "$CWD" } );
389 5 $cv->send();
390 }
391 5 );
392 5 $cv->recv();
393 }
394  
395 0 ▶sub run_test {
Subroutine run_test: not called CC=2 SCAR=17.9
396 0 ▶ unless (Gerrit::Client::Test::have_git()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Gerrit::Client::Test::have_git()) 0 0 0
397 0 plan skip_all => 'No functional git in PATH';
398 0 return;
399 }
400  
401 0 diag "begin inproc test...";
402 0 test_for_each_patchset_inproc;
403 0 clear_reviewed_commits;
404  
405 0 diag "begin forksub test...";
406 0 test_for_each_patchset_forksub;
407 0 clear_reviewed_commits;
408  
409 0 diag "begin cmd test...";
410 0 test_for_each_patchset_cmd;
411  
412 0 return;
413 }
414  
415 #==============================================================================
416  
417 5 ▶if ( !caller ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&CORE::GLOBAL::caller()) 5 0 5
418 5 ▶ if ( $ARGV[0] && $ARGV[0] eq 'record_event' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[0] and $ARGV[0] eq "record_event") 5 0 5
Condition33%1/3
$ARGV[0] and $ARGV[0] eq "record_event"
!ll&&!rl&&r
005
MC/DC0%0/2
$ARGV[0] and $ARGV[0] eq "record_event"
$ARGV[0] $ARGV[0] eq "record_event"
Truth table33%1/3
A and B
A= $ARGV[0]B= $ARGV[0] eq "record_event"
ABresult
0X0
100
111
419 5 record_event_from_child();
420 5 exit 0;
421 }
422 0 run_test;
423 0 done_testing;
424 }
425 1;
« blib/lib/Gerrit/Client/ForEach.pm ↑ Summary