YA resync with mainstem, including VMS patches from others
[p5sagit/p5-mst-13.2.git] / t / lib / ftmp-tempfile.t
CommitLineData
4b19af01 1#!/usr/bin/perl -w
2# Test for File::Temp - tempfile function
ee8c7f54 3
4BEGIN {
4b19af01 5 chdir 't' if -d 't';
6 unshift @INC, '../lib';
7 require Test; import Test;
8 plan(tests => 11);
ee8c7f54 9}
10
ee8c7f54 11use strict;
ee8c7f54 12use File::Spec;
ee8c7f54 13
14# Will need to check that all files were unlinked correctly
4b19af01 15# Set up an END block here to do it
16
17my (@files, @dirs); # Array containing list of dirs/files to test
ee8c7f54 18
19# Loop over an array hoping that the files dont exist
4b19af01 20END { foreach (@files) { ok( !(-e $_) )} }
ee8c7f54 21
22# And a test for directories
4b19af01 23END { foreach (@dirs) { ok( !(-d $_) )} }
24
25# Need to make sure that the END blocks are setup before
26# the ones that File::Temp configures since END blocks are evaluated
27# in revers order and we need to check the files *after* File::Temp
28# removes them
29use File::Temp qw/ tempfile tempdir/;
30
31# Now we start the tests properly
32ok(1);
ee8c7f54 33
34
35# Tempfile
36# Open tempfile in some directory, unlink at end
37my ($fh, $tempfile) = tempfile(
38 UNLINK => 1,
39 SUFFIX => '.txt',
40 );
41
42ok( (-f $tempfile) );
43push(@files, $tempfile);
44
45# TEMPDIR test
46# Create temp directory in current dir
47my $template = 'tmpdirXXXXXX';
48print "# Template: $template\n";
49my $tempdir = tempdir( $template ,
50 DIR => File::Spec->curdir,
51 CLEANUP => 1,
52 );
53
54print "# TEMPDIR: $tempdir\n";
55
56ok( (-d $tempdir) );
57push(@dirs, $tempdir);
58
59# Create file in the temp dir
60($fh, $tempfile) = tempfile(
61 DIR => $tempdir,
62 UNLINK => 1,
63 SUFFIX => '.dat',
64 );
65
66print "# TEMPFILE: Created $tempfile\n";
67
68ok( (-f $tempfile));
69push(@files, $tempfile);
70
71# Test tempfile
72# ..and again
73($fh, $tempfile) = tempfile(
74 DIR => $tempdir,
75 );
76
77
78ok( (-f $tempfile ));
79push(@files, $tempfile);
80
81print "# TEMPFILE: Created $tempfile\n";
82
83# and another (with template)
84
85($fh, $tempfile) = tempfile( 'helloXXXXXXX',
86 DIR => $tempdir,
87 UNLINK => 1,
88 SUFFIX => '.dat',
89 );
90
91print "# TEMPFILE: Created $tempfile\n";
92
93ok( (-f $tempfile) );
94push(@files, $tempfile);
95
4b19af01 96# Now END block will execute to test the removal of directories
97