Upgrade to File-Temp-0.19
[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 => 30;
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 # OO tempdir
48 my $tdir = File::Temp->newdir();
49 my $dirname = "$tdir"; # Stringify overload
50 ok( -d $dirname, "Directory $tdir exists");
51 undef $tdir;
52 ok( !-d $dirname, "Directory should now be gone");
53
54 # Quick basic tempfile test
55 my $qfh = File::Temp->new();
56 my $qfname = "$qfh";
57 ok (-f $qfname, "temp file exists");
58 undef $qfh;
59 ok( !-f $qfname, "temp file now gone");
60
61
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,
68                                    CLEANUP => 1,
69                                  );
70
71 print "# TEMPDIR: $tempdir\n";
72
73 ok( (-d $tempdir), "Does $tempdir directory exist" );
74 push(@dirs, $tempdir);
75
76 # Create file in the temp dir
77 $fh = new File::Temp(
78                      DIR => $tempdir,
79                      SUFFIX => '.dat',
80                     );
81
82 ok( $fh->unlink_on_destroy, "should unlink");
83 print "# TEMPFILE: Created $fh\n";
84
85 ok( (-f "$fh"), "File $fh exists in tempdir?");
86 push(@files, "$fh");
87
88 # Test tempfile
89 # ..and again (without unlinking it)
90 $fh = new File::Temp( DIR => $tempdir, UNLINK => 0 );
91
92 print "# TEMPFILE: Created $fh\n";
93 ok( (-f "$fh" ), "Second file $fh exists in tempdir [nounlink]?");
94 push(@files, "$fh");
95
96 # and another (with template)
97
98 $fh = new File::Temp( TEMPLATE => 'helloXXXXXXX',
99                       DIR => $tempdir,
100                       SUFFIX => '.dat',
101                     );
102
103 print "# TEMPFILE: Created $fh\n";
104
105 ok( (-f "$fh"), "File $fh exists? [from template]" );
106 push(@files, "$fh");
107
108
109 # Create a temporary file that should stay around after
110 # it has been closed
111 $fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 0);
112
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
118
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);
122
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;
129
130 # Make sure destructors run
131 undef $fh;
132
133 # allow end blocks to run
134 $File::Temp::KEEP_ALL = 0;
135
136 # Now END block will execute to test the removal of directories
137 print "# End of tests. Execute END blocks\n";
138