Commit | Line | Data |
51fc852f |
1 | #!/usr/local/bin/perl -w |
262eb13a |
2 | # Test for File::Temp - tempfile function |
3 | |
e2cf2bdb |
4 | BEGIN { |
5 | chdir 't' if -d 't'; |
20822f61 |
6 | @INC = '../lib'; |
e2cf2bdb |
7 | require Test; import Test; |
51fc852f |
8 | plan(tests => 16); |
e2cf2bdb |
9 | } |
10 | |
262eb13a |
11 | use strict; |
262eb13a |
12 | use File::Spec; |
262eb13a |
13 | |
14 | # Will need to check that all files were unlinked correctly |
51fc852f |
15 | # Set up an END block here to do it |
16 | |
17 | # Arrays containing list of dirs/files to test |
18 | my (@files, @dirs, @still_there); |
19 | |
20 | # And a test for files that should still be around |
21 | # These are tidied up |
22 | END { |
23 | foreach (@still_there) { |
24 | ok( -f $_ ); |
25 | ok( unlink( $_ ) ); |
26 | ok( !(-f $_) ); |
27 | } |
28 | } |
262eb13a |
29 | |
30 | # Loop over an array hoping that the files dont exist |
1c19c868 |
31 | END { foreach (@files) { ok( !(-e $_) )} } |
262eb13a |
32 | |
33 | # And a test for directories |
51fc852f |
34 | END { foreach (@dirs) { ok( !(-d $_) )} } |
1c19c868 |
35 | |
36 | # Need to make sure that the END blocks are setup before |
37 | # the ones that File::Temp configures since END blocks are evaluated |
38 | # in revers order and we need to check the files *after* File::Temp |
39 | # removes them |
40 | use File::Temp qw/ tempfile tempdir/; |
41 | |
42 | # Now we start the tests properly |
43 | ok(1); |
262eb13a |
44 | |
45 | |
46 | # Tempfile |
47 | # Open tempfile in some directory, unlink at end |
48 | my ($fh, $tempfile) = tempfile( |
49 | UNLINK => 1, |
50 | SUFFIX => '.txt', |
51 | ); |
52 | |
53 | ok( (-f $tempfile) ); |
54 | push(@files, $tempfile); |
55 | |
56 | # TEMPDIR test |
57 | # Create temp directory in current dir |
58 | my $template = 'tmpdirXXXXXX'; |
59 | print "# Template: $template\n"; |
60 | my $tempdir = tempdir( $template , |
61 | DIR => File::Spec->curdir, |
62 | CLEANUP => 1, |
63 | ); |
64 | |
65 | print "# TEMPDIR: $tempdir\n"; |
66 | |
67 | ok( (-d $tempdir) ); |
68 | push(@dirs, $tempdir); |
69 | |
70 | # Create file in the temp dir |
71 | ($fh, $tempfile) = tempfile( |
72 | DIR => $tempdir, |
73 | UNLINK => 1, |
74 | SUFFIX => '.dat', |
75 | ); |
76 | |
77 | print "# TEMPFILE: Created $tempfile\n"; |
78 | |
79 | ok( (-f $tempfile)); |
80 | push(@files, $tempfile); |
81 | |
82 | # Test tempfile |
83 | # ..and again |
84 | ($fh, $tempfile) = tempfile( |
85 | DIR => $tempdir, |
86 | ); |
87 | |
88 | |
89 | ok( (-f $tempfile )); |
90 | push(@files, $tempfile); |
91 | |
92 | print "# TEMPFILE: Created $tempfile\n"; |
93 | |
94 | # and another (with template) |
95 | |
96 | ($fh, $tempfile) = tempfile( 'helloXXXXXXX', |
97 | DIR => $tempdir, |
98 | UNLINK => 1, |
99 | SUFFIX => '.dat', |
100 | ); |
101 | |
102 | print "# TEMPFILE: Created $tempfile\n"; |
103 | |
104 | ok( (-f $tempfile) ); |
105 | push(@files, $tempfile); |
106 | |
51fc852f |
107 | |
108 | # Create a temporary file that should stay around after |
109 | # it has been closed |
110 | ($fh, $tempfile) = tempfile( 'permXXXXXXX', UNLINK => 0 ); |
111 | print "# TEMPFILE: Created $tempfile\n"; |
112 | ok( -f $tempfile ); |
113 | ok( close( $fh ) ); |
114 | push( @still_there, $tempfile); # check at END |
115 | |
1c19c868 |
116 | # Now END block will execute to test the removal of directories |
117 | |