| 1 | package TAP::Formatter::Jenkins::Session; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use MooseX::NonMoose; | ||
| 5 | extends qw( | ||
| 6 | TAP::Formatter::Console::Session | ||
| 7 | ); | ||
| 8 | |||
| 9 | use TAP::Formatter::Jenkins::MyParser; | ||
| 10 | |||
| 11 | use TAP::Parser::YAMLish::Writer; | ||
| 12 | use File::Path qw(mkpath); | ||
| 13 | use IO::File; | ||
| 14 | |||
| 15 | has 'test_cases' => ( | ||
| 16 | is => 'rw', | ||
| 17 | isa => 'ArrayRef', | ||
| 18 | default => sub { [] }, | ||
| 19 | traits => [qw( Array )], | ||
| 20 | handles => { | ||
| 21 | add_test_case => 'push', | ||
| 22 | }, | ||
| 23 | ); | ||
| 24 | |||
| 25 | has 'passing_todo_ok' => ( | ||
| 26 | is => 'rw', | ||
| 27 | isa => 'Bool', | ||
| 28 | default => 0, | ||
| 29 | ); | ||
| 30 | |||
| 31 | has '_queue' => ( | ||
| 32 | is => 'rw', | ||
| 33 | isa => 'ArrayRef', | ||
| 34 | default => sub { [] }, | ||
| 35 | traits => [qw( Array )], | ||
| 36 | handles => { | ||
| 37 | _queue_add => 'push', | ||
| 38 | }, | ||
| 39 | ); | ||
| 40 | |||
| 41 | ############################################################################### | ||
| 42 | # Subroutine: _initialize($arg_for) | ||
| 43 | ############################################################################### | ||
| 44 | # Custom initializer, so we can accept a new "passing_todo_ok" argument at | ||
| 45 | # instantiation time. | ||
| 46 | sub _initialize { | ||
| 47 | my ($self, $arg_for) = @_; | ||
| 48 | $arg_for ||= {}; | ||
| 49 | |||
| 50 | my $passing_todo_ok = delete $arg_for->{passing_todo_ok}; | ||
| 51 | $self->passing_todo_ok($passing_todo_ok); | ||
| 52 | |||
| 53 | return $self->SUPER::_initialize($arg_for); | ||
| 54 | } | ||
| 55 | |||
| 56 | ############################################################################### | ||
| 57 | # Subroutine: result($result) | ||
| 58 | ############################################################################### | ||
| 59 | # Called by the harness for each line of TAP it receives. | ||
| 60 | # | ||
| 61 | # Queues up all of the TAP output for later conversion to JUnit. | ||
| 62 | sub result { | ||
| 63 | my ( $self, $result ) = @_; | ||
| 64 | |||
| 65 | # add the test result to the queue. | ||
| 66 | $self->_queue_add($result); | ||
| 67 | } | ||
| 68 | |||
| 69 | ############################################################################### | ||
| 70 | # Subroutine: close_test() | ||
| 71 | ############################################################################### | ||
| 72 | # Called to close the test session. | ||
| 73 | # | ||
| 74 | # Flushes the queue if we've got anything left in it, dumps the JUnit to disk | ||
| 75 | # (if necessary), and adds the XML for this test suite to our formatter. | ||
| 76 | sub close_test { | ||
| 77 | my $self = shift; | ||
| 78 | my $parser = $self->parser; | ||
| 79 | |||
| 80 | # Process the queued up TAP stream | ||
| 81 | my $test_name = $self->name; | ||
| 82 | my $queue = $self->_queue; | ||
| 83 | my $test_suite; | ||
| 84 | my $has_ended; | ||
| 85 | |||
| 86 | # Collect up all of the captured test output | ||
| 87 | my $captured = join '', map { $_->raw . "\n" } @$queue; | ||
| 88 | my $result = TAP::Formatter::Jenkins::MyParser->new($captured); | ||
| 89 | while ( ! $has_ended ) { | ||
| 90 | # Failed test | ||
| 91 | if ( $result->fail ) { | ||
| 92 | $self->add_test_case( $self->_parse_failed_test_case($result) ); | ||
| 93 | } | ||
| 94 | # End of tap | ||
| 95 | elsif ( $result->has_ended ) { | ||
| 96 | $test_suite = join '', @{ $self->test_cases }; | ||
| 97 | |||
| 98 | $has_ended = 1; | ||
| 99 | } | ||
| 100 | # Plan error | ||
| 101 | elsif ( $result->is_plan_report ) { | ||
| 102 | $self->add_test_case( $self->_test_more_comment_to_yamlish($result) ); | ||
| 103 | |||
| 104 | $result->next; | ||
| 105 | } | ||
| 106 | # Yamlish dump | ||
| 107 | elsif ( $result->is_yamlish_start ) { | ||
| 108 | $self->add_test_case( $self->_parse_yamlish_dump_from_tap($result) ); | ||
| 109 | } | ||
| 110 | # Subtest plan | ||
| 111 | elsif ( $result->is_subtest_plan ) { | ||
| 112 | $self->add_test_case( $result->as_string. "\n" ); | ||
| 113 | |||
| 114 | $result->next; | ||
| 115 | } | ||
| 116 | # Other useless | ||
| 117 | elsif ( $result->is_time_test ) { | ||
| 118 | $result->next; | ||
| 119 | } | ||
| 120 | # die | ||
| 121 | elsif ( $result->like_die ) { | ||
| 122 | $self->add_test_case( $self->_parse_unknown_str($result) ); | ||
| 123 | } | ||
| 124 | else { | ||
| 125 | $self->add_test_case( $result->as_string. "\n" ); | ||
| 126 | |||
| 127 | $result->next; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | $self->formatter->test_suites->{ $test_name } = $test_suite; | ||
| 132 | $self->_dump_tap_to_file($test_suite); | ||
| 133 | } | ||
| 134 | |||
| 135 | ############################################################################### | ||
| 136 | # Subroutine: _dump_tap_to_file($test_suite) | ||
| 137 | ############################################################################### | ||
| 138 | # Dumps the TAP, to the directory specified by 'PERL_TEST_HARNESS_DUMP_TAP'. | ||
| 139 | sub _dump_tap_to_file { | ||
| 140 | my ( $self, $test_suite ) = @_; | ||
| 141 | |||
| 142 | if ( my $spool_dir = $ENV{PERL_TEST_HARNESS_DUMP_TAP} ) { | ||
| 143 | my $tap_name = $self->name; | ||
| 144 | $tap_name =~ s/^\///; | ||
| 145 | $tap_name =~ s/\//-/g; | ||
| 146 | $tap_name =~ s/\.t/.tap/; | ||
| 147 | |||
| 148 | my $spool = File::Spec->catfile( $spool_dir, $tap_name ); | ||
| 149 | |||
| 150 | # Create target dir | ||
| 151 | my ($vol, $dir, undef) = File::Spec->splitpath($spool); | ||
| 152 | my $path = File::Spec->catpath( $vol, $dir, '' ); | ||
| 153 | mkpath($path); | ||
| 154 | |||
| 155 | # Dump to disk | ||
| 156 | my $fout = IO::File->new( $spool, '>' ) | ||
| 157 | || die "Can't write $spool ( $! )\n"; | ||
| 158 | $fout->print($test_suite); | ||
| 159 | $fout->close(); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | ############################################################################### | ||
| 164 | # Parses comments after failed test case | ||
| 165 | sub _parse_failed_test_case { | ||
| 166 | my ( $self, $result ) = @_; | ||
| 167 | |||
| 168 | my $yamlish_diag; | ||
| 169 | my $comment = ''; | ||
| 170 | |||
| 171 | my $test_case = $result->as_string. "\n"; | ||
| 172 | $result->next; | ||
| 173 | |||
| 174 | if ( $self->passing_todo_ok && $test_case =~ /#\sTODO/ ) { | ||
| 175 | while ( $result->as_string !~ /^(not\s)?ok\s/ ) { | ||
| 176 | $result->next; | ||
| 177 | } | ||
| 178 | |||
| 179 | $test_case =~ s/^not\s//; | ||
| 180 | return $test_case; | ||
| 181 | } | ||
| 182 | |||
| 183 | # Empty spaces | ||
| 184 | while ( ! $result->as_string ) { | ||
| 185 | $result->next; | ||
| 186 | } | ||
| 187 | |||
| 188 | # Get comments | ||
| 189 | while ( | ||
| 190 | $result->is_comment | ||
| 191 | && ! $result->is_yamlish_start | ||
| 192 | || ! $result->as_string | ||
| 193 | ) { | ||
| 194 | $comment .= $result->as_string. "\n"; | ||
| 195 | $result->next; | ||
| 196 | } | ||
| 197 | |||
| 198 | # Get yamlish | ||
| 199 | if ( $result->is_yamlish_start ) { | ||
| 200 | $yamlish_diag = $self->_parse_yamlish_dump_from_tap($result); | ||
| 201 | } | ||
| 202 | |||
| 203 | # Merge coment, yamlish | ||
| 204 | $test_case .= $yamlish_diag | ||
| 205 | ? $comment. $yamlish_diag | ||
| 206 | : $comment && $comment =~ /failed.+test/i | ||
| 207 | ? $self->_test_more_comment_to_yamlish( $result, $comment ) | ||
| 208 | : $comment; | ||
| 209 | |||
| 210 | return $test_case; | ||
| 211 | } | ||
| 212 | |||
| 213 | ############################################################################### | ||
| 214 | # Get Yamlish from Test::More output | ||
| 215 | sub _parse_yamlish_dump_from_tap { | ||
| 216 | my ( $self, $result ) = @_; | ||
| 217 | |||
| 218 | my $yamlish_diag; | ||
| 219 | |||
| 220 | while ( | ||
| 221 | $result | ||
| 222 | && | ||
| 223 | ! $result->is_yamlish_end | ||
| 224 | ) { | ||
| 225 | next unless $result->as_string; | ||
| 226 | |||
| 227 | $yamlish_diag .= $result->as_string. "\n"; | ||
| 228 | $result->next; | ||
| 229 | } | ||
| 230 | |||
| 231 | $yamlish_diag .= $result->as_string. "\n"; | ||
| 232 | $result->next; | ||
| 233 | |||
| 234 | $yamlish_diag =~ s/^#\s//gm; | ||
| 235 | |||
| 236 | return $yamlish_diag; | ||
| 237 | } | ||
| 238 | |||
| 239 | ############################################################################### | ||
| 240 | # Converts Test::More comment to Yamlish | ||
| 241 | sub _test_more_comment_to_yamlish { | ||
| 242 | my ( $self, $result, $comment ) = @_; | ||
| 243 | |||
| 244 | $comment //= $result->as_string; | ||
| 245 | |||
| 246 | return $comment if $comment =~ /^\s+---/m; | ||
| 247 | |||
| 248 | my @strs = split /\n/, $comment; | ||
| 249 | |||
| 250 | my ( $got, $expected, $msg ) = ( '', '', '' ); | ||
| 251 | foreach ( @strs ) { | ||
| 252 | s/\s*structures\sbegin\sdiffering\sat:\s*//i; | ||
| 253 | |||
| 254 | if ( /got(?::\s(.*)|(->[\[{].+[\]}])\s=\s(.*))/ ) { | ||
| 255 | $got = $1 || { $2 => $3 }; | ||
| 256 | } | ||
| 257 | elsif ( /expected(?::\s(.*)|(->[\[{].+[\]}])\s=\s(.*))/ ) { | ||
| 258 | $expected = $1 || { $2 => $3 }; | ||
| 259 | } | ||
| 260 | elsif ( $_ ) { | ||
| 261 | $msg .= $_. ' '; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | $msg =~ s/#\s+//g; | ||
| 266 | |||
| 267 | return $self->_yamlish_diag( $got, $expected, $msg ); | ||
| 268 | } | ||
| 269 | |||
| 270 | ############################################################################### | ||
| 271 | # Dumps Yamlish | ||
| 272 | sub _yamlish_diag { | ||
| 273 | my ( $self, $got, $expected, $msg ) = @_; | ||
| 274 | |||
| 275 | my %diagnostic = ( 'message' => $msg ); | ||
| 276 | |||
| 277 | if ( $got && $expected ) { | ||
| 278 | $diagnostic{'data'} = { | ||
| 279 | 'got' => $got, | ||
| 280 | 'expected' => $expected, | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | my $res; | ||
| 285 | my $yw = TAP::Parser::YAMLish::Writer->new; | ||
| 286 | $yw->write( \%diagnostic, \$res ); | ||
| 287 | |||
| 288 | $res =~ s/^/ /gm; | ||
| 289 | |||
| 290 | return $res; | ||
| 291 | } | ||
| 292 | |||
| 293 | ############################################################################### | ||
| 294 | # Gets unlnown strings, and logs | ||
| 295 | sub _parse_unknown_str { | ||
| 296 | my ( $self, $result ) = @_; | ||
| 297 | |||
| 298 | my $msg; | ||
| 299 | do { | ||
| 300 | $msg .= $result->as_string. "\n"; | ||
| 301 | |||
| 302 | $result->next; | ||
| 303 | } while ( $result->like_die ); | ||
| 304 | |||
| 305 | if ( $result->is_plan_report || $result->is_return_code ) { | ||
| 306 | $msg = $self->_yamlish_diag( undef, undef, $msg ); | ||
| 307 | } | ||
| 308 | else { | ||
| 309 | $msg =~ s/^([^#])/\# $1/gm; | ||
| 310 | |||
| 311 | my $unknown_strs = "unknown_strs.log"; | ||
| 312 | |||
| 313 | open my $unknown_strs_fh, ">>", $unknown_strs or die "Can not open file $_"; | ||
| 314 | print $unknown_strs_fh "$msg\n"; | ||
| 315 | close $unknown_strs_fh; | ||
| 316 | } | ||
| 317 | |||
| 318 | return $msg; | ||
| 319 | } | ||
| 320 | |||
| 321 | 1; | ||
| 322 | |||
| 323 | =head1 NAME | ||
| 324 | |||
| 325 | TAP::Formatter::Jenkins::Session - Harness output delegate for Jenkins TAP Plugin | ||
| 326 | output | ||
| 327 | |||
| 328 | =head1 DESCRIPTION | ||
| 329 | |||
| 330 | C<TAP::Formatter::Jenkins::Session> provides Jenkins TAP Plugin output formatting | ||
| 331 | for C<TAP::Harness>. | ||
| 332 | |||
| 333 | =head1 METHODS | ||
| 334 | |||
| 335 | =over | ||
| 336 | |||
| 337 | =item B<_initialize($arg_for)> | ||
| 338 | |||
| 339 | Over-ridden private initializer, so we can accept a new "passing_todo_ok" | ||
| 340 | argument at instantiation time. | ||
| 341 | |||
| 342 | =item B<result($result)> | ||
| 343 | |||
| 344 | Called by the harness for each line of TAP it receives. | ||
| 345 | |||
| 346 | Internally, all of the TAP is added to a queue until we hit the start of | ||
| 347 | the "next" test (at which point we flush the queue. This allows us to | ||
| 348 | capture any error output or diagnostic info that comes after a test | ||
| 349 | failure. | ||
| 350 | |||
| 351 | =item B<close_test()> | ||
| 352 | |||
| 353 | Called to close the test session. | ||
| 354 | |||
| 355 | Flushes the queue if we've got anything left in it, dumps the Jenkins TAP Plugin | ||
| 356 | formatting output to disk. | ||
| 357 | |||
| 358 | =item B<add_test_case($case)> | ||
| 359 | |||
| 360 | Adds an test C<$case> to the list of test_cases we've run in this session. | ||
| 361 | |||
| 362 | =back | ||
| 363 | |||
| 364 | =head1 AUTHOR | ||
| 365 | |||
| 366 | Evgeniy Vostrov <vostrov.e@gmail.com> | ||
| 367 | |||
| 368 | =head1 COPYRIGHT | ||
| 369 | |||
| 370 | This is free software; you can redistribute it and/or modify it under the same | ||
| 371 | terms as Perl itself. | ||
| 372 | |||
| 373 | =head1 SEE ALSO | ||
| 374 | |||
| 375 | L<TAP::Formatter::Jenkins>. | ||
| 376 | L<TAP::Formatter::Jenkins::MyParser>. | ||
| 377 | |||
| 378 | =cut |