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