1#!/usr/local/bin/perl -w
2# Test for File::Temp - OO interface
3
4use strict;
5use Test::More tests => 18;
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
47# TEMPDIR test
48# Create temp directory in current dir
49my $template = 'tmpdirXXXXXX';
50print "# Template: $template\n";
51my $tempdir = File::Temp::tempdir( $template ,
52				   DIR => File::Spec->curdir,
53				   CLEANUP => 1,
54				 );
55
56print "# TEMPDIR: $tempdir\n";
57
58ok( (-d $tempdir), "Does $tempdir directory exist" );
59push(@dirs, $tempdir);
60
61# Create file in the temp dir
62$fh = new File::Temp(
63		     DIR => $tempdir,
64		     SUFFIX => '.dat',
65		    );
66
67print "# TEMPFILE: Created $fh\n";
68
69ok( (-f "$fh"), "File $fh exists in tempdir?");
70push(@files, "$fh");
71
72# Test tempfile
73# ..and again (without unlinking it)
74$fh = new File::Temp( DIR => $tempdir, UNLINK => 0 );
75
76print "# TEMPFILE: Created $fh\n";
77ok( (-f "$fh" ), "Second file $fh exists in tempdir [nounlink]?");
78push(@files, "$fh");
79
80# and another (with template)
81
82$fh = new File::Temp( TEMPLATE => 'helloXXXXXXX',
83		      DIR => $tempdir,
84		      SUFFIX => '.dat',
85		    );
86
87print "# TEMPFILE: Created $fh\n";
88
89ok( (-f "$fh"), "File $fh exists? [from template]" );
90push(@files, "$fh");
91
92
93# Create a temporary file that should stay around after
94# it has been closed
95$fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 0);
96
97print "# TEMPFILE: Created $fh\n";
98ok( -f "$fh", "File $fh exists?" );
99ok( close( $fh ), "Close file $fh" );
100push( @still_there, "$fh"); # check at END
101
102# Make sure destructors run
103undef $fh;
104
105# Now END block will execute to test the removal of directories
106print "# End of tests. Execute END blocks\n";
107
108