File: readdir.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (117 lines) | stat: -rw-r--r-- 3,203 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!./perl

BEGIN {
    chdir 't' if -d 't';
    require './test.pl';
    set_up_inc('../lib');
}

use strict;
use warnings;
our ($fh, @fh, %fh);

eval 'opendir(NOSUCH, "no/such/directory");';
skip_all($@) if $@;

for my $i (1..2000) {
    local *OP;
    opendir(OP, "op") or die "can't opendir: $!";
    # should auto-closedir() here
}

is(opendir(OP, "op"), 1);
my @D = grep(/^[^\.].*\.t$/i, readdir(OP));
closedir(OP);

my $expect;
{
    open my $man, '<', '../MANIFEST' or die "Can't open ../MANIFEST: $!";
    while (<$man>) {
	++$expect if m!^t/op/[^/]+\t!;
    }
}

my ($min, $max) = ($expect - 10, $expect + 10);
within(scalar @D, $expect, 10, 'counting op/*.t');

my @R = sort @D;
my @G = sort <op/*.t>;
if ($G[0] =~ m#.*\](\w+\.t)#i) {
    # grep is to convert filespecs returned from glob under VMS to format
    # identical to that returned by readdir
    @G = grep(s#.*\](\w+\.t).*#op/$1#i,<op/*.t>);
}
while (@R && @G && $G[0] eq 'op/'.$R[0]) {
	shift(@R);
	shift(@G);
}
is(scalar @R, 0, 'readdir results all accounted for');
is(scalar @G, 0, 'glob results all accounted for');

is(opendir($fh, "op"), 1);
is(ref $fh, 'GLOB');
is(opendir($fh[0], "op"), 1);
is(ref $fh[0], 'GLOB');
is(opendir($fh{abc}, "op"), 1);
is(ref $fh{abc}, 'GLOB');
isnt("$fh", "$fh[0]");
isnt("$fh", "$fh{abc}");

# See that perl does not segfault upon readdir($x=".");
# https://github.com/Perl/perl5/issues/9813
fresh_perl_like(<<'EOP', qr/^no crash/, {}, 'GH #9813');
  eval {
    my $x = ".";
    my @files = readdir($x);
  };
  print "no crash";
EOP

SKIP:
{ # [perl #118651]
  # test that readdir doesn't modify errno on successfully reaching the end of the list
  # in scalar context, POSIX requires that readdir() not modify errno on end-of-directory

  my @s;
  ok(opendir(OP, "op"), "opendir op");
  $! = 0;
  while (defined(my $f = readdir OP)) {
    push @s, $f
      if $f =~ /^[^\.].*\.t$/i;
  }
  my $errno = $! + 0;
  closedir OP;
  is(@s, @D, "should be the same number of files, scalar or list")
    or skip "mismatch on file count - presumably a readdir error", 1;
  is($errno, 0, "errno preserved");
}

SKIP:
{
    open my $fh, "<", "op"
      or skip "can't open a directory on this platform", 10;
    my $warned;
    local $SIG{__WARN__} = sub { $warned = "@_" };
    ok(!readdir($fh), "cannot readdir file handle");
    like($warned, qr/readdir\(\) attempted on handle \$fh opened with open/,
         "check the message");
    undef $warned;
    ok(!telldir($fh), "cannot telldir file handle");
    like($warned, qr/telldir\(\) attempted on handle \$fh opened with open/,
         "check the message");
    undef $warned;
    ok(!seekdir($fh, 0), "cannot seekdir file handle");
    like($warned, qr/seekdir\(\) attempted on handle \$fh opened with open/,
         "check the message");
    undef $warned;
    ok(!rewinddir($fh), "cannot rewinddir file handle");
    like($warned, qr/rewinddir\(\) attempted on handle \$fh opened with open/,
         "check the message");
    undef $warned;
    ok(!closedir($fh), "cannot closedir file handle");
    like($warned, qr/closedir\(\) attempted on handle \$fh opened with open/,
         "check the message");
    undef $warned;
}

done_testing();