1#!/usr/local/bin/perl -w 2# Test for File::Temp - tempfile function 3 4use strict; 5use Test::More tests => 28; 6use File::Spec; 7use Cwd qw/ cwd /; 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 ($_) = /(^.*)/; # untaint for testing under taint mode 20 ok( -f $_, "File $_ is still present" ); 21 ok( unlink( $_ ), "Unlink file" ); 22 ok( !(-f $_), "File is no longer present" ); 23 } 24} 25 26# Loop over an array hoping that the files dont exist 27END { foreach (@files) { ok( !(-e $_), "File $_ should not be present" )} } 28 29# And a test for directories 30END { foreach (@dirs) { ok( !(-d $_), "Dir $_ should not be present" )} } 31 32# Need to make sure that the END blocks are setup before 33# the ones that File::Temp configures since END blocks are evaluated 34# in revers order and we need to check the files *after* File::Temp 35# removes them 36use File::Temp qw/ tempfile tempdir/; 37 38# Now we start the tests properly 39ok(1, "Start test"); 40 41 42# Tempfile 43# Open tempfile in some directory, unlink at end 44my ($fh, $tempfile) = tempfile( 45 UNLINK => 1, 46 SUFFIX => '.txt', 47 ); 48 49ok( (-f $tempfile), "Tempfile exists" ); 50# Should still be around after closing 51ok( close( $fh ), "Tempfile closed" ); 52ok( (-f $tempfile), "Tempfile exists" ); 53# Check again at exit 54push(@files, $tempfile); 55 56# TEMPDIR test 57# Create temp directory in current dir 58my $template = 'tmpdirXXXXXX'; 59print "# Template: $template\n"; 60my $tempdir = tempdir( $template , 61 DIR => File::Spec->curdir, 62 CLEANUP => 1, 63 ); 64 65print "# TEMPDIR: $tempdir\n"; 66 67ok( (-d $tempdir), "Local tempdir exists" ); 68push(@dirs, File::Spec->rel2abs($tempdir)); 69 70my $tempdir2 = tempdir( TEMPLATE => "customXXXXX", 71 DIR => File::Spec->curdir, 72 CLEANUP => 1, 73 ); 74 75print "# TEMPDIR2: $tempdir2\n"; 76 77like( $tempdir2, qr/custom/, "tempdir with TEMPLATE" ); 78push(@dirs, File::Spec->rel2abs($tempdir)); 79 80# Create file in the temp dir 81($fh, $tempfile) = tempfile( 82 DIR => $tempdir, 83 UNLINK => 1, 84 SUFFIX => '.dat', 85 ); 86 87print "# TEMPFILE: Created $tempfile\n"; 88 89ok( (-f $tempfile), "Local temp file exists with .dat extension"); 90push(@files, File::Spec->rel2abs($tempfile)); 91 92# Test tempfile 93# ..and again 94($fh, $tempfile) = tempfile( 95 DIR => $tempdir, 96 ); 97 98 99ok( (-f $tempfile ), "Local tempfile in tempdir exists"); 100push(@files, File::Spec->rel2abs($tempfile)); 101 102# Test tempfile 103# ..and another with changed permissions (read-only) 104($fh, $tempfile) = tempfile( 105 DIR => $tempdir, 106 ); 107chmod 0444, $tempfile; 108 109ok( (-f $tempfile ), "Local tempfile in tempdir exists read-only"); 110push(@files, File::Spec->rel2abs($tempfile)); 111 112print "# TEMPFILE: Created $tempfile\n"; 113 114# and another (with template) 115 116($fh, $tempfile) = tempfile( 'helloXXXXXXX', 117 DIR => $tempdir, 118 UNLINK => 1, 119 SUFFIX => '.dat', 120 ); 121 122print "# TEMPFILE: Created $tempfile\n"; 123 124ok( (-f $tempfile), "Local tempfile in tempdir with .dat extension exists" ); 125push(@files, File::Spec->rel2abs($tempfile)); 126 127 128# and another (with TEMPLATE) 129 130($fh, $tempfile) = tempfile( TEMPLATE => 'goodbyeXXXXXXX', 131 DIR => $tempdir, 132 UNLINK => 1, 133 SUFFIX => '.dat', 134 ); 135 136print "# TEMPFILE: Created $tempfile\n"; 137 138ok( (-f $tempfile), "Local tempfile in tempdir with TEMPLATE" ); 139push(@files, File::Spec->rel2abs($tempfile)); 140 141# Create a temporary file that should stay around after 142# it has been closed 143($fh, $tempfile) = tempfile( 'permXXXXXXX', UNLINK => 0 ); 144print "# TEMPFILE: Created $tempfile\n"; 145ok( -f $tempfile, "Long-lived temp file" ); 146ok( close( $fh ), "Close long-lived temp file" ); 147push( @still_there, File::Spec->rel2abs($tempfile) ); # check at END 148 149# Would like to create a temp file and just retrieve the handle 150# but the test is problematic since: 151# - We dont know the filename so we cant check that it is tidied 152# correctly 153# - The unlink0 required on unix for tempfile creation will fail 154# on NFS 155# Try to do what we can. 156# Tempfile croaks on error so we need an eval 157$fh = eval { tempfile( 'ftmpXXXXX', DIR => File::Temp::_wrap_file_spec_tmpdir() ) }; 158 159if ($fh) { 160 161 # print something to it to make sure something is there 162 ok( print($fh "Test\n"), "Write to temp file" ); 163 164 # Close it - can not check it is gone since we dont know the name 165 ok( close($fh), "Close temp file" ); 166 167} else { 168 SKIP: { 169 skip "Skip Failed probably due to NFS", 2; 170 } 171} 172 173# Create temp directory and chdir to it; it should still be removed on exit. 174$tempdir = tempdir(CLEANUP => 1); 175 176print "# TEMPDIR: $tempdir\n"; 177 178ok( (-d $tempdir), "Temp directory in temp dir" ); 179chdir $tempdir or die $!; 180push(@dirs, File::Spec->rel2abs($tempdir)); 181 182# Now END block will execute to test the removal of directories 183print "# End of tests. Execute END blocks in directory ". cwd() ."\n"; 184 185