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