]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - lib/Test/Valgrind/Session.pm
Revamp test skipping logic
[perl/modules/Test-Valgrind.git] / lib / Test / Valgrind / Session.pm
1 package Test::Valgrind::Session;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 Test::Valgrind::Session - Test::Valgrind session object.
9
10 =head1 VERSION
11
12 Version 1.15
13
14 =cut
15
16 our $VERSION = '1.15';
17
18 =head1 DESCRIPTION
19
20 This class supervises the execution of the C<valgrind> process.
21 It also acts as a dispatcher between the different components.
22
23 =cut
24
25 use File::Spec   ();
26 use Scalar::Util ();
27
28 use Fcntl (); # F_SETFD
29 use POSIX (); # SIGKILL
30
31 use version ();
32
33 use base qw<Test::Valgrind::Carp>;
34
35 =head1 METHODS
36
37 =head2 C<new>
38
39     my $tvs = Test::Valgrind::Session->new(
40      search_dirs    => \@search_dirs,
41      valgrind       => $valgrind,  # One candidate
42      valgrind       => \@valgrind, # Several candidates
43      min_version    => $min_version,
44      regen_def_supp => $regen_def_supp,
45      no_def_supp    => $no_def_supp,
46      allow_no_supp  => $allow_no_supp,
47      extra_supps    => \@extra_supps,
48     );
49
50 The package constructor, which takes several options :
51
52 =over 4
53
54 =item *
55
56 All the directories from C<@search_dirs> will have F<valgrind> appended to create a list of candidates for the C<valgrind> executable.
57
58 Defaults to the current C<PATH> environment variable.
59
60 =item *
61
62 If a simple scalar C<$valgrind> is passed as the value to C<'valgrind'>, it will be the only candidate.
63 C<@search_dirs> will then be ignored.
64
65 If an array refernce C<\@valgrind> is passed, its values will be I<prepended> to the list of the candidates resulting from C<@search_dirs>.
66
67 =item *
68
69 C<$min_version> specifies the minimal C<valgrind> version required.
70 The constructor will croak if it's not able to find an adequate C<valgrind> from the supplied candidates list and search path.
71
72 Defaults to none.
73
74 =item *
75
76 If C<$regen_def_supp> is true, the default suppression file associated with the tool and the command will be forcefully regenerated.
77
78 Defaults to false.
79
80 =item *
81
82 If C<$no_def_supp> is true, C<valgrind> won't read the default suppression file associated with the tool and the command.
83
84 Defaults to false.
85
86 =item *
87
88 If C<$allow_no_supp> is true, the command will always be run into C<valgrind> even if no appropriate suppression file is available.
89
90 Defaults to false.
91
92 =item *
93
94 C<$extra_supps> is a reference to an array of optional suppression files that will be passed to C<valgrind>.
95
96 Defaults to none.
97
98 =back
99
100 =cut
101
102 sub new {
103  my $class = shift;
104  $class = ref($class) || $class;
105
106  my %args = @_;
107
108  my @paths;
109  my $vg = delete $args{valgrind};
110  if (defined $vg and not ref $vg) {
111   @paths = ($vg);
112  } else {
113   push @paths, @$vg if $vg and ref $vg eq 'ARRAY';
114   my $dirs = delete $args{search_dirs};
115   $dirs = [ File::Spec->path ] unless $dirs;
116   push @paths, map File::Spec->catfile($_, 'valgrind'), @$dirs
117                                                         if ref $dirs eq 'ARRAY';
118  }
119  $class->_croak('Empty valgrind candidates list') unless @paths;
120
121  my $min_version = delete $args{min_version};
122  defined and not ref and $_ = version->new($_) for $min_version;
123
124  my ($valgrind, $version);
125  for (@paths) {
126   next unless -x;
127   my $ver = qx/$_ --version/;
128   if ($ver =~ /^valgrind-(\d+(\.\d+)*)/) {
129    if ($min_version) {
130     $version = version->new($1);
131     next if $version < $min_version;
132    } else {
133     $version = $1;
134    }
135    $valgrind = $_;
136    last;
137   }
138  }
139  $class->_croak('No appropriate valgrind executable could be found')
140                                                        unless defined $valgrind;
141
142  my $extra_supps = delete $args{extra_supps};
143  $extra_supps    = [ ] unless $extra_supps and ref $extra_supps eq 'ARRAY';
144  @$extra_supps   = grep { defined && -f $_ && -r _ } @$extra_supps;
145
146  bless {
147   valgrind       => $valgrind,
148   version        => $version,
149   regen_def_supp => delete($args{regen_def_supp}),
150   no_def_supp    => delete($args{no_def_supp}),
151   allow_no_supp  => delete($args{allow_no_supp}),
152   extra_supps    => $extra_supps,
153  }, $class;
154 }
155
156 =head2 C<valgrind>
157
158     my $valgrind_path = $tvs->valgrind;
159
160 The path to the selected C<valgrind> executable.
161
162 =head2 C<version>
163
164     my $valgrind_version = $tvs->version;
165
166 The L<version> object associated to the selected C<valgrind>.
167
168 =cut
169
170 sub version {
171  my ($self) = @_;
172
173  my $version = $self->{version};
174  $self->{version} = $version = version->new($version) unless ref $version;
175
176  return $version;
177 }
178
179 =head2 C<regen_def_supp>
180
181     my $regen_def_supp = $tvs->regen_def_supp;
182
183 Read-only accessor for the C<regen_def_supp> option.
184
185 =cut
186
187 =head2 C<no_def_supp>
188
189     my $no_def_supp = $tvs->no_def_supp;
190
191 Read-only accessor for the C<no_def_supp> option.
192
193 =head2 C<allow_no_supp>
194
195     my $allow_no_supp = $tvs->allow_no_supp;
196
197 Read-only accessor for the C<allow_no_supp> option.
198
199 =cut
200
201 eval "sub $_ { \$_[0]->{$_} }" for qw<
202  valgrind
203  regen_def_supp
204  no_def_supp
205  allow_no_supp
206 >;
207
208 =head2 C<extra_supps>
209
210     my @extra_supps = $tvs->extra_supps;
211
212 Read-only accessor for the C<extra_supps> option.
213
214 =cut
215
216 sub extra_supps { @{$_[0]->{extra_supps} || []} }
217
218 =head2 C<run>
219
220     $tvs->run(
221      action  => $action,
222      tool    => $tool,
223      command => $command,
224     );
225
226 Runs the command C<$command> through C<valgrind> with the tool C<$tool>, which will report to the action C<$action>.
227
228 If the command is a L<Test::Valgrind::Command::Aggregate> object, the action and the tool will be initialized once before running all the aggregated commands.
229
230 =cut
231
232 sub run {
233  my ($self, %args) = @_;
234
235  for (qw<action tool command>) {
236   my $base = 'Test::Valgrind::' . ucfirst;
237   my $value = $args{$_};
238   $self->_croak("Invalid $_") unless Scalar::Util::blessed($value)
239                                                          and $value->isa($base);
240   $self->$_($args{$_})
241  }
242
243  my $cmd = $self->command;
244  if ($cmd->isa('Test::Valgrind::Command::Aggregate')) {
245   for my $subcmd ($cmd->commands) {
246    $args{command} = $subcmd;
247    $self->run(%args);
248   }
249   return;
250  }
251
252  $self->report($self->report_class->new_diag(
253   'Using valgrind ' . $self->version . ' located at ' . $self->valgrind
254  ));
255
256  my $env = $self->command->env($self);
257
258  my @supp_args;
259  if ($self->do_suppressions) {
260   push @supp_args, '--gen-suppressions=all';
261  } else {
262   if (!$self->no_def_supp) {
263    my $def_supp = $self->def_supp_file;
264    my $forced;
265    if ($self->regen_def_supp and -e $def_supp) {
266     1 while unlink $def_supp;
267     $forced = 1;
268    }
269    if (defined $def_supp and not -e $def_supp) {
270     $self->report($self->report_class->new_diag(
271      'Generating suppressions' . ($forced ? ' (forced)' : '') . '...'
272     ));
273     require Test::Valgrind::Suppressions;
274     Test::Valgrind::Suppressions->generate(
275      tool    => $self->tool,
276      command => $self->command,
277      target  => $def_supp,
278     );
279     $self->_croak('Couldn\'t generate suppressions') unless -e $def_supp;
280     $self->report($self->report_class->new_diag(
281      "Suppressions for this perl stored in $def_supp"
282     ));
283    }
284   }
285   my @supp_files = grep {
286    -e $_ and $self->command->check_suppressions_file($_)
287   } $self->suppressions;
288   if (@supp_files > 1) {
289    my $files_list = join "\n", map "    $_", @supp_files;
290    $self->report($self->report_class->new_diag(
291     "Using suppressions from:\n$files_list"
292    ));
293   } elsif (@supp_files) {
294    $self->report($self->report_class->new_diag(
295     "Using suppressions from $supp_files[0]"
296    ));
297   } elsif ($self->allow_no_supp) {
298    $self->report($self->report_class->new_diag("No suppressions used"));
299   } else {
300    $self->_croak("No compatible suppressions available");
301   }
302   @supp_args = map "--suppressions=$_", @supp_files;
303  }
304
305  my $guard = Test::Valgrind::Session::Guard->new(sub { $self->finish });
306  $self->start;
307
308  pipe my $vrdr, my $vwtr or $self->_croak("pipe(\$vrdr, \$vwtr): $!");
309  {
310   my $oldfh = select $vrdr;
311   $|++;
312   select $oldfh;
313  }
314
315  my $pid = fork;
316  $self->_croak("fork(): $!") unless defined $pid;
317
318  if ($pid == 0) {
319   {
320    local $@;
321    eval { setpgrp(0, 0) };
322   }
323   close $vrdr or $self->_croak("close(\$vrdr): $!");
324   fcntl $vwtr, Fcntl::F_SETFD(), 0
325                               or $self->_croak("fcntl(\$vwtr, F_SETFD, 0): $!");
326
327   my @args = (
328    $self->valgrind,
329    $self->tool->args($self),
330    @supp_args,
331    $self->parser->args($self, $vwtr),
332    $self->command->args($self),
333   );
334
335 #  $self->report($self->report_class->new_diag("@args"));
336
337   exec { $args[0] } @args or $self->_croak("exec @args: $!");
338  }
339
340  local $SIG{INT} = sub {
341   kill -(POSIX::SIGKILL()) => $pid;
342   waitpid $pid, 0;
343   die 'valgrind analysis was interrupted';
344  };
345
346  close $vwtr or $self->_croak("close(\$vwtr): $!");
347
348  $self->parser->parse($self, $vrdr);
349
350  $self->{exit_code} = (waitpid($pid, 0) == $pid) ? $? >> 8 : 255;
351
352  close $vrdr or $self->_croak("close(\$vrdr): $!");
353
354  return;
355 }
356
357 sub Test::Valgrind::Session::Guard::new     { bless \($_[1]), $_[0] }
358
359 sub Test::Valgrind::Session::Guard::DESTROY { ${$_[0]}->() }
360
361 =head2 C<action>
362
363 Read-only accessor for the C<action> associated to the current run.
364
365 =head2 C<tool>
366
367 Read-only accessor for the C<tool> associated to the current run.
368
369 =head2 C<parser>
370
371 Read-only accessor for the C<parser> associated to the current tool.
372
373 =head2 C<command>
374
375 Read-only accessor for the C<command> associated to the current run.
376
377 =cut
378
379 my @members;
380 BEGIN {
381  @members = qw<action tool command parser>;
382  for (@members) {
383   eval "sub $_ { \@_ <= 1 ? \$_[0]->{$_} : (\$_[0]->{$_} = \$_[1]) }";
384   die if $@;
385  }
386 }
387
388 =head2 C<do_suppressions>
389
390 Forwards to C<< ->action->do_suppressions >>.
391
392 =cut
393
394 sub do_suppressions { $_[0]->action->do_suppressions }
395
396 =head2 C<parser_class>
397
398 Calls C<< ->tool->parser_class >> with the current session object as the unique argument.
399
400 =cut
401
402 sub parser_class { $_[0]->tool->parser_class($_[0]) }
403
404 =head2 C<report_class>
405
406 Calls C<< ->tool->report_class >> with the current session object as the unique argument.
407
408 =cut
409
410 sub report_class { $_[0]->tool->report_class($_[0]) }
411
412 =head2 C<def_supp_file>
413
414 Returns an absolute path to the default suppression file associated to the current session.
415
416 C<undef> will be returned as soon as any of C<< ->command->suppressions_tag >> or C<< ->tool->suppressions_tag >> are also C<undef>.
417 Otherwise, the file part of the name is builded by joining those two together, and the directory part is roughly F<< File::HomeDir->my_home / .perl / Test-Valgrind / suppressions / $VERSION >>.
418
419 =cut
420
421 sub def_supp_file {
422  my ($self) = @_;
423
424  my $tool_tag = $self->tool->suppressions_tag($self);
425  return unless defined $tool_tag;
426
427  my $cmd_tag = $self->command->suppressions_tag($self);
428  return unless defined $cmd_tag;
429
430  require File::HomeDir; # So that it's not needed at configure time.
431
432  return File::Spec->catfile(
433   File::HomeDir->my_home,
434   '.perl',
435   'Test-Valgrind',
436   'suppressions',
437   $VERSION,
438   "$tool_tag-$cmd_tag.supp",
439  );
440 }
441
442 =head2 C<suppressions>
443
444     my @suppressions = $tvs->suppressions;
445
446 Returns the list of all the suppressions that will be passed to C<valgrind>.
447 Honors L</no_def_supp> and L</extra_supps>.
448
449 =cut
450
451 sub suppressions {
452  my ($self) = @_;
453
454  my @supps;
455  unless ($self->no_def_supp) {
456   my $def_supp = $self->def_supp_file;
457   push @supps, $def_supp if defined $def_supp;
458  }
459  push @supps, $self->extra_supps;
460
461  return @supps;
462 }
463
464 =head2 C<start>
465
466     $tvs->start;
467
468 Starts the action and tool associated to the current run.
469 It's automatically called at the beginning of L</run>.
470
471 =cut
472
473 sub start {
474  my $self = shift;
475
476  delete @{$self}{qw<last_status exit_code>};
477
478  $self->tool->start($self);
479  $self->parser($self->parser_class->new)->start($self);
480  $self->action->start($self);
481
482  return;
483 }
484
485 =head2 C<abort>
486
487     $tvs->abort($msg);
488
489 Forwards to C<< ->action->abort >> after unshifting the session object to the argument list.
490
491 =cut
492
493 sub abort {
494  my $self = shift;
495
496  $self->action->abort($self, @_);
497 }
498
499 =head2 C<report>
500
501     $tvs->report($report);
502
503 Forwards to C<< ->action->report >> after unshifting the session object to the argument list.
504
505 =cut
506
507 sub report {
508  my ($self, $report) = @_;
509
510  return unless defined $report;
511
512  for my $handler (qw<tool command>) {
513   $report = $self->$handler->filter($self, $report);
514   return unless defined $report;
515  }
516
517  $self->action->report($self, $report);
518 }
519
520 =head2 C<finish>
521
522     $tvs->finish;
523
524 Finishes the action and tool associated to the current run.
525 It's automatically called at the end of L</run>.
526
527 =cut
528
529 sub finish {
530  my ($self) = @_;
531
532  my $action = $self->action;
533
534  $action->finish($self);
535  $self->parser->finish($self);
536  $self->tool->finish($self);
537
538  my $status = $action->status($self);
539  $self->{last_status} = defined $status ? $status : $self->{exit_code};
540
541  $self->$_(undef) for @members;
542
543  return;
544 }
545
546 =head2 C<status>
547
548     my $status = $tvs->status;
549
550 Returns the status code of the last run of the session.
551
552 =cut
553
554 sub status { $_[0]->{last_status} }
555
556 =head1 SEE ALSO
557
558 L<Test::Valgrind>, L<Test::Valgrind::Action>, L<Test::Valgrind::Command>, L<Test::Valgrind::Tool>, L<Test::Valgrind::Parser>.
559
560 L<version>, L<File::HomeDir>.
561
562 =head1 AUTHOR
563
564 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
565
566 You can contact me by mail or on C<irc.perl.org> (vincent).
567
568 =head1 BUGS
569
570 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>.
571 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
572
573 =head1 SUPPORT
574
575 You can find documentation for this module with the perldoc command.
576
577     perldoc Test::Valgrind::Session
578
579 =head1 COPYRIGHT & LICENSE
580
581 Copyright 2009,2010,2011,2013,2015 Vincent Pit, all rights reserved.
582
583 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
584
585 =cut
586
587 1; # End of Test::Valgrind::Session