]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blobdiff - lib/Test/Valgrind.pm
Allow using Test::Valgrind on code given by the -e command-line flag
[perl/modules/Test-Valgrind.git] / lib / Test / Valgrind.pm
index 3c52e85bc814416e96797d65dc13f5c0516ad042..5f7414f8d7ad94a570709b1fb4cfc81edeaf2a78 100644 (file)
@@ -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<valgrind> 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.