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