Commit | Line | Data |
---|---|---|
e2cf2bdb | 1 | #!/usr/bin/perl -w |
262eb13a GS |
2 | # Test for File::Temp - POSIX functions |
3 | ||
e2cf2bdb JH |
4 | BEGIN { |
5 | chdir 't' if -d 't'; | |
20822f61 | 6 | @INC = '../lib'; |
e2cf2bdb JH |
7 | require Test; import Test; |
8 | plan(tests => 7); | |
9 | } | |
10 | ||
262eb13a | 11 | use strict; |
262eb13a GS |
12 | |
13 | use File::Temp qw/ :POSIX unlink0 /; | |
f5cf745e RG |
14 | use FileHandle; |
15 | ||
262eb13a GS |
16 | ok(1); |
17 | ||
18 | # TMPNAM - scalar | |
19 | ||
20 | print "# TMPNAM: in a scalar context: \n"; | |
21 | my $tmpnam = tmpnam(); | |
22 | ||
23 | # simply check that the file does not exist | |
24 | # Not a 100% water tight test though if another program | |
25 | # has managed to create one in the meantime. | |
26 | ok( !(-e $tmpnam )); | |
27 | ||
28 | print "# TMPNAM file name: $tmpnam\n"; | |
29 | ||
91e74348 | 30 | # TMPNAM list context |
262eb13a GS |
31 | # Not strict posix behaviour |
32 | (my $fh, $tmpnam) = tmpnam(); | |
33 | ||
91e74348 | 34 | print "# TMPNAM: in list context: $fh $tmpnam\n"; |
262eb13a GS |
35 | |
36 | # File is opened - make sure it exists | |
37 | ok( (-e $tmpnam )); | |
38 | ||
0e939f40 JH |
39 | # Unlink it - a possible NFS issue again if TMPDIR is not a local disk |
40 | my $status = unlink0($fh, $tmpnam); | |
41 | if ($status) { | |
42 | ok( $status ); | |
43 | } else { | |
44 | skip("Skip test failed probably due to \$TMPDIR being on NFS",1); | |
45 | } | |
262eb13a GS |
46 | |
47 | # TMPFILE | |
48 | ||
49 | $fh = tmpfile(); | |
50 | ||
0e939f40 JH |
51 | if (defined $fh) { |
52 | ok( $fh ); | |
53 | print "# TMPFILE: tmpfile got FH $fh\n"; | |
54 | ||
55 | $fh->autoflush(1) if $] >= 5.006; | |
56 | ||
57 | # print something to it | |
58 | my $original = "Hello a test\n"; | |
59 | print "# TMPFILE: Wrote line: $original"; | |
60 | print $fh $original | |
61 | or die "Error printing to tempfile\n"; | |
262eb13a | 62 | |
0e939f40 JH |
63 | # rewind it |
64 | ok( seek($fh,0,0) ); | |
262eb13a | 65 | |
0e939f40 JH |
66 | # Read from it |
67 | my $line = <$fh>; | |
262eb13a | 68 | |
0e939f40 JH |
69 | print "# TMPFILE: Read line: $line"; |
70 | ok( $original, $line); | |
71 | ||
72 | close($fh); | |
73 | ||
74 | } else { | |
75 | # Skip all the remaining tests | |
76 | foreach (1..3) { | |
77 | skip("Skip test failed probably due to \$TMPDIR being on NFS",1); | |
78 | } | |
79 | } | |
262eb13a GS |
80 | |
81 | ||
262eb13a | 82 | |
262eb13a | 83 |