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 /;
24 # Create file in temp directory
25 my $template = File::Spec->catfile(File::Spec->tmpdir, 'wowserXXXX');
27 (my $fh, $template) = mkstemp($template);
29 print "# MKSTEMP: FH is $fh File is $template fileno=".fileno($fh)."\n";
30 # Check if the file exists
34 $fh->autoflush(1) if $] >= 5.006;
36 # Try printing something to the file
37 my $string = "woohoo\n";
46 # compare with previous string
50 # This test fails on Windows NT since it seems that the size returned by
51 # stat(filehandle) does not always equal the size of the stat(filename)
52 # This must be due to caching. In particular this test writes 7 bytes
53 # to the file which are not recognised by stat(filename)
54 # Simply waiting 3 seconds seems to be enough for the system to update
56 if ($^O eq 'MSWin32') {
59 my $status = unlink0($fh, $template);
63 skip("Skip test failed probably due to \$TMPDIR being on NFS",1);
67 # File with suffix. This is created in the current directory so
68 # may be problematic on NFS
70 $template = "suffixXXXXXX";
73 ($fh, my $fname) = mkstemps($template, $suffix);
75 print "# MKSTEMPS: File is $template -> $fname fileno=".fileno($fh)."\n";
76 # Check if the file exists
79 # This fails if you are running on NFS
80 # If this test fails simply skip it rather than doing a hard failure
81 $status = unlink0($fh, $fname);
86 skip("Skip test failed probably due to cwd being on NFS",1)
92 $template = File::Spec->catdir(File::Spec->tmpdir, 'tmpdirXXXXXX');
94 my $tmpdir = mkdtemp($template);
96 print "# MKDTEMP: Name is $tmpdir from template $template\n";
100 # Need to tidy up after myself
104 # Just a filename, not opened
106 $template = File::Spec->catfile(File::Spec->tmpdir, 'mytestXXXXXX');
108 my $tmpfile = mktemp($template);
110 print "# MKTEMP: Tempfile is $template -> $tmpfile\n";
112 # Okay if template no longer has XXXXX in
115 ok( ($tmpfile !~ /XXXXX$/) );