tempfile.t revision 1.1.1.1
1#!/usr/local/bin/perl -w 2# Test for File::Temp - tempfile function 3 4use strict; 5use Test; 6BEGIN { plan tests => 22} 7use File::Spec; 8 9# Will need to check that all files were unlinked correctly 10# Set up an END block here to do it 11 12# Arrays containing list of dirs/files to test 13my (@files, @dirs, @still_there); 14 15# And a test for files that should still be around 16# These are tidied up 17END { 18 foreach (@still_there) { 19 ok( -f $_ ); 20 ok( unlink( $_ ) ); 21 ok( !(-f $_) ); 22 } 23} 24 25# Loop over an array hoping that the files dont exist 26END { foreach (@files) { ok( !(-e $_) )} } 27 28# And a test for directories 29END { foreach (@dirs) { ok( !(-d $_) )} } 30 31# Need to make sure that the END blocks are setup before 32# the ones that File::Temp configures since END blocks are evaluated 33# in revers order and we need to check the files *after* File::Temp 34# removes them 35use File::Temp qw/ tempfile tempdir/; 36 37# Now we start the tests properly 38ok(1); 39 40 41# Tempfile 42# Open tempfile in some directory, unlink at end 43my ($fh, $tempfile) = tempfile( 44 UNLINK => 1, 45 SUFFIX => '.txt', 46 ); 47 48ok( (-f $tempfile) ); 49# Should still be around after closing 50ok( close( $fh ) ); 51ok( (-f $tempfile) ); 52# Check again at exit 53push(@files, $tempfile); 54 55# TEMPDIR test 56# Create temp directory in current dir 57my $template = 'tmpdirXXXXXX'; 58print "# Template: $template\n"; 59my $tempdir = tempdir( $template , 60 DIR => File::Spec->curdir, 61 CLEANUP => 1, 62 ); 63 64print "# TEMPDIR: $tempdir\n"; 65 66ok( (-d $tempdir) ); 67push(@dirs, $tempdir); 68 69# Create file in the temp dir 70($fh, $tempfile) = tempfile( 71 DIR => $tempdir, 72 UNLINK => 1, 73 SUFFIX => '.dat', 74 ); 75 76print "# TEMPFILE: Created $tempfile\n"; 77 78ok( (-f $tempfile)); 79push(@files, $tempfile); 80 81# Test tempfile 82# ..and again 83($fh, $tempfile) = tempfile( 84 DIR => $tempdir, 85 ); 86 87 88ok( (-f $tempfile )); 89push(@files, $tempfile); 90 91# Test tempfile 92# ..and another with changed permissions (read-only) 93($fh, $tempfile) = tempfile( 94 DIR => $tempdir, 95 ); 96chmod 0444, $tempfile; 97 98ok( (-f $tempfile )); 99push(@files, $tempfile); 100 101print "# TEMPFILE: Created $tempfile\n"; 102 103# and another (with template) 104 105($fh, $tempfile) = tempfile( 'helloXXXXXXX', 106 DIR => $tempdir, 107 UNLINK => 1, 108 SUFFIX => '.dat', 109 ); 110 111print "# TEMPFILE: Created $tempfile\n"; 112 113ok( (-f $tempfile) ); 114push(@files, $tempfile); 115 116 117# Create a temporary file that should stay around after 118# it has been closed 119($fh, $tempfile) = tempfile( 'permXXXXXXX', UNLINK => 0 ); 120print "# TEMPFILE: Created $tempfile\n"; 121ok( -f $tempfile ); 122ok( close( $fh ) ); 123push( @still_there, $tempfile); # check at END 124 125# Would like to create a temp file and just retrieve the handle 126# but the test is problematic since: 127# - We dont know the filename so we cant check that it is tidied 128# correctly 129# - The unlink0 required on unix for tempfile creation will fail 130# on NFS 131# Try to do what we can. 132# Tempfile croaks on error so we need an eval 133$fh = eval { tempfile( 'ftmpXXXXX', DIR => File::Spec->tmpdir ) }; 134 135if ($fh) { 136 137 # print something to it to make sure something is there 138 ok( print $fh "Test\n" ); 139 140 # Close it - can not check it is gone since we dont know the name 141 ok( close($fh) ); 142 143} else { 144 skip "Skip Failed probably due to NFS", 1; 145 skip "Skip Failed probably due to NFS", 1; 146} 147 148# Now END block will execute to test the removal of directories 149print "# End of tests. Execute END blocks\n"; 150 151