]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - t/lib/Test/Valgrind/FakeValgrind.pm
Silence some useless warnings from File::Temp
[perl/modules/Test-Valgrind.git] / t / lib / Test / Valgrind / FakeValgrind.pm
1 package Test::Valgrind::FakeValgrind;
2
3 use strict;
4 use warnings;
5
6 use Config ();
7 use File::Spec;
8 use File::Temp;
9
10 sub _dummy_valgrind_code {
11  my ($version, $body) = @_;
12
13  my $perl = $^X;
14  unless (-e $perl and -x $perl) {
15   $perl = $Config::Config{perlpath};
16   unless (-e $perl and -x $perl) {
17    return undef;
18   }
19  }
20
21  if (defined $body) {
22   $body = "\n$body";
23  } else {
24   $body = '';
25  }
26
27  return <<" FAKE_VG";
28 #!$perl
29 if (\@ARGV == 1 && \$ARGV[0] eq '--version') {
30  print "valgrind-$version\n";
31  exit 0;
32 }$body
33  FAKE_VG
34 }
35
36 sub new {
37  my ($class, %args) = @_;
38
39  my $exe_name = $args{exe_name};
40  my $version  = $args{version} || '3.1.0';
41  my $body     = $args{body};
42
43  my $self = { };
44
45  if (defined $exe_name) {
46   $self->{tmp_dir_obj}  = File::Temp->newdir(CLEANUP => 1);
47   $self->{tmp_dir}      = $self->{tmp_dir_obj}->dirname;
48   $self->{tmp_file}     = File::Spec->catfile($self->{tmp_dir}, $exe_name);
49  } else {
50   # Can't use the OO interface if we don't wan't the file to be opened by
51   # default, but then we have to deal with cleanup ourselves.
52   local $^W = 0;
53   (undef, my $tmp_file) = File::Temp::tempfile(
54    TEMPLATE => 'fakevgXXXX',
55    TMPDIR   => 1,
56    CLEANUP  => 0,
57    OPEN     => 0,
58   );
59   $self->{tmp_file}     = $tmp_file;
60   my ($vol, $dir)       = File::Spec->splitpath($self->{tmp_file});
61   $self->{tmp_dir}      = File::Spec->catpath($vol, $dir, '');
62  }
63
64  my $code = _dummy_valgrind_code($version, $body);
65  return 'Could not generate the dummy valgrind executable' unless $code;
66
67  return 'Temporary file already exists' if -s $self->{tmp_file};
68
69  {
70   open my $vg_fh, '>', $self->{tmp_file};
71   print $vg_fh $code;
72   close $vg_fh;
73   chmod 0755, $self->{tmp_file};
74  }
75
76  bless $self, $class;
77 }
78
79 sub path    { $_[0]->{tmp_file} }
80
81 sub dir     { $_[0]->{tmp_dir} }
82
83 sub DESTROY { 1 while unlink $_[0]->{tmp_file} }
84
85 1;