| 1 |
package Test2::Harness::Auditor::Watcher; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Carp qw/croak confess/; |
| 8 |
use Scalar::Util qw/blessed/; |
| 9 |
use List::Util qw/first max/; |
| 10 |
|
| 11 |
use Test2::Harness::Util::UUID qw/gen_uuid/; |
| 12 |
|
| 13 |
use Test2::Harness::Util qw/hub_truth parse_exit/; |
| 14 |
|
| 15 |
use Test2::Harness::Auditor::TimeTracker; |
| 16 |
|
| 17 |
use Test2::Harness::Util::HashBase qw{ |
| 18 |
-job |
| 19 |
-try |
| 20 |
|
| 21 |
-assertion_count |
| 22 |
-exit |
| 23 |
-plan |
| 24 |
-_errors |
| 25 |
-_failures |
| 26 |
-_sub_failures |
| 27 |
-_plans |
| 28 |
-_info |
| 29 |
-_sub_info |
| 30 |
-_subtest_id |
| 31 |
-nested |
| 32 |
-subtests |
| 33 |
-numbers |
| 34 |
-times |
| 35 |
-halt |
| 36 |
-failed_subtest_tree |
| 37 |
}; |
| 38 |
|
| 39 |
sub init { |
| 40 |
my $self = shift; |
| 41 |
|
| 42 |
croak "'job' is a required attribute" |
| 43 |
unless $self->{+JOB}; |
| 44 |
|
| 45 |
croak "'try' is a required attribute" |
| 46 |
unless defined $self->{+TRY}; |
| 47 |
|
| 48 |
$self->{+_FAILURES} = 0; |
| 49 |
$self->{+_ERRORS} = 0; |
| 50 |
$self->{+ASSERTION_COUNT} = 0; |
| 51 |
|
| 52 |
$self->{+NUMBERS} = {}; |
| 53 |
$self->{+TIMES} = Test2::Harness::Auditor::TimeTracker->new(); |
| 54 |
|
| 55 |
$self->{+NESTED} = 0 unless defined $self->{+NESTED}; |
| 56 |
} |
| 57 |
|
| 58 |
sub pass { !$_[0]->fail } |
| 59 |
sub file { $_[0]->{+JOB}->{file} } |
| 60 |
sub fail { !!$_[0]->fail_error_facet_list } |
| 61 |
|
| 62 |
sub has_exit { defined $_[0]->{+EXIT} } |
| 63 |
sub has_plan { defined $_[0]->{+PLAN} } |
| 64 |
|
| 65 |
sub process { |
| 66 |
my $self = shift; |
| 67 |
my ($event) = @_; |
| 68 |
|
| 69 |
my $f = $event->{facet_data}; |
| 70 |
my $hf = hub_truth($f); |
| 71 |
|
| 72 |
my $nested = $hf->{nested} || 0; |
| 73 |
|
| 74 |
$self->times->process($event, $f, $self->{+ASSERTION_COUNT}) unless $nested; |
| 75 |
|
| 76 |
return if $hf->{buffered}; |
| 77 |
|
| 78 |
my $is_ours = $nested == $self->{+NESTED}; |
| 79 |
|
| 80 |
return unless $is_ours || $f->{from_tap}; |
| 81 |
|
| 82 |
# Add parent if we start a buffered subtest |
| 83 |
if ($f->{harness} && $f->{harness}->{subtest_start}) { |
| 84 |
my $st = $self->{+SUBTESTS}->{$nested + 1} ||= {}; |
| 85 |
$st->{event} = $event; |
| 86 |
$f->{harness_watcher}->{no_render} = 1; |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
my @out; |
| 91 |
|
| 92 |
# Not actually a subtest end, someone printed to STDOUT |
| 93 |
if ($f->{from_tap} && $f->{harness}->{subtest_end} && !($self->{+SUBTESTS} && keys %{$self->{+SUBTESTS}})) { |
| 94 |
# Alter $f so that this incorrect event is not sent to the renderer. |
| 95 |
$f->{harness_watcher}->{no_render} = 1; |
| 96 |
|
| 97 |
# Make a new $f and $event for the rest of the processing. |
| 98 |
$f = { |
| 99 |
%{$f}, |
| 100 |
harness_watcher => {added_by_watcher => 1}, |
| 101 |
parent => undef, |
| 102 |
trace => undef, |
| 103 |
harness => { |
| 104 |
%{$f->{harness} || {}}, |
| 105 |
subtest_end => undef, |
| 106 |
}, |
| 107 |
info => [ |
| 108 |
@{$f->{info} || []}, |
| 109 |
{ |
| 110 |
details => $f->{from_tap}->{details}, |
| 111 |
tag => $f->{from_tap}->{source} || 'STDOUT', |
| 112 |
from_harness => 1, |
| 113 |
} |
| 114 |
], |
| 115 |
}; |
| 116 |
|
| 117 |
$event = Test2::Harness::Event->new(stamp => time, job_try => $self->{+TRY}, facet_data => $f); |
| 118 |
} |
| 119 |
|
| 120 |
push @out => $event; |
| 121 |
|
| 122 |
# Close any deeper subtests, unless this event is nothing but output. A |
| 123 |
# line the test wrote to STDOUT or STDERR that is not TAP says nothing |
| 124 |
# about nesting, it just happens to have no indentation, so a warning from |
| 125 |
# a module must not end a subtest that is still being parsed. |
| 126 |
push @out => $self->close_deeper_subtests($nested, $event) |
| 127 |
unless $self->output_only($f); |
| 128 |
|
| 129 |
unless ($is_ours) { |
| 130 |
my $st = $self->{+SUBTESTS}->{$nested} ||= {}; |
| 131 |
my $fd = {%$f}; |
| 132 |
push @{$st->{children}} => $fd; |
| 133 |
return @out; |
| 134 |
} |
| 135 |
|
| 136 |
$self->subtest_process($f, $event); |
| 137 |
return @out; |
| 138 |
} |
| 139 |
|
| 140 |
sub close_deeper_subtests { |
| 141 |
my $self = shift; |
| 142 |
my ($nested, $event) = @_; |
| 143 |
|
| 144 |
my $sts = $self->{+SUBTESTS} or return; |
| 145 |
|
| 146 |
my @out; |
| 147 |
for my $n (sort { $b <=> $a } grep { $_ > $nested } keys %$sts) { |
| 148 |
my $st = delete $sts->{$n}; |
| 149 |
my $se = $st->{event} || $event; |
| 150 |
|
| 151 |
my $fd = $se->{facet_data}; |
| 152 |
delete $fd->{harness_watcher}->{no_render}; |
| 153 |
$fd->{parent}->{hid} ||= $n; |
| 154 |
$fd->{parent}->{children} ||= $st->{children}; |
| 155 |
$fd->{harness}->{closed_by} = $event; |
| 156 |
$fd->{harness}->{closed_by_eid} = $event->{event_id}; |
| 157 |
|
| 158 |
my $pn = $n - 1; |
| 159 |
|
| 160 |
if ($st->{event}) { |
| 161 |
if ($pn > $self->{+NESTED}) { |
| 162 |
push @{$sts->{$pn}->{children}} => $fd; |
| 163 |
} |
| 164 |
elsif ($pn == $self->{+NESTED}) { |
| 165 |
$self->subtest_process($fd, $se); |
| 166 |
push @out => $se; |
| 167 |
} |
| 168 |
} |
| 169 |
else { |
| 170 |
push @out => $se if $self->{+NESTED} && $pn == $self->{+NESTED}; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return @out; |
| 175 |
} |
| 176 |
|
| 177 |
sub output_only { |
| 178 |
my $self = shift; |
| 179 |
my ($f) = @_; |
| 180 |
|
| 181 |
# TAP tells us about nesting, even when all it holds is a comment. |
| 182 |
return 0 if $f->{from_tap}; |
| 183 |
|
| 184 |
return 0 if $f->{assert} || $f->{plan} || $f->{parent} || $f->{control} || $f->{errors}; |
| 185 |
return 0 if $f->{harness} && ($f->{harness}->{subtest_start} || $f->{harness}->{subtest_end}); |
| 186 |
|
| 187 |
return $f->{info} ? 1 : 0; |
| 188 |
} |
| 189 |
|
| 190 |
sub subtest_process { |
| 191 |
my $self = shift; |
| 192 |
my ($f, $event) = @_; |
| 193 |
|
| 194 |
my $closer = delete $f->{harness}->{closed_by}; |
| 195 |
$event ||= Test2::Harness::Event->new(facet_data => $f, job_try => $self->{+TRY}); |
| 196 |
|
| 197 |
$self->{+NUMBERS}->{$f->{assert}->{number}}++ |
| 198 |
if $f->{assert} && $f->{assert}->{number}; |
| 199 |
|
| 200 |
if ($f->{parent} && $f->{assert}) { |
| 201 |
my $name = $f->{assert}->{details} // "unnamed subtest ($f->{trace}->{frame}->[1] line $f->{trace}->{frame}->[2])"; |
| 202 |
|
| 203 |
my $subwatcher = blessed($self)->new(nested => $self->{+NESTED} + 1, job => $self->{+JOB}, try => $self->{+TRY}); |
| 204 |
|
| 205 |
my $id = 1; |
| 206 |
for my $sf (@{$f->{parent}->{children}}) { |
| 207 |
$sf->{harness}->{job_id} ||= $f->{harness}->{job_id}; |
| 208 |
$sf->{harness}->{run_id} ||= $f->{harness}->{run_id}; |
| 209 |
$sf->{harness}->{event_id} ||= $sf->{about}->{uuid} ||= gen_uuid(); |
| 210 |
$subwatcher->subtest_process($sf); |
| 211 |
} |
| 212 |
|
| 213 |
my @errors = $subwatcher->subtest_fail_error_facet_list(); |
| 214 |
|
| 215 |
if ($f->{harness}->{subtest_start}) { |
| 216 |
push @{$f->{errors}} => {tag => 'REASON', fail => 1, from_harness => 1, details => "Buffered subtest ended abruptly (missing closing brace event)"} |
| 217 |
unless $closer && $closer->{facet_data}->{harness}->{subtest_end}; |
| 218 |
} |
| 219 |
|
| 220 |
my $fail = 0; |
| 221 |
if (@errors) { |
| 222 |
push @{$f->{errors}} => @errors; |
| 223 |
$fail = 1; |
| 224 |
} |
| 225 |
else { |
| 226 |
$fail ||= $f->{assert} && !$f->{assert}->{pass} && !($f->{amnesty} && @{$f->{amnesty}}); |
| 227 |
$fail ||= $f->{control} && ($f->{control}->{halt} || $f->{control}->{terminate}); |
| 228 |
$fail ||= $f->{errors} && first { $_->{fail} } @{$f->{errors}}; |
| 229 |
} |
| 230 |
|
| 231 |
if ($fail) { |
| 232 |
$self->{+_SUB_FAILURES}++; |
| 233 |
|
| 234 |
# Populate the tree up to this subtest |
| 235 |
my $tree = $self->{+FAILED_SUBTEST_TREE} //= []; |
| 236 |
push @$tree => [$name, $subwatcher->{+FAILED_SUBTEST_TREE} // []]; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
$self->{+ASSERTION_COUNT}++ if $f->{assert}; |
| 241 |
|
| 242 |
if ($f->{assert} && !$f->{assert}->{pass} && !($f->{amnesty} && @{$f->{amnesty}})) { |
| 243 |
$self->{+_FAILURES}++; |
| 244 |
} |
| 245 |
|
| 246 |
if ($f->{control} || $f->{errors}) { |
| 247 |
my $err ||= $f->{control} && ($f->{control}->{halt} || $f->{control}->{terminate}); |
| 248 |
$err ||= $f->{errors} && first { $_->{fail} } @{$f->{errors}}; |
| 249 |
$self->{+_ERRORS}++ if $err; |
| 250 |
$self->{+HALT} = $f->{control}->{details} || '1' if $f->{control} && $f->{control}->{halt} && (!$self->{+HALT} || $self->{+HALT} eq '1'); |
| 251 |
} |
| 252 |
|
| 253 |
if ($f->{plan} && !$f->{plan}->{none}) { |
| 254 |
$self->{+_PLANS}++; |
| 255 |
$self->{+PLAN} = $f->{plan}; |
| 256 |
} |
| 257 |
|
| 258 |
if ($f->{harness_job_exit}) { |
| 259 |
$self->{+EXIT} = $f->{harness_job_exit}->{exit}; |
| 260 |
|
| 261 |
my $file = $self->file(); |
| 262 |
|
| 263 |
my $end = $f->{harness_job_end} = { |
| 264 |
file => $file, |
| 265 |
rel_file => File::Spec->abs2rel($file), |
| 266 |
abs_file => File::Spec->rel2abs($file), |
| 267 |
retry => $f->{harness_job_exit}->{retry}, |
| 268 |
fail => $self->fail(), |
| 269 |
stamp => $f->{harness_job_exit}->{stamp}, |
| 270 |
}; |
| 271 |
|
| 272 |
my $plan = $self->plan; |
| 273 |
$end->{skip} = $plan->{details} || "No reason given" if $plan && !$plan->{count}; |
| 274 |
|
| 275 |
my $times = $self->times; |
| 276 |
if ($times && $times->useful) { |
| 277 |
$end->{times} = $times->data_dump; |
| 278 |
push @{$f->{harness_job_fields}} => $times->job_fields; |
| 279 |
push @{$f->{info}} => {tag => 'TIME', details => $times->summary, table => $times->table}; |
| 280 |
} |
| 281 |
|
| 282 |
push @{$f->{errors}} => $self->fail_error_facet_list; |
| 283 |
} |
| 284 |
|
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
sub subtest_fail_error_facet_list { |
| 289 |
my $self = shift; |
| 290 |
|
| 291 |
return @{$self->{+_SUB_INFO}} if $self->{+_SUB_INFO}; |
| 292 |
|
| 293 |
my @out; |
| 294 |
|
| 295 |
my $plan = $self->{+PLAN} ? $self->{+PLAN}->{count} : undef; |
| 296 |
my $count = $self->{+ASSERTION_COUNT}; |
| 297 |
|
| 298 |
my $numbers = $self->{+NUMBERS}; |
| 299 |
my $max = max(keys %$numbers); |
| 300 |
if ($max) { |
| 301 |
for my $i (1 .. $max) { |
| 302 |
if (!$numbers->{$i}) { |
| 303 |
push @out => {tag => 'REASON', fail => 1, from_harness => 1, details => "Assertion number $i was never seen"}; |
| 304 |
} |
| 305 |
elsif ($numbers->{$i} > 1) { |
| 306 |
push @out => {tag => 'REASON', fail => 1, from_harness => 1, details => "Assertion number $i was seen more than once"}; |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
if (!$self->{+_PLANS}) { |
| 312 |
if ($count) { |
| 313 |
push @out => {tag => 'REASON', fail => 1, from_harness => 1, details => "No plan was declared"}; |
| 314 |
} |
| 315 |
else { |
| 316 |
push @out => {tag => 'REASON', fail => 1, from_harness => 1, details => "No plan was declared, and no assertions were made."}; |
| 317 |
} |
| 318 |
} |
| 319 |
elsif ($self->{+_PLANS} > 1) { |
| 320 |
push @out => {tag => 'REASON', fail => 1, from_harness => 1, details => "Too many plans were declared (Count: $self->{+_PLANS})"}; |
| 321 |
} |
| 322 |
|
| 323 |
push @out => {tag => 'REASON', fail => 1, from_harness => 1, details => "Planned for $plan assertions, but saw $self->{+ASSERTION_COUNT}"} |
| 324 |
if $plan && $count != $plan; |
| 325 |
|
| 326 |
push @out => {tag => 'REASON', fail => 1, from_harness => 1, details => "Subtest failures were encountered (Count: $self->{+_SUB_FAILURES})"} |
| 327 |
if $self->{+_SUB_FAILURES}; |
| 328 |
|
| 329 |
return @out; |
| 330 |
} |
| 331 |
|
| 332 |
sub fail_error_facet_list { |
| 333 |
my $self = shift; |
| 334 |
|
| 335 |
return @{$self->{+_INFO}} if $self->{+_INFO}; |
| 336 |
|
| 337 |
my @out; |
| 338 |
|
| 339 |
my $incomplete_subtests = values %{$self->{+SUBTESTS}}; |
| 340 |
push @out => {tag => 'REASON', fail => 1, from_harness => 1, details => "One or more incomplete subtests (Count: $incomplete_subtests)"} |
| 341 |
if $incomplete_subtests; |
| 342 |
|
| 343 |
if (my $wstat = $self->{+EXIT}) { |
| 344 |
if ($wstat == -1) { |
| 345 |
push @out => {tag => 'REASON', fail => 1, from_harness => 1, details => "The harness could not get the exit code! (Code: $wstat)"}; |
| 346 |
} |
| 347 |
else { |
| 348 |
my $e = parse_exit($wstat); |
| 349 |
if ($e->{err}) { |
| 350 |
push @out => {tag => 'REASON', fail => 1, from_harness => 1, details => "Test script returned error (Err: $e->{err})"}; |
| 351 |
} |
| 352 |
if ($e->{sig}) { |
| 353 |
push @out => {tag => 'REASON', fail => 1, from_harness => 1, details => "Test script returned error (Signal: $e->{sig})"}; |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
push @out => {tag => 'REASON', fail => 1, from_harness => 1, details => "Errors were encountered (Count: $self->{+_ERRORS})"} |
| 359 |
if $self->{+_ERRORS}; |
| 360 |
|
| 361 |
push @out => {tag => 'REASON', fail => 1, from_harness => 1, details => "Assertion failures were encountered (Count: $self->{+_FAILURES})"} |
| 362 |
if $self->{+_FAILURES}; |
| 363 |
|
| 364 |
push @out => $self->subtest_fail_error_facet_list(); |
| 365 |
|
| 366 |
return @out; |
| 367 |
} |
| 368 |
|
| 369 |
1; |
| 370 |
|
| 371 |
__END__ |
| 372 |
|
| 373 |
=pod |
| 374 |
|
| 375 |
=encoding UTF-8 |
| 376 |
|
| 377 |
=head1 NAME |
| 378 |
|
| 379 |
Test2::Harness::Auditor::Watcher - Class to monitor events for a single job and |
| 380 |
pass judgement on the result. |
| 381 |
|
| 382 |
=head1 DESCRIPTION |
| 383 |
|
| 384 |
This module represents a per-job state tracker. This module sees every event |
| 385 |
and manages the state produced. In the end this tracker determines if a test |
| 386 |
job passed or failed, and why. |
| 387 |
|
| 388 |
=head1 SYNOPSIS |
| 389 |
|
| 390 |
use Test2::Harness::Auditor::Watcher; |
| 391 |
|
| 392 |
my $watcher = Test2::Harness::Auditor::Watcher->new(); |
| 393 |
|
| 394 |
for my $event (@events) { |
| 395 |
$watcher->process($event); |
| 396 |
} |
| 397 |
|
| 398 |
print "Pass!" if $watcher->pass; |
| 399 |
print "Fail!" if $watcher->fail; |
| 400 |
|
| 401 |
=head1 METHODS |
| 402 |
|
| 403 |
=over 4 |
| 404 |
|
| 405 |
=item $int = $watcher->assertion_count() |
| 406 |
|
| 407 |
Number of assertions that have been seen. |
| 408 |
|
| 409 |
=item $exit = $watcher->exit() |
| 410 |
|
| 411 |
If the job has exited this will return the exit value (integer, 0 or greater). |
| 412 |
If the job has not exited yet (or at least if the watcher has not seen the exit |
| 413 |
event yet) this will return undef. |
| 414 |
|
| 415 |
=item $bool = $watcher->fail() |
| 416 |
|
| 417 |
Returns true if the job has failed/is failing. |
| 418 |
|
| 419 |
=item @error_facets = $watcher->fail_error_facet_list |
| 420 |
|
| 421 |
Used internally to get a list of 'error' facets to inject into the |
| 422 |
harness_job_exit event. |
| 423 |
|
| 424 |
=item $file = $watcher->file |
| 425 |
|
| 426 |
If the test file is known this will return it (string). This will return undef |
| 427 |
if the file is not yet known. |
| 428 |
|
| 429 |
=item $string = $watcher->halt |
| 430 |
|
| 431 |
If the test was halted (bail-out) this will contain the human readable reason. |
| 432 |
|
| 433 |
=item $bool = $watcher->has_exit |
| 434 |
|
| 435 |
Check if the exit value is known. |
| 436 |
|
| 437 |
=item $bool = $watcher->has_plan |
| 438 |
|
| 439 |
Check if a plan has been seen. |
| 440 |
|
| 441 |
=item $job = $watcher->job |
| 442 |
|
| 443 |
If the job is known this will return the detailed structure of the job. |
| 444 |
|
| 445 |
=item $int = $watcher->nested |
| 446 |
|
| 447 |
If this watcher represents a subtest this will be an integer greater than 0, |
| 448 |
the top-level test is 0. |
| 449 |
|
| 450 |
=item $hash = $watcher->numbers |
| 451 |
|
| 452 |
This is an internal state tracking what test numbers have been seen. This is |
| 453 |
really only applicable in tests that produced TAP. |
| 454 |
|
| 455 |
=item @events = $watcher->close_deeper_subtests($nested, $event) |
| 456 |
|
| 457 |
Closes every subtest open below the given nesting level, as the given event |
| 458 |
ends them. Returns the events the renderer still needs. |
| 459 |
|
| 460 |
=item $bool = $watcher->output_only($facet_data) |
| 461 |
|
| 462 |
True if the facet data holds nothing but output the test wrote to STDOUT or |
| 463 |
STDERR. Such an event says nothing about where the test is in a subtest, so it |
| 464 |
does not close one. |
| 465 |
|
| 466 |
=item $bool = $watcher->pass |
| 467 |
|
| 468 |
Check if the test job is passing. |
| 469 |
|
| 470 |
=item $plan_facet = $watcher->plan() |
| 471 |
|
| 472 |
If the plan facet has been seen this will return it. |
| 473 |
|
| 474 |
=item $watcher->process($event); |
| 475 |
|
| 476 |
Modify the state based on the provided event. |
| 477 |
|
| 478 |
=item $watcher->subtest_fail_error_facet_list |
| 479 |
|
| 480 |
Used internally to get a list of 'error' facets to inject into the |
| 481 |
harness_job_exit event. |
| 482 |
|
| 483 |
=item $times = $watcher->times() |
| 484 |
|
| 485 |
Returns the L<Test2::Harness::Auditor::TimeTracker> instance. |
| 486 |
|
| 487 |
=item $int = $watcher->try() |
| 488 |
|
| 489 |
Sometimes a job is run more than once, in those cases this will be an integer |
| 490 |
greater than 0 representing the try. 0 is used for the first try. |
| 491 |
|
| 492 |
=back |
| 493 |
|
| 494 |
=head1 SOURCE |
| 495 |
|
| 496 |
The source code repository for Test2-Harness can be found at |
| 497 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 498 |
|
| 499 |
=head1 MAINTAINERS |
| 500 |
|
| 501 |
=over 4 |
| 502 |
|
| 503 |
=item Chad Granum E<lt>[email protected]<gt> |
| 504 |
|
| 505 |
=back |
| 506 |
|
| 507 |
=head1 AUTHORS |
| 508 |
|
| 509 |
=over 4 |
| 510 |
|
| 511 |
=item Chad Granum E<lt>[email protected]<gt> |
| 512 |
|
| 513 |
=back |
| 514 |
|
| 515 |
=head1 COPYRIGHT |
| 516 |
|
| 517 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 518 |
|
| 519 |
This program is free software; you can redistribute it and/or |
| 520 |
modify it under the same terms as Perl itself. |
| 521 |
|
| 522 |
See F<http://dev.perl.org/licenses/> |
| 523 |
|
| 524 |
=cut |