Upgrade to File::Temp 0.08 from Tim Jenness via CPAN.
[p5sagit/p5-mst-13.2.git] / t / lib / ftmp-mktemp.t
CommitLineData
1c19c868 1#!/usr/local/bin/perl -w
262eb13a 2
3# Test for mktemp family of commands in File::Temp
4# Use STANDARD safe level for these tests
5
6use strict;
7use Test;
8BEGIN { plan tests => 9 }
9
10use File::Spec;
11use File::Path;
12use File::Temp qw/ :mktemp unlink0 /;
13
14ok(1);
15
16# MKSTEMP - test
17
18# Create file in temp directory
19my $template = File::Spec->catfile(File::Spec->tmpdir, 'wowserXXXX');
20
21(my $fh, $template) = mkstemp($template);
22
23print "# MKSTEMP: FH is $fh File is $template fileno=".fileno($fh)."\n";
24# Check if the file exists
25ok( (-e $template) );
26
27# Autoflush
28$fh->autoflush(1) if $] >= 5.006;
29
30# Try printing something to the file
31my $string = "woohoo\n";
32print $fh $string;
33
34# rewind the file
35ok(seek( $fh, 0, 0));
36
37# Read from the file
38my $line = <$fh>;
39
40# compare with previous string
41ok($string, $line);
42
43# Tidy up
44# This test fails on Windows NT since it seems that the size returned by
45# stat(filehandle) does not always equal the size of the stat(filename)
46# This must be due to caching. In particular this test writes 7 bytes
47# to the file which are not recognised by stat(filename)
1c19c868 48# Simply waiting 3 seconds seems to be enough for the system to update
262eb13a 49
50if ($^O eq 'MSWin32') {
51 sleep 3;
52}
53ok( unlink0($fh, $template) );
54
55
56# MKSTEMPS
57# File with suffix. This is created in the current directory
58
59$template = "suffixXXXXXX";
60my $suffix = ".dat";
61
62($fh, my $fname) = mkstemps($template, $suffix);
63
64print "# MKSTEMPS: File is $template -> $fname fileno=".fileno($fh)."\n";
65# Check if the file exists
66ok( (-e $fname) );
67
1c19c868 68# This fails if you are running on NFS
69# If this test fails simply skip it rather than doing a hard failure
70my $status = unlink0($fh, $fname);
262eb13a 71
1c19c868 72if ($status) {
73 ok($status);
74} else {
75 skip("Skip test failed probably due to NFS",1)
76}
262eb13a 77
78# MKDTEMP
79# Temp directory
80
81$template = File::Spec->catdir(File::Spec->tmpdir, 'tmpdirXXXXXX');
82
83my $tmpdir = mkdtemp($template);
84
85print "# MKDTEMP: Name is $tmpdir from template $template\n";
86
87ok( (-d $tmpdir ) );
88
89# Need to tidy up after myself
90rmtree($tmpdir);
91
92# MKTEMP
93# Just a filename, not opened
94
95$template = File::Spec->catfile(File::Spec->tmpdir, 'mytestXXXXXX');
96
97my $tmpfile = mktemp($template);
98
99print "# MKTEMP: Tempfile is $template -> $tmpfile\n";
100
101# Okay if template no longer has XXXXX in
102
103
104ok( ($tmpfile !~ /XXXXX$/) );