]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blobdiff - t/lib/Test/Valgrind/FakeValgrind.pm
Make sure the fake valgrind test executables are not opened when used
[perl/modules/Test-Valgrind.git] / t / lib / Test / Valgrind / FakeValgrind.pm
index 098bae02fed9b17e04dd9a13c67ca131077778d5..71077bd65b8275cef5e74f9dc284edd76fedb2a0 100644 (file)
@@ -8,7 +8,7 @@ use File::Spec;
 use File::Temp;
 
 sub _dummy_valgrind_code {
- my ($version) = @_;
+ my ($version, $body) = @_;
 
  my $perl = $^X;
  unless (-e $perl and -x $perl) {
@@ -18,11 +18,18 @@ sub _dummy_valgrind_code {
   }
  }
 
+ if (defined $body) {
+  $body = "\n$body";
+ } else {
+  $body = '';
+ }
+
  return <<" FAKE_VG";
 #!$perl
 if (\@ARGV == 1 && \$ARGV[0] eq '--version') {
  print "valgrind-$version\n";
-}
+ exit 0;
+}$body
  FAKE_VG
 }
 
@@ -31,6 +38,7 @@ sub new {
 
  my $exe_name = $args{exe_name};
  my $version  = $args{version} || '3.1.0';
+ my $body     = $args{body};
 
  my $self = { };
 
@@ -39,13 +47,20 @@ sub new {
   $self->{tmp_dir}      = $self->{tmp_dir_obj}->dirname;
   $self->{tmp_file}     = File::Spec->catfile($self->{tmp_dir}, $exe_name);
  } else {
-  $self->{tmp_file_obj} = File::Temp->new(UNLINK => 1);
-  $self->{tmp_file}     = $self->{tmp_file_obj}->filename;
+  # Can't use the OO interface if we don't wan't the file to be opened by
+  # default, but then we have to deal with cleanup ourselves.
+  (undef, my $tmp_file) = File::Temp::tempfile(
+   TEMPLATE => 'fakevgXXXX',
+   TMPDIR   => 1,
+   CLEANUP  => 0,
+   OPEN     => 0,
+  );
+  $self->{tmp_file}     = $tmp_file;
   my ($vol, $dir)       = File::Spec->splitpath($self->{tmp_file});
   $self->{tmp_dir}      = File::Spec->catpath($vol, $dir, '');
  }
 
- my $code = _dummy_valgrind_code($version);
+ my $code = _dummy_valgrind_code($version, $body);
  return 'Could not generate the dummy valgrind executable' unless $code;
 
  return 'Temporary file already exists' if -s $self->{tmp_file};
@@ -60,8 +75,10 @@ sub new {
  bless $self, $class;
 }
 
-sub path { $_[0]->{tmp_file} }
+sub path    { $_[0]->{tmp_file} }
+
+sub dir     { $_[0]->{tmp_dir} }
 
-sub dir  { $_[0]->{tmp_dir} }
+sub DESTROY { 1 while unlink $_[0]->{tmp_file} }
 
 1;