3 # Test for mktemp family of commands in File::Temp
4 # Use STANDARD safe level for these tests
9 require Test; import Test;
17 use File::Temp qw/ :mktemp unlink0 /;
23 # Create file in temp directory
24 my $template = File::Spec->catfile(File::Spec->tmpdir, 'wowserXXXX');
26 (my $fh, $template) = mkstemp($template);
28 print "# MKSTEMP: FH is $fh File is $template fileno=".fileno($fh)."\n";
29 # Check if the file exists
33 $fh->autoflush(1) if $] >= 5.006;
35 # Try printing something to the file
36 my $string = "woohoo\n";
45 # compare with previous string
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)
53 # Simply waiting 3 seconds seems to be enough for the system to update
55 if ($^O eq 'MSWin32') {
58 ok( unlink0($fh, $template) );
62 # File with suffix. This is created in the current directory
64 $template = "suffixXXXXXX";
67 ($fh, my $fname) = mkstemps($template, $suffix);
69 print "# MKSTEMPS: File is $template -> $fname fileno=".fileno($fh)."\n";
70 # Check if the file exists
73 # This fails if you are running on NFS
74 # If this test fails simply skip it rather than doing a hard failure
75 my $status = unlink0($fh, $fname);
80 skip("Skip test failed probably due to NFS",1)
86 $template = File::Spec->catdir(File::Spec->tmpdir, 'tmpdirXXXXXX');
88 my $tmpdir = mkdtemp($template);
90 print "# MKDTEMP: Name is $tmpdir from template $template\n";
94 # Need to tidy up after myself
98 # Just a filename, not opened
100 $template = File::Spec->catfile(File::Spec->tmpdir, 'mytestXXXXXX');
102 my $tmpfile = mktemp($template);
104 print "# MKTEMP: Tempfile is $template -> $tmpfile\n";
106 # Okay if template no longer has XXXXX in
109 ok( ($tmpfile !~ /XXXXX$/) );