| 1 | package App::ForkProve::SourceHandler; | ||
| 2 | use strict; | ||
| 3 | use parent qw(TAP::Parser::SourceHandler); | ||
| 4 | |||
| 5 | use App::ForkProve::PipeIterator; | ||
| 6 | use Scalar::Util 'openhandle'; | ||
| 7 | use TAP::Parser::IteratorFactory; | ||
| 8 | TAP::Parser::IteratorFactory->register_handler(__PACKAGE__); | ||
| 9 | |||
| 10 | use TAP::Parser::SourceHandler::Perl; | ||
| 11 | |||
| 12 | sub can_handle { | ||
| 13 | my($class, $src) = @_; | ||
| 14 | |||
| 15 | # I think there's a TAP::Parser bug thinking the first line of .t | ||
| 16 | # file as a shebang even if it doesn't begin with '#!' | ||
| 17 | local $src->meta->{file}{shebang} = undef | ||
| 18 | if $src->meta->{file}{shebang} !~ /^#!/; | ||
| 19 | |||
| 20 | my $is_perl = TAP::Parser::SourceHandler::Perl->can_handle($src); | ||
| 21 | return 1 if $is_perl > 0.5 && !$class->ignore($src->meta->{file}); | ||
| 22 | return 0; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub ignore { | ||
| 26 | my($class, $file) = @_; | ||
| 27 | $ENV{PERL_FORKPROVE_IGNORE} && ($file->{dir} . $file->{basename}) =~ /$ENV{PERL_FORKPROVE_IGNORE}/; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub make_iterator { | ||
| 31 | my($class, $src) = @_; | ||
| 32 | |||
| 33 | my $path = $src->meta->{file}{dir} . $src->meta->{file}{basename}; | ||
| 34 | my @inc = map { s/^-I//; $_ } grep { /^-I/ } @{ $src->switches }; | ||
| 35 | |||
| 36 | $class->_autoflush(\*STDOUT); | ||
| 37 | $class->_autoflush(\*STDERR); | ||
| 38 | |||
| 39 | pipe my $reader, my $writer; | ||
| 40 | my $pid = fork; | ||
| 41 | if ($pid) { | ||
| 42 | close $writer; | ||
| 43 | return App::ForkProve::PipeIterator->new($reader, $pid); | ||
| 44 | } else { | ||
| 45 | close $reader; | ||
| 46 | open STDOUT, ">&", $writer; | ||
| 47 | open STDERR, ">&", $writer if $src->merge; | ||
| 48 | _run($path, \@inc); | ||
| 49 | exit; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | sub _run { | ||
| 54 | my ($t, $inc) = @_; | ||
| 55 | |||
| 56 | # Many tests especially Exception tests rely on test file name being | ||
| 57 | # passed as t/foo.t without a leading path | ||
| 58 | local $0 = $t; | ||
| 59 | local @INC = (@$inc, @INC); | ||
| 60 | |||
| 61 | # if FindBin is preloaded, reset it with the new $0 | ||
| 62 | if (defined &FindBin::init) { | ||
| 63 | FindBin::init() | ||
| 64 | } | ||
| 65 | |||
| 66 | # open DATA from test script | ||
| 67 | { | ||
| 68 | close ::DATA; | ||
| 69 | if (open my $fh, $t) { | ||
| 70 | my $code = do { local $/; <$fh> }; | ||
| 71 | if(my($data) = $code =~ /^__(?:END|DATA)__$(.*)/ms){ | ||
| 72 | open ::DATA, '<', \$data | ||
| 73 | or die "Can't open string as DATA. $!"; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | # restore DATA for all preloaded modules | ||
| 79 | my $data = \%App::ForkProve::Data; | ||
| 80 | for my $mod (keys %$data) { | ||
| 81 | my ($was_open, $prev_file, $prev_pos) = @{ $data->{$mod} }; | ||
| 82 | |||
| 83 | my $fh = do { | ||
| 84 | no strict 'refs'; | ||
| 85 | *{ $mod . '::DATA' } | ||
| 86 | }; | ||
| 87 | |||
| 88 | # note that we need to ensure that each forked copy is using a | ||
| 89 | # different file handle, or else concurrent processes will interfere | ||
| 90 | # with each other | ||
| 91 | |||
| 92 | close $fh if openhandle($fh); | ||
| 93 | |||
| 94 | if ($was_open) { | ||
| 95 | if (open $fh, '<', $prev_file) { | ||
| 96 | seek($fh, $prev_pos, 0); | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | warn "Couldn't reopen DATA for $mod: $!"; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | # restore defaults | ||
| 105 | Getopt::Long::ConfigDefaults(); | ||
| 106 | |||
| 107 | # reset the state of empty pattern matches, so that they have the same | ||
| 108 | # behavior as running in a clean process. | ||
| 109 | # see "The empty pattern //" in perlop. | ||
| 110 | # note that this has to be dynamically scoped and can't go to other subs | ||
| 111 | "" =~ /^/; | ||
| 112 | |||
| 113 | # Test::Builder is loaded? Reset the $Test object to make it unaware | ||
| 114 | # that it's a forked off proecess so that subtests won't run | ||
| 115 | if (defined $Test::Builder::Test) { | ||
| 116 | $Test::Builder::Test->reset; | ||
| 117 | } | ||
| 118 | |||
| 119 | # avoid child processes sharing the same seed value as the parent | ||
| 120 | srand(); | ||
| 121 | |||
| 122 | # clear @ARGV it's localized in App::ForkProve | ||
| 123 | local @ARGV = (); | ||
| 124 | |||
| 125 | # do() can't tell if a test can't be read or a .t's last statement | ||
| 126 | # returned undef with $! set somewhere. Fortunately in case of | ||
| 127 | # prove, non-readable .t will fail earlier in prove itself. | ||
| 128 | eval q{ package main; do $t; die $@ if $@; 1 } or die $@; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _autoflush { | ||
| 132 | my ( $class, $flushed ) = @_; | ||
| 133 | my $old_fh = select $flushed; | ||
| 134 | $| = 1; | ||
| 135 | select $old_fh; | ||
| 136 | } | ||
| 137 | |||
| 138 | 1; |