| 1 |
package Test2::Harness::Collector::TapParser; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Importer 'Importer' => 'import'; |
| 8 |
|
| 9 |
our @EXPORT_OK = qw{ |
| 10 |
parse_stdout_tap |
| 11 |
parse_stderr_tap |
| 12 |
parse_tap_line |
| 13 |
}; |
| 14 |
|
| 15 |
sub parse_stdout_tap { |
| 16 |
my ($line) = @_; |
| 17 |
my $facet_data = __PACKAGE__->_parse_tap_line($line) or return undef; |
| 18 |
$facet_data->{from_tap} = { source => 'STDOUT', details => $line }; |
| 19 |
return $facet_data; |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
sub parse_stderr_tap { |
| 24 |
my ($line) = @_; |
| 25 |
|
| 26 |
# STDERR only has comments |
| 27 |
return unless $line =~ m/^\s*#/; |
| 28 |
|
| 29 |
my $facet_data = __PACKAGE__->_parse_tap_line($line) or return undef; |
| 30 |
$facet_data->{info}->[-1]->{tag} = 'DIAG'; |
| 31 |
$facet_data->{info}->[-1]->{debug} = 1; |
| 32 |
$facet_data->{from_tap} = { source => 'STDERR', details => $line }; |
| 33 |
|
| 34 |
return $facet_data; |
| 35 |
} |
| 36 |
|
| 37 |
sub parse_tap_line { |
| 38 |
my ($line) = @_; |
| 39 |
return __PACKAGE__->_parse_tap_line($line); |
| 40 |
} |
| 41 |
|
| 42 |
sub _parse_tap_line { |
| 43 |
my $class = shift; |
| 44 |
my ($line) = @_; |
| 45 |
chomp($line); |
| 46 |
|
| 47 |
my ($lead, $lead_len, $nest, $str) = ('', 0, 0, $line); |
| 48 |
if ($line =~ m/^(\s+)\S/) { |
| 49 |
$lead = $1; |
| 50 |
$str =~ s/^\Q$lead\E//mg; |
| 51 |
|
| 52 |
$lead =~ s/\t/ /g; |
| 53 |
$lead_len = length($lead); |
| 54 |
|
| 55 |
# indentation other than 0 or a multiple of 4 spaces... not an event |
| 56 |
return undef if $lead_len % 4; |
| 57 |
|
| 58 |
$nest = $lead_len / 4; |
| 59 |
} |
| 60 |
|
| 61 |
my @types = qw/buffered_subtest comment plan bail version/; |
| 62 |
for my $type (@types) { |
| 63 |
my $sub = "parse_tap_$type"; |
| 64 |
my $facet_data = $class->$sub($str) or next; |
| 65 |
$facet_data->{trace}->{nested} = $nest; |
| 66 |
$facet_data->{hubs}->[0]->{nested} = $nest; |
| 67 |
return $facet_data; |
| 68 |
} |
| 69 |
|
| 70 |
return undef; |
| 71 |
} |
| 72 |
|
| 73 |
sub parse_tap_buffered_subtest { |
| 74 |
my $class = shift; |
| 75 |
my ($line) = @_; |
| 76 |
|
| 77 |
# End of a buffered subtest. |
| 78 |
return {parent => {}, harness => {subtest_end => 1}} if $line =~ m/^\}\s*$/; |
| 79 |
|
| 80 |
my $facet_data = $class->parse_tap_ok($line) or return undef; |
| 81 |
return $facet_data unless $facet_data->{assert}->{details} =~ s/\s*\{\s*$//g; |
| 82 |
|
| 83 |
$facet_data->{parent} = { |
| 84 |
details => $facet_data->{assert}->{details}, |
| 85 |
}; |
| 86 |
$facet_data->{harness}->{subtest_start} = 1; |
| 87 |
|
| 88 |
return $facet_data; |
| 89 |
} |
| 90 |
|
| 91 |
sub parse_tap_ok { |
| 92 |
my $class = shift; |
| 93 |
my ($line) = @_; |
| 94 |
|
| 95 |
my ($pass, $todo, $skip, $num, @errors); |
| 96 |
|
| 97 |
return undef unless $line =~ s/^(not )?ok\b//; |
| 98 |
$pass = !$1; |
| 99 |
|
| 100 |
push @errors => "'ok' is not immediately followed by a space." |
| 101 |
if $line && !($line =~ m/^ /); |
| 102 |
|
| 103 |
if ($line =~ s/^(\s*)(\d+)\b//) { |
| 104 |
my $space = $1; |
| 105 |
$num = $2; |
| 106 |
|
| 107 |
push @errors => "Extra space after 'ok'" |
| 108 |
if length($space) > 1; |
| 109 |
} |
| 110 |
|
| 111 |
# Not strictly compliant, but compliant with what Test-Simple does... |
| 112 |
# Standard does not have a todo & skip. |
| 113 |
if ($line =~ s/#\s*(todo & skip|todo|skip)(.*)$//i) { |
| 114 |
my ($directive, $reason) = ($1, $2); |
| 115 |
|
| 116 |
push @errors => "No space before the '#' for the '$directive' directive." |
| 117 |
unless $line =~ s/\s+$//; |
| 118 |
|
| 119 |
push @errors => "No space between '$directive' directive and reason." |
| 120 |
if $reason && !($reason =~ s/^\s+//); |
| 121 |
|
| 122 |
$skip = $reason if $directive =~ m/skip/i; |
| 123 |
$todo = $reason if $directive =~ m/todo/i; |
| 124 |
} |
| 125 |
|
| 126 |
# Standard says that everything after the ok (except the number) is part of |
| 127 |
# the name. Most things add a dash between them, and I am deviating from |
| 128 |
# standards by stripping it and surrounding whitespace. |
| 129 |
$line =~ s/\s*-\s*//; |
| 130 |
|
| 131 |
$line =~ s/^\s+//; |
| 132 |
$line =~ s/\s+$//; |
| 133 |
|
| 134 |
my $is_subtest = ($line =~ m/^Subtest:\s*(.*)$/) ? ($1 or 1) : undef; |
| 135 |
|
| 136 |
my $facet_data = { |
| 137 |
assert => { |
| 138 |
pass => $pass, |
| 139 |
no_debug => 1, |
| 140 |
details => $line, |
| 141 |
defined $num ? (number => $num) : (), |
| 142 |
}, |
| 143 |
}; |
| 144 |
|
| 145 |
$facet_data->{parent} = { |
| 146 |
details => $is_subtest, |
| 147 |
} if defined $is_subtest; |
| 148 |
|
| 149 |
push @{$facet_data->{amnesty}} => { |
| 150 |
tag => 'SKIP', |
| 151 |
details => $skip, |
| 152 |
} if defined $skip; |
| 153 |
|
| 154 |
push @{$facet_data->{amnesty}} => { |
| 155 |
tag => 'TODO', |
| 156 |
details => $todo, |
| 157 |
} if defined $todo; |
| 158 |
|
| 159 |
push @{$facet_data->{info}} => { |
| 160 |
details => $_, |
| 161 |
debug => 1, |
| 162 |
tag => 'PARSER', |
| 163 |
} for @errors; |
| 164 |
|
| 165 |
return $facet_data; |
| 166 |
} |
| 167 |
|
| 168 |
sub parse_tap_version { |
| 169 |
my $class = shift; |
| 170 |
my ($line) = @_; |
| 171 |
|
| 172 |
return undef unless $line =~ m/^TAP version\s/; |
| 173 |
|
| 174 |
return { |
| 175 |
about => { |
| 176 |
details => $line, |
| 177 |
}, |
| 178 |
info => [ |
| 179 |
{ |
| 180 |
tag => 'INFO', |
| 181 |
debug => 0, |
| 182 |
details => $line, |
| 183 |
} |
| 184 |
], |
| 185 |
}; |
| 186 |
} |
| 187 |
|
| 188 |
sub parse_tap_plan { |
| 189 |
my $class = shift; |
| 190 |
my ($line) = @_; |
| 191 |
|
| 192 |
return undef unless $line =~ s/^1\.\.(\d+)//; |
| 193 |
my $max = $1; |
| 194 |
|
| 195 |
my ($directive, $reason) = ("", ""); |
| 196 |
|
| 197 |
if ($max == 0) { |
| 198 |
if ($line =~ s/^\s*#\s*//) { |
| 199 |
if ($line =~ s/^(skip)\S*\s*//i) { |
| 200 |
$directive = uc($1); |
| 201 |
$reason = $line; |
| 202 |
$line = ""; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
$directive ||= "SKIP"; |
| 207 |
$reason ||= "no reason given"; |
| 208 |
} |
| 209 |
|
| 210 |
my $facet_data = { |
| 211 |
plan => { |
| 212 |
count => $max, |
| 213 |
skip => ($directive eq 'SKIP') ? 1 : 0, |
| 214 |
details => $reason, |
| 215 |
} |
| 216 |
}; |
| 217 |
|
| 218 |
push @{$facet_data->{info}} => { |
| 219 |
details => 'Extra characters after plan.', |
| 220 |
debug => 1, |
| 221 |
tag => 'PARSER', |
| 222 |
} if $line =~ m/\S/; |
| 223 |
|
| 224 |
return $facet_data; |
| 225 |
} |
| 226 |
|
| 227 |
sub parse_tap_bail { |
| 228 |
my $class = shift; |
| 229 |
my ($line) = @_; |
| 230 |
|
| 231 |
return undef unless $line =~ m/^Bail out!\s*(.*)$/; |
| 232 |
|
| 233 |
return { |
| 234 |
control => { |
| 235 |
halt => 1, |
| 236 |
details => $1, |
| 237 |
} |
| 238 |
}; |
| 239 |
} |
| 240 |
|
| 241 |
sub parse_tap_comment { |
| 242 |
my $class = shift; |
| 243 |
my ($line) = @_; |
| 244 |
|
| 245 |
return undef unless $line =~ m/^\s*#/; |
| 246 |
|
| 247 |
$line =~ s/^\s*# ?//msg; |
| 248 |
|
| 249 |
return { |
| 250 |
info => [ |
| 251 |
{ |
| 252 |
details => $line, |
| 253 |
tag => 'NOTE', |
| 254 |
debug => 0, |
| 255 |
} |
| 256 |
] |
| 257 |
}; |
| 258 |
} |
| 259 |
|
| 260 |
1; |
| 261 |
|
| 262 |
__END__ |
| 263 |
|
| 264 |
=pod |
| 265 |
|
| 266 |
=encoding UTF-8 |
| 267 |
|
| 268 |
=head1 NAME |
| 269 |
|
| 270 |
Test2::Harness::Collector::TapParser - Produce EventFacets from a line of TAP. |
| 271 |
|
| 272 |
=head1 DESCRIPTION |
| 273 |
|
| 274 |
This module is responsible for reading and processing any TAP output from |
| 275 |
tests. Lines of TAP output are processed into L<Test2::Event> facet data. Note |
| 276 |
that C<< Test2 -> TAP -> Test2 >> is lossy at the C<< Test2 -> TAP >> step. |
| 277 |
|
| 278 |
=head1 SYNOPSIS |
| 279 |
|
| 280 |
use Test2::Harness::Collector::TapParser qw/parse_tap_line/; |
| 281 |
|
| 282 |
my $facet_data = parse_tap_line("1..1"); |
| 283 |
is( |
| 284 |
$facet_data, |
| 285 |
{ |
| 286 |
trace => {nested => 0}, |
| 287 |
hubs => [{nested => 0}], |
| 288 |
plan => { |
| 289 |
details => '', |
| 290 |
count => 1, |
| 291 |
skip => 0, |
| 292 |
}, |
| 293 |
}, |
| 294 |
"Parsed the plan" |
| 295 |
); |
| 296 |
|
| 297 |
$facet_data = parse_tap_line("# foo"); |
| 298 |
is( |
| 299 |
$facet_data, |
| 300 |
{ |
| 301 |
trace => { nested => 0 }, |
| 302 |
hubs => [ { nested => 0 } ], |
| 303 |
info => [ |
| 304 |
{ |
| 305 |
tag => 'NOTE', |
| 306 |
details => 'foo', |
| 307 |
debug => 0, |
| 308 |
}, |
| 309 |
], |
| 310 |
}, |
| 311 |
|
| 312 |
"Parsed the note" |
| 313 |
); |
| 314 |
|
| 315 |
$facet_data = parse_tap_line("ok 1"); |
| 316 |
is( |
| 317 |
$facet_data, |
| 318 |
{ |
| 319 |
trace => {nested => 0}, |
| 320 |
hubs => [{nested => 0}], |
| 321 |
assert => { |
| 322 |
no_debug => 1, |
| 323 |
pass => 1, |
| 324 |
number => '1', |
| 325 |
details => '', |
| 326 |
}, |
| 327 |
}, |
| 328 |
"Parsed the assertion" |
| 329 |
); |
| 330 |
|
| 331 |
=head1 EXPORTS |
| 332 |
|
| 333 |
=over 4 |
| 334 |
|
| 335 |
=item $facet_data = parse_tap_line($line) |
| 336 |
|
| 337 |
Parse a line of TAP. It is assumed to be STDOUT thus all comments are turned |
| 338 |
into notes. Using this export will B<NOT> add the usual C<from_tap> facet. It |
| 339 |
is better to use one of the other 2 exports. |
| 340 |
|
| 341 |
=item $facet_data = parse_stdout_tap($line) |
| 342 |
|
| 343 |
Parse a line of TAP from stdout. |
| 344 |
|
| 345 |
=item $facet_data = parse_stderr_tap($line) |
| 346 |
|
| 347 |
Parse a line of TAP from stderr. This will B<ONLY> parse comment lines (ones |
| 348 |
that start with a C<#>, which may be indented). All comments will be treated as |
| 349 |
diag's, all other lines will be ignored. |
| 350 |
|
| 351 |
=back |
| 352 |
|
| 353 |
=head1 SOURCE |
| 354 |
|
| 355 |
The source code repository for Test2-Harness can be found at |
| 356 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 357 |
|
| 358 |
=head1 MAINTAINERS |
| 359 |
|
| 360 |
=over 4 |
| 361 |
|
| 362 |
=item Chad Granum E<lt>[email protected]<gt> |
| 363 |
|
| 364 |
=back |
| 365 |
|
| 366 |
=head1 AUTHORS |
| 367 |
|
| 368 |
=over 4 |
| 369 |
|
| 370 |
=item Chad Granum E<lt>[email protected]<gt> |
| 371 |
|
| 372 |
=back |
| 373 |
|
| 374 |
=head1 COPYRIGHT |
| 375 |
|
| 376 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 377 |
|
| 378 |
This program is free software; you can redistribute it and/or |
| 379 |
modify it under the same terms as Perl itself. |
| 380 |
|
| 381 |
See F<http://dev.perl.org/licenses/> |
| 382 |
|
| 383 |
=cut |