use strict;
use Test::More tests => 30;
use File::Spec;
my (@files, @dirs, @still_there);
END {
foreach (@still_there) {
ok( -f $_, "Check $_ exists" );
ok( unlink( $_ ), "Unlinked $_" );
ok( !(-f $_), "$_ no longer there");
}
}
END { foreach (@files) { ok( !(-e $_), "File $_ should not be there" )} }
END { foreach (@dirs) { ok( !(-d $_), "Directory $_ should not be there" ) } }
BEGIN {use_ok( "File::Temp" ); }
my $fh = new File::Temp( SUFFIX => '.txt' );
ok( (-f "$fh"), "File $fh exists" );
ok( close( $fh ), "Close file $fh" );
ok( (-f "$fh"), "File $fh still exists after close" );
push(@files, "$fh");
my $tdir = File::Temp->newdir();
my $dirname = "$tdir"; ok( -d $dirname, "Directory $tdir exists");
undef $tdir;
ok( !-d $dirname, "Directory should now be gone");
my $qfh = File::Temp->new();
my $qfname = "$qfh";
ok (-f $qfname, "temp file exists");
undef $qfh;
ok( !-f $qfname, "temp file now gone");
my $template = 'tmpdirXXXXXX';
print "# Template: $template\n";
my $tempdir = File::Temp::tempdir( $template ,
DIR => File::Spec->curdir,
CLEANUP => 1,
);
print "# TEMPDIR: $tempdir\n";
ok( (-d $tempdir), "Does $tempdir directory exist" );
push(@dirs, $tempdir);
$fh = new File::Temp(
DIR => $tempdir,
SUFFIX => '.dat',
);
ok( $fh->unlink_on_destroy, "should unlink");
print "# TEMPFILE: Created $fh\n";
ok( (-f "$fh"), "File $fh exists in tempdir?");
push(@files, "$fh");
$fh = new File::Temp( DIR => $tempdir, UNLINK => 0 );
print "# TEMPFILE: Created $fh\n";
ok( (-f "$fh" ), "Second file $fh exists in tempdir [nounlink]?");
push(@files, "$fh");
$fh = new File::Temp( TEMPLATE => 'helloXXXXXXX',
DIR => $tempdir,
SUFFIX => '.dat',
);
print "# TEMPFILE: Created $fh\n";
ok( (-f "$fh"), "File $fh exists? [from template]" );
push(@files, "$fh");
$fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 0);
print "# TEMPFILE: Created $fh\n";
ok( -f "$fh", "File $fh exists?" );
ok( close( $fh ), "Close file $fh" );
ok( ! $fh->unlink_on_destroy, "should not unlink");
push( @still_there, "$fh");
$fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 1);
print "# TEMPFILE: Created $fh\n";
ok( -f "$fh", "File $fh exists?" );
ok( close( $fh ), "Close file $fh" );
ok( $fh->unlink_on_destroy, "should unlink (in principal)");
push( @still_there, "$fh"); $File::Temp::KEEP_ALL = 1;
undef $fh;
$File::Temp::KEEP_ALL = 0;
print "# End of tests. Execute END blocks\n";