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