Commit | Line | Data |
262eb13a |
1 | #!./perl |
2 | |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
5 | unshift @INC, '../lib'; |
6 | } |
7 | |
8 | # Test for File::Temp - tempfile function |
9 | |
10 | use strict; |
11 | use Test; |
12 | BEGIN { plan tests => 10} |
13 | use File::Spec; |
14 | use File::Temp qw/ tempfile tempdir/; |
15 | |
16 | # Will need to check that all files were unlinked correctly |
17 | # Set up an END block here to do it (since the END blocks |
18 | # set up by File::Temp will be evaluated in reverse order we |
19 | # set ours up first.... |
20 | |
21 | # Loop over an array hoping that the files dont exist |
22 | my @files; |
23 | eval q{ END { foreach (@files) { ok( !(-e $_) )} } 1; } || die; |
24 | |
25 | # And a test for directories |
26 | my @dirs; |
27 | eval q{ END { foreach (@dirs) { ok( !(-d $_) )} } 1; } || die; |
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 | # no tests yet to make sure that the END{} blocks correctly remove |
92 | # the files |