Add File::Temp's OO test.
authorJarkko Hietaniemi <jhi@iki.fi>
Fri, 22 Aug 2003 04:46:01 +0000 (04:46 +0000)
committerJarkko Hietaniemi <jhi@iki.fi>
Fri, 22 Aug 2003 04:46:01 +0000 (04:46 +0000)
p4raw-id: //depot/perl@20810

MANIFEST
lib/File/Temp/t/object.t [new file with mode: 0644]

index a8935e0..47d7111 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -1208,6 +1208,7 @@ lib/File/stat.pm          By-name interface to Perl's builtin stat
 lib/File/stat.t                        See if File::stat works
 lib/File/Temp.pm               create safe temporary files and file handles
 lib/File/Temp/t/mktemp.t       See if File::Temp works
+lib/File/Temp/t/object.t       See if File::Temp works
 lib/File/Temp/t/posix.t                See if File::Temp works
 lib/File/Temp/t/security.t     See if File::Temp works
 lib/File/Temp/t/tempfile.t     See if File::Temp works
diff --git a/lib/File/Temp/t/object.t b/lib/File/Temp/t/object.t
new file mode 100644 (file)
index 0000000..5828866
--- /dev/null
@@ -0,0 +1,107 @@
+#!/usr/local/bin/perl -w
+# Test for File::Temp - OO interface
+
+use strict;
+use Test::More tests => 18;
+use File::Spec;
+
+# Will need to check that all files were unlinked correctly
+# Set up an END block here to do it
+
+# Arrays containing list of dirs/files to test
+my (@files, @dirs, @still_there);
+
+# And a test for files that should still be around
+# These are tidied up
+END {
+  foreach (@still_there) {
+    ok( -f $_, "Check $_ exists" );
+    ok( unlink( $_ ), "Unlinked $_" );
+    ok( !(-f $_), "$_ no longer there");
+  }
+}
+
+# Loop over an array hoping that the files dont exist
+END { foreach (@files) { ok( !(-e $_), "File $_ should not be there" )} }
+
+# And a test for directories
+END { foreach (@dirs)  { ok( !(-d $_), "Directory $_ should not be there" ) } }
+
+# Need to make sure that the END blocks are setup before
+# the ones that File::Temp configures since END blocks are evaluated
+# in reverse order and we need to check the files *after* File::Temp
+# removes them
+BEGIN {use_ok( "File::Temp" ); }
+
+# Tempfile
+# Open tempfile in some directory, unlink at end
+my $fh = new File::Temp( SUFFIX => '.txt' );
+
+ok( (-f "$fh"), "File $fh exists"  );
+# Should still be around after closing
+ok( close( $fh ), "Close file $fh" );
+ok( (-f "$fh"), "File $fh still exists after close" );
+# Check again at exit
+push(@files, "$fh");
+
+# TEMPDIR test
+# Create temp directory in current dir
+my $template = 'tmpdirXXXXXX';
+print "# Template: $template\n";
+my $tempdir = File::Temp::tempdir( $template ,
+                                  DIR => File::Spec->curdir,
+                                  CLEANUP => 1,
+                                );
+
+print "# TEMPDIR: $tempdir\n";
+
+ok( (-d $tempdir), "Does $tempdir directory exist" );
+push(@dirs, $tempdir);
+
+# Create file in the temp dir
+$fh = new File::Temp(
+                    DIR => $tempdir,
+                    SUFFIX => '.dat',
+                   );
+
+print "# TEMPFILE: Created $fh\n";
+
+ok( (-f "$fh"), "File $fh exists in tempdir?");
+push(@files, "$fh");
+
+# Test tempfile
+# ..and again (without unlinking it)
+$fh = new File::Temp( DIR => $tempdir, UNLINK => 0 );
+
+print "# TEMPFILE: Created $fh\n";
+ok( (-f "$fh" ), "Second file $fh exists in tempdir [nounlink]?");
+push(@files, "$fh");
+
+# and another (with template)
+
+$fh = new File::Temp( TEMPLATE => 'helloXXXXXXX',
+                     DIR => $tempdir,
+                     SUFFIX => '.dat',
+                   );
+
+print "# TEMPFILE: Created $fh\n";
+
+ok( (-f "$fh"), "File $fh exists? [from template]" );
+push(@files, "$fh");
+
+
+# Create a temporary file that should stay around after
+# it has been closed
+$fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 0);
+
+print "# TEMPFILE: Created $fh\n";
+ok( -f "$fh", "File $fh exists?" );
+ok( close( $fh ), "Close file $fh" );
+push( @still_there, "$fh"); # check at END
+
+# Make sure destructors run
+undef $fh;
+
+# Now END block will execute to test the removal of directories
+print "# End of tests. Execute END blocks\n";
+