Upgrade to File::Temp 0.16
[p5sagit/p5-mst-13.2.git] / lib / File / Temp / t / object.t
1 #!/usr/local/bin/perl -w
2 # Test for File::Temp - OO interface
3
4 use strict;
5 use Test::More tests => 26;
6 use File::Spec;
7
8 # Will need to check that all files were unlinked correctly
9 # Set up an END block here to do it
10
11 # Arrays containing list of dirs/files to test
12 my (@files, @dirs, @still_there);
13
14 # And a test for files that should still be around
15 # These are tidied up
16 END {
17   foreach (@still_there) {
18     ok( -f $_, "Check $_ exists" );
19     ok( unlink( $_ ), "Unlinked $_" );
20     ok( !(-f $_), "$_ no longer there");
21   }
22 }
23
24 # Loop over an array hoping that the files dont exist
25 END { foreach (@files) { ok( !(-e $_), "File $_ should not be there" )} }
26
27 # And a test for directories
28 END { foreach (@dirs)  { ok( !(-d $_), "Directory $_ should not be there" ) } }
29
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
33 # removes them
34 BEGIN {use_ok( "File::Temp" ); }
35
36 # Tempfile
37 # Open tempfile in some directory, unlink at end
38 my $fh = new File::Temp( SUFFIX => '.txt' );
39
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" );
44 # Check again at exit
45 push(@files, "$fh");
46
47 # TEMPDIR test
48 # Create temp directory in current dir
49 my $template = 'tmpdirXXXXXX';
50 print "# Template: $template\n";
51 my $tempdir = File::Temp::tempdir( $template ,
52                                    DIR => File::Spec->curdir,
53                                    CLEANUP => 1,
54                                  );
55
56 print "# TEMPDIR: $tempdir\n";
57
58 ok( (-d $tempdir), "Does $tempdir directory exist" );
59 push(@dirs, $tempdir);
60
61 # Create file in the temp dir
62 $fh = new File::Temp(
63                      DIR => $tempdir,
64                      SUFFIX => '.dat',
65                     );
66
67 ok( $fh->unlink_on_destroy, "should unlink");
68 print "# TEMPFILE: Created $fh\n";
69
70 ok( (-f "$fh"), "File $fh exists in tempdir?");
71 push(@files, "$fh");
72
73 # Test tempfile
74 # ..and again (without unlinking it)
75 $fh = new File::Temp( DIR => $tempdir, UNLINK => 0 );
76
77 print "# TEMPFILE: Created $fh\n";
78 ok( (-f "$fh" ), "Second file $fh exists in tempdir [nounlink]?");
79 push(@files, "$fh");
80
81 # and another (with template)
82
83 $fh = new File::Temp( TEMPLATE => 'helloXXXXXXX',
84                       DIR => $tempdir,
85                       SUFFIX => '.dat',
86                     );
87
88 print "# TEMPFILE: Created $fh\n";
89
90 ok( (-f "$fh"), "File $fh exists? [from template]" );
91 push(@files, "$fh");
92
93
94 # Create a temporary file that should stay around after
95 # it has been closed
96 $fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 0);
97
98 print "# TEMPFILE: Created $fh\n";
99 ok( -f "$fh", "File $fh exists?" );
100 ok( close( $fh ), "Close file $fh" );
101 ok( ! $fh->unlink_on_destroy, "should not unlink");
102 push( @still_there, "$fh"); # check at END
103
104 # Now create a temp file that will remain when the object
105 # goes out of scope because of $KEEP_ALL
106 $fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 1);
107
108 print "# TEMPFILE: Created $fh\n";
109 ok( -f "$fh", "File $fh exists?" );
110 ok( close( $fh ), "Close file $fh" );
111 ok( $fh->unlink_on_destroy, "should unlink (in principal)");
112 push( @still_there, "$fh"); # check at END
113 $File::Temp::KEEP_ALL = 1;
114
115 # Make sure destructors run
116 undef $fh;
117
118 # allow end blocks to run
119 $File::Temp::KEEP_ALL = 0;
120
121 # Now END block will execute to test the removal of directories
122 print "# End of tests. Execute END blocks\n";
123