Integrate with Sarathy.
[p5sagit/p5-mst-13.2.git] / t / lib / ftmp-tempfile.t
CommitLineData
262eb13a 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 unshift @INC, '../lib';
6}
7
8# Test for File::Temp - tempfile function
9
10use strict;
11use Test;
12BEGIN { plan tests => 10}
13use File::Spec;
14use 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
22my @files;
23eval q{ END { foreach (@files) { ok( !(-e $_) )} } 1; } || die;
24
25# And a test for directories
26my @dirs;
27eval q{ END { foreach (@dirs) { ok( !(-d $_) )} } 1; } || die;
28
29
30# Tempfile
31# Open tempfile in some directory, unlink at end
32my ($fh, $tempfile) = tempfile(
33 UNLINK => 1,
34 SUFFIX => '.txt',
35 );
36
37ok( (-f $tempfile) );
38push(@files, $tempfile);
39
40# TEMPDIR test
41# Create temp directory in current dir
42my $template = 'tmpdirXXXXXX';
43print "# Template: $template\n";
44my $tempdir = tempdir( $template ,
45 DIR => File::Spec->curdir,
46 CLEANUP => 1,
47 );
48
49print "# TEMPDIR: $tempdir\n";
50
51ok( (-d $tempdir) );
52push(@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
61print "# TEMPFILE: Created $tempfile\n";
62
63ok( (-f $tempfile));
64push(@files, $tempfile);
65
66# Test tempfile
67# ..and again
68($fh, $tempfile) = tempfile(
69 DIR => $tempdir,
70 );
71
72
73ok( (-f $tempfile ));
74push(@files, $tempfile);
75
76print "# 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
86print "# TEMPFILE: Created $tempfile\n";
87
88ok( (-f $tempfile) );
89push(@files, $tempfile);
90
91# no tests yet to make sure that the END{} blocks correctly remove
92# the files