1 #!/usr/local/bin/perl -w
3 # Test for mktemp family of commands in File::Temp
4 # Use STANDARD safe level for these tests
8 BEGIN { plan tests => 9 }
12 use File::Temp qw/ :mktemp unlink0 /;
18 # Create file in temp directory
19 my $template = File::Spec->catfile(File::Spec->tmpdir, 'wowserXXXX');
21 (my $fh, $template) = mkstemp($template);
23 print "# MKSTEMP: FH is $fh File is $template fileno=".fileno($fh)."\n";
24 # Check if the file exists
28 $fh->autoflush(1) if $] >= 5.006;
30 # Try printing something to the file
31 my $string = "woohoo\n";
40 # compare with previous string
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)
48 # Simply waiting 3 seconds seems to be enough for the system to update
50 if ($^O eq 'MSWin32') {
53 ok( unlink0($fh, $template) );
57 # File with suffix. This is created in the current directory
59 $template = "suffixXXXXXX";
62 ($fh, my $fname) = mkstemps($template, $suffix);
64 print "# MKSTEMPS: File is $template -> $fname fileno=".fileno($fh)."\n";
65 # Check if the file exists
68 # This fails if you are running on NFS
69 # If this test fails simply skip it rather than doing a hard failure
70 my $status = unlink0($fh, $fname);
75 skip("Skip test failed probably due to NFS",1)
81 $template = File::Spec->catdir(File::Spec->tmpdir, 'tmpdirXXXXXX');
83 my $tmpdir = mkdtemp($template);
85 print "# MKDTEMP: Name is $tmpdir from template $template\n";
89 # Need to tidy up after myself
93 # Just a filename, not opened
95 $template = File::Spec->catfile(File::Spec->tmpdir, 'mytestXXXXXX');
97 my $tmpfile = mktemp($template);
99 print "# MKTEMP: Tempfile is $template -> $tmpfile\n";
101 # Okay if template no longer has XXXXX in
104 ok( ($tmpfile !~ /XXXXX$/) );