- # Test::Harness parses $ENV{HARNESS_OPTIONS}, TAP::Harness does not
- local $ENV{HARNESS_OPTIONS};
- my $h = TAP::Harness->new({ jobs => $jobs, rules => $rules, ($fork ? (fork => $fork) : ())});
- if ($state) {
- $h->callback(
- after_test => sub {
- $state->observe_test(@_);
- }
- );
- $h->callback(
- after_runtests => sub {
- $state->commit(@_);
+my %options;
+
+my $type = 'perl';
+
+# Load TAP::Parser now as otherwise it could be required in the short time span
+# in which the harness process chdirs into ext/Dist
+require TAP::Parser;
+
+my $h = TAP::Harness->new({
+ rules => $rules,
+ color => $color,
+ jobs => $jobs,
+ verbosity => $Verbose,
+ timer => $ENV{HARNESS_TIMER},
+ exec => sub {
+ my ($harness, $test) = @_;
+
+ my $options = $options{$test};
+ if (!defined $options) {
+ $options = $options{$test} = _scan_test($test, $type);
+ }
+
+ (local $Valgrind_Log = "$test.valgrind-current") =~ s/^.*\///;
+
+ return [ split ' ', _cmd($options, $type) ];
+ },
+});
+
+# Print valgrind output after test completes
+if ($ENV{PERL_VALGRIND}) {
+ $h->callback(
+ after_test => sub {
+ my ($job) = @_;
+ my $test = $job->[0];
+ my $vfile = "$test.valgrind-current";
+ $vfile =~ s/^.*\///;
+
+ if ( (! -z $vfile) && open(my $voutput, '<', $vfile)) {
+ print "$test: Valgrind output:\n";
+ print "$test: $_" for <$voutput>;
+ close($voutput);