xref: /openbsd-src/gnu/usr.bin/perl/t/op/stat_errors.t (revision 5759b3d249badf144a6240f7eec4dcf9df003e6b)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    require './test.pl';
6    set_up_inc('../lib');
7    skip_all_if_miniperl("miniperl can't load PerlIO::scalar");
8}
9
10plan(tests => 2*11*29);
11
12use Errno qw(EBADF ENOENT);
13
14open(SCALARFILE, "<", \"wibble") or die $!; # needs PerlIO::scalar
15open(CLOSEDFILE, "<", "./test.pl") or die $!;
16close(CLOSEDFILE) or die $!;
17opendir(CLOSEDDIR, "../lib") or die $!;
18closedir(CLOSEDDIR) or die $!;
19
20foreach my $op (
21    qw(stat lstat),
22    (map { "-$_" } qw(r w x o R W X O e z s f d l p S b c t u g k T B M A C)),
23) {
24    foreach my $arg (
25	(map { ($_, "\\*$_") }
26	    qw(NEVEROPENED SCALARFILE CLOSEDFILE CLOSEDDIR _)),
27	"\"tmpnotexist\"",
28    ) {
29	my $argdesc = $arg;
30	if ($arg eq "_") {
31	    my @z = lstat "tmpnotexist";
32	    $argdesc .= " with prior stat fail";
33	}
34	SKIP: {
35	    if ($op eq "-l" && $arg =~ /\A\\/) {
36		# The op weirdly stringifies the globref and uses it as
37		# a filename, rather than treating it as a file handle.
38		# That might be a bug, but while that behaviour exists it
39		# needs to be exempted from these tests.
40		skip "-l on globref", 2;
41	    }
42	    if ($op eq "-t" && $arg eq "\"tmpnotexist\"") {
43		# The op doesn't operate on filenames.
44		skip "-t on filename", 2;
45	    }
46	    $! = 0;
47	    my $res = eval "$op $arg";
48	    my $err = $!;
49	    is $res, $op =~ /\A-/ ? undef : !!0, "result of $op $arg";
50	    is 0+$err,
51		$arg eq "\"tmpnotexist\"" ||
52		    ($op =~ /\A-[TB]\z/ && $arg =~ /_\z/) ? ENOENT : EBADF,
53		"error from $op $arg";
54	}
55    }
56}
57
581;
59