Blame t/mktemp.t

Packit a89ea5
#!/usr/local/bin/perl -w
Packit a89ea5
Packit a89ea5
# Test for mktemp family of commands in File::Temp
Packit a89ea5
# Use STANDARD safe level for these tests
Packit a89ea5
Packit a89ea5
use strict;
Packit a89ea5
use Test::More tests => 9;
Packit a89ea5
Packit a89ea5
use File::Spec;
Packit a89ea5
use File::Path;
Packit a89ea5
use File::Temp qw/ :mktemp unlink0 /;
Packit a89ea5
use FileHandle;
Packit a89ea5
Packit a89ea5
ok(1);
Packit a89ea5
Packit a89ea5
# MKSTEMP - test
Packit a89ea5
Packit a89ea5
# Create file in temp directory
Packit a89ea5
my $template = File::Spec->catfile(File::Temp::_wrap_file_spec_tmpdir(), 'wowserXXXX');
Packit a89ea5
Packit a89ea5
(my $fh, $template) = mkstemp($template);
Packit a89ea5
Packit a89ea5
print "# MKSTEMP: FH is $fh File is $template fileno=".fileno($fh)."\n";
Packit a89ea5
# Check if the file exists
Packit a89ea5
ok( (-e $template) );
Packit a89ea5
Packit a89ea5
# Autoflush
Packit a89ea5
$fh->autoflush(1) if $] >= 5.006;
Packit a89ea5
Packit a89ea5
# Try printing something to the file
Packit a89ea5
my $string = "woohoo\n";
Packit a89ea5
print $fh $string;
Packit a89ea5
Packit a89ea5
# rewind the file
Packit a89ea5
ok(seek( $fh, 0, 0));
Packit a89ea5
Packit a89ea5
# Read from the file
Packit a89ea5
my $line = <$fh>;
Packit a89ea5
Packit a89ea5
# compare with previous string
Packit a89ea5
ok($string, $line);
Packit a89ea5
Packit a89ea5
# Tidy up
Packit a89ea5
# This test fails on Windows NT since it seems that the size returned by 
Packit a89ea5
# stat(filehandle) does not always equal the size of the stat(filename)
Packit a89ea5
# This must be due to caching. In particular this test writes 7 bytes
Packit a89ea5
# to the file which are not recognised by stat(filename)
Packit a89ea5
# Simply waiting 3 seconds seems to be enough for the system to update
Packit a89ea5
Packit a89ea5
if ($^O eq 'MSWin32') {
Packit a89ea5
  sleep 3;
Packit a89ea5
}
Packit a89ea5
my $status = unlink0($fh, $template);
Packit a89ea5
if ($status) {
Packit a89ea5
  ok( $status );
Packit a89ea5
} else {
Packit a89ea5
    SKIP: {
Packit a89ea5
        skip("Skip test failed probably due to \$TMPDIR being on NFS",1);
Packit a89ea5
    }
Packit a89ea5
}
Packit a89ea5
Packit a89ea5
# MKSTEMPS
Packit a89ea5
# File with suffix. This is created in the current directory so
Packit a89ea5
# may be problematic on NFS
Packit a89ea5
Packit a89ea5
$template = "suffixXXXXXX";
Packit a89ea5
my $suffix = ".dat";
Packit a89ea5
Packit a89ea5
($fh, my $fname) = mkstemps($template, $suffix);
Packit a89ea5
Packit a89ea5
print "# MKSTEMPS: File is $template -> $fname fileno=".fileno($fh)."\n";
Packit a89ea5
# Check if the file exists
Packit a89ea5
ok( (-e $fname) );
Packit a89ea5
Packit a89ea5
# This fails if you are running on NFS
Packit a89ea5
# If this test fails simply skip it rather than doing a hard failure
Packit a89ea5
$status = unlink0($fh, $fname);
Packit a89ea5
Packit a89ea5
if ($status) {
Packit a89ea5
  ok($status);
Packit a89ea5
} else {
Packit a89ea5
    SKIP: {
Packit a89ea5
        skip("Skip test failed probably due to cwd being on NFS",1)
Packit a89ea5
    }
Packit a89ea5
}
Packit a89ea5
Packit a89ea5
# MKDTEMP
Packit a89ea5
# Temp directory
Packit a89ea5
Packit a89ea5
$template = File::Spec->catdir(File::Temp::_wrap_file_spec_tmpdir(), 'tmpdirXXXXXX');
Packit a89ea5
Packit a89ea5
my $tmpdir = mkdtemp($template);
Packit a89ea5
Packit a89ea5
print "# MKDTEMP: Name is $tmpdir from template $template\n";
Packit a89ea5
Packit a89ea5
ok( (-d $tmpdir ) );
Packit a89ea5
Packit a89ea5
# Need to tidy up after myself
Packit a89ea5
rmtree($tmpdir);
Packit a89ea5
Packit a89ea5
# MKTEMP
Packit a89ea5
# Just a filename, not opened
Packit a89ea5
Packit a89ea5
$template = File::Spec->catfile(File::Temp::_wrap_file_spec_tmpdir(), 'mytestXXXXXX');
Packit a89ea5
Packit a89ea5
my $tmpfile = mktemp($template);
Packit a89ea5
Packit a89ea5
print "# MKTEMP: Tempfile is $template -> $tmpfile\n";
Packit a89ea5
Packit a89ea5
# Okay if template no longer has XXXXX in
Packit a89ea5
Packit a89ea5
Packit a89ea5
ok( ($tmpfile !~ /XXXXX$/) );