1 package Test::Valgrind::FakeValgrind;
10 sub _dummy_valgrind_code {
11 my ($version, $body) = @_;
14 unless (-e $perl and -x $perl) {
15 $perl = $Config::Config{perlpath};
16 unless (-e $perl and -x $perl) {
29 if (\@ARGV == 1 && \$ARGV[0] eq '--version') {
30 print "valgrind-$version\n";
37 my ($class, %args) = @_;
39 my $exe_name = $args{exe_name};
40 my $version = $args{version} || '3.1.0';
41 my $body = $args{body};
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);
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 (undef, my $tmp_file) = File::Temp::tempfile(
53 TEMPLATE => 'fakevgXXXX',
58 $self->{tmp_file} = $tmp_file;
59 my ($vol, $dir) = File::Spec->splitpath($self->{tmp_file});
60 $self->{tmp_dir} = File::Spec->catpath($vol, $dir, '');
63 my $code = _dummy_valgrind_code($version, $body);
64 return 'Could not generate the dummy valgrind executable' unless $code;
66 return 'Temporary file already exists' if -s $self->{tmp_file};
69 open my $vg_fh, '>', $self->{tmp_file};
72 chmod 0755, $self->{tmp_file};
78 sub path { $_[0]->{tmp_file} }
80 sub dir { $_[0]->{tmp_dir} }
82 sub DESTROY { 1 while unlink $_[0]->{tmp_file} }