| 1 | use v5.14; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package TAP::Parser::SourceHandler::Feature 0.87; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | TAP::Parser::SourceHandler::Feature - Test::BDD::Cucumber's prove integration | ||
| 9 | |||
| 10 | =head1 VERSION | ||
| 11 | |||
| 12 | version 0.87 | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | |||
| 17 | use Path::Class qw/file/; | ||
| 18 | use Test2::API qw/context/; | ||
| 19 | |||
| 20 | use base 'TAP::Parser::SourceHandler'; | ||
| 21 | |||
| 22 | use TAP::Parser::Iterator::PherkinStream; | ||
| 23 | |||
| 24 | use App::pherkin; | ||
| 25 | |||
| 26 | use Test::BDD::Cucumber::Loader; | ||
| 27 | use Test::BDD::Cucumber::Harness::TAP; | ||
| 28 | |||
| 29 | use Path::Class qw/file/; | ||
| 30 | |||
| 31 | TAP::Parser::IteratorFactory->register_handler(__PACKAGE__); | ||
| 32 | |||
| 33 | sub can_handle { | ||
| 34 | my ( $class, $source ) = @_; | ||
| 35 | |||
| 36 | if ( $source->meta->{'is_file'} | ||
| 37 | && $source->meta->{'file'}->{'basename'} =~ m/\.feature$/ ) | ||
| 38 | { | ||
| 39 | |||
| 40 | my $dir = $source->meta->{'file'}->{'dir'}; | ||
| 41 | unless ( $source->{'pherkins'}->{$dir} ) { | ||
| 42 | |||
| 43 | my $pherkin = App::pherkin->new(); | ||
| 44 | |||
| 45 | # Reformulate before passing to the cmd line parser | ||
| 46 | my @cmd_line; | ||
| 47 | my %options = %{ $source->config_for($class) }; | ||
| 48 | while ( my ( $key, $value ) = each %options ) { | ||
| 49 | |||
| 50 | # --feature-option 'tags=@something' | ||
| 51 | # --feature-option 'tags=@somethingelse' | ||
| 52 | # | ||
| 53 | # is passed in %options as: | ||
| 54 | # { ..., tags => [ '@something', '@somethingelse' ], ... } | ||
| 55 | # | ||
| 56 | # Now unfold that back into an argument array as | ||
| 57 | # --tags @something --tags @somethingelse | ||
| 58 | $value = [ $value ] unless ref $value; | ||
| 59 | for my $v (@$value) { | ||
| 60 | # Nasty hack | ||
| 61 | if ( length $key > 1 ) { | ||
| 62 | push( @cmd_line, "--$key", $v ); | ||
| 63 | } else { | ||
| 64 | push( @cmd_line, "-$key", $v ); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | my ( $executor, @features ) | ||
| 70 | = $pherkin->_pre_run( @cmd_line, $dir ); | ||
| 71 | |||
| 72 | $source->{'pherkins'}->{$dir} = { | ||
| 73 | pherkin => $pherkin, | ||
| 74 | executor => $executor, | ||
| 75 | features => { | ||
| 76 | map { ( file( $_->document->filename ) . '' ) => $_ } | ||
| 77 | @features | ||
| 78 | } | ||
| 79 | }; | ||
| 80 | } | ||
| 81 | return 1; | ||
| 82 | } | ||
| 83 | |||
| 84 | return 0; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub make_iterator { | ||
| 88 | my ( $class, $source ) = @_; | ||
| 89 | |||
| 90 | my ( $input_out_fh, $output_out_fh ); | ||
| 91 | pipe $input_out_fh, $output_out_fh; | ||
| 92 | binmode $output_out_fh, ':utf8'; | ||
| 93 | |||
| 94 | my ( $input_err_fh, $output_err_fh ); | ||
| 95 | pipe $input_err_fh, $output_err_fh; | ||
| 96 | binmode $output_err_fh, ':utf8'; | ||
| 97 | |||
| 98 | # Don't cache the output so prove sees it immediately | ||
| 99 | # (pipes are stdio buffered by default) | ||
| 100 | $output_out_fh->autoflush(1); | ||
| 101 | $output_err_fh->autoflush(1); | ||
| 102 | |||
| 103 | my $dir = $source->meta->{'file'}->{'dir'}; | ||
| 104 | my $runtime = $source->{'pherkins'}->{$dir} | ||
| 105 | || die "No pherkin instantiation for [$dir]"; | ||
| 106 | |||
| 107 | my $executor = $runtime->{'executor'}; | ||
| 108 | my $pherkin = $runtime->{'pherkin'}; | ||
| 109 | |||
| 110 | my $pid = fork; | ||
| 111 | if ($pid) { | ||
| 112 | close $output_out_fh; | ||
| 113 | close $output_err_fh; | ||
| 114 | return TAP::Parser::Iterator::PherkinStream | ||
| 115 | ->new($input_out_fh, $input_err_fh, $pherkin, $pid); | ||
| 116 | } | ||
| 117 | |||
| 118 | # prevent uncaught exceptions to return up the call stack | ||
| 119 | # causing essentially two prove instances to run. | ||
| 120 | eval { | ||
| 121 | close $input_out_fh; | ||
| 122 | close $input_err_fh; | ||
| 123 | my $harness = | ||
| 124 | Test::BDD::Cucumber::Harness::TAP->new({ fail_skip => 1 }); | ||
| 125 | |||
| 126 | my $context = context(); | ||
| 127 | # Without the step to set the handles TAP will end up on STDOUT/STDERR | ||
| 128 | $context->hub->format->set_handles([$output_out_fh, $output_err_fh]); | ||
| 129 | $context->release; | ||
| 130 | $pherkin->harness($harness); | ||
| 131 | my $filename = | ||
| 132 | file( $dir . $source->meta->{'file'}->{'basename'} ) . ''; | ||
| 133 | |||
| 134 | my $feature = $runtime->{'features'}->{$filename} | ||
| 135 | || die "Feature not pre-loaded: [$filename]; have: " | ||
| 136 | . ( join '; ', keys %{ $runtime->{'features'} } ); | ||
| 137 | |||
| 138 | my $exit_code = $pherkin->_run_tests( $executor, $feature ); | ||
| 139 | |||
| 140 | close $output_out_fh; | ||
| 141 | close $output_err_fh; | ||
| 142 | $pherkin->_post_run; | ||
| 143 | |||
| 144 | exit $exit_code; | ||
| 145 | }; | ||
| 146 | exit 255; | ||
| 147 | } | ||
| 148 | |||
| 149 | 1; |