]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - lib/Test/Valgrind.pm
Allow passing a command to Test::Valgrind->import
[perl/modules/Test-Valgrind.git] / lib / Test / Valgrind.pm
1 package Test::Valgrind;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 Test::Valgrind - Test Perl code through valgrind.
9
10 =head1 VERSION
11
12 Version 1.00
13
14 =cut
15
16 our $VERSION = '1.00';
17
18 =head1 SYNOPSIS
19
20     # From the command-line
21     perl -MTest::Valgrind leaky.pl
22
23     # In a test file
24     use Test::More;
25     eval 'use Test::Valgrind';
26     plan skip_all => 'Test::Valgrind is required to test your distribution with valgrind' if $@;
27     ...
28
29     # In all the test files of a directory
30     prove --exec 'perl -Iblib/lib -Iblib/arch -MTest::Valgrind' t/*.t
31
32 =head1 DESCRIPTION
33
34 This module is a front-end to the C<Test::Valgrind::*> API that lets you run Perl code through the C<memcheck> tool of the C<valgrind> memory debugger, to test it for memory errors and leaks.
35 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>.
36 The actual run will then take place, and tests will be passed or failed according to the result of the analysis.
37
38 Due to the nature of perl's memory allocator, this module can't track leaks of Perl objects.
39 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>.
40 As such, it's complementary to the other very good leak detectors listed in the L</SEE ALSO> section.
41
42 =head1 CONFIGURATION
43
44 You can pass parameters to C<import> as a list of key / value pairs, where valid keys are :
45
46 =over 4
47
48 =item *
49
50 C<< command => $command >>
51
52 The L<Test::Valgrind::Command> object (or class name) to use.
53
54 Defaults to L<Test::Valgrind::Command::PerlScript>.
55
56 =item *
57
58 C<< tool => $tool >>
59
60 The L<Test::Valgrind::Tool> object (or class name) to use.
61
62 Defaults to L<Test::Valgrind::Tool::memcheck>.
63
64 =item *
65
66 C<< action => $action >>
67
68 The L<Test::Valgrind::Action> object (or class name) to use.
69
70 Defaults to L<Test::Valgrind::Action::Test>.
71
72 =item *
73
74 C<< diag => $bool >>
75
76 If true, print the output of the test script as diagnostics.
77
78 =item *
79
80 C<< callers => $number >>
81
82 Specify the maximum stack depth studied when valgrind encounters an error.
83 Raising this number improves granularity.
84
85 Default is 12.
86
87 =item *
88
89 C<< extra_supps => \@files >>
90
91 Also use suppressions from C<@files> besides C<perl>'s.
92
93 =item *
94
95 C<< no_def_supp => $bool >>
96
97 If true, do not use the default suppression file.
98
99 =back
100
101 =cut
102
103 # We use as little modules as possible in run mode so that they don't pollute
104 # the analysis. Hence all the requires.
105
106 my $run;
107
108 sub import {
109  shift;
110
111  if (@_ % 2) {
112   require Carp;
113   Carp::croak('Optional arguments must be passed as key => value pairs');
114  }
115  my %args = @_;
116
117  if (defined $args{run} or $run) {
118   require Perl::Destruct::Level;
119   Perl::Destruct::Level::set_destruct_level(3);
120   {
121    my $oldfh = select STDOUT;
122    $|++;
123    select $oldfh;
124   }
125   $run = 1;
126   return;
127  }
128
129  my ($file, $pm, $next);
130  my $l = 0;
131  while ($l < 1000) {
132   $next = (caller $l++)[1];
133   last unless defined $next;
134   next if $next eq '-e' or $next =~ /^\s*\(\s*eval\s*\d*\s*\)\s*$/ or !-f $next;
135   if ($next =~ /\.pm$/) {
136    $pm   = $next;
137   } else {
138    $file = $next;
139   }
140  }
141  unless (defined($file) or defined($file = $pm)) {
142   require Test::Builder;
143   Test::Builder->new->diag('Couldn\'t find a valid source file');
144   return;
145  }
146
147  my $instanceof = sub {
148   require Scalar::Util;
149   Scalar::Util::blessed($_[0]) && $_[0]->isa($_[1]);
150  };
151
152  my $cmd = delete $args{command};
153  unless ($cmd->$instanceof('Test::Valgrind::Command')) {
154   require Test::Valgrind::Command;
155   $cmd = Test::Valgrind::Command->new(
156    command => $cmd || 'PerlScript',
157    file    => $file,
158    args    => [ '-MTest::Valgrind=run,1' ],
159   );
160  }
161
162  my $tool = delete $args{tool};
163  unless ($tool->$instanceof('Test::Valgrind::Tool')) {
164   require Test::Valgrind::Tool;
165   $tool = Test::Valgrind::Tool->new(
166    tool     => $tool || 'memcheck',
167    callers  => delete($args{callers}),
168   );
169  }
170
171  my $action = delete $args{action};
172  unless ($action->$instanceof('Test::Valgrind::Action')) {
173   require Test::Valgrind::Action;
174   $action = Test::Valgrind::Action->new(
175    action => $action || 'Test',
176    diag   => delete($args{diag}),
177   );
178  }
179
180  require Test::Valgrind::Session;
181  my $sess = eval {
182   Test::Valgrind::Session->new(
183    min_version => $tool->requires_version,
184    map { $_ => delete $args{$_} } qw/extra_supps no_def_supp/
185   );
186  };
187  unless ($sess) {
188   $action->abort($sess, $@);
189   exit $action->status($sess);
190  }
191
192  eval {
193   $sess->run(
194    command => $cmd,
195    tool    => $tool,
196    action  => $action,
197   );
198  };
199  if ($@) {
200   require Test::Valgrind::Report;
201   $action->report($sess, Test::Valgrind::Report->new_diag($@));
202  }
203
204  my $status = $sess->status;
205  $status = 255 unless defined $status;
206
207  exit $status;
208 }
209
210 END {
211  if ($run and eval { require DynaLoader; 1 }) {
212   my @rest;
213   DynaLoader::dl_unload_file($_) or push @rest, $_ for @DynaLoader::dl_librefs;
214   @DynaLoader::dl_librefs = @rest;
215  }
216 }
217
218 =head1 CAVEATS
219
220 You can't use this module to test code given by the C<-e> command-line switch.
221
222 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.
223
224 This module is not really secure. It's definitely not taint safe. That shouldn't be a problem for test files.
225
226 What your tests output to C<STDOUT> and C<STDERR> is eaten unless you pass the C<diag> option, in which case it will be reprinted as diagnostics.
227
228 =head1 DEPENDENCIES
229
230 Valgrind 3.1.0 (L<http://valgrind.org>).
231
232 L<XML::Twig>, L<version>, L<File::HomeDir>, L<Env::Sanctify>, L<Perl::Destruct::Level>.
233
234 =head1 SEE ALSO
235
236 All the C<Test::Valgrind::*> API, including L<Test::Valgrind::Command>, L<Test::Valgrind::Tool>, L<Test::Valgrind::Action> and L<Test::Valgrind::Session>.
237
238 L<Test::LeakTrace>.
239
240 L<Devel::Leak>, L<Devel::LeakTrace>, L<Devel::LeakTrace::Fast>.
241
242 =head1 AUTHOR
243
244 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
245
246 You can contact me by mail or on C<irc.perl.org> (vincent).
247
248 =head1 BUGS
249
250 Please report any bugs or feature requests to C<bug-test-valgrind at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Valgrind>.
251 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
252
253 =head1 SUPPORT
254
255 You can find documentation for this module with the perldoc command.
256
257     perldoc Test::Valgrind
258
259 =head1 ACKNOWLEDGEMENTS
260
261 RafaĆ«l Garcia-Suarez, for writing and instructing me about the existence of L<Perl::Destruct::Level> (Elizabeth Mattijsen is a close second).
262
263 H.Merijn Brand, for daring to test this thing.
264
265 All you people that showed interest in this module, which motivated me into completely rewriting it.
266
267 =head1 COPYRIGHT & LICENSE
268
269 Copyright 2008-2009 Vincent Pit, all rights reserved.
270
271 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
272
273 =cut
274
275 1; # End of Test::Valgrind