1#!/usr/local/bin/perl -w 2# Test for File::Temp - OO interface 3 4use strict; 5use Test::More tests => 26; 6use File::Spec; 7 8# Will need to check that all files were unlinked correctly 9# Set up an END block here to do it 10 11# Arrays containing list of dirs/files to test 12my (@files, @dirs, @still_there); 13 14# And a test for files that should still be around 15# These are tidied up 16END { 17 foreach (@still_there) { 18 ok( -f $_, "Check $_ exists" ); 19 ok( unlink( $_ ), "Unlinked $_" ); 20 ok( !(-f $_), "$_ no longer there"); 21 } 22} 23 24# Loop over an array hoping that the files dont exist 25END { foreach (@files) { ok( !(-e $_), "File $_ should not be there" )} } 26 27# And a test for directories 28END { foreach (@dirs) { ok( !(-d $_), "Directory $_ should not be there" ) } } 29 30# Need to make sure that the END blocks are setup before 31# the ones that File::Temp configures since END blocks are evaluated 32# in reverse order and we need to check the files *after* File::Temp 33# removes them 34BEGIN {use_ok( "File::Temp" ); } 35 36# Tempfile 37# Open tempfile in some directory, unlink at end 38my $fh = new File::Temp( SUFFIX => '.txt' ); 39 40ok( (-f "$fh"), "File $fh exists" ); 41# Should still be around after closing 42ok( close( $fh ), "Close file $fh" ); 43ok( (-f "$fh"), "File $fh still exists after close" ); 44# Check again at exit 45push(@files, "$fh"); 46 47# TEMPDIR test 48# Create temp directory in current dir 49my $template = 'tmpdirXXXXXX'; 50print "# Template: $template\n"; 51my $tempdir = File::Temp::tempdir( $template , 52 DIR => File::Spec->curdir, 53 CLEANUP => 1, 54 ); 55 56print "# TEMPDIR: $tempdir\n"; 57 58ok( (-d $tempdir), "Does $tempdir directory exist" ); 59push(@dirs, $tempdir); 60 61# Create file in the temp dir 62$fh = new File::Temp( 63 DIR => $tempdir, 64 SUFFIX => '.dat', 65 ); 66 67ok( $fh->unlink_on_destroy, "should unlink"); 68print "# TEMPFILE: Created $fh\n"; 69 70ok( (-f "$fh"), "File $fh exists in tempdir?"); 71push(@files, "$fh"); 72 73# Test tempfile 74# ..and again (without unlinking it) 75$fh = new File::Temp( DIR => $tempdir, UNLINK => 0 ); 76 77print "# TEMPFILE: Created $fh\n"; 78ok( (-f "$fh" ), "Second file $fh exists in tempdir [nounlink]?"); 79push(@files, "$fh"); 80 81# and another (with template) 82 83$fh = new File::Temp( TEMPLATE => 'helloXXXXXXX', 84 DIR => $tempdir, 85 SUFFIX => '.dat', 86 ); 87 88print "# TEMPFILE: Created $fh\n"; 89 90ok( (-f "$fh"), "File $fh exists? [from template]" ); 91push(@files, "$fh"); 92 93 94# Create a temporary file that should stay around after 95# it has been closed 96$fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 0); 97 98print "# TEMPFILE: Created $fh\n"; 99ok( -f "$fh", "File $fh exists?" ); 100ok( close( $fh ), "Close file $fh" ); 101ok( ! $fh->unlink_on_destroy, "should not unlink"); 102push( @still_there, "$fh"); # check at END 103 104# Now create a temp file that will remain when the object 105# goes out of scope because of $KEEP_ALL 106$fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 1); 107 108print "# TEMPFILE: Created $fh\n"; 109ok( -f "$fh", "File $fh exists?" ); 110ok( close( $fh ), "Close file $fh" ); 111ok( $fh->unlink_on_destroy, "should unlink (in principal)"); 112push( @still_there, "$fh"); # check at END 113$File::Temp::KEEP_ALL = 1; 114 115# Make sure destructors run 116undef $fh; 117 118# allow end blocks to run 119$File::Temp::KEEP_ALL = 0; 120 121# Now END block will execute to test the removal of directories 122print "# End of tests. Execute END blocks\n"; 123 124