Upgrade to File-Temp-0.19
[p5sagit/p5-mst-13.2.git] / lib / File / Temp / t / object.t
CommitLineData
4aed9cd6 1#!/usr/local/bin/perl -w
2# Test for File::Temp - OO interface
3
4use strict;
b0ad0448 5use Test::More tests => 30;
4aed9cd6 6use 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
12my (@files, @dirs, @still_there);
13
14# And a test for files that should still be around
15# These are tidied up
16END {
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
25END { foreach (@files) { ok( !(-e $_), "File $_ should not be there" )} }
26
27# And a test for directories
28END { 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
34BEGIN {use_ok( "File::Temp" ); }
35
36# Tempfile
37# Open tempfile in some directory, unlink at end
38my $fh = new File::Temp( SUFFIX => '.txt' );
39
40ok( (-f "$fh"), "File $fh exists" );
41# Should still be around after closing
42ok( close( $fh ), "Close file $fh" );
43ok( (-f "$fh"), "File $fh still exists after close" );
44# Check again at exit
45push(@files, "$fh");
46
b0ad0448 47# OO tempdir
48my $tdir = File::Temp->newdir();
49my $dirname = "$tdir"; # Stringify overload
50ok( -d $dirname, "Directory $tdir exists");
51undef $tdir;
52ok( !-d $dirname, "Directory should now be gone");
53
54# Quick basic tempfile test
55my $qfh = File::Temp->new();
56my $qfname = "$qfh";
57ok (-f $qfname, "temp file exists");
58undef $qfh;
59ok( !-f $qfname, "temp file now gone");
60
61
62# TEMPDIR test as somewhere to put the temp files
4aed9cd6 63# Create temp directory in current dir
64my $template = 'tmpdirXXXXXX';
65print "# Template: $template\n";
66my $tempdir = File::Temp::tempdir( $template ,
67 DIR => File::Spec->curdir,
68 CLEANUP => 1,
69 );
70
71print "# TEMPDIR: $tempdir\n";
72
73ok( (-d $tempdir), "Does $tempdir directory exist" );
74push(@dirs, $tempdir);
75
76# Create file in the temp dir
77$fh = new File::Temp(
78 DIR => $tempdir,
79 SUFFIX => '.dat',
80 );
81
05fb677a 82ok( $fh->unlink_on_destroy, "should unlink");
4aed9cd6 83print "# TEMPFILE: Created $fh\n";
84
85ok( (-f "$fh"), "File $fh exists in tempdir?");
86push(@files, "$fh");
87
88# Test tempfile
89# ..and again (without unlinking it)
90$fh = new File::Temp( DIR => $tempdir, UNLINK => 0 );
91
92print "# TEMPFILE: Created $fh\n";
93ok( (-f "$fh" ), "Second file $fh exists in tempdir [nounlink]?");
94push(@files, "$fh");
95
96# and another (with template)
97
98$fh = new File::Temp( TEMPLATE => 'helloXXXXXXX',
99 DIR => $tempdir,
100 SUFFIX => '.dat',
101 );
102
103print "# TEMPFILE: Created $fh\n";
104
105ok( (-f "$fh"), "File $fh exists? [from template]" );
106push(@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
113print "# TEMPFILE: Created $fh\n";
114ok( -f "$fh", "File $fh exists?" );
115ok( close( $fh ), "Close file $fh" );
05fb677a 116ok( ! $fh->unlink_on_destroy, "should not unlink");
4aed9cd6 117push( @still_there, "$fh"); # check at END
118
05fb677a 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
123print "# TEMPFILE: Created $fh\n";
124ok( -f "$fh", "File $fh exists?" );
125ok( close( $fh ), "Close file $fh" );
126ok( $fh->unlink_on_destroy, "should unlink (in principal)");
127push( @still_there, "$fh"); # check at END
128$File::Temp::KEEP_ALL = 1;
129
4aed9cd6 130# Make sure destructors run
131undef $fh;
132
05fb677a 133# allow end blocks to run
134$File::Temp::KEEP_ALL = 0;
135
4aed9cd6 136# Now END block will execute to test the removal of directories
137print "# End of tests. Execute END blocks\n";
138