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