From: Vincent Pit Date: Tue, 14 Apr 2009 16:22:07 +0000 (+0200) Subject: Allow using Test::Valgrind on code given by the -e command-line flag X-Git-Tag: v1.01~3 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FTest-Valgrind.git;a=commitdiff_plain;h=de578745159a2190a170c5da40dc4950220b087c Allow using Test::Valgrind on code given by the -e command-line flag --- diff --git a/Makefile.PL b/Makefile.PL index 5397afa..badd52a 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -64,7 +64,8 @@ my %PREREQ_PM = ( 'File::HomeDir' => '0.86', 'File::Path' => 0, 'File::Spec' => 0, - 'File::Temp' => 0, + 'File::Temp' => '0.14', # OO interface + 'Filter::Util::Call' => 0, 'Fcntl' => 0, 'POSIX' => 0, 'Perl::Destruct::Level' => 0, diff --git a/lib/Test/Valgrind.pm b/lib/Test/Valgrind.pm index 3c52e85..5f7414f 100644 --- a/lib/Test/Valgrind.pm +++ b/lib/Test/Valgrind.pm @@ -20,11 +20,14 @@ our $VERSION = '1.00'; # From the command-line perl -MTest::Valgrind leaky.pl + # From the command-line, snippet style + perl -MTest::Valgrind -e 'leaky()' + # In a test file use Test::More; eval 'use Test::Valgrind'; plan skip_all => 'Test::Valgrind is required to test your distribution with valgrind' if $@; - ... + leaky(); # In all the test files of a directory prove --exec 'perl -Iblib/lib -Iblib/arch -MTest::Valgrind' t/*.t @@ -222,31 +225,52 @@ sub import { my $file = delete $args{file}; unless (defined $file) { - my ($pm, $next); - my $l = 0; - while ($l < 1000) { - $next = (caller $l++)[1]; + my ($next, $last_pm); + for (my $l = 0; 1; ++$l) { + $next = (caller $l)[1]; last unless defined $next; - next if $next eq '-e' - or $next =~ /^\s*\(\s*eval\s*\d*\s*\)\s*$/ - or !-f $next; - if ($next =~ /\.pm$/) { - $pm = $next; + next if $next =~ /^\s*\(\s*eval\s*\d*\s*\)\s*$/; + if ($next =~ /\.pmc?$/) { + $last_pm = $next; } else { $file = $next; + last; } } - unless (defined($file) or defined($file = $pm)) { - require Test::Builder; - Test::Builder->new->diag('Couldn\'t find a valid source file'); - return; - } + $file = $last_pm unless defined $file; + } + + unless (defined $file) { + require Test::Builder; + Test::Builder->new->diag('Couldn\'t find a valid source file'); + return; } - exit $class->analyse( - file => $file, - %args, - ); + if ($file ne '-e') { + exit $class->analyse( + file => $file, + %args, + ); + } + + require File::Temp; + my $tmp = File::Temp->new; + + require Filter::Util::Call; + Filter::Util::Call::filter_add(sub { + my $status = Filter::Util::Call::filter_read(); + if ($status > 0) { + print $tmp $_; + } elsif ($status == 0) { + close $tmp; + my $code = $class->analyse( + file => $tmp->filename, + %args, + ); + exit $code; + } + $status; + }); } END { @@ -259,8 +283,6 @@ END { =head1 CAVEATS -You can't use this module to test code given by the C<-e> command-line switch. - Perl 5.8 is notorious for leaking like there's no tomorrow, so the suppressions are very likely not to be very accurate on it. Anyhow, results will most likely be better if your perl is built with debugging enabled. Using the latest C available will also help. This module is not really secure. It's definitely not taint safe. That shouldn't be a problem for test files.