1#!/usr/local/bin/perl -w 2# Test for File::Temp - OO interface 3 4use strict; 5use Test::More tests => 35; 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# Check for misuse 37eval { File::Temp->tempfile }; 38like( $@, qr/can't be called as a method/, "File::Temp->tempfile error" ); 39eval { File::Temp->tempdir }; 40like( $@, qr/can't be called as a method/, "File::Temp->tempfile error" ); 41 42# Tempfile 43# Open tempfile in some directory, unlink at end 44my $fh = new File::Temp( SUFFIX => '.txt' ); 45 46ok( (-f "$fh"), "File $fh exists" ); 47# Should still be around after closing 48ok( close( $fh ), "Close file $fh" ); 49ok( (-f "$fh"), "File $fh still exists after close" ); 50# Check again at exit 51push(@files, "$fh"); 52 53# OO tempdir 54my $tdir = File::Temp->newdir(); 55my $dirname = "$tdir"; # Stringify overload 56ok( -d $dirname, "Directory $tdir exists"); 57undef $tdir; 58ok( !-d $dirname, "Directory should now be gone"); 59 60# with template 61$tdir = File::Temp->newdir( TEMPLATE => 'helloXXXXX' ); 62like( "$tdir", qr/hello/, "Directory with TEMPLATE" ); 63undef $tdir; 64 65$tdir = File::Temp->newdir( 'helloXXXXX' ); 66like( "$tdir", qr/hello/, "Directory with leading template" ); 67undef $tdir; 68 69# Quick basic tempfile test 70my $qfh = File::Temp->new(); 71my $qfname = "$qfh"; 72ok (-f $qfname, "temp file exists"); 73undef $qfh; 74ok( !-f $qfname, "temp file now gone"); 75 76 77# TEMPDIR test as somewhere to put the temp files 78# Create temp directory in current dir 79my $template = 'tmpdirXXXXXX'; 80print "# Template: $template\n"; 81my $tempdir = File::Temp::tempdir( $template , 82 DIR => File::Spec->curdir, 83 CLEANUP => 1, 84 ); 85 86print "# TEMPDIR: $tempdir\n"; 87 88ok( (-d $tempdir), "Does $tempdir directory exist" ); 89push(@dirs, $tempdir); 90 91# Create file in the temp dir 92$fh = new File::Temp( 93 DIR => $tempdir, 94 SUFFIX => '.dat', 95 ); 96 97ok( $fh->unlink_on_destroy, "should unlink"); 98print "# TEMPFILE: Created $fh\n"; 99 100ok( (-f "$fh"), "File $fh exists in tempdir?"); 101push(@files, "$fh"); 102 103# Test tempfile 104# ..and again (without unlinking it) 105$fh = new File::Temp( DIR => $tempdir, UNLINK => 0 ); 106 107print "# TEMPFILE: Created $fh\n"; 108ok( (-f "$fh" ), "Second file $fh exists in tempdir [nounlink]?"); 109push(@files, "$fh"); 110 111# and another (with template) 112 113$fh = new File::Temp( TEMPLATE => 'helloXXXXXXX', 114 DIR => $tempdir, 115 SUFFIX => '.dat', 116 ); 117 118print "# TEMPFILE: Created $fh\n"; 119 120# and with a leading template 121$fh = File::Temp->new( 'helloXXXXXXX', 122 DIR => $tempdir, 123 SUFFIX => '.dat', 124 ); 125 126print "# TEMPFILE: Created $fh\n"; 127 128ok( (-f "$fh"), "File $fh exists? [from leading template]" ); 129like( "$fh", qr/hello/, "saw template" ); 130push(@files, "$fh"); 131 132 133 134# Create a temporary file that should stay around after 135# it has been closed 136$fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 0); 137 138print "# TEMPFILE: Created $fh\n"; 139ok( -f "$fh", "File $fh exists?" ); 140ok( close( $fh ), "Close file $fh" ); 141ok( ! $fh->unlink_on_destroy, "should not unlink"); 142push( @still_there, "$fh"); # check at END 143 144# Now create a temp file that will remain when the object 145# goes out of scope because of $KEEP_ALL 146$fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 1); 147 148print "# TEMPFILE: Created $fh\n"; 149ok( -f "$fh", "File $fh exists?" ); 150ok( close( $fh ), "Close file $fh" ); 151ok( $fh->unlink_on_destroy, "should unlink (in principle)"); 152push( @still_there, "$fh"); # check at END 153$File::Temp::KEEP_ALL = 1; 154 155# Make sure destructors run 156undef $fh; 157 158# allow end blocks to run 159$File::Temp::KEEP_ALL = 0; 160 161# Now END block will execute to test the removal of directories 162print "# End of tests. Execute END blocks\n"; 163 164