1 #!/usr/local/bin/perl -w
2 # Test for File::Temp - tempfile function
7 require Test; import Test;
14 # Will need to check that all files were unlinked correctly
15 # Set up an END block here to do it
17 # Arrays containing list of dirs/files to test
18 my (@files, @dirs, @still_there);
20 # And a test for files that should still be around
23 foreach (@still_there) {
30 # Loop over an array hoping that the files dont exist
31 END { foreach (@files) { ok( !(-e $_) )} }
33 # And a test for directories
34 END { foreach (@dirs) { ok( !(-d $_) )} }
36 # Need to make sure that the END blocks are setup before
37 # the ones that File::Temp configures since END blocks are evaluated
38 # in revers order and we need to check the files *after* File::Temp
40 use File::Temp qw/ tempfile tempdir/;
42 # Now we start the tests properly
47 # Open tempfile in some directory, unlink at end
48 my ($fh, $tempfile) = tempfile(
54 push(@files, $tempfile);
57 # Create temp directory in current dir
58 my $template = 'tmpdirXXXXXX';
59 print "# Template: $template\n";
60 my $tempdir = tempdir( $template ,
61 DIR => File::Spec->curdir,
65 print "# TEMPDIR: $tempdir\n";
68 push(@dirs, $tempdir);
70 # Create file in the temp dir
71 ($fh, $tempfile) = tempfile(
77 print "# TEMPFILE: Created $tempfile\n";
80 push(@files, $tempfile);
84 ($fh, $tempfile) = tempfile(
90 push(@files, $tempfile);
92 print "# TEMPFILE: Created $tempfile\n";
94 # and another (with template)
96 ($fh, $tempfile) = tempfile( 'helloXXXXXXX',
102 print "# TEMPFILE: Created $tempfile\n";
104 ok( (-f $tempfile) );
105 push(@files, $tempfile);
108 # Create a temporary file that should stay around after
110 ($fh, $tempfile) = tempfile( 'permXXXXXXX', UNLINK => 0 );
111 print "# TEMPFILE: Created $tempfile\n";
114 push( @still_there, $tempfile); # check at END
116 # Now END block will execute to test the removal of directories