1 #!/usr/local/bin/perl -w
2 # Test for File::Temp - OO interface
5 use Test::More tests => 30;
8 # Will need to check that all files were unlinked correctly
9 # Set up an END block here to do it
11 # Arrays containing list of dirs/files to test
12 my (@files, @dirs, @still_there);
14 # And a test for files that should still be around
17 foreach (@still_there) {
18 ok( -f $_, "Check $_ exists" );
19 ok( unlink( $_ ), "Unlinked $_" );
20 ok( !(-f $_), "$_ no longer there");
24 # Loop over an array hoping that the files dont exist
25 END { foreach (@files) { ok( !(-e $_), "File $_ should not be there" )} }
27 # And a test for directories
28 END { foreach (@dirs) { ok( !(-d $_), "Directory $_ should not be there" ) } }
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
34 BEGIN {use_ok( "File::Temp" ); }
37 # Open tempfile in some directory, unlink at end
38 my $fh = new File::Temp( SUFFIX => '.txt' );
40 ok( (-f "$fh"), "File $fh exists" );
41 # Should still be around after closing
42 ok( close( $fh ), "Close file $fh" );
43 ok( (-f "$fh"), "File $fh still exists after close" );
48 my $tdir = File::Temp->newdir();
49 my $dirname = "$tdir"; # Stringify overload
50 ok( -d $dirname, "Directory $tdir exists");
52 ok( !-d $dirname, "Directory should now be gone");
54 # Quick basic tempfile test
55 my $qfh = File::Temp->new();
57 ok (-f $qfname, "temp file exists");
59 ok( !-f $qfname, "temp file now gone");
62 # TEMPDIR test as somewhere to put the temp files
63 # Create temp directory in current dir
64 my $template = 'tmpdirXXXXXX';
65 print "# Template: $template\n";
66 my $tempdir = File::Temp::tempdir( $template ,
67 DIR => File::Spec->curdir,
71 print "# TEMPDIR: $tempdir\n";
73 ok( (-d $tempdir), "Does $tempdir directory exist" );
74 push(@dirs, $tempdir);
76 # Create file in the temp dir
82 ok( $fh->unlink_on_destroy, "should unlink");
83 print "# TEMPFILE: Created $fh\n";
85 ok( (-f "$fh"), "File $fh exists in tempdir?");
89 # ..and again (without unlinking it)
90 $fh = new File::Temp( DIR => $tempdir, UNLINK => 0 );
92 print "# TEMPFILE: Created $fh\n";
93 ok( (-f "$fh" ), "Second file $fh exists in tempdir [nounlink]?");
96 # and another (with template)
98 $fh = new File::Temp( TEMPLATE => 'helloXXXXXXX',
103 print "# TEMPFILE: Created $fh\n";
105 ok( (-f "$fh"), "File $fh exists? [from template]" );
109 # Create a temporary file that should stay around after
111 $fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 0);
113 print "# TEMPFILE: Created $fh\n";
114 ok( -f "$fh", "File $fh exists?" );
115 ok( close( $fh ), "Close file $fh" );
116 ok( ! $fh->unlink_on_destroy, "should not unlink");
117 push( @still_there, "$fh"); # check at END
119 # Now create a temp file that will remain when the object
120 # goes out of scope because of $KEEP_ALL
121 $fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 1);
123 print "# TEMPFILE: Created $fh\n";
124 ok( -f "$fh", "File $fh exists?" );
125 ok( close( $fh ), "Close file $fh" );
126 ok( $fh->unlink_on_destroy, "should unlink (in principal)");
127 push( @still_there, "$fh"); # check at END
128 $File::Temp::KEEP_ALL = 1;
130 # Make sure destructors run
133 # allow end blocks to run
134 $File::Temp::KEEP_ALL = 0;
136 # Now END block will execute to test the removal of directories
137 print "# End of tests. Execute END blocks\n";