1b39c5158Smillert#!/usr/local/bin/perl -w 2b39c5158Smillert# Test for File::Temp - tempfile function 3b39c5158Smillert 4b39c5158Smillertuse strict; 5*256a93a4Safresh1use Test::More tests => 30; 6b39c5158Smillertuse File::Spec; 791f110e0Safresh1use Cwd qw/ cwd /; 8b39c5158Smillert 9b39c5158Smillert# Will need to check that all files were unlinked correctly 10b39c5158Smillert# Set up an END block here to do it 11b39c5158Smillert 12b39c5158Smillert# Arrays containing list of dirs/files to test 13b39c5158Smillertmy (@files, @dirs, @still_there); 14b39c5158Smillert 15b39c5158Smillert# And a test for files that should still be around 16b39c5158Smillert# These are tidied up 17b39c5158SmillertEND { 18b39c5158Smillert foreach (@still_there) { 1991f110e0Safresh1 ($_) = /(^.*)/; # untaint for testing under taint mode 2091f110e0Safresh1 ok( -f $_, "File $_ is still present" ); 2191f110e0Safresh1 ok( unlink( $_ ), "Unlink file" ); 2291f110e0Safresh1 ok( !(-f $_), "File is no longer present" ); 23b39c5158Smillert } 24b39c5158Smillert} 25b39c5158Smillert 26b39c5158Smillert# Loop over an array hoping that the files dont exist 2791f110e0Safresh1END { foreach (@files) { ok( !(-e $_), "File $_ should not be present" )} } 28b39c5158Smillert 29b39c5158Smillert# And a test for directories 3091f110e0Safresh1END { foreach (@dirs) { ok( !(-d $_), "Dir $_ should not be present" )} } 31b39c5158Smillert 32b39c5158Smillert# Need to make sure that the END blocks are setup before 33b39c5158Smillert# the ones that File::Temp configures since END blocks are evaluated 34b39c5158Smillert# in revers order and we need to check the files *after* File::Temp 35b39c5158Smillert# removes them 36b39c5158Smillertuse File::Temp qw/ tempfile tempdir/; 37b39c5158Smillert 38b39c5158Smillert# Now we start the tests properly 3991f110e0Safresh1ok(1, "Start test"); 40b39c5158Smillert 41b39c5158Smillert 42b39c5158Smillert# Tempfile 43b39c5158Smillert# Open tempfile in some directory, unlink at end 44b39c5158Smillertmy ($fh, $tempfile) = tempfile( 45b39c5158Smillert UNLINK => 1, 46b39c5158Smillert SUFFIX => '.txt', 47b39c5158Smillert ); 48b39c5158Smillert 4991f110e0Safresh1ok( (-f $tempfile), "Tempfile exists" ); 50b39c5158Smillert# Should still be around after closing 5191f110e0Safresh1ok( close( $fh ), "Tempfile closed" ); 5291f110e0Safresh1ok( (-f $tempfile), "Tempfile exists" ); 53b39c5158Smillert# Check again at exit 54b39c5158Smillertpush(@files, $tempfile); 55b39c5158Smillert 56b39c5158Smillert# TEMPDIR test 57b39c5158Smillert# Create temp directory in current dir 58b39c5158Smillertmy $template = 'tmpdirXXXXXX'; 59b39c5158Smillertprint "# Template: $template\n"; 60b39c5158Smillertmy $tempdir = tempdir( $template , 61b39c5158Smillert DIR => File::Spec->curdir, 62b39c5158Smillert CLEANUP => 1, 63b39c5158Smillert ); 64b39c5158Smillert 65b39c5158Smillertprint "# TEMPDIR: $tempdir\n"; 66b39c5158Smillert 6791f110e0Safresh1ok( (-d $tempdir), "Local tempdir exists" ); 6891f110e0Safresh1push(@dirs, File::Spec->rel2abs($tempdir)); 6991f110e0Safresh1 7091f110e0Safresh1my $tempdir2 = tempdir( TEMPLATE => "customXXXXX", 7191f110e0Safresh1 DIR => File::Spec->curdir, 7291f110e0Safresh1 CLEANUP => 1, 7391f110e0Safresh1 ); 7491f110e0Safresh1 7591f110e0Safresh1print "# TEMPDIR2: $tempdir2\n"; 7691f110e0Safresh1 7791f110e0Safresh1like( $tempdir2, qr/custom/, "tempdir with TEMPLATE" ); 7891f110e0Safresh1push(@dirs, File::Spec->rel2abs($tempdir)); 79b39c5158Smillert 80b39c5158Smillert# Create file in the temp dir 81b39c5158Smillert($fh, $tempfile) = tempfile( 82b39c5158Smillert DIR => $tempdir, 83b39c5158Smillert UNLINK => 1, 84b39c5158Smillert SUFFIX => '.dat', 85b39c5158Smillert ); 86b39c5158Smillert 87b39c5158Smillertprint "# TEMPFILE: Created $tempfile\n"; 88b39c5158Smillert 8991f110e0Safresh1ok( (-f $tempfile), "Local temp file exists with .dat extension"); 9091f110e0Safresh1push(@files, File::Spec->rel2abs($tempfile)); 91b39c5158Smillert 92b39c5158Smillert# Test tempfile 93b39c5158Smillert# ..and again 94b39c5158Smillert($fh, $tempfile) = tempfile( 95b39c5158Smillert DIR => $tempdir, 96b39c5158Smillert ); 97b39c5158Smillert 98b39c5158Smillert 9991f110e0Safresh1ok( (-f $tempfile ), "Local tempfile in tempdir exists"); 10091f110e0Safresh1push(@files, File::Spec->rel2abs($tempfile)); 101b39c5158Smillert 102b39c5158Smillert# Test tempfile 103*256a93a4Safresh1# ..and another with default permissions 104b39c5158Smillert($fh, $tempfile) = tempfile( 105b39c5158Smillert DIR => $tempdir, 106b39c5158Smillert ); 107b39c5158Smillert 108*256a93a4Safresh1# From perlport on chmod: 109*256a93a4Safresh1# 110*256a93a4Safresh1# (Win32) Only good for changing "owner" read-write access; 111*256a93a4Safresh1# "group" and "other" bits are meaningless. 112*256a93a4Safresh1# 113*256a93a4Safresh1# So we don't check the full permissions -- it will be 0444 on Win32 114*256a93a4Safresh1# instead of 0400. Instead, just check the owner bits. 115*256a93a4Safresh1 116*256a93a4Safresh1is((stat($tempfile))[2] & 00700, 0600, 'created tempfile with default permissions'); 117*256a93a4Safresh1push(@files, File::Spec->rel2abs($tempfile)); 118*256a93a4Safresh1 119*256a93a4Safresh1# Test tempfile 120*256a93a4Safresh1# ..and another with changed permissions 121*256a93a4Safresh1($fh, $tempfile) = tempfile( 122*256a93a4Safresh1 DIR => $tempdir, 123*256a93a4Safresh1 PERMS => 0400, 124*256a93a4Safresh1 ); 125*256a93a4Safresh1 126*256a93a4Safresh1is((stat($tempfile))[2] & 00700, 0400, 'created tempfile with changed permissions'); 12791f110e0Safresh1push(@files, File::Spec->rel2abs($tempfile)); 128b39c5158Smillert 129b39c5158Smillertprint "# TEMPFILE: Created $tempfile\n"; 130b39c5158Smillert 131b39c5158Smillert# and another (with template) 132b39c5158Smillert 133b39c5158Smillert($fh, $tempfile) = tempfile( 'helloXXXXXXX', 134b39c5158Smillert DIR => $tempdir, 135b39c5158Smillert UNLINK => 1, 136b39c5158Smillert SUFFIX => '.dat', 137b39c5158Smillert ); 138b39c5158Smillert 139b39c5158Smillertprint "# TEMPFILE: Created $tempfile\n"; 140b39c5158Smillert 14191f110e0Safresh1ok( (-f $tempfile), "Local tempfile in tempdir with .dat extension exists" ); 14291f110e0Safresh1push(@files, File::Spec->rel2abs($tempfile)); 143b39c5158Smillert 144b39c5158Smillert 14591f110e0Safresh1# and another (with TEMPLATE) 14691f110e0Safresh1 14791f110e0Safresh1($fh, $tempfile) = tempfile( TEMPLATE => 'goodbyeXXXXXXX', 14891f110e0Safresh1 DIR => $tempdir, 14991f110e0Safresh1 UNLINK => 1, 15091f110e0Safresh1 SUFFIX => '.dat', 15191f110e0Safresh1 ); 15291f110e0Safresh1 15391f110e0Safresh1print "# TEMPFILE: Created $tempfile\n"; 15491f110e0Safresh1 15591f110e0Safresh1ok( (-f $tempfile), "Local tempfile in tempdir with TEMPLATE" ); 15691f110e0Safresh1push(@files, File::Spec->rel2abs($tempfile)); 15791f110e0Safresh1 158b39c5158Smillert# Create a temporary file that should stay around after 159b39c5158Smillert# it has been closed 160b39c5158Smillert($fh, $tempfile) = tempfile( 'permXXXXXXX', UNLINK => 0 ); 161b39c5158Smillertprint "# TEMPFILE: Created $tempfile\n"; 16291f110e0Safresh1ok( -f $tempfile, "Long-lived temp file" ); 16391f110e0Safresh1ok( close( $fh ), "Close long-lived temp file" ); 16491f110e0Safresh1push( @still_there, File::Spec->rel2abs($tempfile) ); # check at END 165b39c5158Smillert 166b39c5158Smillert# Would like to create a temp file and just retrieve the handle 167b39c5158Smillert# but the test is problematic since: 168b39c5158Smillert# - We dont know the filename so we cant check that it is tidied 169b39c5158Smillert# correctly 170b39c5158Smillert# - The unlink0 required on unix for tempfile creation will fail 171b39c5158Smillert# on NFS 172b39c5158Smillert# Try to do what we can. 173b39c5158Smillert# Tempfile croaks on error so we need an eval 174f3efcd01Safresh1$fh = eval { tempfile( 'ftmpXXXXX', DIR => File::Temp::_wrap_file_spec_tmpdir() ) }; 175b39c5158Smillert 176b39c5158Smillertif ($fh) { 177b39c5158Smillert 178b39c5158Smillert # print something to it to make sure something is there 17991f110e0Safresh1 ok( print($fh "Test\n"), "Write to temp file" ); 180b39c5158Smillert 181b39c5158Smillert # Close it - can not check it is gone since we dont know the name 18291f110e0Safresh1 ok( close($fh), "Close temp file" ); 183b39c5158Smillert 184b39c5158Smillert} else { 185f3efcd01Safresh1 SKIP: { 186f3efcd01Safresh1 skip "Skip Failed probably due to NFS", 2; 187f3efcd01Safresh1 } 188b39c5158Smillert} 189b39c5158Smillert 19091f110e0Safresh1# Create temp directory and chdir to it; it should still be removed on exit. 19191f110e0Safresh1$tempdir = tempdir(CLEANUP => 1); 19291f110e0Safresh1 19391f110e0Safresh1print "# TEMPDIR: $tempdir\n"; 19491f110e0Safresh1 19591f110e0Safresh1ok( (-d $tempdir), "Temp directory in temp dir" ); 19691f110e0Safresh1chdir $tempdir or die $!; 19791f110e0Safresh1push(@dirs, File::Spec->rel2abs($tempdir)); 19891f110e0Safresh1 199b39c5158Smillert# Now END block will execute to test the removal of directories 20091f110e0Safresh1print "# End of tests. Execute END blocks in directory ". cwd() ."\n"; 201b39c5158Smillert 202