Perlito5X/Test2/API.pm untested

filter: Statementstmt 0.0% 0 / 308 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 106 MC/DCmcdc 0.0% 0 / 57 Subroutinesub 0.0% 0 / 75 Podpod 0.0% 0 / 52 totaltotal 0.0% 0 / 696 CC 50 SCAR 46.5
CC
50
Cov
0%
context
342.0
run_subtest
342.0
context_do
42.0
CRAP 104.3
1 package Test2::API;
2 use strict;
3 use warnings;
4  
5 BEGIN {
6 $ENV{TEST_ACTIVE} ||= 1;
7 $ENV{TEST2_ACTIVE} = 1;
8 }
9  
10 our $VERSION = '1.302073';
11  
12  
13 my $INST;
14 my $ENDING = 0;
15 sub test2_set_is_end { ($ENDING) = @_ ? @_ : (1) }
16 sub test2_get_is_end { $ENDING }
17  
18 use Test2::API::Instance(\$INST);
19 # Set the exit status
20 END {
21 test2_set_is_end(); # See gh #16
22 $INST->set_exit();
23 }
24  
25 # See gh #16
26 {
27 no warnings;
28 INIT { eval 'END { test2_set_is_end() }; 1' or die $@ }
29 }
30  
31 BEGIN {
32 no warnings 'once';
33 if($] ge '5.014' || $ENV{T2_CHECK_DEPTH} || $Test2::API::DO_DEPTH_CHECK) {
34 *DO_DEPTH_CHECK = sub() { 1 };
35 }
36 else {
37 *DO_DEPTH_CHECK = sub() { 0 };
38 }
39 }
40  
41 use Test2::Util::Trace();
42  
43 use Test2::Hub::Subtest();
44 use Test2::Hub::Interceptor();
45 use Test2::Hub::Interceptor::Terminator();
46  
47 use Test2::Event::Ok();
48 use Test2::Event::Diag();
49 use Test2::Event::Note();
50 use Test2::Event::Plan();
51 use Test2::Event::Bail();
52 use Test2::Event::Exception();
53 use Test2::Event::Waiting();
54 use Test2::Event::Skip();
55 use Test2::Event::Subtest();
56  
57 use Carp qw/carp croak confess longmess/;
58 use Scalar::Util qw/blessed weaken/;
59 use Test2::Util qw/get_tid/;
60  
61 our @EXPORT_OK = qw{
62 context release
63 context_do
64 no_context
65 intercept
66 run_subtest
67  
68 test2_init_done
69 test2_load_done
70  
71 test2_set_is_end
72 test2_get_is_end
73  
74 test2_pid
75 test2_tid
76 test2_stack
77 test2_no_wait
78  
79 test2_add_callback_context_aquire
80 test2_add_callback_context_acquire
81 test2_add_callback_context_init
82 test2_add_callback_context_release
83 test2_add_callback_exit
84 test2_add_callback_post_load
85 test2_list_context_aquire_callbacks
86 test2_list_context_acquire_callbacks
87 test2_list_context_init_callbacks
88 test2_list_context_release_callbacks
89 test2_list_exit_callbacks
90 test2_list_post_load_callbacks
91  
92 test2_ipc
93 test2_ipc_drivers
94 test2_ipc_add_driver
95 test2_ipc_polling
96 test2_ipc_disable_polling
97 test2_ipc_enable_polling
98 test2_ipc_get_pending
99 test2_ipc_set_pending
100 test2_ipc_enable_shm
101  
102 test2_formatter
103 test2_formatters
104 test2_formatter_add
105 test2_formatter_set
106 };
107 BEGIN { require Exporter; our @ISA = qw(Exporter) }
108  
109 my $STACK = $INST->stack;
110 my $CONTEXTS = $INST->contexts;
111 my $INIT_CBS = $INST->context_init_callbacks;
112 my $ACQUIRE_CBS = $INST->context_acquire_callbacks;
113  
114 sub test2_init_done { $INST->finalized }
115 sub test2_load_done { $INST->loaded }
116  
117 sub test2_pid { $INST->pid }
118 sub test2_tid { $INST->tid }
119 sub test2_stack { $INST->stack }
120 sub test2_no_wait {
121 $INST->set_no_wait(@_) if @_;
122 $INST->no_wait;
123 }
124  
125 sub test2_add_callback_context_acquire { $INST->add_context_acquire_callback(@_) }
126 sub test2_add_callback_context_aquire { $INST->add_context_acquire_callback(@_) }
127 sub test2_add_callback_context_init { $INST->add_context_init_callback(@_) }
128 sub test2_add_callback_context_release { $INST->add_context_release_callback(@_) }
129 sub test2_add_callback_exit { $INST->add_exit_callback(@_) }
130 sub test2_add_callback_post_load { $INST->add_post_load_callback(@_) }
131 sub test2_list_context_aquire_callbacks { @{$INST->context_acquire_callbacks} }
132 sub test2_list_context_acquire_callbacks { @{$INST->context_acquire_callbacks} }
133 sub test2_list_context_init_callbacks { @{$INST->context_init_callbacks} }
134 sub test2_list_context_release_callbacks { @{$INST->context_release_callbacks} }
135 sub test2_list_exit_callbacks { @{$INST->exit_callbacks} }
136 sub test2_list_post_load_callbacks { @{$INST->post_load_callbacks} }
137  
138 sub test2_ipc { $INST->ipc }
139 sub test2_ipc_add_driver { $INST->add_ipc_driver(@_) }
140 sub test2_ipc_drivers { @{$INST->ipc_drivers} }
141 sub test2_ipc_polling { $INST->ipc_polling }
142 sub test2_ipc_enable_polling { $INST->enable_ipc_polling }
143 sub test2_ipc_disable_polling { $INST->disable_ipc_polling }
144 sub test2_ipc_get_pending { $INST->get_ipc_pending }
145 sub test2_ipc_set_pending { $INST->set_ipc_pending(@_) }
146 sub test2_ipc_enable_shm { $INST->ipc_enable_shm }
147  
148 sub test2_formatter { $INST->formatter }
149 sub test2_formatters { @{$INST->formatters} }
150 sub test2_formatter_add { $INST->add_formatter(@_) }
151 sub test2_formatter_set {
152 my ($formatter) = @_;
153 croak "No formatter specified" unless $formatter;
154 croak "Global Formatter already set" if $INST->formatter_set;
155 $INST->set_formatter($formatter);
156 }
157  
158 # Private, for use in Test2::API::Context
159 sub _contexts_ref { $INST->contexts }
160 sub _context_acquire_callbacks_ref { $INST->context_acquire_callbacks }
161 sub _context_init_callbacks_ref { $INST->context_init_callbacks }
162 sub _context_release_callbacks_ref { $INST->context_release_callbacks }
163  
164 # Private, for use in Test2::IPC
165 sub _set_ipc { $INST->set_ipc(@_) }
166  
167 sub context_do(&;@) {
168 my $code = shift;
169 my @args = @_;
170  
171 my $ctx = context(level => 1);
172  
173 my $want = wantarray;
174  
175 my @out;
176 my $ok = eval {
177 $want ? @out = $code->($ctx, @args) :
178 defined($want) ? $out[0] = $code->($ctx, @args) :
179 $code->($ctx, @args) ;
180 1;
181 };
182 my $err = $@;
183  
184 $ctx->release;
185  
186 die $err unless $ok;
187  
188 return @out if $want;
189 return $out[0] if defined $want;
190 return;
191 }
192  
193 sub no_context(&;$) {
194 my ($code, $hid) = @_;
195 $hid ||= $STACK->top->hid;
196  
197 my $ctx = $CONTEXTS->{$hid};
198 delete $CONTEXTS->{$hid};
199 my $ok = eval { $code->(); 1 };
200 my $err = $@;
201  
202 $CONTEXTS->{$hid} = $ctx;
203 weaken($CONTEXTS->{$hid});
204  
205 die $err unless $ok;
206  
207 return;
208 };
209  
210 sub context {
211 # We need to grab these before anything else to ensure they are not
212 # changed.
213 my ($errno, $eval_error, $child_error) = (0 + $!, $@, $?);
214  
215 my %params = (level => 0, wrapped => 0, @_);
216  
217 # If something is getting a context then the sync system needs to be
218 # considered loaded...
219 $INST->load unless $INST->{loaded};
220  
221 croak "context() called, but return value is ignored"
222 unless defined wantarray;
223  
224 my $stack = $params{stack} || $STACK;
225 my $hub = $params{hub} || (@$stack ? $stack->[-1] : $stack->top);
226 my $hid = $hub->{hid};
227 my $current = $CONTEXTS->{$hid};
228  
229 $_->(\%params) for @$ACQUIRE_CBS;
230 map $_->(\%params), @{$hub->{_context_acquire}} if $hub->{_context_acquire};
231  
232 # This is for https://github.com/Test-More/test-more/issues/16
233 # and https://rt.perl.org/Public/Bug/Display.html?id=127774
234 my $phase = ${^GLOBAL_PHASE} || 'NA';
235 my $end_phase = $ENDING || $phase eq 'END' || $phase eq 'DESTRUCT';
236  
237 my $level = 1 + $params{level};
238 my ($pkg, $file, $line, $sub) = $end_phase ? caller(0) : caller($level);
239 unless ($pkg || $end_phase) {
240 confess "Could not find context at depth $level" unless $params{fudge};
241 ($pkg, $file, $line, $sub) = caller(--$level) while ($level >= 0 && !$pkg);
242 }
243  
244 my $depth = $level;
245 $depth++ while DO_DEPTH_CHECK && !$end_phase && (!$current || $depth <= $current->{_depth} + $params{wrapped}) && caller($depth + 1);
246 $depth -= $params{wrapped};
247 my $depth_ok = !DO_DEPTH_CHECK || $end_phase || !$current || $current->{_depth} < $depth;
248  
249 if ($current && $params{on_release} && $depth_ok) {
250 $current->{_on_release} ||= [];
251 push @{$current->{_on_release}} => $params{on_release};
252 }
253  
254 # I know this is ugly....
255 ($!, $@, $?) = ($errno, $eval_error, $child_error) and return bless(
256 {
257 %$current,
258 _is_canon => undef,
259 errno => $errno,
260 eval_error => $eval_error,
261 child_error => $child_error,
262 _is_spawn => [$pkg, $file, $line, $sub],
263 },
264 'Test2::API::Context'
265 ) if $current && $depth_ok;
266  
267 # Handle error condition of bad level
268 if ($current) {
269 unless (${$current->{_aborted}}) {
270 _canon_error($current, [$pkg, $file, $line, $sub, $depth])
271 unless $current->{_is_canon};
272  
273 _depth_error($current, [$pkg, $file, $line, $sub, $depth])
274 unless $depth_ok;
275 }
276  
277 $current->release if $current->{_is_canon};
278  
279 delete $CONTEXTS->{$hid};
280 }
281  
282 # Directly bless the object here, calling new is a noticeable performance
283 # hit with how often this needs to be called.
284 my $trace = bless(
285 {
286 frame => [$pkg, $file, $line, $sub],
287 pid => $$,
288 tid => get_tid(),
289 },
290 'Test2::Util::Trace'
291 );
292  
293 # Directly bless the object here, calling new is a noticeable performance
294 # hit with how often this needs to be called.
295 my $aborted = 0;
296 $current = bless(
297 {
298 _aborted => \$aborted,
299 stack => $stack,
300 hub => $hub,
301 trace => $trace,
302 _is_canon => 1,
303 _depth => $depth,
304 errno => $errno,
305 eval_error => $eval_error,
306 child_error => $child_error,
307 $params{on_release} ? (_on_release => [$params{on_release}]) : (),
308 },
309 'Test2::API::Context'
310 );
311  
312 $CONTEXTS->{$hid} = $current;
313 weaken($CONTEXTS->{$hid});
314  
315 $_->($current) for @$INIT_CBS;
316 map $_->($current), @{$hub->{_context_init}} if $hub->{_context_init};
317  
318 $params{on_init}->($current) if $params{on_init};
319  
320 ($!, $@, $?) = ($errno, $eval_error, $child_error);
321  
322 return $current;
323 }
324  
325 sub _depth_error {
326 _existing_error(@_, <<" EOT");
327 context() was called to retrieve an existing context, however the existing
328 context was created in a stack frame at the same, or deeper level. This usually
329 means that a tool failed to release the context when it was finished.
330 EOT
331 }
332  
333 sub _canon_error {
334 _existing_error(@_, <<" EOT");
335 context() was called to retrieve an existing context, however the existing
336 context has an invalid internal state (!_canon_count). This should not normally
337 happen unless something is mucking about with internals...
338 EOT
339 }
340  
341 sub _existing_error {
342 my ($ctx, $details, $msg) = @_;
343 my ($pkg, $file, $line, $sub, $depth) = @$details;
344  
345 my $oldframe = $ctx->{trace}->frame;
346 my $olddepth = $ctx->{_depth};
347  
348 my $mess = longmess();
349  
350 warn <<" EOT";
351 $msg
352 Old context details:
353 File: $oldframe->[1]
354 Line: $oldframe->[2]
355 Tool: $oldframe->[3]
356 Depth: $olddepth
357  
358 New context details:
359 File: $file
360 Line: $line
361 Tool: $sub
362 Depth: $depth
363  
364 Trace: $mess
365  
366 Removing the old context and creating a new one...
367 EOT
368 }
369  
370 sub release($;$) {
371 $_[0]->release;
372 return $_[1];
373 }
374  
375 sub intercept(&) {
376 my $code = shift;
377  
378 my $ctx = context();
379  
380 my $ipc;
381 if (my $global_ipc = test2_ipc()) {
382 my $driver = blessed($global_ipc);
383 $ipc = $driver->new;
384 }
385  
386 my $hub = Test2::Hub::Interceptor->new(
387 ipc => $ipc,
388 no_ending => 1,
389 );
390  
391 my @events;
392 $hub->listen(sub { push @events => $_[1] });
393  
394 $ctx->stack->top; # Make sure there is a top hub before we begin.
395 $ctx->stack->push($hub);
396  
397 my ($ok, $err) = (1, undef);
398 T2_SUBTEST_WRAPPER: {
399 # Do not use 'try' cause it localizes __DIE__
400 $ok = eval { $code->(hub => $hub, context => $ctx->snapshot); 1 };
401 $err = $@;
402  
403 # They might have done 'BEGIN { skip_all => "whatever" }'
404 if (!$ok && $err =~ m/Label not found for "last T2_SUBTEST_WRAPPER"/ || (blessed($err) && $err->isa('Test2::Hub::Interceptor::Terminator'))) {
405 $ok = 1;
406 $err = undef;
407 }
408 }
409  
410 $hub->cull;
411 $ctx->stack->pop($hub);
412  
413 my $trace = $ctx->trace;
414 $ctx->release;
415  
416 die $err unless $ok;
417  
418 $hub->finalize($trace, 1)
419 if $ok
420 && !$hub->no_ending
421 && !$hub->ended;
422  
423 return \@events;
424 }
425  
426 sub run_subtest {
427 my ($name, $code, $params, @args) = @_;
428  
429 $params = {buffered => $params} unless ref $params;
430 my $buffered = delete $params->{buffered};
431 my $inherit_trace = delete $params->{inherit_trace};
432  
433 my $ctx = context();
434  
435 $ctx->note($name) unless $buffered;
436  
437 my $parent = $ctx->hub;
438  
439 my $stack = $ctx->stack || $STACK;
440 my $hub = $stack->new_hub(
441 class => 'Test2::Hub::Subtest',
442 %$params,
443 );
444  
445 my @events;
446 $hub->set_nested( $parent->isa('Test2::Hub::Subtest') ? $parent->nested + 1 : 1 );
447 $hub->listen(sub { push @events => $_[1] });
448  
449 if ($buffered) {
450 if (my $format = $hub->format) {
451 my $hide = $format->can('hide_buffered') ? $format->hide_buffered : 1;
452 $hub->format(undef) if $hide;
453 }
454 }
455 elsif (! $parent->format) {
456 # If our parent has no format that means we're in a buffered subtest
457 # and now we're trying to run a streaming subtest. There's really no
458 # way for that to work, so we need to force the use of a buffered
459 # subtest here as
460 # well. https://github.com/Test-More/test-more/issues/721
461 $buffered = 1;
462 }
463  
464 if ($inherit_trace) {
465 my $orig = $code;
466 $code = sub {
467 my $st_ctx = Test2::API::Context->new(
468 trace => $ctx->trace,
469 hub => $hub,
470 );
471 $st_ctx->do_in_context($orig, @args);
472 };
473 }
474  
475 my ($ok, $err, $finished);
476 T2_SUBTEST_WRAPPER: {
477 # Do not use 'try' cause it localizes __DIE__
478 $ok = eval { $code->(@args); 1 };
479 $err = $@;
480  
481 # They might have done 'BEGIN { skip_all => "whatever" }'
482 if (!$ok && $err =~ m/Label not found for "last T2_SUBTEST_WRAPPER"/ || (blessed($err) && blessed($err) eq 'Test::Builder::Exception')) {
483 $ok = undef;
484 $err = undef;
485 }
486 else {
487 $finished = 1;
488 }
489 }
490 $stack->pop($hub);
491  
492 my $trace = $ctx->trace;
493  
494 if (!$finished) {
495 if(my $bailed = $hub->bailed_out) {
496 $ctx->bail($bailed->reason);
497 }
498 my $code = $hub->exit_code;
499 $ok = !$code;
500 $err = "Subtest ended with exit code $code" if $code;
501 }
502  
503 $hub->finalize($trace, 1)
504 if $ok
505 && !$hub->no_ending
506 && !$hub->ended;
507  
508 my $pass = $ok && $hub->is_passing;
509 my $e = $ctx->build_event(
510 'Subtest',
511 pass => $pass,
512 name => $name,
513 subtest_id => $hub->id,
514 buffered => $buffered,
515 subevents => \@events,
516 );
517  
518 my $plan_ok = $hub->check_plan;
519  
520 $ctx->hub->send($e);
521  
522 $ctx->failure_diag($e) unless $e->pass;
523  
524 $ctx->diag("Caught exception in subtest: $err") unless $ok;
525  
526 $ctx->diag("Bad subtest plan, expected " . $hub->plan . " but ran " . $hub->count)
527 if defined($plan_ok) && !$plan_ok;
528  
529 $ctx->release;
530 return $pass;
531 }
532  
533 # There is a use-cycle between API and API/Context. Context needs to use some
534 # API functions as the package is compiling. Test2::API::context() needs
535 # Test2::API::Context to be loaded, but we cannot 'require' the module there as
536 # it causes a very noticeable performance impact with how often context() is
537 # called.
538 require Test2::API::Context;
539  
540 1;
541  
542 __END__