| 1 |
package Test2::Formatter::Test2; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Test2::Util::Term qw/term_size/; |
| 8 |
use Test2::Harness::Util qw/hub_truth apply_encoding/; |
| 9 |
use Test2::Harness::Util::Term qw/USE_ANSI_COLOR/; |
| 10 |
use Test2::Util qw/IS_WIN32 clone_io/; |
| 11 |
use Time::HiRes qw/time/; |
| 12 |
use IO::Handle; |
| 13 |
|
| 14 |
use File::Spec(); |
| 15 |
use Test2::Formatter::Test2::Composer; |
| 16 |
|
| 17 |
use parent 'Test2::Formatter'; |
| 18 |
|
| 19 |
sub import { |
| 20 |
my $class = shift; |
| 21 |
return if $ENV{HARNESS_ACTIVE}; |
| 22 |
$class->SUPER::import; |
| 23 |
} |
| 24 |
|
| 25 |
use Test2::Util::HashBase qw{ |
| 26 |
-composer |
| 27 |
-last_depth |
| 28 |
-_buffered |
| 29 |
<job_io |
| 30 |
+io |
| 31 |
<enc_io |
| 32 |
-_encoding |
| 33 |
-show_buffer |
| 34 |
-color |
| 35 |
-progress |
| 36 |
-tty |
| 37 |
-no_wrap |
| 38 |
-verbose |
| 39 |
-job_length |
| 40 |
-ecount |
| 41 |
-job_colors |
| 42 |
-active_files |
| 43 |
-_active_disp |
| 44 |
-_file_stats |
| 45 |
-job_names |
| 46 |
-is_persistent |
| 47 |
-interactive |
| 48 |
}; |
| 49 |
|
| 50 |
sub TAG_WIDTH() { 8 } |
| 51 |
|
| 52 |
sub hide_buffered() { 0 } |
| 53 |
|
| 54 |
sub DEFAULT_TAG_COLOR() { |
| 55 |
return ( |
| 56 |
'DEBUG' => Term::ANSIColor::color('red'), |
| 57 |
'DIAG' => Term::ANSIColor::color('yellow'), |
| 58 |
'ERROR' => Term::ANSIColor::color('red'), |
| 59 |
'FATAL' => Term::ANSIColor::color('bold red'), |
| 60 |
'FAIL' => Term::ANSIColor::color('red'), |
| 61 |
'HALT' => Term::ANSIColor::color('bold red'), |
| 62 |
'PASS' => Term::ANSIColor::color('green'), |
| 63 |
'! PASS !' => Term::ANSIColor::color('cyan'), |
| 64 |
'TODO' => Term::ANSIColor::color('cyan'), |
| 65 |
'NO PLAN' => Term::ANSIColor::color('yellow'), |
| 66 |
'SKIP' => Term::ANSIColor::color('bold cyan'), |
| 67 |
'SKIP ALL' => Term::ANSIColor::color('bold white on_blue'), |
| 68 |
'STDERR' => Term::ANSIColor::color('yellow'), |
| 69 |
'RUN INFO' => Term::ANSIColor::color('bold bright_blue'), |
| 70 |
'JOB INFO' => Term::ANSIColor::color('bold bright_blue'), |
| 71 |
'LAUNCH' => Term::ANSIColor::color('bold bright_white'), |
| 72 |
'RETRY' => Term::ANSIColor::color('bold bright_white'), |
| 73 |
'PASSED' => Term::ANSIColor::color('bold bright_green'), |
| 74 |
'TO RETRY' => Term::ANSIColor::color('bold bright_yellow'), |
| 75 |
'FAILED' => Term::ANSIColor::color('bold bright_red'), |
| 76 |
'REASON' => Term::ANSIColor::color('magenta'), |
| 77 |
'TIMEOUT' => Term::ANSIColor::color('magenta'), |
| 78 |
'TIME' => Term::ANSIColor::color('blue'), |
| 79 |
'MEMORY' => Term::ANSIColor::color('blue'), |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
sub DEFAULT_FACET_COLOR() { |
| 84 |
return ( |
| 85 |
time => Term::ANSIColor::color('blue'), |
| 86 |
memory => Term::ANSIColor::color('blue'), |
| 87 |
about => Term::ANSIColor::color('magenta'), |
| 88 |
amnesty => Term::ANSIColor::color('cyan'), |
| 89 |
assert => Term::ANSIColor::color('bold bright_white'), |
| 90 |
control => Term::ANSIColor::color('bold red'), |
| 91 |
error => Term::ANSIColor::color('yellow'), |
| 92 |
info => Term::ANSIColor::color('yellow'), |
| 93 |
meta => Term::ANSIColor::color('magenta'), |
| 94 |
parent => Term::ANSIColor::color('magenta'), |
| 95 |
trace => Term::ANSIColor::color('bold red'), |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
# These colors all look decent enough to use, ordered to avoid putting similar ones together |
| 100 |
use constant DEFAULT_JOB_COLOR_NAMES => ( |
| 101 |
'bold green on_blue', |
| 102 |
'bold blue on_white', |
| 103 |
'bold black on_cyan', |
| 104 |
'bold green on_bright_black', |
| 105 |
'bold dark blue on_white', |
| 106 |
'bold black on_green', |
| 107 |
'bold cyan on_blue', |
| 108 |
'bold black on_white', |
| 109 |
'bold white on_cyan', |
| 110 |
'bold cyan on_bright_black', |
| 111 |
'bold white on_green', |
| 112 |
'bold bright_black on_white', |
| 113 |
'bold white on_blue', |
| 114 |
'bold bright_cyan on_green', |
| 115 |
'bold blue on_cyan', |
| 116 |
'bold white on_bright_black', |
| 117 |
'bold bright_black on_green', |
| 118 |
'bold bright_green on_blue', |
| 119 |
'bold bright_blue on_white', |
| 120 |
'bold bright_white on_bright_black', |
| 121 |
'bold yellow on_blue', |
| 122 |
'bold bright_black on_cyan', |
| 123 |
'bold bright_green on_bright_black', |
| 124 |
'bold blue on_green', |
| 125 |
'bold bright_cyan on_blue', |
| 126 |
'bold bright_blue on_cyan', |
| 127 |
'bold dark bright_white on_bright_black', |
| 128 |
'bold bright_blue on_green', |
| 129 |
'bold dark bright_blue on_white', |
| 130 |
'bold bright_white on_blue', |
| 131 |
'bold bright_cyan on_bright_black', |
| 132 |
'bold bright_white on_cyan', |
| 133 |
'bold bright_white on_green', |
| 134 |
'bold bright_yellow on_blue', |
| 135 |
#'bold magenta on_white', |
| 136 |
#'bold dark magenta on_white', |
| 137 |
#'bold dark cyan on_white', |
| 138 |
'bold dark bright_cyan on_bright_black', |
| 139 |
#'bold dark bright_green on_black', |
| 140 |
#'bold dark bright_yellow on_black', |
| 141 |
); |
| 142 |
|
| 143 |
sub DEFAULT_JOB_COLOR() { |
| 144 |
return map { Term::ANSIColor::color($_) } DEFAULT_JOB_COLOR_NAMES; |
| 145 |
} |
| 146 |
|
| 147 |
sub DEFAULT_COLOR() { |
| 148 |
return ( |
| 149 |
reset => Term::ANSIColor::color('reset'), |
| 150 |
blob => Term::ANSIColor::color('bold bright_black on_white'), |
| 151 |
tree => Term::ANSIColor::color('bold bright_white'), |
| 152 |
tag_border => Term::ANSIColor::color('bold bright_white'), |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
my %FACET_TAG_BORDERS = ( |
| 157 |
'default' => ['[', ']'], |
| 158 |
'amnesty' => ['{', '}'], |
| 159 |
'info' => ['(', ')'], |
| 160 |
'error' => ['<', '>'], |
| 161 |
'parent' => [' ', ' '], |
| 162 |
); |
| 163 |
|
| 164 |
sub init { |
| 165 |
my $self = shift; |
| 166 |
|
| 167 |
$self->{+COMPOSER} ||= Test2::Formatter::Test2::Composer->new; |
| 168 |
|
| 169 |
$self->{+VERBOSE} = 1 unless defined $self->{+VERBOSE}; |
| 170 |
|
| 171 |
$self->{+JOB_LENGTH} ||= 2; |
| 172 |
|
| 173 |
my $io = $self->{+IO} = clone_io($self->{+IO} || \*STDOUT) or die "Cannot get a filehandle: $!"; |
| 174 |
$io->autoflush(1); |
| 175 |
|
| 176 |
$self->{+TTY} = -t $io unless defined $self->{+TTY}; |
| 177 |
|
| 178 |
my $use_color = ref($self->{+COLOR}) ? 1 : delete($self->{+COLOR}); |
| 179 |
$use_color = $self->{+TTY} unless defined $use_color; |
| 180 |
|
| 181 |
if ($use_color && USE_ANSI_COLOR) { |
| 182 |
$self->{+SHOW_BUFFER} = 1 unless defined $self->{+SHOW_BUFFER}; |
| 183 |
|
| 184 |
if ($use_color) { |
| 185 |
$self->{+COLOR} = { |
| 186 |
DEFAULT_COLOR(), |
| 187 |
TAGS => {DEFAULT_TAG_COLOR()}, |
| 188 |
FACETS => {DEFAULT_FACET_COLOR()}, |
| 189 |
JOBS => [DEFAULT_JOB_COLOR()], |
| 190 |
} unless defined $self->{+COLOR}; |
| 191 |
|
| 192 |
$self->{+JOB_COLORS} = {free => [@{$self->{+COLOR}->{JOBS}}]}; |
| 193 |
} |
| 194 |
} |
| 195 |
else { |
| 196 |
$self->{+SHOW_BUFFER} = 0 unless defined $self->{+SHOW_BUFFER}; |
| 197 |
} |
| 198 |
|
| 199 |
$self->{+ECOUNT} //= 0; |
| 200 |
|
| 201 |
my $reset = $use_color ? Term::ANSIColor::color('reset') : ''; |
| 202 |
my $cyan = $use_color ? Term::ANSIColor::color('cyan') : ''; |
| 203 |
$self->{+_ACTIVE_DISP} = ["[${cyan}INITIALIZING${reset}]", '']; |
| 204 |
$self->{+_FILE_STATS} = { |
| 205 |
passed => 0, |
| 206 |
failed => 0, |
| 207 |
running => 0, |
| 208 |
todo => 0, |
| 209 |
total => 0, |
| 210 |
}; |
| 211 |
|
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
sub io { |
| 216 |
my $self = shift; |
| 217 |
my ($job_id) = @_; |
| 218 |
return $self->{+IO} unless defined $job_id; |
| 219 |
return $self->{+JOB_IO}->{$job_id} // $self->{+IO}; |
| 220 |
} |
| 221 |
|
| 222 |
sub encoding { |
| 223 |
my $self = shift; |
| 224 |
|
| 225 |
if (@_) { |
| 226 |
my ($enc, $job_id) = @_; |
| 227 |
if (defined $job_id) { |
| 228 |
my $io; |
| 229 |
|
| 230 |
unless ($io = $self->{+ENC_IO}->{$enc}) { |
| 231 |
$io = $self->{+ENC_IO}->{$enc} = clone_io($self->{+IO} || \*STDOUT) or die "Cannot get a filehandle: $!"; |
| 232 |
$io->autoflush(1); |
| 233 |
apply_encoding($io, $enc); |
| 234 |
} |
| 235 |
|
| 236 |
$self->{+JOB_IO}->{$job_id} = $io; |
| 237 |
} |
| 238 |
else { |
| 239 |
apply_encoding($self->{+IO}, $enc); |
| 240 |
} |
| 241 |
$self->{+_ENCODING} = $enc; |
| 242 |
} |
| 243 |
|
| 244 |
return $self->{+_ENCODING}; |
| 245 |
} |
| 246 |
|
| 247 |
if ($^C) { |
| 248 |
no warnings 'redefine'; |
| 249 |
*write = sub {}; |
| 250 |
} |
| 251 |
sub write { |
| 252 |
my ($self, $e, $num, $f) = @_; |
| 253 |
$f ||= $e->facet_data; |
| 254 |
|
| 255 |
my $should_show = $self->update_active_disp($f); |
| 256 |
|
| 257 |
$self->{+ECOUNT}++; |
| 258 |
|
| 259 |
my $job_id = $f->{harness}->{job_id}; |
| 260 |
$self->encoding($f->{control}->{encoding}, $job_id) if $f->{control}->{encoding}; |
| 261 |
|
| 262 |
my $hf = hub_truth($f); |
| 263 |
my $depth = $hf->{nested} || 0; |
| 264 |
|
| 265 |
return if $depth && (!$self->{+SHOW_BUFFER} || !$self->{+PROGRESS}); |
| 266 |
|
| 267 |
my $lines; |
| 268 |
if (!$self->{+VERBOSE}) { |
| 269 |
if ($depth) { |
| 270 |
$lines = []; |
| 271 |
} |
| 272 |
else { |
| 273 |
$lines = $self->render_quiet($f); |
| 274 |
} |
| 275 |
} |
| 276 |
elsif ($depth) { |
| 277 |
my $tree = $self->render_tree($f, '>'); |
| 278 |
$lines = $self->render_buffered_event($f, $tree); |
| 279 |
} |
| 280 |
else { |
| 281 |
my $tree = $self->render_tree($f,); |
| 282 |
$lines = $self->render_event($f, $tree); |
| 283 |
} |
| 284 |
|
| 285 |
$should_show ||= $lines && @$lines; |
| 286 |
unless ($should_show || $self->{+VERBOSE}) { |
| 287 |
if (my $last = $self->{last_rendered}) { |
| 288 |
return if time - $last < 0.2; |
| 289 |
$self->{last_rendered} = time; |
| 290 |
} |
| 291 |
else { |
| 292 |
$self->{last_rendered} = time; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
push @{$self->{+JOB_COLORS}->{free}} => delete $self->{+JOB_COLORS}->{used}->{$job_id} |
| 297 |
if $job_id && $f->{harness_job_end}; |
| 298 |
|
| 299 |
# Local is expensive! Only do it if we really need to. |
| 300 |
local($\, $,) = (undef, '') if $\ || $,; |
| 301 |
|
| 302 |
my $io = $self->io($job_id); |
| 303 |
if ($self->{+_BUFFERED}) { |
| 304 |
print $io "\r\e[K"; |
| 305 |
$self->{+_BUFFERED} = 0; |
| 306 |
} |
| 307 |
|
| 308 |
if (!$self->{+VERBOSE}) { |
| 309 |
print $io $_, "\n" for @$lines; |
| 310 |
if ($self->{+TTY} && $self->{+PROGRESS}) { |
| 311 |
print $io $self->render_status($f); |
| 312 |
$self->{+_BUFFERED} = 1; |
| 313 |
} |
| 314 |
} |
| 315 |
elsif ($depth && $lines && @$lines && !$self->{+INTERACTIVE}) { |
| 316 |
print $io $lines->[0]; |
| 317 |
$self->{+_BUFFERED} = 1; |
| 318 |
} |
| 319 |
else { |
| 320 |
print $io $_, "\n" for @$lines; |
| 321 |
} |
| 322 |
|
| 323 |
delete $self->{+JOB_IO}->{$job_id} if $job_id && $f->{harness_job_end}; |
| 324 |
} |
| 325 |
|
| 326 |
sub finalize { |
| 327 |
my $self = shift; |
| 328 |
|
| 329 |
my $io = $self->{+IO}; |
| 330 |
print $io "\r\e[K" if $self->{+_BUFFERED}; |
| 331 |
|
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
sub step { |
| 336 |
my $self = shift; |
| 337 |
|
| 338 |
return unless $self->update_active_disp; |
| 339 |
|
| 340 |
my $io = $self->io(0); |
| 341 |
if ($self->{+_BUFFERED}) { |
| 342 |
print $io "\r\e[K"; |
| 343 |
$self->{+_BUFFERED} = 0; |
| 344 |
} |
| 345 |
|
| 346 |
if ($self->{+TTY} && $self->{+PROGRESS}) { |
| 347 |
print $io $self->render_status(); |
| 348 |
$self->{+_BUFFERED} = 1; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
sub update_active_disp { |
| 353 |
my $self = shift; |
| 354 |
my ($f) = @_; |
| 355 |
my $should_show = 0; |
| 356 |
|
| 357 |
my $stats = $self->{+_FILE_STATS}; |
| 358 |
|
| 359 |
my $out = 0; |
| 360 |
$out = $self->update_spinner($stats) unless $stats->{started}; |
| 361 |
|
| 362 |
return $out unless $f; |
| 363 |
|
| 364 |
if (my $task = $f->{harness_job_queued}) { |
| 365 |
$self->{+JOB_NAMES}->{$task->{job_id}} = $task->{job_name} || $task->{job_id}; |
| 366 |
$stats->{total}++; |
| 367 |
$stats->{todo}++; |
| 368 |
} |
| 369 |
|
| 370 |
if ($f->{harness_job_launch}) { |
| 371 |
my $job = $f->{harness_job}; |
| 372 |
$self->{+ACTIVE_FILES}->{File::Spec->abs2rel($job->{file})} = $job->{job_name} || $job->{job_id}; |
| 373 |
$should_show = 1; |
| 374 |
$stats->{running}++; |
| 375 |
$stats->{todo}--; |
| 376 |
$stats->{started} //= 1; |
| 377 |
} |
| 378 |
|
| 379 |
if ($f->{harness_job_end}) { |
| 380 |
my $file = $f->{harness_job_end}->{file}; |
| 381 |
delete $self->{+ACTIVE_FILES}->{File::Spec->abs2rel($file)}; |
| 382 |
$should_show = 1; |
| 383 |
$stats->{running}--; |
| 384 |
|
| 385 |
if ($f->{harness_job_end}->{fail}) { |
| 386 |
$stats->{failed}++; |
| 387 |
} |
| 388 |
else { |
| 389 |
$stats->{passed}++; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
return $out unless $should_show; |
| 394 |
|
| 395 |
my $statline = join '|' => ( |
| 396 |
$self->_highlight($stats->{passed}, 'P', 'green'), |
| 397 |
$self->_highlight($stats->{failed}, 'F', 'red'), |
| 398 |
$self->_highlight($stats->{running}, 'R', 'cyan'), |
| 399 |
$self->_highlight($stats->{todo}, 'T', 'yellow'), |
| 400 |
); |
| 401 |
|
| 402 |
$statline = "[$statline]"; |
| 403 |
|
| 404 |
my $active = $self->{+ACTIVE_FILES}; |
| 405 |
|
| 406 |
return $self->{+_ACTIVE_DISP} = [$statline, ''] unless $active && keys %$active; |
| 407 |
|
| 408 |
my $reset = $self->reset; |
| 409 |
|
| 410 |
my $str .= "("; |
| 411 |
{ |
| 412 |
no warnings 'numeric'; |
| 413 |
$str .= join(' ' => map { m{([^/]+)$}; "$active->{$_}:$1" } sort { ($active->{$a} || 0) <=> ($active->{$b} || 0) or $a cmp $b } keys %$active); |
| 414 |
} |
| 415 |
$str .= ")"; |
| 416 |
|
| 417 |
$self->{+_ACTIVE_DISP} = [$statline, $str]; |
| 418 |
|
| 419 |
return 1; |
| 420 |
} |
| 421 |
|
| 422 |
sub update_spinner { |
| 423 |
my $self = shift; |
| 424 |
my ($stats) = @_; |
| 425 |
|
| 426 |
$stats->{spinner} //= '|'; |
| 427 |
$stats->{spinner_time} //= time - 1; |
| 428 |
$stats->{blink_time} //= time - 1; |
| 429 |
$stats->{blink} //= ''; |
| 430 |
|
| 431 |
if (time - $stats->{spinner_time} > 0.1) { |
| 432 |
$stats->{spinner_time} = time; |
| 433 |
my $start = substr($stats->{spinner}, 0, 1); |
| 434 |
$stats->{spinner} = '\\' if $start eq '-'; |
| 435 |
$stats->{spinner} = '-' if $start eq '/'; |
| 436 |
$stats->{spinner} = '/' if $start eq '|'; |
| 437 |
$stats->{spinner} = '|' if $start eq '\\'; |
| 438 |
} |
| 439 |
elsif(time - $stats->{blink_time} > 0.5) { |
| 440 |
$stats->{blink_time} = time; |
| 441 |
$stats->{blink} = $stats->{blink} ? '' : 'bold bright_'; |
| 442 |
} |
| 443 |
else { |
| 444 |
return 0; |
| 445 |
} |
| 446 |
|
| 447 |
my $yellow = $self->{+COLOR} ? Term::ANSIColor::color($stats->{blink} . 'yellow') : ''; |
| 448 |
my $cyan = $self->{+COLOR} ? Term::ANSIColor::color('cyan') : ''; |
| 449 |
my $green = $self->{+COLOR} ? Term::ANSIColor::color('bold bright_green') : ''; |
| 450 |
my $bold = $self->{+COLOR} ? Term::ANSIColor::color('bold bright_white') : ''; |
| 451 |
my $reset = $self->reset; |
| 452 |
|
| 453 |
$self->{+_ACTIVE_DISP} = [ |
| 454 |
join( |
| 455 |
'' => ( |
| 456 |
$bold => "[ ", $reset, |
| 457 |
$green => $stats->{spinner}, $reset, |
| 458 |
'' => " ", |
| 459 |
$self->{+IS_PERSISTENT} |
| 460 |
? ( |
| 461 |
$yellow => "Waiting for busy runner", $reset, |
| 462 |
'' => " ", |
| 463 |
$reset => "(see ", $reset, |
| 464 |
$cyan => "yath status", $reset, |
| 465 |
$reset => ")", $reset, |
| 466 |
) |
| 467 |
: ($yellow => "INITIALIZING", $reset), |
| 468 |
'' => " ", |
| 469 |
$green => $stats->{spinner}, $reset, |
| 470 |
$bold => " ]", $reset, |
| 471 |
) |
| 472 |
), |
| 473 |
'', |
| 474 |
]; |
| 475 |
|
| 476 |
return 1; |
| 477 |
} |
| 478 |
|
| 479 |
sub _highlight { |
| 480 |
my $self = shift; |
| 481 |
my ($val, $label, $color) = @_; |
| 482 |
|
| 483 |
return "${label}:${val}" unless $val && $self->{+COLOR}; |
| 484 |
return sprintf('%s%s:%d%s', Term::ANSIColor::color($color), $label, $val, $self->reset); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
sub colorstrip { |
| 489 |
my $self = shift; |
| 490 |
my ($str) = @_; |
| 491 |
|
| 492 |
return $str unless USE_ANSI_COLOR; |
| 493 |
return Term::ANSIColor::colorstrip($str); |
| 494 |
} |
| 495 |
|
| 496 |
sub render_status { |
| 497 |
my $self = shift; |
| 498 |
|
| 499 |
my $reset = $self->reset; |
| 500 |
my $cyan = $self->{+COLOR} ? Term::ANSIColor::color('cyan') : ''; |
| 501 |
|
| 502 |
my $str = "$self->{+_ACTIVE_DISP}->[0] Events: $self->{+ECOUNT} ${cyan}$self->{+_ACTIVE_DISP}->[1]${reset}"; |
| 503 |
|
| 504 |
my $max = term_size() || 80; |
| 505 |
|
| 506 |
if (length($str) > $max) { |
| 507 |
my $nocolor = $self->colorstrip($str); |
| 508 |
$str = substr($nocolor, 0, $max - 8) . " ...)$reset" if length($nocolor) > $max; |
| 509 |
$str =~ s/\(/$cyan(/; |
| 510 |
$str =~ s/^\[[^\]]+\]/$self->{+_ACTIVE_DISP}->[0]/; |
| 511 |
} |
| 512 |
|
| 513 |
return $str; |
| 514 |
} |
| 515 |
|
| 516 |
sub render_buffered_event { |
| 517 |
my $self = shift; |
| 518 |
my ($f, $tree) = @_; |
| 519 |
|
| 520 |
my $comp = $self->{+COMPOSER}->render_one_line($f) or return; |
| 521 |
|
| 522 |
return unless @$comp; |
| 523 |
return [$self->build_line($tree, @$comp)]; |
| 524 |
} |
| 525 |
|
| 526 |
sub render_event { |
| 527 |
my $self = shift; |
| 528 |
my ($f, $tree) = @_; |
| 529 |
|
| 530 |
my $comps = $self->{+COMPOSER}->render_verbose($f); |
| 531 |
|
| 532 |
my (@parent, @times); |
| 533 |
|
| 534 |
if ($f->{parent}) { |
| 535 |
@parent = $self->render_parent($f, $tree); |
| 536 |
|
| 537 |
if (@$comps && $comps->[-1]->[0] eq 'times') { |
| 538 |
my $times = pop(@$comps); |
| 539 |
@times = $self->build_line($tree, @$times); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
my @out; |
| 544 |
|
| 545 |
for my $comp (@$comps) { |
| 546 |
my $ctree = $tree; |
| 547 |
substr($ctree, -2, 2, '+~') if $comp->[0] eq 'assert' && $f->{parent}; |
| 548 |
push @out => $self->build_line($ctree, @$comp); |
| 549 |
} |
| 550 |
|
| 551 |
push @out => (@parent, @times); |
| 552 |
|
| 553 |
return \@out; |
| 554 |
} |
| 555 |
|
| 556 |
sub render_quiet { |
| 557 |
my $self = shift; |
| 558 |
my ($f, $tree) = @_; |
| 559 |
|
| 560 |
my @out; |
| 561 |
|
| 562 |
my $comps = $self->{+COMPOSER}->render_brief($f); |
| 563 |
for my $comp (@$comps) { |
| 564 |
my $ctree = $tree ||= $self->render_tree($f); |
| 565 |
substr($ctree, -2, 2, '+~') if $comp->[0] eq 'assert' && $f->{parent}; |
| 566 |
push @out => $self->build_line($ctree, @$comp); |
| 567 |
} |
| 568 |
|
| 569 |
if ($f->{parent} && !$f->{amnesty}) { |
| 570 |
push @out => $self->render_parent($f, $tree ||= $self->render_tree($f), quiet => 1); |
| 571 |
} |
| 572 |
|
| 573 |
return \@out; |
| 574 |
} |
| 575 |
|
| 576 |
sub reset { |
| 577 |
my $self = shift; |
| 578 |
return $self->{+COLOR} ? $self->{+COLOR}->{reset} : ''; |
| 579 |
} |
| 580 |
|
| 581 |
sub job_color { |
| 582 |
my $self = shift; |
| 583 |
my ($id, $set) = @_; |
| 584 |
return '' unless $self->{+JOB_COLORS}; |
| 585 |
return $self->{+JOB_COLORS}->{used}->{$id} || '' unless $set; |
| 586 |
return $self->{+JOB_COLORS}->{used}->{$id} ||= shift @{$self->{+JOB_COLORS}->{free}} || ''; |
| 587 |
} |
| 588 |
|
| 589 |
sub render_tree { |
| 590 |
my $self = shift; |
| 591 |
my ($f, $char) = @_; |
| 592 |
$char ||= '|'; |
| 593 |
|
| 594 |
my $job = ''; |
| 595 |
if ($f->{harness} && $f->{harness}->{job_id}) { |
| 596 |
my $id = $f->{harness}->{job_id}; |
| 597 |
my $name = $self->{+JOB_NAMES}->{$id}; |
| 598 |
|
| 599 |
my ($color, $reset) = ('', ''); |
| 600 |
if ($self->{+JOB_COLORS}) { |
| 601 |
$color = $self->job_color($id, 'set'); |
| 602 |
$reset = $self->reset; |
| 603 |
} |
| 604 |
|
| 605 |
my $len = length($name); |
| 606 |
if (!$self->{+JOB_LENGTH} || $len > $self->{+JOB_LENGTH}) { |
| 607 |
$self->{+JOB_LENGTH} = $len; |
| 608 |
} |
| 609 |
else { |
| 610 |
$len = $self->{+JOB_LENGTH}; |
| 611 |
} |
| 612 |
|
| 613 |
$job = sprintf("%sjob %${len}s%s ", $color, $name, $reset || ''); |
| 614 |
} |
| 615 |
|
| 616 |
my $hf = hub_truth($f); |
| 617 |
my $depth = $hf->{nested} || 0; |
| 618 |
|
| 619 |
my @pipes = (' ', map $char, 1 .. $depth); |
| 620 |
return join(' ' => $job, @pipes) . ' '; |
| 621 |
} |
| 622 |
|
| 623 |
sub build_line { |
| 624 |
my $self = shift; |
| 625 |
my ($tree, $facet, $tag, $text) = @_; |
| 626 |
|
| 627 |
$tree ||= ''; |
| 628 |
$tag ||= ''; |
| 629 |
$text ||= ''; |
| 630 |
chomp($text); |
| 631 |
|
| 632 |
substr($tree, -2, 1, '+') if $facet eq 'assert'; |
| 633 |
|
| 634 |
$tag = substr($tag, 0 - TAG_WIDTH, TAG_WIDTH) if length($tag) > TAG_WIDTH; |
| 635 |
|
| 636 |
my $max = $self->{+TTY} && !$self->{+NO_WRAP} ? (term_size() || 80) : undef; |
| 637 |
my $color = $self->{+COLOR}; |
| 638 |
my $reset = $self->reset; |
| 639 |
my $tcolor = $color ? $color->{TAGS}->{$tag} || $color->{FACETS}->{$facet} || '' : ''; |
| 640 |
|
| 641 |
my ($ps, $pe) = @{$FACET_TAG_BORDERS{$facet} || $FACET_TAG_BORDERS{default}}; |
| 642 |
|
| 643 |
$tag = uc($tag); |
| 644 |
my $length = length($tag); |
| 645 |
if ($length > TAG_WIDTH) { |
| 646 |
$tag = substr($tag, 0, TAG_WIDTH); |
| 647 |
} |
| 648 |
elsif($length < TAG_WIDTH) { |
| 649 |
my $pad = (TAG_WIDTH - $length) / 2; |
| 650 |
my $padl = $pad + (TAG_WIDTH - $length) % 2; |
| 651 |
$tag = (' ' x $padl) . $tag . (' ' x $pad); |
| 652 |
} |
| 653 |
|
| 654 |
my $start; |
| 655 |
if ($color) { |
| 656 |
my $border = $color->{tag_border} || ''; |
| 657 |
$start = "${reset}${border}${ps}${reset}${tcolor}${tag}${reset}${border}${pe}${reset}"; |
| 658 |
} |
| 659 |
else { |
| 660 |
$start = "${ps}${tag}${pe}"; |
| 661 |
} |
| 662 |
$start .= " "; |
| 663 |
|
| 664 |
if ($tree) { |
| 665 |
if ($color) { |
| 666 |
my $trcolor = $color->{tree} || ''; |
| 667 |
$start .= $trcolor . $tree . $reset; |
| 668 |
} |
| 669 |
else { |
| 670 |
$start .= $tree; |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
my @lines = split /[\r\n]/, $text; |
| 675 |
@lines = ($text) unless @lines; |
| 676 |
|
| 677 |
my @out; |
| 678 |
for my $line (@lines) { |
| 679 |
if(@lines > 1 && $max && length("$ps$tag$pe $tree$line") > $max) { |
| 680 |
@out = (); |
| 681 |
last; |
| 682 |
} |
| 683 |
|
| 684 |
if ($color) { |
| 685 |
push @out => "${start}${tcolor}${line}$reset"; |
| 686 |
} |
| 687 |
else { |
| 688 |
push @out => "${start}${line}"; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
return @out if @out; |
| 693 |
|
| 694 |
return ( |
| 695 |
"$start----- START -----", |
| 696 |
$text, |
| 697 |
"$start------ END ------", |
| 698 |
) unless $color; |
| 699 |
|
| 700 |
my $blob = $color->{blob} || ''; |
| 701 |
return ( |
| 702 |
"$start${blob}----- START -----$reset", |
| 703 |
"${tcolor}${text}${reset}", |
| 704 |
"$start${blob}------ END ------$reset", |
| 705 |
); |
| 706 |
} |
| 707 |
|
| 708 |
sub render_parent { |
| 709 |
my $self = shift; |
| 710 |
my ($f, $tree, %params) = @_; |
| 711 |
|
| 712 |
my $meth = $params{quiet} ? 'render_quiet' : 'render_event'; |
| 713 |
|
| 714 |
my @out; |
| 715 |
for my $sf (@{$f->{parent}->{children}}) { |
| 716 |
$sf->{harness} ||= $f->{harness}; |
| 717 |
my $tree = $self->render_tree($sf); |
| 718 |
push @out => @{$self->$meth($sf, $tree)}; |
| 719 |
} |
| 720 |
|
| 721 |
return unless @out; |
| 722 |
|
| 723 |
push @out => ( |
| 724 |
$self->build_line("$tree^", 'parent', '', ''), |
| 725 |
); |
| 726 |
|
| 727 |
return @out; |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
sub DESTROY { |
| 732 |
my $self = shift; |
| 733 |
|
| 734 |
my $io = $self->{+IO} or return; |
| 735 |
|
| 736 |
# Local is expensive! Only do it if we really need to. |
| 737 |
local($\, $,) = (undef, '') if $\ || $,; |
| 738 |
|
| 739 |
print $io Term::ANSIColor::color('reset') |
| 740 |
if USE_ANSI_COLOR; |
| 741 |
|
| 742 |
print $io "\n"; |
| 743 |
} |
| 744 |
|
| 745 |
1; |
| 746 |
|
| 747 |
__END__ |
| 748 |
|
| 749 |
=pod |
| 750 |
|
| 751 |
=encoding UTF-8 |
| 752 |
|
| 753 |
=head1 NAME |
| 754 |
|
| 755 |
Test2::Formatter::Test2 - An alternative to TAP, used by Test2::Harness. |
| 756 |
|
| 757 |
=head1 DESCRIPTION |
| 758 |
|
| 759 |
This formatter is the primary formatter used for final result rendering when |
| 760 |
you use Test2::Harness. This formatter is NOT designed to have its output |
| 761 |
consumed by code/machine/harnesses. The goal of this formatter is to have |
| 762 |
output that is easily read by humans. |
| 763 |
|
| 764 |
=head1 SYNOPSIS |
| 765 |
|
| 766 |
If you are running a test directly with perl and want to use this formatter: |
| 767 |
|
| 768 |
$ perl -MTest2::Formatter::Test2 path/to/test.t |
| 769 |
|
| 770 |
You could also use the module directly in your test, but that is not |
| 771 |
recommended as your test would then be unable to be run via prove or other |
| 772 |
harnesses. |
| 773 |
|
| 774 |
=head1 SOURCE |
| 775 |
|
| 776 |
The source code repository for Test2-Harness can be found at |
| 777 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 778 |
|
| 779 |
=head1 MAINTAINERS |
| 780 |
|
| 781 |
=over 4 |
| 782 |
|
| 783 |
=item Chad Granum E<lt>[email protected]<gt> |
| 784 |
|
| 785 |
=back |
| 786 |
|
| 787 |
=head1 AUTHORS |
| 788 |
|
| 789 |
=over 4 |
| 790 |
|
| 791 |
=item Chad Granum E<lt>[email protected]<gt> |
| 792 |
|
| 793 |
=back |
| 794 |
|
| 795 |
=head1 COPYRIGHT |
| 796 |
|
| 797 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 798 |
|
| 799 |
This program is free software; you can redistribute it and/or |
| 800 |
modify it under the same terms as Perl itself. |
| 801 |
|
| 802 |
See F<http://dev.perl.org/licenses/> |
| 803 |
|
| 804 |
=cut |