1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#!./perl
BEGIN {
chdir 't' if -d 't';
unshift @INC, '../lib';
}
# Test for File::Temp - tempfile function
use strict;
use Test;
BEGIN { plan tests => 10}
use File::Spec;
use File::Temp qw/ tempfile tempdir/;
# Will need to check that all files were unlinked correctly
# Set up an END block here to do it (since the END blocks
# set up by File::Temp will be evaluated in reverse order we
# set ours up first....
# Loop over an array hoping that the files dont exist
my @files;
eval q{ END { foreach (@files) { ok( !(-e $_) )} } 1; } || die;
# And a test for directories
my @dirs;
eval q{ END { foreach (@dirs) { ok( !(-d $_) )} } 1; } || die;
# Tempfile
# Open tempfile in some directory, unlink at end
my ($fh, $tempfile) = tempfile(
UNLINK => 1,
SUFFIX => '.txt',
);
ok( (-f $tempfile) );
push(@files, $tempfile);
# TEMPDIR test
# Create temp directory in current dir
my $template = 'tmpdirXXXXXX';
print "# Template: $template\n";
my $tempdir = tempdir( $template ,
DIR => File::Spec->curdir,
CLEANUP => 1,
);
print "# TEMPDIR: $tempdir\n";
ok( (-d $tempdir) );
push(@dirs, $tempdir);
# Create file in the temp dir
($fh, $tempfile) = tempfile(
DIR => $tempdir,
UNLINK => 1,
SUFFIX => '.dat',
);
print "# TEMPFILE: Created $tempfile\n";
ok( (-f $tempfile));
push(@files, $tempfile);
# Test tempfile
# ..and again
($fh, $tempfile) = tempfile(
DIR => $tempdir,
);
ok( (-f $tempfile ));
push(@files, $tempfile);
print "# TEMPFILE: Created $tempfile\n";
# and another (with template)
($fh, $tempfile) = tempfile( 'helloXXXXXXX',
DIR => $tempdir,
UNLINK => 1,
SUFFIX => '.dat',
);
print "# TEMPFILE: Created $tempfile\n";
ok( (-f $tempfile) );
push(@files, $tempfile);
# no tests yet to make sure that the END{} blocks correctly remove
# the files
|