]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blobdiff - lib/Test/Valgrind.pm
This is 1.10
[perl/modules/Test-Valgrind.git] / lib / Test / Valgrind.pm
index 601a949997f5991b7c7df4a0f17f402ba9d414d4..ea437038640fdb841c0e4ab8ab9556aca2bd8d2d 100644 (file)
@@ -5,26 +5,29 @@ use warnings;
 
 =head1 NAME
 
-Test::Valgrind - Test Perl code through valgrind.
+Test::Valgrind - Generate suppressions, analyse and test any command with valgrind.
 
 =head1 VERSION
 
-Version 1.00
+Version 1.10
 
 =cut
 
-our $VERSION = '1.00';
+our $VERSION = '1.10';
 
 =head1 SYNOPSIS
 
     # 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
@@ -35,6 +38,9 @@ This module is a front-end to the C<Test::Valgrind::*> API that lets you run Per
 If they aren't available yet, it will first generate suppressions for the current C<perl> interpreter and store them in the portable flavour of F<~/.perl/Test-Valgrind/suppressions/$VERSION>.
 The actual run will then take place, and tests will be passed or failed according to the result of the analysis.
 
+The complete API is much more versatile than this.
+It allows you to run I<any> executable under valgrind, generate the corresponding suppressions and convert the analysis output to TAP so that it can be incorporated into your project's testsuite.
+
 Due to the nature of perl's memory allocator, this module can't track leaks of Perl objects.
 This includes non-mortalized scalars and memory cycles. However, it can track leaks of chunks of memory allocated in XS extensions with C<Newx> and friends or C<malloc>.
 As such, it's complementary to the other very good leak detectors listed in the L</SEE ALSO> section.
@@ -162,7 +168,9 @@ sub analyse {
   );
  };
  unless ($sess) {
-  $action->abort($sess, $@);
+  my $err = $@;
+  $err =~ s/^(Empty valgrind candidates list|No appropriate valgrind executable could be found)\s+at.*/$1/;
+  $action->abort($sess, $err);
   return $action->status($sess);
  }
 
@@ -186,8 +194,8 @@ sub analyse {
 
 =head2 C<import [ %options ]>
 
-In the parent process, L</import> calls L</analyse> with the arguments it received itself - except that if no C<file> option was supplied, it tries to pick the highest caller context that looks like a script.
-When the analyse finishes, it exists with the status that was returned.
+In the parent process, L</import> calls L</analyse> with the arguments it received itself - except that if no C<file> option was supplied, it tries to pick the first caller context that looks like a script.
+When the analyse ends, it exits with the status that was returned.
 
 In the child process, it just C<return>s so that the calling code is actually run under C<valgrind>.
 
@@ -220,32 +228,70 @@ sub import {
   return;
  }
 
- my ($file, $pm, $next);
- my $l = 0;
- while ($l < 1000) {
-  $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;
-  } else {
-   $file = $next;
+ my $file = delete $args{file};
+ unless (defined $file) {
+  my ($next, $last_pm);
+  for (my $l = 0; 1; ++$l) {
+   $next = (caller $l)[1];
+   last unless defined $next;
+   next if $next =~ /^\s*\(\s*eval\s*\d*\s*\)\s*$/;
+   if ($next =~ /\.pmc?$/) {
+    $last_pm = $next;
+   } else {
+    $file = $next;
+    last;
+   }
   }
+  $file = $last_pm unless defined $file;
  }
- unless (defined($file) or defined($file = $pm)) {
+
+ 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;
+ });
 }
 
+=head1 VARIABLES
+
+=head2 C<$dl_unload>
+
+When set to true, all dynamic extensions that were loaded during the analysis will be unloaded at C<END> time by L<DynaLoader/dl_unload_file>.
+
+Since this obfuscates error stack traces, it's disabled by default.
+
+=cut
+
+our $dl_unload;
+
 END {
- if ($run and eval { require DynaLoader; 1 }) {
+ if ($dl_unload and $run and eval { require DynaLoader; 1 }) {
   my @rest;
   DynaLoader::dl_unload_file($_) or push @rest, $_ for @DynaLoader::dl_librefs;
   @DynaLoader::dl_librefs = @rest;
@@ -254,8 +300,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.